@trackunit/shared-utils 0.0.14 → 0.0.16-alpha-500b40a315.0

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
  *
@@ -965,6 +993,7 @@ exports.getPointCoordinateFromGeoJsonObject = getPointCoordinateFromGeoJsonObjec
965
993
  exports.getResizedDimensions = getResizedDimensions;
966
994
  exports.getStartOfDay = getStartOfDay;
967
995
  exports.groupBy = groupBy;
996
+ exports.groupTinyDataToOthers = groupTinyDataToOthers;
968
997
  exports.hourIntervals = hourIntervals;
969
998
  exports.intersection = intersection;
970
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
  *
@@ -932,4 +960,4 @@ const titleCase = (s) => {
932
960
  */
933
961
  const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
934
962
 
935
- 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,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "0.0.14",
3
+ "version": "0.0.16-alpha-500b40a315.0",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -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;
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";