@punks/backend-core 0.0.27 → 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.
@@ -1,4 +1,5 @@
1
1
  export * from "./abstractions";
2
2
  export * from "./logging";
3
+ export * from "./models";
3
4
  export * from "./types";
4
5
  export * from "./utils";
@@ -0,0 +1 @@
1
+ export * from "./trees";
@@ -0,0 +1,8 @@
1
+ export interface ITreeNode<TKey, TValue> {
2
+ id: TKey;
3
+ value: TValue;
4
+ children: ITreeNode<TKey, TValue>[];
5
+ }
6
+ export interface ITree<TKey, TValue> {
7
+ root: ITreeNode<TKey, TValue>[];
8
+ }
@@ -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";
@@ -2,3 +2,7 @@ export declare const removeUndefinedProps: <T extends Record<string, any>>(obj:
2
2
  recursive?: boolean;
3
3
  }) => T;
4
4
  export declare const isNullOrUndefined: (value: any) => boolean;
5
+ export declare const buildObject: <T>(...props: {
6
+ key: string;
7
+ value: any;
8
+ }[]) => T;
@@ -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) => {
@@ -314,6 +324,27 @@ const removeUndefinedProps = (obj, options = { recursive: false }) => {
314
324
  const isNullOrUndefined = (value) => {
315
325
  return value === null || value === undefined;
316
326
  };
327
+ const buildObject = (...props) => {
328
+ const out = {};
329
+ for (const prop of props) {
330
+ const propPath = prop.key.split(".");
331
+ const propName = propPath[0];
332
+ const nestedPropPath = propPath.slice(1).join(".");
333
+ if (nestedPropPath) {
334
+ out[propName] = {
335
+ ...(out[propName] ?? {}),
336
+ ...buildObject({
337
+ key: nestedPropPath,
338
+ value: prop.value,
339
+ }),
340
+ };
341
+ }
342
+ else {
343
+ out[propName] = prop.value;
344
+ }
345
+ }
346
+ return out;
347
+ };
317
348
 
318
349
  const pluralize = (word) => {
319
350
  return word.endsWith("s") ? `${word}es` : `${word}s`;
@@ -323,6 +354,37 @@ function sleep(ms) {
323
354
  return new Promise((resolve) => setTimeout(resolve, ms));
324
355
  }
325
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
+
326
388
  const mapOrThrow = ({ mapper, throw: throwFn, }) => {
327
389
  const output = mapper();
328
390
  if (!output) {
@@ -28354,5 +28416,5 @@ const excelParse = (file, options) => {
28354
28416
  .map((x) => aggregateRow(header, x, options));
28355
28417
  };
28356
28418
 
28357
- export { ExcelKeyTransform, Log, LogLevel, 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 };
28358
28420
  //# sourceMappingURL=index.js.map