@coderich/graphql-shape
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "@coderich/graphql-shape", | ||
"main": "src/GraphQLShape.js", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
@@ -6,4 +6,4 @@ const get = require('lodash.get'); | ||
['push', 'pop', 'shift', 'unshift'].forEach((fn) => { | ||
exports[fn] = (v, ...rest) => { | ||
v?.[fn]?.(...rest); | ||
exports[fn] = (v, ...args) => { | ||
v?.[fn]?.(...args); | ||
return v; | ||
@@ -14,3 +14,3 @@ }; | ||
// Comparison methods | ||
exports.in = (a, ...rest) => Util.ensureArray(a).some(el => rest.flat().includes(el)); | ||
exports.in = (a, ...args) => Util.ensureArray(a).some(el => args.flat().includes(el)); | ||
exports.nin = (...args) => !exports.in(...args); | ||
@@ -25,4 +25,4 @@ Object.entries({ | ||
}).forEach(([key, fn]) => { | ||
exports[key] = (v, ...rest) => { | ||
return Util.uvl(Util.pairs(rest).reduce((prev, [value, result], i) => { | ||
exports[key] = (v, ...args) => { | ||
return Util.uvl(Util.pairs(args).reduce((prev, [value, result], i) => { | ||
if (prev !== undefined) return prev; | ||
@@ -38,11 +38,11 @@ if (result === undefined) return i > 0 ? value : fn(v, value); | ||
exports.not = el => !el; | ||
exports.or = (...args) => args.flat().some(el => el); | ||
exports.and = (...args) => args.flat().every(el => el); | ||
exports.or = (...args) => args.flat().some(el => el); | ||
// Math | ||
exports.add = (v, ...rest) => rest.reduce((prev, curr) => prev + curr, v); | ||
exports.sub = (v, ...rest) => rest.reduce((prev, curr) => prev - curr, v); | ||
exports.div = (v, ...rest) => rest.reduce((prev, curr) => prev / curr, v); | ||
exports.mul = (v, ...rest) => rest.reduce((prev, curr) => prev * curr, v); | ||
exports.mod = (v, ...rest) => rest.reduce((prev, curr) => prev % curr, v); | ||
exports.add = (v, ...args) => args.flat().reduce((prev, curr) => prev + curr, v); | ||
exports.sub = (v, ...args) => args.flat().reduce((prev, curr) => prev - curr, v); | ||
exports.div = (v, ...args) => args.flat().reduce((prev, curr) => prev / curr, v); | ||
exports.mul = (v, ...args) => args.flat().reduce((prev, curr) => prev * curr, v); | ||
exports.mod = (v, ...args) => args.flat().reduce((prev, curr) => prev % curr, v); | ||
@@ -58,2 +58,10 @@ // Utility methods | ||
exports.unflatten = Util.unflatten; | ||
exports.pick = (v, ...rest) => rest.flat().reduce((prev, key) => Object.assign(prev, { [key]: v[key] }), {}); | ||
// Pick keys (with optional rename) | ||
exports.pick = (v, ...args) => args.reduce((prev, mixed) => { | ||
let key, $key; | ||
if (Array.isArray(mixed)) [key, $key] = mixed; | ||
else if (typeof mixed === 'object') [[key, $key]] = Object.entries(mixed); | ||
else key = $key = mixed; | ||
return Object.assign(prev, { [$key]: v[key] }); | ||
}, {}); |
@@ -118,7 +118,7 @@ const Util = require('@coderich/util'); | ||
transforms.forEach(({ key, ops = [] }) => { | ||
const hoisted = []; | ||
const thunks = []; | ||
// We assign data here because it's possible to modify the root/data itself (key: '') | ||
data = Util.pathmap(key, data, (value, info) => { | ||
const values = [value]; | ||
const vars = [value]; | ||
@@ -136,4 +136,2 @@ ops.forEach((op) => { | ||
e.data = { json, mixed }; | ||
console.log(json); | ||
console.log(mixed); | ||
throw e; | ||
@@ -147,14 +145,26 @@ } | ||
const [[fnName, args]] = Object.entries(el); | ||
value = Util.map(value, v => GraphQLShape.#resolveValueFunction(v, values, fnName, args)); | ||
// value = Util.map(value, v => GraphQLShape.#resolveValueFunction(v, values, fnName, args)); | ||
value = Util.map(value, v => GraphQLShape.#resolveValueFunction(v, vars, fnName, args)); | ||
}); | ||
break; | ||
} | ||
case 'assign': { | ||
value = GraphQLShape.#resolveVariableArgs(vars, mixed); | ||
break; | ||
} | ||
case 'rename': { | ||
thunks.push(() => { | ||
info.parent[GraphQLShape.#resolveVariableArgs(vars, mixed)] = value; | ||
delete info.parent[info.key]; | ||
}); | ||
break; | ||
} | ||
case 'hoist': { | ||
if (!mixed) hoisted.push(info); | ||
Object.assign(info.parent, value); | ||
thunks.push(() => { | ||
Object.assign(info.parent, value); | ||
if (!mixed) delete info.parent[info.key]; | ||
}); | ||
break; | ||
} | ||
default: { | ||
value = GraphQLShape.#resolveValueFunction(value, values, fn, mixed); | ||
value = GraphQLShape.#resolveValueFunction(value, vars, fn, mixed); | ||
break; | ||
@@ -164,3 +174,3 @@ } | ||
values.push(value); | ||
vars.push(value); | ||
}); | ||
@@ -172,4 +182,4 @@ | ||
// Delete any hoisted keys | ||
hoisted.forEach(hoist => delete hoist.parent[hoist.key]); | ||
// Deferred processing | ||
thunks.forEach(thunk => thunk()); | ||
}); | ||
@@ -180,9 +190,13 @@ | ||
static #resolveValueFunction(value, values, fn, ...args) { | ||
// Argument replacement variables | ||
args = Util.ensureArray(args).flat().map((arg) => { | ||
static #resolveVariableArgs(vars, args) { | ||
return Util.map(args, (arg) => { | ||
const match = `${arg}`.match(/\$(\d)/); | ||
return match ? values[match[1]] : arg; | ||
return match ? vars[match[1]] : arg; | ||
}); | ||
} | ||
static #resolveValueFunction(value, vars, fn, ...args) { | ||
// Argument replacement variables | ||
args = GraphQLShape.#resolveVariableArgs(vars, args.flat()); | ||
// Core functions have a special syntax | ||
@@ -189,0 +203,0 @@ if (core[fn]) { |
9793
270