@punks/backend-core 0.0.28 → 0.0.29
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/dist/cjs/index.js +43 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/models/index.d.ts +1 -0
- package/dist/cjs/types/models/trees.d.ts +8 -0
- package/dist/cjs/types/utils/array.d.ts +1 -0
- package/dist/cjs/types/utils/index.d.ts +1 -0
- package/dist/cjs/types/utils/trees.d.ts +7 -0
- package/dist/cjs/types/utils/trees.spec.d.ts +1 -0
- package/dist/esm/index.js +42 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/esm/types/models/index.d.ts +1 -0
- package/dist/esm/types/models/trees.d.ts +8 -0
- package/dist/esm/types/utils/array.d.ts +1 -0
- package/dist/esm/types/utils/index.d.ts +1 -0
- package/dist/esm/types/utils/trees.d.ts +7 -0
- package/dist/esm/types/utils/trees.spec.d.ts +1 -0
- package/dist/index.d.ts +18 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./trees";
|
|
@@ -21,4 +21,5 @@ export declare const distinct: <T>(values: T[]) => T[];
|
|
|
21
21
|
export declare const distinctElements: <T>(values: T[], comparer: (value: T) => any) => T[];
|
|
22
22
|
export declare const jsonDistinct: <T>(values: T[]) => T[];
|
|
23
23
|
export declare const iterate: <T>(values: T[], action: (item: T, next?: T | undefined, prev?: T | undefined) => void) => void;
|
|
24
|
+
export declare const groupBy: <TKey, TValue>(values: TValue[], keySelector: (value: TValue) => TKey) => Map<TKey, TValue[]>;
|
|
24
25
|
export {};
|
|
@@ -6,6 +6,7 @@ export * from "./strings";
|
|
|
6
6
|
export * from "./objects";
|
|
7
7
|
export * from "./text";
|
|
8
8
|
export * from "./threading";
|
|
9
|
+
export { buildTree, TreeNodeBuilder } from "./trees";
|
|
9
10
|
export * from "./mappings";
|
|
10
11
|
export * from "./uid";
|
|
11
12
|
export { ExcelSheetDefinition, ExcelKeyTransform, ExcelColumnDefinition as ExcelRowBuilder, ExcelParseOptions, excelBuild, excelParse, } from "./xls";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ITree, ITreeNode } from "../models";
|
|
2
|
+
export type TreeNodeBuilder<TKey, TRecord, TTreeNode extends ITreeNode<TKey, TRecord>> = {
|
|
3
|
+
idSelector: (record: TRecord) => TKey;
|
|
4
|
+
parentIdSelector: (record: TRecord) => TKey | undefined;
|
|
5
|
+
additionalPropsBuilder?: (record: TRecord, ancestors: TRecord[], children: TRecord[]) => Promise<Omit<TTreeNode, "id" | "children" | "value">>;
|
|
6
|
+
};
|
|
7
|
+
export declare const buildTree: <TKey, TRecord, TTreeNode extends ITreeNode<TKey, TRecord>>(data: TRecord[], nodeBuilder: TreeNodeBuilder<TKey, TRecord, TTreeNode>) => Promise<ITree<TKey, TRecord>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/esm/index.js
CHANGED
|
@@ -211,6 +211,16 @@ const iterate = (values, action) => {
|
|
|
211
211
|
action(value, values?.[index + 1], values?.[index - 1]);
|
|
212
212
|
});
|
|
213
213
|
};
|
|
214
|
+
const groupBy = (values, keySelector) => {
|
|
215
|
+
const map = new Map();
|
|
216
|
+
values.forEach((value) => {
|
|
217
|
+
const key = keySelector(value);
|
|
218
|
+
const items = map.get(key) || [];
|
|
219
|
+
items.push(value);
|
|
220
|
+
map.set(key, items);
|
|
221
|
+
});
|
|
222
|
+
return map;
|
|
223
|
+
};
|
|
214
224
|
|
|
215
225
|
const DEFAULT_DELIMITER = ";";
|
|
216
226
|
const splitRows = (data) => {
|
|
@@ -344,6 +354,37 @@ function sleep(ms) {
|
|
|
344
354
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
345
355
|
}
|
|
346
356
|
|
|
357
|
+
const buildNode = async (record, { ancestors, nodesDict, childrenDict, nodeBuilder, }) => {
|
|
358
|
+
const children = childrenDict.get(nodeBuilder.idSelector(record)) || [];
|
|
359
|
+
const childrenNodes = await Promise.all(children.map((x) => buildNode(x, {
|
|
360
|
+
ancestors: [...ancestors, record],
|
|
361
|
+
nodesDict,
|
|
362
|
+
childrenDict,
|
|
363
|
+
nodeBuilder,
|
|
364
|
+
})));
|
|
365
|
+
const additionalProps = await nodeBuilder.additionalPropsBuilder?.(record, ancestors, children);
|
|
366
|
+
return {
|
|
367
|
+
...(additionalProps ?? {}),
|
|
368
|
+
value: record,
|
|
369
|
+
id: nodeBuilder.idSelector(record),
|
|
370
|
+
children: childrenNodes,
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
const buildTree = async (data, nodeBuilder) => {
|
|
374
|
+
const nodesDict = toMap(data, (x) => nodeBuilder.idSelector(x));
|
|
375
|
+
const childrenDict = groupBy(data.filter((x) => nodeBuilder.parentIdSelector(x)), (x) => nodeBuilder.parentIdSelector(x));
|
|
376
|
+
const roots = data.filter((x) => !nodeBuilder.parentIdSelector(x));
|
|
377
|
+
const rootNodes = await Promise.all(roots.map((x) => buildNode(x, {
|
|
378
|
+
ancestors: [],
|
|
379
|
+
nodesDict,
|
|
380
|
+
childrenDict,
|
|
381
|
+
nodeBuilder,
|
|
382
|
+
})));
|
|
383
|
+
return {
|
|
384
|
+
root: rootNodes,
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
|
|
347
388
|
const mapOrThrow = ({ mapper, throw: throwFn, }) => {
|
|
348
389
|
const output = mapper();
|
|
349
390
|
if (!output) {
|
|
@@ -28375,5 +28416,5 @@ const excelParse = (file, options) => {
|
|
|
28375
28416
|
.map((x) => aggregateRow(header, x, options));
|
|
28376
28417
|
};
|
|
28377
28418
|
|
|
28378
|
-
export { ExcelKeyTransform, Log, LogLevel, buildObject, byField, byFieldDesc, camelToKebabCase, camelToSnakeCase, csvBuild, csvParse, distinct, distinctElements, ensureDirectory, ensureStartSlash, ensureTailingSlash, excelBuild, excelParse, first, flatten, getDirectoryFilePaths, getDirectoryPath, indexes, isNullOrUndefined, iterate, joinPath, jsonDistinct, last, mapOrThrow, newUuid, notNull, notUndefined, pluralize, range, removeUndefinedProps, selectMany, sleep, sort, splitPath, toCamelCase, toDict, toItemsDict, toItemsMap, toMap, toTitleCase, trim$1 as trim, trimEnd, trimStart };
|
|
28419
|
+
export { ExcelKeyTransform, Log, LogLevel, buildObject, buildTree, byField, byFieldDesc, camelToKebabCase, camelToSnakeCase, csvBuild, csvParse, distinct, distinctElements, ensureDirectory, ensureStartSlash, ensureTailingSlash, excelBuild, excelParse, first, flatten, getDirectoryFilePaths, getDirectoryPath, groupBy, indexes, isNullOrUndefined, iterate, joinPath, jsonDistinct, last, mapOrThrow, newUuid, notNull, notUndefined, pluralize, range, removeUndefinedProps, selectMany, sleep, sort, splitPath, toCamelCase, toDict, toItemsDict, toItemsMap, toMap, toTitleCase, trim$1 as trim, trimEnd, trimStart };
|
|
28379
28420
|
//# sourceMappingURL=index.js.map
|