@trackunit/shared-utils 0.0.13 → 0.0.15

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/index.cjs.js CHANGED
@@ -133,6 +133,34 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
133
133
  }
134
134
  };
135
135
 
136
+ /**
137
+ * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
138
+ * It can help charts to look better.
139
+ *
140
+ * @param data Input data
141
+ * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
142
+ * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
143
+ */
144
+ function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
145
+ if (!data || data.length < 2) {
146
+ return data;
147
+ }
148
+ const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
149
+ if (data.filter(x => x.count <= breakingPoint).length < 2) {
150
+ return data;
151
+ }
152
+ const others = data
153
+ .filter(x => x.count <= breakingPoint)
154
+ .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
155
+ const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
156
+ limitToStartGrouping / 100;
157
+ if (!canGroupToOthers) {
158
+ return data;
159
+ }
160
+ const temp = data.filter(x => x.count > breakingPoint);
161
+ return [...temp, others].sort((a, b) => a.count - b.count);
162
+ }
163
+
136
164
  /**
137
165
  * Converts meters to yards
138
166
  *
@@ -423,24 +451,28 @@ searchTerm) => {
423
451
  /**
424
452
  * Group an array of items by a key.
425
453
  *
426
- * @param list The array of items to group.
427
- * @param getKey A function to get the key to group by.
428
- * @returns A map of the items grouped by the key.
429
- * @example groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname) // Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
454
+ * @template T The type of items in the list.
455
+ * @template K The type of the key.
456
+ * @param {T[]} list The array of items to group.
457
+ * @param {(item: T) => K} getKey A function to get the key to group by.
458
+ * @returns {Map<K, T[]>} A map of the items grouped by the key.
459
+ * @example
460
+ * groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
461
+ * Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
430
462
  */
431
463
  function groupBy(list, getKey) {
432
- const map = new Map();
464
+ const groupedMap = new Map();
433
465
  list.forEach(item => {
434
466
  const key = getKey(item);
435
- const collection = map.get(key);
467
+ const collection = groupedMap.get(key);
436
468
  if (!collection) {
437
- map.set(key, [item]);
469
+ groupedMap.set(key, [item]);
438
470
  }
439
471
  else {
440
472
  collection.push(item);
441
473
  }
442
474
  });
443
- return map;
475
+ return groupedMap;
444
476
  }
445
477
 
446
478
  const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
@@ -961,6 +993,7 @@ exports.getPointCoordinateFromGeoJsonObject = getPointCoordinateFromGeoJsonObjec
961
993
  exports.getResizedDimensions = getResizedDimensions;
962
994
  exports.getStartOfDay = getStartOfDay;
963
995
  exports.groupBy = groupBy;
996
+ exports.groupTinyDataToOthers = groupTinyDataToOthers;
964
997
  exports.hourIntervals = hourIntervals;
965
998
  exports.intersection = intersection;
966
999
  exports.isStringArrayEqual = isStringArrayEqual;
package/index.esm.js CHANGED
@@ -129,6 +129,34 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
129
129
  }
130
130
  };
131
131
 
132
+ /**
133
+ * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
134
+ * It can help charts to look better.
135
+ *
136
+ * @param data Input data
137
+ * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
138
+ * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
139
+ */
140
+ function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
141
+ if (!data || data.length < 2) {
142
+ return data;
143
+ }
144
+ const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
145
+ if (data.filter(x => x.count <= breakingPoint).length < 2) {
146
+ return data;
147
+ }
148
+ const others = data
149
+ .filter(x => x.count <= breakingPoint)
150
+ .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
151
+ const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
152
+ limitToStartGrouping / 100;
153
+ if (!canGroupToOthers) {
154
+ return data;
155
+ }
156
+ const temp = data.filter(x => x.count > breakingPoint);
157
+ return [...temp, others].sort((a, b) => a.count - b.count);
158
+ }
159
+
132
160
  /**
133
161
  * Converts meters to yards
134
162
  *
@@ -419,24 +447,28 @@ searchTerm) => {
419
447
  /**
420
448
  * Group an array of items by a key.
421
449
  *
422
- * @param list The array of items to group.
423
- * @param getKey A function to get the key to group by.
424
- * @returns A map of the items grouped by the key.
425
- * @example groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname) // Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
450
+ * @template T The type of items in the list.
451
+ * @template K The type of the key.
452
+ * @param {T[]} list The array of items to group.
453
+ * @param {(item: T) => K} getKey A function to get the key to group by.
454
+ * @returns {Map<K, T[]>} A map of the items grouped by the key.
455
+ * @example
456
+ * groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
457
+ * Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
426
458
  */
