feat: migrate to esm

This commit is contained in:
2023-03-16 01:58:13 +01:00
parent fd740b3091
commit 4df19e8c20
512 changed files with 3016 additions and 2222 deletions

View File

@@ -1,6 +1,7 @@
{
"name": "@openstapps/collection-utils",
"version": "2.1.0",
"type": "module",
"scripts": {
"build": "rimraf lib && tsc",
"format": "prettier .",
@@ -20,6 +21,6 @@
"prettier": "2.8.3",
"rimraf": "4.4.0",
"ts-node": "10.9.1",
"typescript": "4.4.4"
"typescript": "4.8.4"
}
}

View File

@@ -16,8 +16,12 @@
/**
* Returns the difference between two arrays.
*/
export function differenceBy<T>(a: T[], b: T[], transform: (item: T) => unknown) {
export function differenceBy<T>(
a: T[],
b: T[],
transform: (item: T) => unknown
) {
const disallowed = new Set(b.map(transform));
return a.filter(item => !disallowed.has(transform(item)));
return a.filter((item) => !disallowed.has(transform(item)));
}

View File

@@ -19,12 +19,12 @@
* If the path doesn't exist, undefined is returned.
*/
export function get<U = unknown>(object: object, path: string): U {
return path.split('.').reduce(
return path.split(".").reduce(
(accumulator, current) =>
accumulator?.hasOwnProperty(current)
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
(accumulator as any)[current]
: undefined,
object,
object
) as unknown as U;
}

View File

@@ -16,9 +16,12 @@
/**
* Group an array by a function
*/
export function groupBy<T>(collection: T[], group: (item: T) => string | undefined): Record<string, T[]> {
export function groupBy<T>(
collection: T[],
group: (item: T) => string | undefined
): Record<string, T[]> {
return collection.reduce((accumulator: Record<string, T[]>, item) => {
const key = group(item) ?? '';
const key = group(item) ?? "";
accumulator[key] = accumulator[key] ?? [];
accumulator[key].push(item);
return accumulator;
@@ -28,9 +31,12 @@ export function groupBy<T>(collection: T[], group: (item: T) => string | undefin
/**
* Group an array by a function (returns a Map, whose keys keep order info of items entry)
*/
export function groupByStable<T>(collection: T[], group: (item: T) => string | undefined): Map<string, T[]> {
export function groupByStable<T>(
collection: T[],
group: (item: T) => string | undefined
): Map<string, T[]> {
return collection.reduce((accumulator: Map<string, T[]>, item) => {
const key = group(item) ?? '';
const key = group(item) ?? "";
accumulator.set(key, accumulator.get(key) ?? []);
accumulator.get(key)?.push(item);
return accumulator;
@@ -40,6 +46,9 @@ export function groupByStable<T>(collection: T[], group: (item: T) => string | u
/**
*
*/
export function groupByProperty<T extends object>(collection: T[], property: keyof T): Record<string, T[]> {
return groupBy(collection, item => item[property] as unknown as string);
export function groupByProperty<T extends object>(
collection: T[],
property: keyof T
): Record<string, T[]> {
return groupBy(collection, (item) => item[property] as unknown as string);
}

View File

@@ -19,7 +19,10 @@
* each key is the last element responsible for generating the key. The
* iteratee is invoked with one argument: (value).
*/
export function keyBy<T>(collection: T[], key: (item: T) => string | number): Record<string, T> {
export function keyBy<T>(
collection: T[],
key: (item: T) => string | number
): Record<string, T> {
return collection.reduce((accumulator, item) => {
accumulator[key(item)] = item;
return accumulator;

View File

@@ -18,9 +18,9 @@
*/
export function mapValues<T extends object, U>(
object: T,
transform: (value: T[keyof T], key: keyof T) => U,
): {[key in keyof T]: U} {
const result = {} as {[key in keyof T]: U};
transform: (value: T[keyof T], key: keyof T) => U
): { [key in keyof T]: U } {
const result = {} as { [key in keyof T]: U };
for (const key in object) {
if (object.hasOwnProperty(key)) {

View File

@@ -16,8 +16,11 @@
/**
* Returns the minimum value of a collection.
*/
export function minBy<T>(array: T[], transform: (item: T) => number | undefined): T {
export function minBy<T>(
array: T[],
transform: (item: T) => number | undefined
): T {
const transforms = array.map(transform);
const min = Math.min(...(transforms.filter(it => !!it) as number[]));
const min = Math.min(...(transforms.filter((it) => !!it) as number[]));
return array.find((_, i) => transforms[i] === min) as T;
}

View File

@@ -16,8 +16,11 @@
/**
* Returns a new object without the specified keys.
*/
export function omit<T extends object, U extends keyof T>(object: T, ...keys: U[]): Omit<T, U> {
const out = {...object};
export function omit<T extends object, U extends keyof T>(
object: T,
...keys: U[]
): Omit<T, U> {
const out = { ...object };
for (const key of keys) delete out[key];
return out as Exclude<T, U>;
}

View File

@@ -17,12 +17,15 @@
* Partitions a list into two lists. One with the elements that satisfy a predicate,
* and one with the elements that don't satisfy the predicate.
*/
export function partition<T>(array: T[], transform: (item: T) => boolean): [T[], T[]] {
export function partition<T>(
array: T[],
transform: (item: T) => boolean
): [T[], T[]] {
return array.reduce<[T[], T[]]>(
(accumulator, item) => {
accumulator[transform(item) ? 0 : 1].push(item);
return accumulator;
},
[[], []],
[[], []]
);
}

View File

@@ -16,7 +16,10 @@
/**
* Pick a set of properties from an object
*/
export function pick<T extends object, U extends keyof T>(object: T, keys: U[]): Pick<T, U> {
export function pick<T extends object, U extends keyof T>(
object: T,
keys: U[]
): Pick<T, U> {
return keys.reduce((accumulator, key) => {
if (object.hasOwnProperty(key)) {
accumulator[key] = object[key];
@@ -30,7 +33,7 @@ export function pick<T extends object, U extends keyof T>(object: T, keys: U[]):
*/
export function pickBy<T extends object, U extends keyof T>(
object: T,
predicate: (value: T[U], key: U) => boolean,
predicate: (value: T[U], key: U) => boolean
): Pick<T, U> {
return (Object.keys(object) as U[]).reduce((accumulator, key) => {
if (predicate(object[key], key)) {

View File

@@ -16,7 +16,7 @@
/**
* sort function for two strings
*/
export function stringSort(a = '', b = ''): number {
export function stringSort(a = "", b = ""): number {
if (a < b) return -1;
if (a > b) return 1;
return 0;
@@ -25,10 +25,12 @@ export function stringSort(a = '', b = ''): number {
/**
* sort function for two strings that allows for a custom transform
*/
export function stringSortBy<T>(map: (item: T) => string | undefined): (a: T, b: T) => number {
export function stringSortBy<T>(
map: (item: T) => string | undefined
): (a: T, b: T) => number {
return (a: T, b: T): number => {
const aValue = map(a) || '';
const bValue = map(b) || '';
const aValue = map(a) || "";
const bValue = map(b) || "";
if (aValue < bValue) return -1;
if (aValue > bValue) return 1;
return 0;

View File

@@ -18,14 +18,20 @@
*/
export function sumBy<T extends object>(
collection: T[],
transform: (value: T) => number | undefined,
transform: (value: T) => number | undefined
): number {
return collection.reduce((accumulator, item) => accumulator + (transform(item) || 0), 0);
return collection.reduce(
(accumulator, item) => accumulator + (transform(item) || 0),
0
);
}
/**
* Sum an array of numbers
*/
export function sum(collection: Array<number | undefined>): number {
return collection.reduce<number>((accumulator, item) => accumulator + (item || 0), 0);
return collection.reduce<number>(
(accumulator, item) => accumulator + (item || 0),
0
);
}

View File

@@ -22,7 +22,10 @@ export type Tree<T> = {
/**
*
*/
export function treeGroupBy<T>(items: T[], transform: (item: T) => string[]): Tree<T> {
export function treeGroupBy<T>(
items: T[],
transform: (item: T) => string[]
): Tree<T> {
const tree: Tree<T> = {};
for (const item of items) {

View File

@@ -16,11 +16,14 @@
/**
* Filter out duplicates from an array.
*/
export function uniqBy<T>(array: T[], transform: (item: T) => string | number): T[] {
export function uniqBy<T>(
array: T[],
transform: (item: T) => string | number
): T[] {
return Object.values(
array.reduce((accumulator, current) => {
accumulator[transform(current)] = current;
return accumulator;
}, {} as Record<string | number, T>),
}, {} as Record<string | number, T>)
);
}

View File

@@ -13,11 +13,16 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {chunk} from '../src/chunk';
import {expect} from "chai";
import { chunk } from "../src/chunk.js";
import { expect } from "chai";
describe('chunk', function () {
it('should chunk items in the correct sizes', function () {
expect(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)).to.deep.equal([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]);
describe("chunk", function () {
it("should chunk items in the correct sizes", function () {
expect(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)).to.deep.equal([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10],
]);
});
});

View File

@@ -12,14 +12,14 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {differenceBy} from '../src/difference';
import {expect} from "chai";
import { differenceBy } from "../src/difference.js";
import { expect } from "chai";
describe('differenceBy', function () {
it('should return the difference of two arrays', function () {
describe("differenceBy", function () {
it("should return the difference of two arrays", function () {
const a = [1, 2, 3, 4, 5];
const b = [1, 2, 3];
expect(differenceBy(a, b, it => it)).to.deep.equal([4, 5]);
expect(differenceBy(a, b, (it) => it)).to.deep.equal([4, 5]);
});
});

View File

@@ -12,29 +12,29 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {get} from '../src/get';
import {expect} from "chai";
import { get } from "../src/get.js";
import { expect } from "chai";
describe('get', function () {
it('should get a simple path', function () {
describe("get", function () {
it("should get a simple path", function () {
const object = {
a: {
b: {
c: 'd',
c: "d",
},
},
};
expect(get(object, 'a.b.c')).to.equal('d');
expect(get(object, "a.b.c")).to.equal("d");
});
it('should return undefined for a non-existent path', function () {
it("should return undefined for a non-existent path", function () {
const object = {
a: {
b: {
c: 'd',
c: "d",
},
},
};
expect(get(object, 'a.b.c.d')).to.be.undefined;
expect(get(object, "a.b.c.d")).to.be.undefined;
});
});

View File

@@ -12,113 +12,113 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {groupBy, groupByStable, groupByProperty} from '../src/group-by';
import {expect} from "chai";
import { groupBy, groupByStable, groupByProperty } from "../src/group-by.js";
import { expect } from "chai";
describe('groupBy', () => {
it('should group an array by a key', () => {
describe("groupBy", () => {
it("should group an array by a key", () => {
const array = [
{id: 1, name: 'one'},
{id: 2, name: 'two'},
{id: 3, name: 'three'},
{id: 4, name: 'four'},
{id: 5, name: 'five'},
{ id: 1, name: "one" },
{ id: 2, name: "two" },
{ id: 3, name: "three" },
{ id: 4, name: "four" },
{ id: 5, name: "five" },
];
const result = groupBy(array, it => it.name);
const result = groupBy(array, (it) => it.name);
expect(result).to.deep.equal({
one: [{id: 1, name: 'one'}],
two: [{id: 2, name: 'two'}],
three: [{id: 3, name: 'three'}],
four: [{id: 4, name: 'four'}],
five: [{id: 5, name: 'five'}],
one: [{ id: 1, name: "one" }],
two: [{ id: 2, name: "two" }],
three: [{ id: 3, name: "three" }],
four: [{ id: 4, name: "four" }],
five: [{ id: 5, name: "five" }],
});
});
it('should handle multiple elements per group', () => {
it("should handle multiple elements per group", () => {
const array = [
{id: 1, name: 'one'},
{id: 2, name: 'two'},
{id: 3, name: 'three'},
{id: 4, name: 'four'},
{id: 5, name: 'five'},
{id: 6, name: 'one'},
{id: 7, name: 'two'},
{id: 8, name: 'three'},
{id: 9, name: 'four'},
{id: 10, name: 'five'},
{ id: 1, name: "one" },
{ id: 2, name: "two" },
{ id: 3, name: "three" },
{ id: 4, name: "four" },
{ id: 5, name: "five" },
{ id: 6, name: "one" },
{ id: 7, name: "two" },
{ id: 8, name: "three" },
{ id: 9, name: "four" },
{ id: 10, name: "five" },
];
const result = groupBy(array, it => it.name);
const result = groupBy(array, (it) => it.name);
expect(result).to.deep.equal({
one: [
{id: 1, name: 'one'},
{id: 6, name: 'one'},
{ id: 1, name: "one" },
{ id: 6, name: "one" },
],
two: [
{id: 2, name: 'two'},
{id: 7, name: 'two'},
{ id: 2, name: "two" },
{ id: 7, name: "two" },
],
three: [
{id: 3, name: 'three'},
{id: 8, name: 'three'},
{ id: 3, name: "three" },
{ id: 8, name: "three" },
],
four: [
{id: 4, name: 'four'},
{id: 9, name: 'four'},
{ id: 4, name: "four" },
{ id: 9, name: "four" },
],
five: [
{id: 5, name: 'five'},
{id: 10, name: 'five'},
{ id: 5, name: "five" },
{ id: 10, name: "five" },
],
});
});
});
describe('groupByStable', () => {
describe("groupByStable", () => {
const array = [
{id: 2, name: 'two'},
{id: 4, name: 'three'},
{id: 3, name: 'three'},
{id: 1, name: 'one'},
{ id: 2, name: "two" },
{ id: 4, name: "three" },
{ id: 3, name: "three" },
{ id: 1, name: "one" },
];
const result = groupByStable(array, it => it.name);
const result = groupByStable(array, (it) => it.name);
it('should group an array by keys', () => {
expect(result.get('one')).to.deep.equal([{id: 1, name: 'one'}]);
expect(result.get('two')).to.deep.equal([{id: 2, name: 'two'}]);
expect(result.get('three')).to.deep.equal([
{id: 4, name: 'three'},
{id: 3, name: 'three'},
it("should group an array by keys", () => {
expect(result.get("one")).to.deep.equal([{ id: 1, name: "one" }]);
expect(result.get("two")).to.deep.equal([{ id: 2, name: "two" }]);
expect(result.get("three")).to.deep.equal([
{ id: 4, name: "three" },
{ id: 3, name: "three" },
]);
});
it('should provide ordered keys', () => {
it("should provide ordered keys", () => {
// eslint-disable-next-line unicorn/prefer-spread
expect(Array.from(result.keys())).to.deep.equal(['two', 'three', 'one']);
expect(Array.from(result.keys())).to.deep.equal(["two", "three", "one"]);
});
});
describe('groupByProperty', function () {
it('should group by property', () => {
describe("groupByProperty", function () {
it("should group by property", () => {
const array = [
{id: 1, name: 'one'},
{id: 2, name: 'two'},
{id: 3, name: 'three'},
{id: 4, name: 'four'},
{id: 5, name: 'five'},
{ id: 1, name: "one" },
{ id: 2, name: "two" },
{ id: 3, name: "three" },
{ id: 4, name: "four" },
{ id: 5, name: "five" },
];
const result = groupByProperty(array, 'name');
const result = groupByProperty(array, "name");
expect(result).to.deep.equal({
one: [{id: 1, name: 'one'}],
two: [{id: 2, name: 'two'}],
three: [{id: 3, name: 'three'}],
four: [{id: 4, name: 'four'}],
five: [{id: 5, name: 'five'}],
one: [{ id: 1, name: "one" }],
two: [{ id: 2, name: "two" }],
three: [{ id: 3, name: "three" }],
four: [{ id: 4, name: "four" }],
five: [{ id: 5, name: "five" }],
});
});
});

View File

@@ -12,30 +12,30 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {keyBy} from '../src/key-by';
import {expect} from "chai";
import { keyBy } from "../src/key-by.js";
import { expect } from "chai";
describe('keyBy', function () {
it('should key objects', function () {
describe("keyBy", function () {
it("should key objects", function () {
const objects = [
{
id: 1,
name: 'foo',
name: "foo",
},
{
id: 2,
name: 'bar',
name: "bar",
},
];
const result = keyBy(objects, it => it.id);
const result = keyBy(objects, (it) => it.id);
expect(result).to.deep.equal({
1: {
id: 1,
name: 'foo',
name: "foo",
},
2: {
id: 2,
name: 'bar',
name: "bar",
},
});
});

View File

@@ -12,18 +12,18 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {mapValues} from '../src/map-values';
import {expect} from "chai";
import { mapValues } from "../src/map-values.js";
import { expect } from "chai";
describe('map-values', () => {
it('should map values', () => {
describe("map-values", () => {
it("should map values", () => {
const object = {
a: 1,
b: 2,
c: 3,
};
const result = mapValues(object, value => value * 2);
const result = mapValues(object, (value) => value * 2);
expect(result).to.deep.equal({
a: 2,
@@ -32,14 +32,14 @@ describe('map-values', () => {
});
});
it('should not modify the original object', () => {
it("should not modify the original object", () => {
const object = {
a: 1,
b: 2,
c: 3,
};
mapValues(object, value => value * 2);
mapValues(object, (value) => value * 2);
expect(object).to.deep.equal({
a: 1,

View File

@@ -12,33 +12,33 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {minBy} from '../src/min';
import {expect} from "chai";
import { minBy } from "../src/min.js";
import { expect } from "chai";
describe('minBy', function () {
it('should pick the minimum value based on transform', function () {
describe("minBy", function () {
it("should pick the minimum value based on transform", function () {
expect(
minBy(
[
{id: 1, name: 'A'},
{id: 2, name: 'B'},
{id: 3, name: 'C'},
{ id: 1, name: "A" },
{ id: 2, name: "B" },
{ id: 3, name: "C" },
],
it => it.id,
),
).to.deep.equal({id: 1, name: 'A'});
(it) => it.id
)
).to.deep.equal({ id: 1, name: "A" });
});
it('should not return undefined if there are other choices', function () {
it("should not return undefined if there are other choices", function () {
expect(
minBy(
[
{id: undefined, name: 'B'},
{id: 1, name: 'A'},
{id: undefined, name: 'C'},
{ id: undefined, name: "B" },
{ id: 1, name: "A" },
{ id: undefined, name: "C" },
],
it => it.id,
),
).to.deep.equal({id: 1, name: 'A'});
(it) => it.id
)
).to.deep.equal({ id: 1, name: "A" });
});
});

View File

@@ -12,13 +12,13 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {omit} from '../src/omit';
import {expect} from "chai";
import { omit } from "../src/omit.js";
import { expect } from "chai";
describe('omit', function () {
it('should omit keys', function () {
const object = {a: 1, b: 2, c: 3};
const result = omit(object, 'a', 'c');
expect(result).to.deep.equal({b: 2});
describe("omit", function () {
it("should omit keys", function () {
const object = { a: 1, b: 2, c: 3 };
const result = omit(object, "a", "c");
expect(result).to.deep.equal({ b: 2 });
});
});

View File

@@ -12,12 +12,12 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {partition} from '../src/partition';
import {expect} from "chai";
import { partition } from "../src/partition.js";
import { expect } from "chai";
describe('partition', function () {
it('should partition an array', function () {
expect(partition([1, 2, 3, 4], it => it % 2 === 0)).to.deep.equal([
describe("partition", function () {
it("should partition an array", function () {
expect(partition([1, 2, 3, 4], (it) => it % 2 === 0)).to.deep.equal([
[2, 4],
[1, 3],
]);

View File

@@ -12,13 +12,13 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {pick} from '../src/pick';
import {expect} from "chai";
import { pick } from "../src/pick.js";
import { expect } from "chai";
describe('pick', function () {
it('should pick properties', function () {
const object = {a: 1, b: 2, c: 3};
const result = pick(object, ['a', 'c']);
expect(result).to.deep.equal({a: 1, c: 3});
describe("pick", function () {
it("should pick properties", function () {
const object = { a: 1, b: 2, c: 3 };
const result = pick(object, ["a", "c"]);
expect(result).to.deep.equal({ a: 1, c: 3 });
});
});

View File

@@ -12,18 +12,18 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {shuffle} from '../src/shuffle';
import {expect} from "chai";
import { shuffle } from "../src/shuffle.js";
import { expect } from "chai";
describe('shuffle', function () {
it('should shuffle an array', function () {
describe("shuffle", function () {
it("should shuffle an array", function () {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const shuffled = shuffle(array);
expect(shuffled).not.to.deep.equal(array);
expect(shuffled).to.contain.members(array);
});
it('should not modify the original array', function () {
it("should not modify the original array", function () {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle(array);
expect(array).to.deep.equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

View File

@@ -12,22 +12,31 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {stringSort, stringSortBy} from '../src/string-sort';
import {expect} from "chai";
import { stringSort, stringSortBy } from "../src/string-sort.js";
import { expect } from "chai";
describe('stringSort', () => {
it('should sort an array of strings', () => {
expect(['a', 'c', 'b', 'd'].sort(stringSort)).to.deep.equal(['a', 'b', 'c', 'd']);
});
});
describe('stringSortBy', () => {
it('should sort an array of strings', () => {
expect([{item: 'a'}, {item: 'c'}, {item: 'b'}, {item: 'd'}].sort(stringSortBy(it => it.item))).to.deep.equal([
{item: 'a'},
{item: 'b'},
{item: 'c'},
{item: 'd'},
describe("stringSort", () => {
it("should sort an array of strings", () => {
expect(["a", "c", "b", "d"].sort(stringSort)).to.deep.equal([
"a",
"b",
"c",
"d",
]);
});
});
describe("stringSortBy", () => {
it("should sort an array of strings", () => {
expect(
[{ item: "a" }, { item: "c" }, { item: "b" }, { item: "d" }].sort(
stringSortBy((it) => it.item)
)
).to.deep.equal([
{ item: "a" },
{ item: "b" },
{ item: "c" },
{ item: "d" },
]);
});
});

View File

@@ -12,21 +12,21 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from "chai";
import {sum, sumBy} from '../src/sum';
import { expect } from "chai";
import { sum, sumBy } from "../src/sum.js";
describe('sum', () => {
it('should return the sum of all elements in the collection', () => {
describe("sum", () => {
it("should return the sum of all elements in the collection", () => {
const collection = [1, 2, 3, 4, 5];
const result = sum(collection);
expect(result).to.equal(15);
});
});
describe('sumBy', function () {
it('should return the sum of all elements in the collection', () => {
const collection = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}];
const result = sumBy(collection, it => it.a);
describe("sumBy", function () {
it("should return the sum of all elements in the collection", () => {
const collection = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }];
const result = sumBy(collection, (it) => it.a);
expect(result).to.equal(15);
});
});

View File

@@ -12,34 +12,34 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from "chai";
import {Tree, treeGroupBy} from '../src/tree-group';
import { expect } from "chai";
import { Tree, treeGroupBy } from "../src/tree-group.js";
interface TestItem {
id: number;
path?: string[];
}
describe('tree-group', function () {
it('should create a tree', function () {
describe("tree-group", function () {
it("should create a tree", function () {
const items: Array<TestItem> = [
{
id: 1,
path: ['a', 'b', 'c'],
path: ["a", "b", "c"],
},
{
id: 2,
path: ['a', 'b', 'd'],
path: ["a", "b", "d"],
},
];
const tree = treeGroupBy(items, item => item.path ?? []);
const tree = treeGroupBy(items, (item) => item.path ?? []);
const expectedTree: Tree<TestItem> = {
a: {
b: {
c: {_: [items[0]]},
d: {_: [items[1]]},
c: { _: [items[0]] },
d: { _: [items[1]] },
} as Tree<TestItem>,
} as Tree<TestItem>,
} as Tree<TestItem>;
@@ -47,23 +47,23 @@ describe('tree-group', function () {
expect(tree).to.deep.equal(expectedTree);
});
it('should also sort empty paths', () => {
it("should also sort empty paths", () => {
const items: Array<TestItem> = [
{
id: 1,
path: ['a', 'b', 'c'],
path: ["a", "b", "c"],
},
{
id: 2,
},
];
const tree = treeGroupBy(items, item => item.path ?? []);
const tree = treeGroupBy(items, (item) => item.path ?? []);
const expectedTree: Tree<TestItem> = {
a: {
b: {
c: {_: [items[0]]},
c: { _: [items[0]] },
} as Tree<TestItem>,
} as Tree<TestItem>,
_: [items[1]],

View File

@@ -12,13 +12,15 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from "chai";
import {uniqBy} from '../src/uniq';
import { expect } from "chai";
import { uniqBy } from "../src/uniq.js";
describe('uniq', function () {
it('should return an array with unique values', function () {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = uniqBy(array, it => it);
describe("uniq", function () {
it("should return an array with unique values", function () {
const array = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
];
const result = uniqBy(array, (it) => it);
expect(result).to.deep.equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
});

View File

@@ -12,11 +12,11 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from "chai";
import {zip} from '../src/zip';
import { expect } from "chai";
import { zip } from "../src/zip.js";
describe('zip', function () {
it('should zip arrays together', function () {
describe("zip", function () {
it("should zip arrays together", function () {
expect(zip([1, 2, 3], [4, 5, 6])).to.deep.equal([
[1, 4],
[2, 5],