JSFiddle - React, Tailwind, and code Playground

by Prathameshsb

JavaScript

// Import necessary modules
const parseCsvSync = require('csv-parse/lib/sync');
const fs = require('fs');
const path = require('path');

// Function to read CSV data from the file
const readCsvData = () => {
  // Define the CSV file path
  const fundingFile = 'startup_funding.csv';
  
  // Read the file and convert it to a string
  const fileData = fs.readFileSync(path.join(__dirname, '..', fundingFile)).toString();
  
  // Parse the CSV data and return the result
  return parseCsvSync(fileData);
};

// Function to map a CSV row to an object with named properties
const mapRowToObj = (row) => ({
  permalink: row[0],
  companyName: row[1],
  numberEmployees: row[2],
  category: row[3],
  city: row[4],
  state: row[5],
  fundedDate: row[6],
  raisedAmount: row[7],
  raisedCurrency: row[8],
  round: row[9],
});

// Function to filter and map CSV data based on provided options
const filterAndMap = (options, data) => {
  // Filter the data based on the provided options
  if (options.companyName) {
    data = data.filter(row => options.companyName === row[1]);
  } else if (options.city) {
    data = data.filter(row => options.city === row[4]);
  } else if (options.state) {
    data = data.filter(row => options.state === row[5]);
  } else if (options.round) {
    data = data.filter(row => options.round === row[9]);
  }

  // Map the filtered data to an object
  return mapRowToObj(data[0]);
};

// Function to retrieve filtered data based on options using the 'where' query
const where = (options = {}) => {
  // Read CSV data
  const csvData = readCsvData();
  
  // Filter and map data based on options
  const filteredData = filterAndMap(options, csvData);
  
  // Return the result as an array
  return [filteredData];
};

// Function to retrieve filtered data based on options using the 'findBy' query
const findBy = (options = {}) => {
  // Read CSV data
  const csvData = readCsvData();
  
  // Filter and map data based on options
  return filterAndMap(options,...