This commit is contained in:
barsdeveloper
2024-12-11 23:23:23 +02:00
parent 3de0e74ef0
commit e405a7245d
26 changed files with 471 additions and 181 deletions

View File

@@ -80,7 +80,7 @@ export default class Utility {
}
/**
* @param {Attribute} entity
* @param {IEntity} entity
* @param {String} key
* @returns {Boolean}
*/
@@ -147,29 +147,30 @@ export default class Utility {
/**
* @template T
* @param {Array<T>} a
* @param {Array<T>} b
* @param {Array<T>} reference
* @param {Array<T>} additional
* @param {(v: T) => void} adding - Process added element
* @param {(l: T, r: T) => Boolean} predicate
*/
static mergeArrays(a = [], b = [], predicate = (l, r) => l == r) {
static mergeArrays(reference = [], additional = [], predicate = (l, r) => l == r, adding = v => { }) {
let result = []
a = [...a]
b = [...b]
reference = [...reference]
additional = [...additional]
restart:
while (true) {
for (let j = 0; j < b.length; ++j) {
for (let i = 0; i < a.length; ++i) {
if (predicate(a[i], b[j])) {
for (let j = 0; j < additional.length; ++j) {
for (let i = 0; i < reference.length; ++i) {
if (predicate(reference[i], additional[j])) {
// Found an element in common in the two arrays
result.push(
// Take and append all the elements skipped from a
...a.splice(0, i),
...reference.splice(0, i),
// Take and append all the elements skippend from b
...b.splice(0, j),
...additional.splice(0, j).map(v => (adding(v), v)),
// Take and append the element in common
...a.splice(0, 1)
...reference.splice(0, 1)
)
b.shift() // Remove the same element from b
additional.shift() // Remove the same element from b
continue restart
}
}
@@ -177,7 +178,9 @@ export default class Utility {
break restart
}
// Append remaining the elements in the arrays and make it unique
return [...(new Set(result.concat(...a, ...b)))]
result.push(...reference)
result.push(...additional.filter(vb => !result.some(vr => predicate(vr, vb))))
return result
}
/** @param {String} value */