diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/js/inspectAndFuse | |
add: graphs et rushs
Diffstat (limited to 'graphs/js/inspectAndFuse')
| -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, +}; |
