@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 CHANGED
@@ -220,6 +220,16 @@ const iterate = (values, action) => {
220
220
  action(value, values?.[index + 1], values?.[index - 1]);
221
221
  });
222
222
  };
223
+ const groupBy = (values, keySelector) => {
224
+ const map = new Map();
225
+ values.forEach((value) => {
226
+ const key = keySelector(value);
227
+ const items = map.get(key) || [];
228
+ items.push(value);
229
+ map.set(key, items);
230
+ });
231
+ return map;
232
+ };
223
233
 
224
234
  const DEFAULT_DELIMITER = ";";
225
235
  const splitRows = (data) => {
@@ -353,6 +363,37 @@ function sleep(ms) {
353
363
  return new Promise((resolve) => setTimeout(resolve, ms));
354
364
  }
355
365
 
366
+ const buildNode = async (record, { ancestors, nodesDict, childrenDict, nodeBuilder, }) => {
367
+ const children = childrenDict.get(nodeBuilder.idSelector(record)) || [];
368
+ const childrenNodes = await Promise.all(children.map((x) => buildNode(x, {
369
+ ancestors: [...ancestors, record],
370
+ nodesDict,
371
+ childrenDict,
372
+ nodeBuilder,
373
+ })));
374
+ const additionalProps = await nodeBuilder.additionalPropsBuilder?.(record, ancestors, children);
375
+ return {
376
+ ...(additionalProps ?? {}),
377
+ value: record,
378
+ id: nodeBuilder.idSelector(record),
379
+ children: childrenNodes,
380
+ };
381
+ };
382
+ const buildTree = async (data, nodeBuilder) => {
383
+ const nodesDict = toMap(data, (x) => nodeBuilder.idSelector(x));
384
+ const childrenDict = groupBy(data.filter((x) => nodeBuilder.parentIdSelector(x)), (x) => nodeBuilder.parentIdSelector(x));
385
+ const roots = data.filter((x) => !nodeBuilder.parentIdSelector(x));
386
+ const rootNodes = await Promise.all(roots.map((x) => buildNode(x, {
387
+ ancestors: [],
388
+ nodesDict,
389
+ childrenDict,
390
+ nodeBuilder,
391
+ })));
392
+ return {
393
+ root: rootNodes,
394
+ };
395
+ };
396
+
356
397
  const mapOrThrow = ({ mapper, throw: throwFn, }) => {
357
398
  const output = mapper();
358
399
  if (!output) {
@@ -28386,6 +28427,7 @@ const excelParse = (file, options) => {
28386
28427
 
28387
28428
  exports.Log = Log;
28388
28429
  exports.buildObject = buildObject;
28430
+ exports.buildTree = buildTree;
28389
28431
  exports.byField = byField;
28390
28432
  exports.byFieldDesc = byFieldDesc;
28391
28433
  exports.camelToKebabCase = camelToKebabCase;
@@ -28403,6 +28445,7 @@ exports.first = first;
28403
28445
  exports.flatten = flatten;
28404
28446
  exports.getDirectoryFilePaths = getDirectoryFilePaths;
28405
28447
  exports.getDirectoryPath = getDirectoryPath;
28448
+ exports.groupBy = groupBy;
28406
28449
  exports.indexes = indexes;
28407
28450
  exports.isNullOrUndefined = isNullOrUndefined;
28408
28451
  exports.iterate = iterate;