Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active January 8, 2023 10:48
Show Gist options
  • Select an option

  • Save aeinbu/c07f043f79a80dd6d236c3aa3b4d7e35 to your computer and use it in GitHub Desktop.

Select an option

Save aeinbu/c07f043f79a80dd6d236c3aa3b4d7e35 to your computer and use it in GitHub Desktop.

Revisions

  1. aeinbu revised this gist Jul 27, 2022. No changes.
  2. aeinbu created this gist Jul 27, 2022.
    24 changes: 24 additions & 0 deletions adhoc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    const {
    chain,
    filter,
    count,
    groupBy,
    map
    } = require("./chaining")


    const transforms = [
    map(x => ({
    pono: x.productionOrderNumber,
    eisn: x.endItemSerialNumber,
    pos: x.componentItemPosition,
    alias: x.alias,
    })),
    groupBy(x => x.pos)
    // count
    ]


    console.log(chain(require("./jsonfiles/1.json"), ...transforms))
    console.log("----")
    console.log(chain(require("./jsonfiles/2.json"), ...transforms))
    19 changes: 19 additions & 0 deletions chaining.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // Chaining method
    const chain = (obj, op = obj => obj, ...restOps) => restOps.length > 0
    ? chain(op(obj), ...restOps)
    : op(obj)

    // Example methods to chain
    const filter = predicate => (collection) => collection.filter(predicate)

    const count = collection => collection.length

    const groupBy = keyExtractor => collection => collection.reduce((acc, cur) => {
    acc[keyExtractor(cur)] = [...(acc && acc[keyExtractor(cur)] || []), cur]
    return acc
    }, {})

    const map = mapFn => collection => collection.map(mapFn)

    // Export the methods
    module.exports = {chain, filter, count, groupBy, map}