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

@@ -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],