immutable-class
Version:
A template for creating immutable classes
115 lines (114 loc) • 3.35 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyedArray = void 0;
const tslib_1 = require("tslib");
const has_own_prop_1 = tslib_1.__importDefault(require("has-own-prop"));
const simple_array_1 = require("../simple-array/simple-array");
class KeyedArray {
constructor(keyGetter) {
Object.defineProperty(this, "getKey", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.getKey = keyGetter;
}
static withKey(key) {
return new KeyedArray((x) => x[key]);
}
get(array, key) {
const { getKey } = this;
return simple_array_1.SimpleArray.find(array, x => getKey(x) === key);
}
toRecord(array) {
const { getKey } = this;
const myRecord = {};
for (const a of array) {
const key = getKey(a);
if (myRecord[key])
continue;
myRecord[key] = a;
}
return myRecord;
}
checkValid(array, what, where) {
const { getKey } = this;
const seen = {};
for (const a of array) {
const key = getKey(a);
if (seen[key]) {
throw new Error(['duplicate', what, `'${key}'`, where ? 'in' : undefined, where]
.filter(Boolean)
.join(' '));
}
seen[key] = 1;
}
}
isValid(array) {
const { getKey } = this;
const seen = {};
for (const a of array) {
const key = getKey(a);
if (seen[key])
return false;
seen[key] = 1;
}
return true;
}
overrideByKey(things, thingOverride) {
const { getKey } = this;
const overrideKey = getKey(thingOverride);
let added = false;
things = things.map(t => {
if (getKey(t) === overrideKey) {
added = true;
return thingOverride;
}
else {
return t;
}
});
if (!added)
things.push(thingOverride);
return things;
}
overridesByKey(things, thingOverrides) {
const { getKey } = this;
const keyToIndex = {};
const thingsLength = things.length;
for (let i = 0; i < thingsLength; i++) {
keyToIndex[getKey(things[i])] = i;
}
const newThings = things.slice();
for (const thingOverride of thingOverrides) {
const key = getKey(thingOverride);
if ((0, has_own_prop_1.default)(keyToIndex, key)) {
newThings[keyToIndex[key]] = thingOverride;
}
else {
newThings.push(thingOverride);
}
}
return newThings;
}
dedupe(array) {
const { getKey } = this;
const seen = {};
return array.filter(a => {
const key = getKey(a);
if (seen[key]) {
return false;
}
else {
seen[key] = true;
return true;
}
});
}
deleteByKey(array, key) {
const { getKey } = this;
return array.filter(a => getKey(a) !== key);
}
}
exports.KeyedArray = KeyedArray;
;