Last active
January 8, 2023 10:48
-
-
Save aeinbu/c07f043f79a80dd6d236c3aa3b4d7e35 to your computer and use it in GitHub Desktop.
Quick queries over json files...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: