refactor: build system

This commit is contained in:
2023-03-22 11:45:30 +01:00
parent 4df19e8c20
commit 8cb9285462
427 changed files with 3978 additions and 9810 deletions

View File

@@ -16,12 +16,8 @@
/**
* 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,12 +16,9 @@
/**
* 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;
@@ -31,12 +28,9 @@ export function groupBy<T>(
/**
* 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;
@@ -46,9 +40,6 @@ export function groupByStable<T>(
/**
*
*/
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

@@ -0,0 +1,16 @@
export * from './chunk.js';
export * from './difference.js';
export * from './get.js';
export * from './group-by.js';
export * from './key-by.js';
export * from './map-values.js';
export * from './min.js';
export * from './omit.js';
export * from './partition.js';
export * from './pick.js';
export * from './shuffle.js';
export * from './string-sort.js';
export * from './sum.js';
export * from './tree-group.js';
export * from './uniq.js';
export * from './zip.js';

View File

@@ -19,10 +19,7 @@
* 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,11 +16,8 @@
/**
* 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,11 +16,8 @@
/**
* 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,15 +17,12 @@
* 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,10 +16,7 @@
/**
* 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];
@@ -33,7 +30,7 @@ export function pick<T extends object, U extends keyof T>(
*/
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,12 +25,10 @@ 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,20 +18,14 @@
*/
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,10 +22,7 @@ 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,14 +16,11 @@
/**
* 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>),
);
}