UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

41 lines (40 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Class {@link TransformUtil} encapsulate utility for transforming data from one format to another. */ class TransformUtil { /** * Groups an array of objects by the specified key. * * @template T The type of the objects in the array. * @param array The array of objects to be grouped. * @param key The key of the object by which to group the array. * @returns A record where each key is a distinct value from the specified key in the objects, and the value is an array of objects that share that key. */ static groupBy(array, key) { return array.reduce((acc, currentItem) => { const groupKey = currentItem[key]; if (!acc[groupKey]) { acc[groupKey] = []; } acc[groupKey].push(currentItem); return acc; }, {}); } /** * Transforms an array of data into a two-dimensional array of rows and columns. The length of the sub-arrays will be the specified number of columns. * @param data The array of data to be transformed. * @param columns The number of columns to be created in the two-dimensional array. * @returns A two-dimensional array of rows and columns, where each sub-array is of the specified length. */ static transformArrayToColumns(data, columns) { const result = new Array(); for (let i = 0; i < data.length; i += columns) { const row = data.slice(i, i + columns); result.push(row); } return result; } } exports.default = TransformUtil;