Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/persistence / src / adapters / csv / index.ts
Size: Mime:
const { resolve } = require('path')
const log = require('fliplog')
const parse = require('csv-parse/lib/sync')
const { read, write } = require('flipfile')
const { keyValueToIterator, keys } = require('chain-able/exportsMiddleman')

class Spreadsheet {
  static init() {
    return new Spreadsheet()
      .setReadPath()
      .setWritePath()
      .read()
      .parse()
      .setup()
  }
  setReadPath(pathname = './csv.csv') {
    this.filePath = resolve(__dirname, pathname)
    return this
  }
  setWritePath(pathname = './output.csv') {
    this.outputPath = resolve(__dirname, pathname)
    return this
  }
  read() {
    const contents = read(this.filePath)
    this.contents = contents
    return this
  }
  parse() {
    const parseOptions = { columns: true }
    const rows = parse(this.contents, parseOptions)
    this.rows = rows
    return this
  }
  setup() {
    this.setupOutputString()
    return this
  }
  // private
  setupOutputString() {
    // save this to spreadsheet
    this.outputString = ''
    this.outputString += this.headings
  }
  // in csv, this is first line
  get headings() {
    return Object.keys(this.rows[0]).join(',') + '\n'
  }

  transformRow(row) {
    // is indexed
    // const { img_main, img_alt, photograph, skuid } = row

    return row
  }

  // eslint-disable-next-line
  async onRow(row) {
    log
      .bold('row')
      .data(row)
      .echo()

    const transformedRow = this.transformRow(row)

    const asString =
      Object.values(transformedRow)
        .map(cell => cell)
        .join(',') + '\n'
    this.outputString += asString

    log.blue(asString).echo()

    return Promise.resolve(asString)
  }
  save() {
    const rowrowrowYourBoat = this.rows.map(this.onRow)
    const gentlyDownTheStream = () => write(this.outputPath, this.outputString)
    Promise.all(rowrowrowYourBoat).then(gentlyDownTheStream)
  }

  [Symbol.iterator]() {
    const range = keys(this.rows)
    return keyValueToIterator(range, this.rows, this.rows.length)
  }

  /**
   * ls
   */
  set(columnName = 'columnName.rowName?') {
    //
  }
  get(columnName = 'columnName.rowName?') {}
  has(key) {
    //
  }
  delete(key) {
    //
  }
}

const csv = Spreadsheet.init()

for (const row of csv) {
  console.log({ row })
}