@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
259 lines (257 loc) • 6.83 kB
JavaScript
import {
ifDefined,
ifTrue
} from "./chunk-JSON7A4X.mjs";
import {
toArray
} from "./chunk-CO2AFYVD.mjs";
import {
asString
} from "./chunk-BDA5LB4S.mjs";
import {
ofGet,
ofProperty
} from "./chunk-SJGQU3OG.mjs";
import {
isA
} from "./chunk-ZHXKBOK2.mjs";
import {
on
} from "./chunk-ZPNFXK7Y.mjs";
import {
isArray,
isDefined,
isEmpty
} from "./chunk-DEJ7A5PY.mjs";
// src/types/List.ts
var List = class extends Array {
get ids() {
return this.mapDefined((i) => i.id);
}
isSubSetOf(...items) {
return this.diff(items).length === 0;
}
isSuperSetOf(...items) {
return this.length > items.length && toList(...items).isSubSetOf(...this);
}
isIntersectingWith(...items) {
return this.intersect(items).length > 0;
}
isDisjointWith(...items) {
return !this.isIntersectingWith(...items);
}
areEqual(...items) {
return this.isSubSetOf(...items) && toList(...items).isSubSetOf(...this);
}
asc(p) {
return this.sort((e1, e2) => ofProperty(e1, p) > ofProperty(e2, p) ? 1 : -1);
}
desc(p) {
return this.sort((e1, e2) => ofProperty(e1, p) < ofProperty(e2, p) ? 1 : -1);
}
first(p, params) {
return p ? this.find(p, params) : this[0];
}
firstValue(f, alt) {
return ifDefined(
this.first((t) => !!f(t)),
f,
(v) => ofGet(alt, v)
);
}
isFirst(value) {
return value === this.first();
}
next(p, params) {
return p ? this[this.findIndex(p, params) + 1] : this[0];
}
prev(p, params) {
return p ? this[this.findIndex(p, params) - 1] : this[0];
}
last(p, params) {
return p ? this.filter(p, params).last() : this[this.length - 1];
}
isLast(value) {
return value === this.last();
}
overlaps(...items) {
return toList(...items).some((i) => this.some((t) => i === t));
}
diff(others) {
return this.filter((i) => !others.includes(i));
}
diffByKey(others, key) {
return this.filter((i) => !others.some((o) => o[key] === i[key]));
}
symmetricDiff(others) {
return this.diff(others).concat(toList(...others).diff(this));
}
symmetricDiffByKey(others, key) {
return this.diffByKey(others, key).concat(toList(...others).diffByKey(this, key));
}
intersect(others) {
return this.filter((i) => others.includes(i));
}
intersectByKey(others, key) {
return this.filter((i) => others.some((o) => o[key] === i[key]));
}
intersectBy(others, f) {
return this.filter((i) => others.some((o) => f(i, o)));
}
accumulate(...keys) {
return this.map((d, i, arr) => {
const acc = keys.reduce((acc2, v) => {
acc2[v] = i === 0 ? d[v] : arr[i - 1][v] + d[v];
return acc2;
}, d);
arr[i] = acc;
return acc;
});
}
toJSON() {
return this.reduce((a, i) => {
a.push(JSON.parse(JSON.stringify(i ?? {})));
return a;
}, new Array());
}
map(f, params) {
return toList(super.map(f, params));
}
flatMap(f, params) {
return toList(super.flatMap(f, params));
}
mapDefined(f, params) {
return this.map(f, params).defined();
}
mapAsync(f) {
return Promise.all(super.map((e) => f(e))).then((a) => toList(a));
}
mapSerial(f) {
return super.reduce((p, item) => p.then((results) => on(results, async (r) => r.push(await f(item)))), Promise.resolve(toList()));
}
distinct() {
return this.filter((i, index) => this.indexOf(i) === index);
}
distinctByKey(key) {
const seen = /* @__PURE__ */ new Set();
return this.filter((item) => !seen.has(item[key]) && seen.add(item[key]));
}
distinctByValue() {
const seen = /* @__PURE__ */ new Set();
return this.filter((item) => !seen.has(JSON.stringify(item)) && seen.add(JSON.stringify(item)));
}
filter(p, params) {
return toList(super.filter(p, params));
}
sum(p) {
return this.reduce((sum, i) => sum + p(i), 0);
}
max(key) {
return this.desc(key).first();
}
min(key) {
return this.asc(key).first();
}
byId(id) {
return this.first((i) => asString(i.id) === asString(id));
}
byKey(key, value) {
return this.first((i) => i[key] == value);
}
add(...items) {
super.push(...toArray(...items));
return this;
}
concat(...items) {
return toList(super.concat(...items));
}
reverse() {
return toList(super.reverse());
}
splice(start, deleteCount, ...items) {
return toList(super.splice(start, deleteCount, ...items));
}
remove(item) {
const index = this.indexOf(item);
if (index > -1) {
this.splice(index, 1);
}
return this;
}
replace(key, item) {
const index = this.findIndex((i) => i[key] === item?.[key]);
ifTrue(index != -1, () => this.splice(index, 1, item));
return this;
}
switch(item) {
return this.includes(item) ? this.remove(item) : this.add(item);
}
defined() {
return this.reduce((l, v) => isDefined(v) ? l.add(v) : l, toList());
}
toObject(key, options = {}) {
return this.reduce((o, i) => {
o[i[key]] = i;
if (options.deleteKey)
delete o[i[key]][key];
return o;
}, {});
}
toObjectList(key) {
return this.reduce(
(a, t) => {
const k = t[key];
a[k] = a[k] ?? toList();
a[k].push(t);
return a;
},
{}
);
}
orElse(...alt) {
return !isEmpty(this) ? this : !isEmpty(...alt) ? toList(...alt) : void 0;
}
weave(insertFrom, interval) {
for (let i = interval, n = 0; i <= this.length && n < insertFrom.length; i += interval + 1) {
this.splice(i, 0, insertFrom[n++]);
}
return this;
}
slice(start, end) {
return toList(super.slice(start, end));
}
none(p) {
return !this.some(p);
}
chunk(chunkSize) {
return this.reduce((acc, _, index) => index % chunkSize === 0 ? on(acc, (a) => a.push(this.slice(index, index + chunkSize))) : acc, toList());
}
//we needed to add U because of a Typescript issue with generics
update(p, val) {
return this.map((v, i, a) => p(v, i, a) ? ofGet(val, v, i, a) : v);
}
updateFirst(p, val) {
const index = this.findIndex(p);
return this.update((t, i) => p(t, i, this) && i == index, val);
}
updateFirstById(id, val) {
return this.updateFirst((i) => asString(i?.id) === asString(id), val);
}
updateById(id, val) {
return this.update((i) => asString(i?.id) === asString(id), val);
}
};
var toList = (...items) => new List().add(...items);
var isList = (l) => isDefined(l) && isArray(l) && isA(l, "first", "last", "asc", "desc");
var asList = (c, items = []) => toList(toArray(items).map((i) => new c(i)));
var maxValue = (l, key) => l.desc(key).first()?.[key];
var minValue = (l, key) => l.asc(key).first()?.[key];
export {
List,
toList,
isList,
asList,
maxValue,
minValue
};
//# sourceMappingURL=chunk-A7C3XND3.mjs.map