mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
refactor: remove lodash
This commit is contained in:
27
src/app/_helpers/collections/chunk.spec.ts
Normal file
27
src/app/_helpers/collections/chunk.spec.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 {chunk} from './chunk';
|
||||
|
||||
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)).toEqual([
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
[10],
|
||||
]);
|
||||
});
|
||||
});
|
||||
28
src/app/_helpers/collections/chunk.ts
Normal file
28
src/app/_helpers/collections/chunk.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Chunk array into smaller arrays of a specified size.
|
||||
*
|
||||
* @param array The array to chunk.
|
||||
* @param chunkSize The size of each chunk.
|
||||
*/
|
||||
export function chunk<T>(array: T[], chunkSize = 1): T[][] {
|
||||
const arrayCopy = [...array];
|
||||
const out: T[][] = [];
|
||||
if (chunkSize <= 0) return out;
|
||||
while (arrayCopy.length > 0) out.push(arrayCopy.splice(0, chunkSize));
|
||||
return out;
|
||||
}
|
||||
39
src/app/_helpers/collections/get.spec.ts
Normal file
39
src/app/_helpers/collections/get.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './get';
|
||||
|
||||
describe('get', function () {
|
||||
it('should get a simple path', function () {
|
||||
const object = {
|
||||
a: {
|
||||
b: {
|
||||
c: 'd',
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(get(object, 'a.b.c')).toBe('d');
|
||||
});
|
||||
|
||||
it('should return undefined for a non-existent path', function () {
|
||||
const object = {
|
||||
a: {
|
||||
b: {
|
||||
c: 'd',
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(get(object, 'a.b.c.d')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
30
src/app/_helpers/collections/get.ts
Normal file
30
src/app/_helpers/collections/get.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets a value from a nested object.
|
||||
* The path must be key names separated by dots.
|
||||
* If the path doesn't exist, undefined is returned.
|
||||
*/
|
||||
export function get<U = unknown>(object: object, path: string): U {
|
||||
return path.split('.').reduce(
|
||||
(accumulator, current) =>
|
||||
accumulator?.hasOwnProperty(current)
|
||||
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(accumulator as any)[current]
|
||||
: undefined,
|
||||
object,
|
||||
) as unknown as U;
|
||||
}
|
||||
100
src/app/_helpers/collections/group-by.spec.ts
Normal file
100
src/app/_helpers/collections/group-by.spec.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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, groupByProperty} from './group-by';
|
||||
|
||||
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'},
|
||||
];
|
||||
|
||||
const result = groupBy(array, it => it.name);
|
||||
|
||||
expect(result).toEqual({
|
||||
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', () => {
|
||||
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'},
|
||||
];
|
||||
|
||||
const result = groupBy(array, it => it.name);
|
||||
|
||||
expect(result).toEqual({
|
||||
one: [
|
||||
{id: 1, name: 'one'},
|
||||
{id: 6, name: 'one'},
|
||||
],
|
||||
two: [
|
||||
{id: 2, name: 'two'},
|
||||
{id: 7, name: 'two'},
|
||||
],
|
||||
three: [
|
||||
{id: 3, name: 'three'},
|
||||
{id: 8, name: 'three'},
|
||||
],
|
||||
four: [
|
||||
{id: 4, name: 'four'},
|
||||
{id: 9, name: 'four'},
|
||||
],
|
||||
five: [
|
||||
{id: 5, name: 'five'},
|
||||
{id: 10, name: 'five'},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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'},
|
||||
];
|
||||
|
||||
const result = groupByProperty(array, 'name');
|
||||
|
||||
expect(result).toEqual({
|
||||
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'}],
|
||||
});
|
||||
});
|
||||
});
|
||||
39
src/app/_helpers/collections/group-by.ts
Normal file
39
src/app/_helpers/collections/group-by.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Group an array by a function
|
||||
*/
|
||||
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) || '';
|
||||
accumulator[key] = accumulator[key] ?? [];
|
||||
accumulator[key].push(item);
|
||||
return accumulator;
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export function groupByProperty<T extends object>(
|
||||
collection: T[],
|
||||
property: keyof T,
|
||||
): Record<string, T[]> {
|
||||
return groupBy(collection, item => item[property] as unknown as string);
|
||||
}
|
||||
41
src/app/_helpers/collections/key-by.spec.ts
Normal file
41
src/app/_helpers/collections/key-by.spec.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './key-by';
|
||||
|
||||
describe('keyBy', function () {
|
||||
it('should key objects', function () {
|
||||
const objects = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'foo',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'bar',
|
||||
},
|
||||
];
|
||||
const result = keyBy(objects, it => it.id);
|
||||
expect(result).toEqual({
|
||||
1: {
|
||||
id: 1,
|
||||
name: 'foo',
|
||||
},
|
||||
2: {
|
||||
id: 2,
|
||||
name: 'bar',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
30
src/app/_helpers/collections/key-by.ts
Normal file
30
src/app/_helpers/collections/key-by.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create an object composed of keys generated from the results of running
|
||||
* each element of collection thru iteratee. The corresponding value of
|
||||
* 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> {
|
||||
return collection.reduce((accumulator, item) => {
|
||||
accumulator[key(item)] = item;
|
||||
return accumulator;
|
||||
}, {} as Record<string | number, T>);
|
||||
}
|
||||
50
src/app/_helpers/collections/map-values.spec.ts
Normal file
50
src/app/_helpers/collections/map-values.spec.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './map-values';
|
||||
|
||||
describe('map-values', () => {
|
||||
it('should map values', () => {
|
||||
const object = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 3,
|
||||
};
|
||||
|
||||
const result = mapValues(object, value => value * 2);
|
||||
|
||||
expect(result).toEqual({
|
||||
a: 2,
|
||||
b: 4,
|
||||
c: 6,
|
||||
});
|
||||
});
|
||||
|
||||
it('should not modify the original object', () => {
|
||||
const object = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 3,
|
||||
};
|
||||
|
||||
mapValues(object, value => value * 2);
|
||||
|
||||
expect(object).toEqual({
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
32
src/app/_helpers/collections/map-values.ts
Normal file
32
src/app/_helpers/collections/map-values.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Maps the values of an object to a new object
|
||||
*/
|
||||
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};
|
||||
|
||||
for (const key in object) {
|
||||
if (object.hasOwnProperty(key)) {
|
||||
result[key] = transform(object[key], key);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
44
src/app/_helpers/collections/min.spec.ts
Normal file
44
src/app/_helpers/collections/min.spec.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './min';
|
||||
|
||||
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'},
|
||||
],
|
||||
it => it.id,
|
||||
),
|
||||
).toEqual({id: 1, name: 'A'});
|
||||
});
|
||||
|
||||
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'},
|
||||
],
|
||||
it => it.id,
|
||||
),
|
||||
).toEqual({id: 1, name: 'A'});
|
||||
});
|
||||
});
|
||||
26
src/app/_helpers/collections/min.ts
Normal file
26
src/app/_helpers/collections/min.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the minimum value of a collection.
|
||||
*/
|
||||
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[]));
|
||||
return array.find((_, i) => transforms[i] === min) as T;
|
||||
}
|
||||
23
src/app/_helpers/collections/omit.spec.ts
Normal file
23
src/app/_helpers/collections/omit.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './omit';
|
||||
|
||||
describe('omit', function () {
|
||||
it('should omit keys', function () {
|
||||
const object = {a: 1, b: 2, c: 3};
|
||||
const result = omit(object, 'a', 'c');
|
||||
expect(result).toEqual({b: 2});
|
||||
});
|
||||
});
|
||||
26
src/app/_helpers/collections/omit.ts
Normal file
26
src/app/_helpers/collections/omit.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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};
|
||||
for (const key of keys) delete out[key];
|
||||
return out as Exclude<T, U>;
|
||||
}
|
||||
25
src/app/_helpers/collections/partition.spec.ts
Normal file
25
src/app/_helpers/collections/partition.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './partition';
|
||||
|
||||
describe('partition', function () {
|
||||
it('should partition an array', function () {
|
||||
expect(partition([1, 2, 3, 4], it => it % 2 === 0)).toEqual([
|
||||
[2, 4],
|
||||
[1, 3],
|
||||
]);
|
||||
});
|
||||
});
|
||||
31
src/app/_helpers/collections/partition.ts
Normal file
31
src/app/_helpers/collections/partition.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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[]] {
|
||||
return array.reduce<[T[], T[]]>(
|
||||
(accumulator, item) => {
|
||||
accumulator[transform(item) ? 0 : 1].push(item);
|
||||
return accumulator;
|
||||
},
|
||||
[[], []],
|
||||
);
|
||||
}
|
||||
23
src/app/_helpers/collections/pick.spec.ts
Normal file
23
src/app/_helpers/collections/pick.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './pick';
|
||||
|
||||
describe('pick', function () {
|
||||
it('should pick properties', function () {
|
||||
const object = {a: 1, b: 2, c: 3};
|
||||
const result = pick(object, ['a', 'c']);
|
||||
expect(result).toEqual({a: 1, c: 3});
|
||||
});
|
||||
});
|
||||
29
src/app/_helpers/collections/pick.ts
Normal file
29
src/app/_helpers/collections/pick.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
return keys.reduce((accumulator, key) => {
|
||||
if (object.hasOwnProperty(key)) {
|
||||
accumulator[key] = object[key];
|
||||
}
|
||||
return accumulator;
|
||||
}, {} as Pick<T, U>);
|
||||
}
|
||||
30
src/app/_helpers/collections/shuffle.spec.ts
Normal file
30
src/app/_helpers/collections/shuffle.spec.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './shuffle';
|
||||
|
||||
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.toEqual(array);
|
||||
expect(shuffled).toEqual(jasmine.arrayContaining(array));
|
||||
});
|
||||
|
||||
it('should not modify the original array', function () {
|
||||
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
shuffle(array);
|
||||
expect(array).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
});
|
||||
});
|
||||
28
src/app/_helpers/collections/shuffle.ts
Normal file
28
src/app/_helpers/collections/shuffle.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Shuffles an array
|
||||
*/
|
||||
export function shuffle<T>(array: T[]): T[] {
|
||||
const copy = [...array];
|
||||
const out = [];
|
||||
|
||||
while (copy.length > 0) {
|
||||
out.push(copy.splice(Math.floor(Math.random() * copy.length), 1)[0]);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
32
src/app/_helpers/collections/string-sort.spec.ts
Normal file
32
src/app/_helpers/collections/string-sort.spec.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 './string-sort';
|
||||
|
||||
describe('stringSort', () => {
|
||||
it('should sort an array of strings', () => {
|
||||
expect(['a', 'c', 'b', 'd'].sort(stringSort)).toEqual(['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),
|
||||
),
|
||||
).toEqual([{item: 'a'}, {item: 'b'}, {item: 'c'}, {item: 'd'}]);
|
||||
});
|
||||
});
|
||||
38
src/app/_helpers/collections/string-sort.ts
Normal file
38
src/app/_helpers/collections/string-sort.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sort function for two strings
|
||||
*/
|
||||
export function stringSort(a = '', b = ''): number {
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
return (a: T, b: T): number => {
|
||||
const aValue = map(a) || '';
|
||||
const bValue = map(b) || '';
|
||||
if (aValue < bValue) return -1;
|
||||
if (aValue > bValue) return 1;
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
31
src/app/_helpers/collections/sum.spec.ts
Normal file
31
src/app/_helpers/collections/sum.spec.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 {sum, sumBy} from './sum';
|
||||
|
||||
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).toBe(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);
|
||||
expect(result).toBe(15);
|
||||
});
|
||||
});
|
||||
37
src/app/_helpers/collections/sum.ts
Normal file
37
src/app/_helpers/collections/sum.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sum an an array
|
||||
*/
|
||||
export function sumBy<T extends object>(
|
||||
collection: T[],
|
||||
transform: (value: T) => number | undefined,
|
||||
): number {
|
||||
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,
|
||||
);
|
||||
}
|
||||
25
src/app/_helpers/collections/zip.spec.ts
Normal file
25
src/app/_helpers/collections/zip.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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 {zip} from './zip';
|
||||
|
||||
describe('zip', function () {
|
||||
it('should zip arrays together', function () {
|
||||
expect(zip([1, 2, 3], [4, 5, 6])).toEqual([
|
||||
[1, 4],
|
||||
[2, 5],
|
||||
[3, 6],
|
||||
]);
|
||||
});
|
||||
});
|
||||
21
src/app/_helpers/collections/zip.ts
Normal file
21
src/app/_helpers/collections/zip.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Zip two arrays together.
|
||||
*/
|
||||
export function zip<T, U>(a: T[], b: U[]): [T, U][] {
|
||||
return a.map((_, i) => [a[i], b[i]]);
|
||||
}
|
||||
Reference in New Issue
Block a user