topkat-utils 1.2.19 → 1.2.20

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### v1.2.20
2
+ * remove arrayToObjectSorted
3
+ * greatly improve typings on all compareArrays functions
4
+ * improve error display and logging, adding originalError to extraInfos, cleanStackTrace of original error
5
+ * added eslintconfig
6
+
1
7
  ### v1.2.17
2
8
  * added a relevant toString method to DescriptiveError class
3
9
  * improved error logging
package/README.md CHANGED
@@ -49,7 +49,6 @@ const topkatUtils = {
49
49
  getArrayDiff,
50
50
  getNotInArrayA,
51
51
  noDuplicateFilter,
52
- arrayToObjectSorted,
53
52
  pushIfNotExist,
54
53
  isNotEmptyArray,
55
54
  randomItemInArray,
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,oDAAiC;AACjC,mDAAgC;AAChC,kDAA+B;AAC/B,oDAAiC;AACjC,8CAA2B;AAC3B,qDAAkC;AAClC,mDAAgC;AAChC,mDAAgC;AAChC,oDAAiC;AACjC,qDAAkC;AAClC,qDAAkC;AAClC,kDAA+B;AAC/B,yDAAsC;AACtC,0DAAuC;AACvC,o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yFAAkF;AAElF,UAAU;AACG,QAAA,GAAG,GAAG,QAAQ,CAAA;AACd,QAAA,OAAO,GAAG,oBAAO,CAAA;AACjB,QAAA,gBAAgB,GAAG,+BAAiB,CAAA;AAC1C,MAAM,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAA,4DAA2B,EAAC,CAAC,CAAC,CAAC,CAAA;AAAlE,QAAA,gBAAgB,oBAAkD;AAClE,QAAA,mBAAmB,GAAG,0CAA2B,CAAA;AACjD,QAAA,cAAc,GAAG,0BAAY,CAAA;AAC7B,QAAA,QAAQ,GAAG,wBAAS,CAAA;AACpB,QAAA,cAAc,GAAG,wBAAS,CAAA;AAC1B,QAAA,QAAQ,GAAG,4BAAS,CAAA,CAAC,wBAAwB;AAC7C,QAAA,OAAO,GAAG,0BAAO,CAAA"}
@@ -9,46 +9,33 @@ export declare function shuffleArray(array: any): any;
9
9
  */
10
10
  export declare function ensureIsArrayAndPush(obj: object, addr: string, valToPush: any, onlyUniqueValues: Function): unknown;
11
11
  /** If a string is provided, return it as array else return the value */
12
- export declare function strAsArray(arrOrStr: any): any;
13
- declare type AsArrReturnVal<T, X> = T extends undefined ? (X extends undefined ? void : X) : T extends any[] ? T : T[];
12
+ export declare function strAsArray<T>(arrOrStr: T): T extends string ? string[] : T;
13
+ type AsArrReturnVal<T, X> = T extends undefined ? (X extends undefined ? void : X) : T extends any[] ? T : T[];
14
14
  /** If not an array provided, return the array with the value
15
15
  * /!\ NOTE /!\ In case the value is null or undefined, it will return that value
16
16
  */
17
17
  export declare function asArray<T extends any[] | any, X>(item: T, returnValueIfUndefined?: X): AsArrReturnVal<T, X>;
18
- /** Array comparison
19
- * @return {object} { inCommon, notInB, notInA }
20
- */
21
- export declare function compareArrays(arrayA: any[], arrayB: any[], compare?: (a: any, b: any) => boolean): {
22
- inCommon: any[];
23
- notInB: any[];
24
- notInA: any[];
18
+ /** @return {object} { inCommon, notInB, notInA } */
19
+ export declare function compareArrays<T1, T2>(arrayA?: T1[], arrayB?: T2[], compare?: (a: T1 | T2, b: T1 | T2) => boolean): {
20
+ inCommon: ReturnType<typeof getArrayInCommon<T1, T2>>;
21
+ notInB: ReturnType<typeof getArrayInCommon<T1, T2>>;
22
+ notInA: ReturnType<typeof getArrayInCommon<T2, T1>>;
25
23
  };
26
- /**
27
- * @return [] only elements that are both in arrayA and arrayB
28
- */
29
- export declare function getArrayInCommon(arrayA?: any[], arrayB?: any[], compare?: (a: any, b: any) => boolean): any[];
30
- /**
31
- * @return [] only elements that are in arrayB and not in arrayA
32
- */
33
- export declare function getNotInArrayA(arrayA?: any[], arrayB?: any[], compare?: (a: any, b: any) => boolean): any[];
24
+ /** @return [] only elements that are both in arrayA and arrayB */
25
+ export declare function getArrayInCommon<T1, T2>(arrayA?: T1[], arrayB?: T2[], compare?: (a: T1 | T2, b: T1 | T2) => boolean): T1[];
26
+ /** @return [] only elements that are in arrayB and not in arrayA */
27
+ export declare function getNotInArrayA<T1, T2>(arrayA?: T1[], arrayB?: T2[], compare?: (a: T1 | T2, b: T1 | T2) => boolean): T2[];
34
28
  /**
35
29
  * @return [] only elements that are in neither arrayA and arrayB
36
30
  */
37
- export declare function getArrayDiff(arrayA?: any[], arrayB?: any[], compare?: (a: any, b: any) => boolean): any[];
31
+ export declare function getArrayDiff<T1, T2>(arrayA?: T1[], arrayB?: T2[], compare?: (a: T1 | T2, b: T1 | T2) => boolean): (T1 | T2)[];
38
32
  /** filter duplicate values in an array
39
33
  * @param {function} comparisonFn default:(a, b) => a === b. A function that shall return true if two values are considered equal
40
34
  * @return {array|function}
41
35
  */
42
- export declare function noDuplicateFilter(arr: any, comparisonFn?: (a: any, b: any) => boolean): any[];
36
+ export declare function noDuplicateFilter<T>(arr: T[], comparisonFn?: (a: T, b: T) => boolean): T[];
43
37
  /** Count number of occurence of item in array */
44
38
  export declare function arrayCount(item: any, arr: any[]): number;
45
- /**
46
- * Sort an array in an object of subArrays, no duplicate.
47
- * @param {Array} array
48
- * @param {function} getFieldFromItem (itemOfArray) => field[String|Number]
49
- * tell me how you want to sort your Array
50
- */
51
- export declare function arrayToObjectSorted(array: any, getFieldFromItem: any): {};
52
39
  /**
53
40
  * @param {Function} comparisonFunction default: (itemToPush, itemAlreadyInArray) => itemToPush === itemAlreadyInArray; comparison function to consider the added item duplicate
54
41
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.randomItemInArray = exports.isNotEmptyArray = exports.pushIfNotExist = exports.arrayToObjectSorted = exports.arrayCount = exports.noDuplicateFilter = exports.getArrayDiff = exports.getNotInArrayA = exports.getArrayInCommon = exports.compareArrays = exports.asArray = exports.strAsArray = exports.ensureIsArrayAndPush = exports.shuffleArray = void 0;
3
+ exports.randomItemInArray = exports.isNotEmptyArray = exports.pushIfNotExist = exports.arrayCount = exports.noDuplicateFilter = exports.getArrayDiff = exports.getNotInArrayA = exports.getArrayInCommon = exports.compareArrays = exports.asArray = exports.strAsArray = exports.ensureIsArrayAndPush = exports.shuffleArray = void 0;
4
4
  //----------------------------------------
5
5
  // ARRAY UTILS
6
6
  //----------------------------------------
@@ -53,10 +53,8 @@ function asArray(item, returnValueIfUndefined) {
53
53
  return Array.isArray(item) ? item : [item];
54
54
  }
55
55
  exports.asArray = asArray;
56
- /** Array comparison
57
- * @return {object} { inCommon, notInB, notInA }
58
- */
59
- function compareArrays(arrayA, arrayB, compare = (a, b) => a === b) {
56
+ /** @return {object} { inCommon, notInB, notInA } */
57
+ function compareArrays(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
60
58
  return {
61
59
  inCommon: getArrayInCommon(arrayA, arrayB, compare),
62
60
  notInB: getNotInArrayA(arrayB, arrayA, compare),
@@ -64,9 +62,7 @@ function compareArrays(arrayA, arrayB, compare = (a, b) => a === b) {
64
62
  };
65
63
  }
66
64
  exports.compareArrays = compareArrays;
67
- /**
68
- * @return [] only elements that are both in arrayA and arrayB
69
- */
65
+ /** @return [] only elements that are both in arrayA and arrayB */
70
66
  function getArrayInCommon(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
71
67
  if (!Array.isArray(arrayA) || !Array.isArray(arrayB))
72
68
  return [];
@@ -74,9 +70,7 @@ function getArrayInCommon(arrayA = [], arrayB = [], compare = (a, b) => a === b)
74
70
  return arrayA.filter(a => arrayB.some(b => compare(a, b)));
75
71
  }
76
72
  exports.getArrayInCommon = getArrayInCommon;
77
- /**
78
- * @return [] only elements that are in arrayB and not in arrayA
79
- */
73
+ /** @return [] only elements that are in arrayB and not in arrayA */
80
74
  function getNotInArrayA(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
81
75
  if (!Array.isArray(arrayA) && Array.isArray(arrayB))
82
76
  return arrayB;
@@ -106,21 +100,6 @@ function arrayCount(item, arr) {
106
100
  return arr.reduce((total, item2) => item === item2 ? total + 1 : total, 0);
107
101
  }
108
102
  exports.arrayCount = arrayCount;
109
- /**
110
- * Sort an array in an object of subArrays, no duplicate.
111
- * @param {Array} array
112
- * @param {function} getFieldFromItem (itemOfArray) => field[String|Number]
113
- * tell me how you want to sort your Array
114
- */
115
- function arrayToObjectSorted(array, getFieldFromItem) {
116
- const res = {};
117
- array.forEach(item => {
118
- (0, object_utils_1.objForceWriteIfNotSet)(res, getFieldFromItem(item), []);
119
- res[getFieldFromItem(item)].push(item);
120
- });
121
- return res;
122
- }
123
- exports.arrayToObjectSorted = arrayToObjectSorted;
124
103
  /**
125
104
  * @param {Function} comparisonFunction default: (itemToPush, itemAlreadyInArray) => itemToPush === itemAlreadyInArray; comparison function to consider the added item duplicate
126
105
  */
@@ -1 +1 @@
1
- {"version":3,"file":"array-utils.js","sourceRoot":"","sources":["../../src/array-utils.ts"],"names":[],"mappings":";;;AAAA,0CAA0C;AAC1C,cAAc;AACd,0CAA0C;AAC1C,iDAAwE;AACxE,mCAA+B;AAE/B,SAAgB,YAAY,CAAC,KAAK;IAC9B,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC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import { Color, ErrorOptions } from './types';
2
- export declare type TopkatUtilConfig = Partial<{
2
+ export type TopkatUtilConfig = Partial<{
3
3
  env: string;
4
4
  isProd: boolean;
5
5
  nbOfLogsToKeep: number;
@@ -43,9 +43,9 @@ export declare function getDuration(startDate: any, endDate: any, inMinutes?: bo
43
43
  * @param {Boolean} strict$ if true,
44
44
  */
45
45
  export declare function doDateOverlap(event1: any, event2: any, fieldNameForStartDate$?: string, fieldNameForEndDate$?: string, allowNull$?: boolean, strict$?: boolean): boolean;
46
- declare type DateAllFormat = DateObjectFormat | DateStringFormats;
47
- declare type DateStringFormats = 'dateInt8' | 'dateInt12' | 'humanReadableTimestamp';
48
- declare type DateObjectFormat = 'date';
46
+ type DateAllFormat = DateObjectFormat | DateStringFormats;
47
+ type DateStringFormats = 'dateInt8' | 'dateInt12' | 'humanReadableTimestamp';
48
+ type DateObjectFormat = 'date';
49
49
  export declare function nextWeekDay(fromDate: any, weekDayInt?: 0 | 1 | 2 | 3 | 4 | 5 | 6, outputFormat?: DateStringFormats, sameDayAllowed?: boolean): number;
50
50
  export declare function nextWeekDay(fromDate: any, weekDayInt?: 0 | 1 | 2 | 3 | 4 | 5 | 6, outputFormat?: DateObjectFormat, sameDayAllowed?: boolean): Date;
51
51
  /**
@@ -1,5 +1,5 @@
1
1
  import { Color } from "./types";
2
- declare type NotInfoLogLevel = 'error' | 'warn';
2
+ type NotInfoLogLevel = 'error' | 'warn';
3
3
  export declare const logger: any;
4
4
  /**
5
5
  // console colored output
@@ -1,8 +1,8 @@
1
1
  import { ObjectGeneric } from "./types";
2
2
  export declare function forI<T extends any[] | any>(nbIterations: number, callback: (number: number, previousValue: any, arrayOfPreviousValues: any[]) => T): T[];
3
3
  export declare function forIasync<T extends any[] | any>(nbIterations: number, callback: (number: any) => T): Promise<T[]>;
4
- export declare type RecursiveCallback = (item: any, addr: string, lastElementKey: string | number, parent: ObjectGeneric | any[]) => false | any;
5
- export declare type RecursiveConfig = {
4
+ export type RecursiveCallback = (item: any, addr: string, lastElementKey: string | number, parent: ObjectGeneric | any[]) => false | any;
5
+ export type RecursiveConfig = {
6
6
  disableCircularDependencyRemoval?: boolean;
7
7
  };
8
8
  /**
@@ -67,7 +67,7 @@ export declare function generateObjectId(): any;
67
67
  * * urlPathJoin('http:/', 'kikou.lol') => https://www.kikou.lol
68
68
  */
69
69
  export declare function urlPathJoin(...bits: string[]): string;
70
- export declare type MiniTemplaterOptions = {
70
+ export type MiniTemplaterOptions = {
71
71
  valueWhenNotSet?: string;
72
72
  regexp?: RegExp;
73
73
  valueWhenContentUndefined?: string;
@@ -1,9 +1,9 @@
1
- export declare type ObjectGeneric = {
1
+ export type ObjectGeneric = {
2
2
  [k: string]: any;
3
3
  };
4
- export declare type Color = [number, number, number];
5
- export declare type Override<T1, T2> = Omit<T1, keyof T2> & T2;
6
- export declare type ErrorOptions = {
4
+ export type Color = [number, number, number];
5
+ export type Override<T1, T2> = Omit<T1, keyof T2> & T2;
6
+ export type ErrorOptions = {
7
7
  err?: any;
8
8
  doNotThrow?: boolean;
9
9
  code?: number;
@@ -1,4 +1,4 @@
1
- export declare type BaseTypes = 'objectId' | 'dateInt6' | 'dateInt' | 'dateInt8' | 'dateInt12' | 'time' | 'humanReadableTimestamp' | 'date' | 'dateObject' | 'array' | 'object' | 'buffer' | 'string' | 'function' | 'boolean' | 'number' | 'bigint' | 'year' | 'email' | 'any';
1
+ export type BaseTypes = 'objectId' | 'dateInt6' | 'dateInt' | 'dateInt8' | 'dateInt12' | 'time' | 'humanReadableTimestamp' | 'date' | 'dateObject' | 'array' | 'object' | 'buffer' | 'string' | 'function' | 'boolean' | 'number' | 'bigint' | 'year' | 'email' | 'any';
2
2
  export declare function issetOr(...elms: any[]): boolean;
3
3
  export declare function isEmptyOrNotSet(...elms: any[]): boolean;
4
4
  export declare function isDateObject(variable: any): boolean;
@@ -46,7 +46,7 @@ export declare function checkCtxIntegrity(ctx: any): void;
46
46
  { name: [{'blahVar': blahVarValue, 'myOtherVar': myOtherVarValue}], type: 'string'} // multiple names for same check
47
47
  )
48
48
  ----------------------------------------*/
49
- export declare type ValidatorObject = {
49
+ export type ValidatorObject = {
50
50
  name?: string;
51
51
  value?: any;
52
52
  type?: BaseTypes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topkat-utils",
3
- "version": "1.2.19",
3
+ "version": "1.2.20",
4
4
  "type": "commonjs",
5
5
  "types": "index.ts",
6
6
  "main": "dist",
@@ -22,6 +22,6 @@
22
22
  "@types/node": "^18.11.18",
23
23
  "bump-simple": "^1.0.0",
24
24
  "eslint": "^8.0.0",
25
- "typescript": "^4.4.4"
25
+ "typescript": "^4.9.5"
26
26
  }
27
27
  }
@@ -34,12 +34,10 @@ export function ensureIsArrayAndPush(obj: object, addr: string, valToPush, onlyU
34
34
 
35
35
 
36
36
  /** If a string is provided, return it as array else return the value */
37
- export function strAsArray(arrOrStr) {
38
- return typeof arrOrStr === 'string' ? [arrOrStr] : arrOrStr
37
+ export function strAsArray<T>(arrOrStr: T): T extends string ? string[] : T {
38
+ return typeof arrOrStr === 'string' ? [arrOrStr] : arrOrStr as any
39
39
  }
40
40
 
41
- type NotType<type, T> = Exclude<T, type>
42
-
43
41
  type AsArrReturnVal<T, X> = T extends undefined ? (X extends undefined ? void : X) : T extends any[] ? T : T[]
44
42
  /** If not an array provided, return the array with the value
45
43
  * /!\ NOTE /!\ In case the value is null or undefined, it will return that value
@@ -49,10 +47,12 @@ export function asArray<T extends any[] | any, X>(item: T, returnValueIfUndefine
49
47
  else return Array.isArray(item) ? item : [item] as any
50
48
  }
51
49
 
52
- /** Array comparison
53
- * @return {object} { inCommon, notInB, notInA }
54
- */
55
- export function compareArrays(arrayA: any[], arrayB: any[], compare = (a, b) => a === b) {
50
+ /** @return {object} { inCommon, notInB, notInA } */
51
+ export function compareArrays<T1, T2>(arrayA: T1[] = [], arrayB: T2[] = [], compare = (a: T1 | T2, b: T1 | T2) => a === b): {
52
+ inCommon: ReturnType<typeof getArrayInCommon<T1, T2>>
53
+ notInB: ReturnType<typeof getArrayInCommon<T1, T2>>
54
+ notInA: ReturnType<typeof getArrayInCommon<T2, T1>>
55
+ } {
56
56
  return {
57
57
  inCommon: getArrayInCommon(arrayA, arrayB, compare),
58
58
  notInB: getNotInArrayA(arrayB, arrayA, compare),
@@ -60,18 +60,14 @@ export function compareArrays(arrayA: any[], arrayB: any[], compare = (a, b) =>
60
60
  }
61
61
  }
62
62
 
63
- /**
64
- * @return [] only elements that are both in arrayA and arrayB
65
- */
66
- export function getArrayInCommon(arrayA = [], arrayB = [], compare = (a, b) => a === b): any[] {
63
+ /** @return [] only elements that are both in arrayA and arrayB */
64
+ export function getArrayInCommon<T1, T2>(arrayA: T1[] = [], arrayB: T2[] = [], compare = (a: T1 | T2, b: T1 | T2) => a === b): T1[] {
67
65
  if (!Array.isArray(arrayA) || !Array.isArray(arrayB)) return []
68
66
  else return arrayA.filter(a => arrayB.some(b => compare(a, b)))
69
67
  }
70
68
 
71
- /**
72
- * @return [] only elements that are in arrayB and not in arrayA
73
- */
74
- export function getNotInArrayA(arrayA = [], arrayB = [], compare = (a, b) => a === b): any[] {
69
+ /** @return [] only elements that are in arrayB and not in arrayA */
70
+ export function getNotInArrayA<T1, T2>(arrayA: T1[] = [], arrayB: T2[] = [], compare = (a: T1 | T2, b: T1 | T2) => a === b): T2[] {
75
71
  if (!Array.isArray(arrayA) && Array.isArray(arrayB)) return arrayB
76
72
  else if (!Array.isArray(arrayB)) return []
77
73
  else return arrayB.filter(b => !arrayA.some(a => compare(a, b)))
@@ -80,7 +76,7 @@ export function getNotInArrayA(arrayA = [], arrayB = [], compare = (a, b) => a =
80
76
  /**
81
77
  * @return [] only elements that are in neither arrayA and arrayB
82
78
  */
83
- export function getArrayDiff(arrayA = [], arrayB = [], compare = (a, b) => a === b): any[] {
79
+ export function getArrayDiff<T1, T2>(arrayA: T1[] = [], arrayB: T2[] = [], compare = (a: T1 | T2, b: T1 | T2) => a === b): (T1 | T2)[] {
84
80
  return [...getNotInArrayA(arrayA, arrayB, compare), ...getNotInArrayA(arrayB, arrayA, compare)]
85
81
  }
86
82
 
@@ -88,7 +84,7 @@ export function getArrayDiff(arrayA = [], arrayB = [], compare = (a, b) => a ===
88
84
  * @param {function} comparisonFn default:(a, b) => a === b. A function that shall return true if two values are considered equal
89
85
  * @return {array|function}
90
86
  */
91
- export function noDuplicateFilter(arr, comparisonFn = (a, b) => a === b): any[] {
87
+ export function noDuplicateFilter<T>(arr: T[], comparisonFn = (a: T, b: T) => a === b): T[] {
92
88
  return arr.filter((a, i, arr) => arr.findIndex(b => comparisonFn(a, b)) === i)
93
89
  }
94
90
 
@@ -97,23 +93,6 @@ export function arrayCount(item: any, arr: any[]): number {
97
93
  return arr.reduce((total, item2) => item === item2 ? total + 1 : total, 0)
98
94
  }
99
95
 
100
- /**
101
- * Sort an array in an object of subArrays, no duplicate.
102
- * @param {Array} array
103
- * @param {function} getFieldFromItem (itemOfArray) => field[String|Number]
104
- * tell me how you want to sort your Array
105
- */
106
- export function arrayToObjectSorted(array, getFieldFromItem) {
107
- const res = {}
108
-
109
- array.forEach(item => {
110
- objForceWriteIfNotSet(res, getFieldFromItem(item), [])
111
- res[getFieldFromItem(item)].push(item)
112
- })
113
-
114
- return res
115
- }
116
-
117
96
  /**
118
97
  * @param {Function} comparisonFunction default: (itemToPush, itemAlreadyInArray) => itemToPush === itemAlreadyInArray; comparison function to consider the added item duplicate
119
98
  */