@rickard.antonsson/ts-utility 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # ts-utility
2
+ simple utility functions
@@ -0,0 +1 @@
1
+ export declare const groupBy: <T, U extends string | number>(list: T[], getter: (item: T) => U) => Partial<Record<U, T[]>>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupBy = void 0;
4
+ const groupBy = (list, getter) => {
5
+ const grouped = {};
6
+ list.forEach(item => {
7
+ var _a;
8
+ const by = getter(item);
9
+ grouped[by] = [...((_a = grouped[by]) !== null && _a !== void 0 ? _a : []), item];
10
+ });
11
+ return grouped;
12
+ };
13
+ exports.groupBy = groupBy;
14
+ //# sourceMappingURL=group-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-by.js","sourceRoot":"","sources":["../../../../src/functions/array/group-by.ts"],"names":[],"mappings":";;;AAAO,MAAM,OAAO,GAAG,CACrB,IAAS,EACT,MAAsB,EACG,EAAE;IAC3B,MAAM,OAAO,GAA4B,EAAE,CAAC;IAC5C,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;;QAClB,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QACxB,OAAO,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,MAAA,OAAO,CAAC,EAAE,CAAC,mCAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAVW,QAAA,OAAO,WAUlB"}
@@ -0,0 +1,7 @@
1
+ export declare const simpleSort: <T>(list: T[], ...sortByList: (((item: T) => string | [
2
+ string,
3
+ 'asc' | 'desc'
4
+ ]) | ((item: T) => number | [
5
+ number,
6
+ 'asc' | 'desc'
7
+ ]))[]) => T[];
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.simpleSort = void 0;
4
+ const simpleSort = (list, ...sortByList) => {
5
+ const sortOrderList = sortByList.map(sortBy => {
6
+ const value = sortBy(list[0]);
7
+ return Array.isArray(value) ? value[1] : 'asc';
8
+ });
9
+ const mapped = list.map((item, index) => [
10
+ index,
11
+ ...sortByList
12
+ .map(sortBy => {
13
+ const result = sortBy(item);
14
+ return Array.isArray(result) ? result[0] : result;
15
+ })
16
+ .reverse(),
17
+ ]);
18
+ sortByList.forEach((_item, index) => {
19
+ var _a;
20
+ const sortOrder = (_a = sortOrderList[index]) !== null && _a !== void 0 ? _a : 'asc';
21
+ mapped.sort((a, b) => {
22
+ const aValue = a[index + 1];
23
+ const bValue = b[index + 1];
24
+ if (typeof aValue === 'string' && typeof bValue === 'string') {
25
+ if (sortOrder === 'asc')
26
+ return aValue.localeCompare(bValue);
27
+ return bValue.localeCompare(aValue);
28
+ }
29
+ if (typeof aValue === 'number' && typeof bValue === 'number') {
30
+ if (sortOrder === 'asc')
31
+ return aValue - bValue;
32
+ return bValue - aValue;
33
+ }
34
+ return 0;
35
+ });
36
+ });
37
+ return mapped.map(([index]) => list[index]);
38
+ };
39
+ exports.simpleSort = simpleSort;
40
+ //# sourceMappingURL=simple-sort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-sort.js","sourceRoot":"","sources":["../../../../src/functions/array/simple-sort.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CACxB,IAAS,EACT,GAAG,UAGF,EACD,EAAE;IACF,MAAM,aAAa,GAAG,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;QAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,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CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9C,CAAC,CAAC;AAzCW,QAAA,UAAU,cAyCrB"}
@@ -0,0 +1,2 @@
1
+ import { List } from '../../interfaces/list';
2
+ export declare const filter: <T>(list: List<T>, filter: (item: T) => boolean) => List<T>;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filter = void 0;
4
+ const filter = (list, filter) => Object.entries(list).reduce((acc, [key, item]) => (filter(item) ? { ...acc, [key]: item } : acc), {});
5
+ exports.filter = filter;
6
+ //# sourceMappingURL=filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.js","sourceRoot":"","sources":["../../../../src/functions/list/filter.ts"],"names":[],"mappings":";;;AAEO,MAAM,MAAM,GAAG,CACpB,IAAa,EACb,MAA4B,EACnB,EAAE,CACX,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,CACzB,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAC,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAClE,EAAE,CACH,CAAC;AAPS,QAAA,MAAM,UAOf"}
@@ -0,0 +1,2 @@
1
+ import { List } from '../../interfaces/list';
2
+ export declare const map: <T, TFunction extends (item: T) => any>(items: List<T>, fn: TFunction) => List<ReturnType<TFunction>>;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.map = void 0;
4
+ const map = (items, fn) => Object.entries(items).reduce((acc, [key, item]) => ({ ...acc, [key]: fn(item) }), {});
5
+ exports.map = map;
6
+ //# sourceMappingURL=map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.js","sourceRoot":"","sources":["../../../../src/functions/list/map.ts"],"names":[],"mappings":";;;AAEO,MAAM,GAAG,GAAG,CACjB,KAAc,EACd,EAAa,EACgB,EAAE,CAC/B,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAC1B,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,EAAC,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,EAAC,CAAC,EACjD,EAAE,CACH,CAAC;AAPS,QAAA,GAAG,OAOZ"}
@@ -0,0 +1,2 @@
1
+ import { RecursivePartial } from '../../types/recursive-partial';
2
+ export declare function recursiveSet<T extends {}>(target: T, properties?: RecursivePartial<T>): T;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.recursiveSet = void 0;
4
+ function recursiveSet(target, properties) {
5
+ let result = target;
6
+ if (!properties)
7
+ return result;
8
+ for (const key in result) {
9
+ const currentValue = properties[key];
10
+ if (typeof currentValue === 'undefined')
11
+ continue;
12
+ if (typeof currentValue === 'object' && !Array.isArray(currentValue)) {
13
+ const resultValue = result[key];
14
+ const innerResult = recursiveSet(resultValue, currentValue);
15
+ if (innerResult === resultValue)
16
+ continue;
17
+ result = {
18
+ ...result,
19
+ [key]: {
20
+ ...resultValue,
21
+ ...innerResult,
22
+ },
23
+ };
24
+ }
25
+ else {
26
+ result = {
27
+ ...result,
28
+ [key]: currentValue,
29
+ };
30
+ }
31
+ }
32
+ return result;
33
+ }
34
+ exports.recursiveSet = recursiveSet;
35
+ //# sourceMappingURL=recursive-set.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recursive-set.js","sourceRoot":"","sources":["../../../../src/functions/other/recursive-set.ts"],"names":[],"mappings":";;;AAEA,SAAgB,YAAY,CAC1B,MAAS,EACT,UAAgC;IAEhC,IAAI,MAAM,GAAG,MAAM,CAAC;IAEpB,IAAI,CAAC,UAAU;QAAE,OAAO,MAAM,CAAC;IAE/B,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;QACxB,MAAM,YAAY,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAErC,IAAI,OAAO,YAAY,KAAK,WAAW;YAAE,SAAS;QAElD,IAAI,OAAO,YAAY,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE;YACpE,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YAChC,MAAM,WAAW,GAAG,YAAY,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;YAC5D,IAAI,WAAW,KAAK,WAAW;gBAAE,SAAS;YAE1C,MAAM,GAAG;gBACP,GAAG,MAAM;gBACT,CAAC,GAAG,CAAC,EAAE;oBACL,GAAG,WAAW;oBACd,GAAG,WAAW;iBACf;aACF,CAAC;SACH;aAAM;YACL,MAAM,GAAG;gBACP,GAAG,MAAM;gBACT,CAAC,GAAG,CAAC,EAAE,YAAY;aACpB,CAAC;SACH;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAlCD,oCAkCC"}
@@ -0,0 +1,3 @@
1
+ export interface List<T> {
2
+ [key: string | number]: T;
3
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list.js","sourceRoot":"","sources":["../../../src/interfaces/list.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export declare type RecursivePartial<T> = {
2
+ [P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : T[P];
3
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=recursive-partial.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recursive-partial.js","sourceRoot":"","sources":["../../../src/types/recursive-partial.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@rickard.antonsson/ts-utility",
3
+ "version": "0.0.0",
4
+ "description": "",
5
+ "main": "build/src/index.js",
6
+ "types": "build/src/index.d.ts",
7
+ "files": [
8
+ "build/src"
9
+ ],
10
+ "license": "Apache-2.0",
11
+ "keywords": [],
12
+ "scripts": {
13
+ "test": "jest",
14
+ "lint": "gts lint",
15
+ "clean": "gts clean",
16
+ "compile": "tsc",
17
+ "fix": "gts fix",
18
+ "prepare": "npm.cmd run compile",
19
+ "pretest": "npm.cmd run compile",
20
+ "posttest": "npm.cmd run lint"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/core": "^7.16.0",
24
+ "@babel/preset-env": "^7.16.4",
25
+ "@babel/preset-typescript": "^7.16.0",
26
+ "@types/jest": "^27.0.3",
27
+ "@types/node": "^14.11.2",
28
+ "babel-jest": "^27.4.2",
29
+ "gts": "^3.1.0",
30
+ "jest": "^27.4.3",
31
+ "typescript": "^4.0.3"
32
+ }
33
+ }