427
459
  function groupBy(list, getKey) {
428
- const map = new Map();
460
+ const groupedMap = new Map();
429
461
  list.forEach(item => {
430
462
  const key = getKey(item);
431
- const collection = map.get(key);
463
+ const collection = groupedMap.get(key);
432
464
  if (!collection) {
433
- map.set(key, [item]);
465
+ groupedMap.set(key, [item]);
434
466
  }
435
467
  else {
436
468
  collection.push(item);
437
469
  }
438
470
  });
439
- return map;
471
+ return groupedMap;
440
472
  }
441
473
 
442
474
  const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
@@ -928,4 +960,4 @@ const titleCase = (s) => {
928
960
  */
929
961
  const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
930
962
 
931
- export { DateTimeFormat, HoursAndMinutesFormat, align, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, ensureArray, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, hourIntervals, intersection, isStringArrayEqual, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, titleCase, toID, toIDs, toUUID, toggle, trimIds, trimPath, truthy, unionArraysByKey };
963
+ export { DateTimeFormat, HoursAndMinutesFormat, align, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, ensureArray, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isStringArrayEqual, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, titleCase, toID, toIDs, toUUID, toggle, trimIds, trimPath, truthy, unionArraysByKey };
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=18.x"
8
8
  },
9
- "dependencies": {},
9
+ "dependencies": {
10
+ "@faker-js/faker": "7.6.0"
11
+ },
10
12
  "module": "./index.esm.js",
11
- "main": "./index.cjs.js"
13
+ "main": "./index.cjs.js",
14
+ "peerDependencies": {}
12
15
  }
@@ -0,0 +1,13 @@
1
+ export interface GroupingUtilityData {
2
+ key: string;
3
+ count: number;
4
+ }
5
+ /**
6
+ * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
7
+ * It can help charts to look better.
8
+ *
9
+ * @param data Input data
10
+ * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
11
+ * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
12
+ */
13
+ export declare function groupTinyDataToOthers(data: GroupingUtilityData[] | undefined, smallestCount?: number, limitToStartGrouping?: number): GroupingUtilityData[] | undefined;
@@ -1,9 +1,13 @@
1
1
  /**
2
2
  * Group an array of items by a key.
3
3
  *
4
- * @param list The array of items to group.
5
- * @param getKey A function to get the key to group by.
6
- * @returns A map of the items grouped by the key.
7
- * @example groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname) // Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
4
+ * @template T The type of items in the list.
5
+ * @template K The type of the key.
6
+ * @param {T[]} list The array of items to group.
7
+ * @param {(item: T) => K} getKey A function to get the key to group by.
8
+ * @returns {Map<K, T[]>} A map of the items grouped by the key.
9
+ * @example
10
+ * groupBy([{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }], p => p.surname)
11
+ * Returns: Map { "Doe" => [{ name: "John", surname: "Doe" }, { name: "Jane", surname: "Doe" }] }
8
12
  */
9
13
  export declare function groupBy<T, K>(list: T[], getKey: (item: T) => K): Map<K, T[]>;
package/src/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./DateTimeFormat";
2
2
  export * from "./DateUtils";
3
3
  export * from "./GeoJsonUtils";
4
+ export * from "./GroupingUtility";
4
5
  export * from "./UnitOfMeasurementConverter";
5
6
  export * from "./addressUtils";
6
7
  export * from "./arrayUtils";