@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
43 lines (41 loc) • 1.27 kB
JavaScript
import {
on,
use
} from "./chunk-PF7HDF6B.mjs";
import {
isArray,
isDefined
} from "./chunk-AAND4MKF.mjs";
// src/types/Array.ts
var toArray = (...items) => items.length > 1 ? items : isArray(items[0]) ? items[0] : isDefined(items[0]) ? [items[0]] : [];
var toObject = (key, ...items) => toArray(...items).reduce((o, i) => {
o[i[key]] = i;
return o;
}, {});
var array = {
merge: (first = [], second = [], firstKey = "id", secondKey = "id") => first.map((f) => ({
...f,
...second.find((s) => isDefined(s[secondKey]) && isDefined(f[firstKey]) && s[secondKey] === f[firstKey])
})),
switch: (items = [], item) => use([...items], (res) => {
on(res.indexOf(item), (i) => i !== -1 ? res.splice(i, 1) : res.push(item));
return res;
}),
splitIn: (items = [], length = 2) => {
const res = Array.from({ length }, () => []);
items.forEach((i, index) => res[index % length].push(i));
return res;
},
chunk: (items = [], length = 2) => {
const chunks = Math.ceil(items.length / length);
const res = Array.from({ length: chunks }, () => []);
items.forEach((i, index) => res[Math.floor(index / chunks)].push(i));
return res;
}
};
export {
toArray,
toObject,
array
};
//# sourceMappingURL=chunk-KCY4RPFR.mjs.map