Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active January 8, 2023 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeinbu/c07f043f79a80dd6d236c3aa3b4d7e35 to your computer and use it in GitHub Desktop.
Save aeinbu/c07f043f79a80dd6d236c3aa3b4d7e35 to your computer and use it in GitHub Desktop.
Quick queries over json files...
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))
// 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}
@aeinbu
Copy link
Author

aeinbu commented Jul 27, 2022

Usage:

node adhoc.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment