@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.
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) => {
@@ -323,6 +333,27 @@ const removeUndefinedProps = (obj, options = { recursive: false }) => {
323
333
  const isNullOrUndefined = (value) => {
324
334
  return value === null || value === undefined;
325
335
  };
336
+ const buildObject = (...props) => {
337
+ const out = {};
338
+ for (const prop of props) {
339
+ const propPath = prop.key.split(".");
340
+ const propName = propPath[0];
341
+ const nestedPropPath = propPath.slice(1).join(".");
342
+ if (nestedPropPath) {
343
+ out[propName] = {
344
+ ...(out[propName] ?? {}),
345
+ ...buildObject({
346
+ key: nestedPropPath,
347
+ value: prop.value,
348
+ }),
349
+ };
350
+ }
351
+ else {
352
+ out[propName] = prop.value;
353
+ }
354
+ }
355
+ return out;
356
+ };
326
357
 
327
358
  const pluralize = (word) => {
328
359
  return word.endsWith("s") ? `${word}es` : `${word}s`;
@@ -332,6 +363,37 @@ function sleep(ms) {
332
363
  return new Promise((resolve) => setTimeout(resolve, ms));
333
364
  }
334
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
+
335
397
  const mapOrThrow = ({ mapper, throw: throwFn, }) => {
336
398
  const output = mapper();
337
399
  if (!output) {
@@ -28364,6 +28426,8 @@ const excelParse = (file, options) => {
28364
28426
  };
28365
28427
 
28366
28428
  exports.Log = Log;
28429
+ exports.buildObject = buildObject;
28430
+ exports.buildTree = buildTree;
28367
28431
  exports.byField = byField;
28368
28432
  exports.byFieldDesc = byFieldDesc;
28369
28433
  exports.camelToKebabCase = camelToKebabCase;
@@ -28381,6 +28445,7 @@ exports.first = first;
28381
28445
  exports.flatten = flatten;
28382
28446
  exports.getDirectoryFilePaths = getDirectoryFilePaths;
28383
28447
  exports.getDirectoryPath = getDirectoryPath;
28448
+ exports.groupBy = groupBy;
28384
28449
  exports.indexes = indexes;
28385
28450
  exports.isNullOrUndefined = isNullOrUndefined;
28386
28451
  exports.iterate = iterate;