diff options
Diffstat (limited to 'graphs/js/inspectAndFuse/inspectAndFuse.js')
| -rw-r--r-- | graphs/js/inspectAndFuse/inspectAndFuse.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/graphs/js/inspectAndFuse/inspectAndFuse.js b/graphs/js/inspectAndFuse/inspectAndFuse.js new file mode 100644 index 0000000..2072bb1 --- /dev/null +++ b/graphs/js/inspectAndFuse/inspectAndFuse.js @@ -0,0 +1,77 @@ +function getNumberFields(inputObject) { + if (inputObject == null) { + return new Array(); + } + + const res = new Array(); + + for (const prop in inputObject) { + if (typeof inputObject[prop] === "number") { + res.push(prop); + } + } + + return res; +} +function incrementCounters(inputObject) { + const reg = /counter/i; + const fields = getNumberFields(inputObject); + + if (fields == null) { + return; + } + + fields.forEach((f) => { + if (f.match(reg)) { + inputObject[f]++; + } + }); +} +function deleteUppercaseProperties(inputObject) { + if (inputObject == null) { + return; + } + + const reg = /[a-z]/; + + for (const prop in inputObject) { + if (!prop.match(reg)) { + delete inputObject[prop]; + } else if (inputObject[prop] instanceof Object) { + deleteUppercaseProperties(inputObject[prop]); + } + } +} +function fusion(...objs) { + const res = {}; + + for (const obj of objs) { + for (const prop in obj) { + if (!Object.hasOwn(res, prop)) { + res[prop] = obj[prop]; + } else { + if ( + typeof res[prop] != typeof obj[prop] || + typeof res[prop] === "boolean" + ) { + res[prop] = obj[prop]; + } else if (obj[prop] instanceof Array) { + res[prop] = [...res[prop], ...obj[prop]]; + } else if (typeof prop === "object") { + res[prop] = fusion(res[prop], obj[prop]); + } else { + res[prop] += obj[prop]; + } + } + } + } + + return res; +} + +module.exports = { + fusion, + incrementCounters, + deleteUppercaseProperties, + getNumberFields, +}; |
