mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: migrate to esm
This commit is contained in:
@@ -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)));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
[[], []],
|
||||
[[], []]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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>)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user