|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.RoundedArrayWithIds = exports.ArrayWithIds = void 0; |
| 4 | +const ValueWithId_1 = require("./ValueWithId"); |
| 5 | +class ArrayWithIds { |
| 6 | + constructor(values = [], ids = []) { |
| 7 | + if (values.length !== ids.length) { |
| 8 | + throw new Error("Values and IDs must have the same length"); |
| 9 | + } |
| 10 | + this.values = [...values]; |
| 11 | + this.ids = [...ids]; |
| 12 | + } |
| 13 | + static fromValues(values) { |
| 14 | + const ids = values.map((_, i) => i); |
| 15 | + return new this(values, ids); |
| 16 | + } |
| 17 | + static fromObjects(objects) { |
| 18 | + const values = objects.map((obj) => obj.value); |
| 19 | + const ids = objects.map((obj) => obj.id); |
| 20 | + return new this(values, ids); |
| 21 | + } |
| 22 | + toJSON() { |
| 23 | + return this.values.map((value, index) => ({ |
| 24 | + id: this.ids[index], |
| 25 | + value: value !== null && |
| 26 | + typeof value === "object" && |
| 27 | + "toJSON" in value && |
| 28 | + typeof value.toJSON === "function" |
| 29 | + ? value.toJSON() |
| 30 | + : value, |
| 31 | + })); |
| 32 | + } |
| 33 | + toValueWithIdArray() { |
| 34 | + return this.values.map((value, index) => ValueWithId_1.ValueWithId.fromValueAndId(value, this.ids[index])); |
| 35 | + } |
| 36 | + getElementValueById(id) { |
| 37 | + const index = this.ids.indexOf(id); |
| 38 | + return index !== -1 ? this.values[index] : undefined; |
| 39 | + } |
| 40 | + getElementValueByIndex(index) { |
| 41 | + return this.values[index]; |
| 42 | + } |
| 43 | + getElementIdByIndex(index) { |
| 44 | + return this.ids[index] || undefined; |
| 45 | + } |
| 46 | + getElementIdByValue(value) { |
| 47 | + const index = this.values.findIndex((v) => Array.isArray(v) && Array.isArray(value) |
| 48 | + ? v.length === value.length && v.every((val, idx) => val === value[idx]) |
| 49 | + : v === value); |
| 50 | + return index !== -1 ? this.ids[index] : undefined; |
| 51 | + } |
| 52 | + filterByValues(valuesToKeep) { |
| 53 | + const toHash = (v) => (Array.isArray(v) ? JSON.stringify(v) : String(v)); |
| 54 | + const keepSet = new Set(Array.isArray(valuesToKeep) ? valuesToKeep.map(toHash) : [toHash(valuesToKeep)]); |
| 55 | + const filtered = this.values |
| 56 | + .map((value, i) => [value, this.ids[i]]) |
| 57 | + .filter(([value]) => keepSet.has(toHash(value))); |
| 58 | + this.values = filtered.map(([v]) => v); |
| 59 | + this.ids = filtered.map(([_, id]) => id); |
| 60 | + } |
| 61 | + filterByIndices(indices) { |
| 62 | + const keepSet = new Set(Array.isArray(indices) ? indices : [indices]); |
| 63 | + this.values = this.values.filter((_, i) => keepSet.has(i)); |
| 64 | + this.ids = this.ids.filter((_, i) => keepSet.has(i)); |
| 65 | + } |
| 66 | + filterByIds(ids, invert = false) { |
| 67 | + const idSet = new Set(Array.isArray(ids) ? ids : [ids]); |
| 68 | + const keep = invert |
| 69 | + ? this.ids.map((id, i) => (idSet.has(id) ? -1 : i)).filter((i) => i >= 0) |
| 70 | + : this.ids.map((id, i) => (idSet.has(id) ? i : -1)).filter((i) => i >= 0); |
| 71 | + this.values = keep.map((i) => this.values[i]); |
| 72 | + this.ids = keep.map((i) => this.ids[i]); |
| 73 | + } |
| 74 | + equals(other) { |
| 75 | + if (!(other instanceof ArrayWithIds)) |
| 76 | + return false; |
| 77 | + if (this.values.length !== other.values.length) |
| 78 | + return false; |
| 79 | + if (this.ids.length !== other.ids.length) |
| 80 | + return false; |
| 81 | + return (this.values.every((v, i) => { |
| 82 | + const ov = other.values[i]; |
| 83 | + return Array.isArray(v) && Array.isArray(ov) |
| 84 | + ? v.length === ov.length && v.every((val, idx) => val === ov[idx]) |
| 85 | + : v === ov; |
| 86 | + }) && this.ids.every((id, i) => id === other.ids[i])); |
| 87 | + } |
| 88 | + mapArrayInPlace(func) { |
| 89 | + this.values = this.values.map(func); |
| 90 | + } |
| 91 | + addItem(value, id) { |
| 92 | + const newId = id !== null && id !== void 0 ? id : Math.max(-1, ...this.ids) + 1; |
| 93 | + this.values.push(value); |
| 94 | + this.ids.push(newId); |
| 95 | + } |
| 96 | + removeItem(index, id) { |
| 97 | + if (id !== undefined) { |
| 98 | + index = this.ids.indexOf(id); |
| 99 | + if (index === -1) |
| 100 | + throw new Error("ID not found"); |
| 101 | + } |
| 102 | + if (index < 0 || index >= this.values.length) { |
| 103 | + throw new Error("Index out of range"); |
| 104 | + } |
| 105 | + this.values.splice(index, 1); |
| 106 | + this.ids.splice(index, 1); |
| 107 | + } |
| 108 | +} |
| 109 | +exports.ArrayWithIds = ArrayWithIds; |
| 110 | +class RoundedArrayWithIds extends ArrayWithIds { |
| 111 | + constructor(values = [], ids = [], options = ValueWithId_1.defaultRoundingOptions) { |
| 112 | + super(values, ids); |
| 113 | + this.roundingOptions = options; |
| 114 | + } |
| 115 | + toJSON() { |
| 116 | + return this.values.map((value, index) => new ValueWithId_1.RoundedValueWithId(this.ids[index], value, this.roundingOptions).toJSON()); |
| 117 | + } |
| 118 | +} |
| 119 | +exports.RoundedArrayWithIds = RoundedArrayWithIds; |
0 commit comments