@nu-art/ts-common 0.201.21 → 0.201.23

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.
@@ -9,6 +9,7 @@ export declare class ModuleManager extends Logger {
9
9
  protected modules: Module<any, any>[];
10
10
  static instance: ModuleManager;
11
11
  protected constructor();
12
+ private static resetForTests;
12
13
  filterModules<T>(filter: (module: Module) => boolean): T[];
13
14
  setConfig(config: object): this;
14
15
  addModulePack(modules: Module[]): this;
@@ -38,6 +38,12 @@ class ModuleManager extends Logger_1.Logger {
38
38
  ModuleManager.instance = this;
39
39
  dispatcher_1.Dispatcher.modulesResolver = moduleResolver;
40
40
  }
41
+ // @ts-ignore
42
+ static resetForTests() {
43
+ _modules.length = 0;
44
+ // @ts-ignore
45
+ delete ModuleManager.instance;
46
+ }
41
47
  filterModules(filter) {
42
48
  return this.modules.filter(filter);
43
49
  }
@@ -22,7 +22,7 @@ class MemStorage {
22
22
  this.set = (key, value) => {
23
23
  // console.log(`-- ${this.cache.__myId} set: ${key.key} -> `, value);
24
24
  const currentValue = this.cache[key.key];
25
- if ((0, tools_1.exists)(currentValue) && key.unique) {
25
+ if ((0, tools_1.exists)(currentValue) && key.unique && value !== currentValue) {
26
26
  throw new exceptions_1.BadImplementationException(`Unique storage key is being overridden for key: ${key.key}
27
27
  \ncurrent: ${(0, tools_1.__stringify)(currentValue)}
28
28
  \nnew: ${(0, tools_1.__stringify)(value)}`);
@@ -58,9 +58,15 @@ class MemKey {
58
58
  });
59
59
  this.get = (value) => {
60
60
  // @ts-ignore
61
- return asyncLocalStorage.getStore().get(this, value);
61
+ const memValue = asyncLocalStorage.getStore().get(this, value);
62
+ if (!(0, tools_1.exists)(memValue))
63
+ throw new exceptions_1.BadImplementationException(`Trying to access MemKey(${this.key}) before it was set!`);
64
+ return memValue;
62
65
  };
63
66
  this.set = (value) => {
67
+ // console.log(this.key, value);
68
+ if (!(0, tools_1.exists)(value))
69
+ throw new exceptions_1.BadImplementationException(`Cannot set MemKey(${this.key}) to undefined or null!`);
64
70
  // @ts-ignore
65
71
  return asyncLocalStorage.getStore().set(this, value);
66
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/ts-common",
3
- "version": "0.201.21",
3
+ "version": "0.201.23",
4
4
  "description": "js and ts infra",
5
5
  "keywords": [
6
6
  "TacB0sS",
package/testing/consts.js CHANGED
@@ -13,6 +13,7 @@ exports.expectFailAsync = exports.testSuiteTester = exports.testSuite_RunTest =
13
13
  const chai_1 = require("chai");
14
14
  const module_manager_1 = require("../core/module-manager");
15
15
  const tools_1 = require("../utils/tools");
16
+ const MemStorage_1 = require("../mem-storage/MemStorage");
16
17
  class ModuleManagerTester extends module_manager_1.ModuleManager {
17
18
  constructor() {
18
19
  super();
@@ -30,7 +31,7 @@ const testSuiteTester = (testSuit, ...testcases) => {
30
31
  it(`${testSuit.label} - Preprocessing`, testSuit.preProcessor);
31
32
  }
32
33
  (testcases.length > 0 ? testcases : testSuit.testcases).forEach(testCase => {
33
- testSuite_RunTest(testSuit, testCase);
34
+ new MemStorage_1.MemStorage().init(() => __awaiter(void 0, void 0, void 0, function* () { return testSuite_RunTest(testSuit, testCase); }));
34
35
  });
35
36
  });
36
37
  };
@@ -92,3 +92,4 @@ export declare function asArray<T extends any>(toBeArray: T | T[]): T[];
92
92
  export declare function asOptionalArray<T extends any>(toBeArray?: T | T[]): T[] | undefined;
93
93
  export declare function lastElement<T extends any>(array: T[] | undefined): T | undefined;
94
94
  export declare function firstElement<T extends any>(array?: T[]): T | undefined;
95
+ export declare function arrayIncludesAny<T extends any>(arr1: T[], arr2: T[]): boolean;
@@ -26,7 +26,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
26
26
  });
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.firstElement = exports.lastElement = exports.asOptionalArray = exports.asArray = exports.generateArray = exports.toggleInArray = exports.groupArrayBy = exports.flatArray = exports.batchActionParallel = exports.batchAction = exports.sortArray = exports.reduceToMap = exports.arrayToMap = exports.filterFalsy = exports.filterInstances = exports.filterDuplicates = exports.findDuplicates = exports.filterAsync = exports.toggleElementInArray = exports.addItemToArrayAtIndex = exports.addItemToArray = exports.removeFromArrayByIndex = exports.removeFromArray = exports.removeItemFromArray = exports.filterInOut = void 0;
29
+ exports.arrayIncludesAny = exports.firstElement = exports.lastElement = exports.asOptionalArray = exports.asArray = exports.generateArray = exports.toggleInArray = exports.groupArrayBy = exports.flatArray = exports.batchActionParallel = exports.batchAction = exports.sortArray = exports.reduceToMap = exports.arrayToMap = exports.filterFalsy = exports.filterInstances = exports.filterDuplicates = exports.findDuplicates = exports.filterAsync = exports.toggleElementInArray = exports.addItemToArrayAtIndex = exports.addItemToArray = exports.removeFromArrayByIndex = exports.removeFromArray = exports.removeItemFromArray = exports.filterInOut = void 0;
30
30
  const tools_1 = require("./tools");
31
31
  const object_tools_1 = require("./object-tools");
32
32
  function filterInOut(input, filter) {
@@ -265,3 +265,7 @@ function firstElement(array) {
265
265
  return array === null || array === void 0 ? void 0 : array[1];
266
266
  }
267
267
  exports.firstElement = firstElement;
268
+ function arrayIncludesAny(arr1, arr2) {
269
+ return arr1.some(item => arr2.includes(item));
270
+ }
271
+ exports.arrayIncludesAny = arrayIncludesAny;
@@ -25,6 +25,8 @@ function mergeObject(original, override) {
25
25
  if (original === override) {
26
26
  return override;
27
27
  }
28
+ if (!(0, tools_1.exists)(original))
29
+ return (0, object_tools_1.filterKeys)(override);
28
30
  const returnValue = (0, object_tools_1.deepClone)(original);
29
31
  return Object.keys(override).reduce((obj, key) => {
30
32
  obj[key] = merge(original[key], override[key]);
@@ -60,7 +62,7 @@ function merge(original, override) {
60
62
  if (!(0, tools_1.exists)(override))
61
63
  return undefined;
62
64
  if (!(0, tools_1.exists)(original))
63
- return override;
65
+ return typeof override === 'object' ? (0, object_tools_1.filterKeys)(override) : override;
64
66
  if (typeof original !== typeof override || (typeof original === 'object' && typeof override === 'object' && Array.isArray(original) !== Array.isArray(override)))
65
67
  throw new exceptions_1.BadImplementationException(`trying to merge object of different types!! \\n Original: ${JSON.stringify(original)}\\n Override: ${JSON.stringify(override)}`);
66
68
  if (Array.isArray(original) && Array.isArray(override))
@@ -10,4 +10,4 @@ export declare function cloneObj<T extends TS_Object>(obj: T): T;
10
10
  export declare function partialCompare<T extends any>(one?: T, two?: T, keysToFilterOut?: (keyof T)[]): boolean;
11
11
  export declare function compare<T extends any>(one?: T, two?: T, keys?: (keyof T)[]): boolean;
12
12
  export declare function assert<T>(message: string, expected: T, actual: T): void;
13
- export declare function filterKeys<T extends TS_Object = TS_Object>(obj: T, keys: keyof T | (keyof T)[], filter?: (k: keyof T, obj: T) => boolean): T;
13
+ export declare function filterKeys<T extends TS_Object = TS_Object>(obj: T, keys?: keyof T | (keyof T)[], filter?: (k: keyof T, obj: T) => boolean): T;
@@ -19,6 +19,7 @@
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.filterKeys = exports.assert = exports.compare = exports.partialCompare = exports.cloneObj = exports.cloneArr = exports._setTypedProp = exports._values = exports._keys = exports.deepClone = void 0;
21
21
  const exceptions_1 = require("../core/exceptions/exceptions");
22
+ const array_tools_1 = require("./array-tools");
22
23
  function deepClone(obj) {
23
24
  if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || typeof obj === 'undefined' || obj === null)
24
25
  return obj;
@@ -110,13 +111,11 @@ function assert(message, expected, actual) {
110
111
  throw new exceptions_1.AssertionException(`Assertion Failed:\n -- ${message}\n -- Expected: ${JSON.stringify(expected)}\n -- Actual: ${JSON.stringify(actual)}\n\n`);
111
112
  }
112
113
  exports.assert = assert;
113
- function filterKeys(obj, keys, filter = (k) => obj[k] === undefined || obj[k] === null) {
114
+ function filterKeys(obj, keys = _keys(obj), filter = (k) => obj[k] === undefined || obj[k] === null) {
114
115
  if (typeof obj !== 'object' || obj === null) {
115
116
  throw new exceptions_1.BadImplementationException('Passed parameter for "obj" must be an object');
116
117
  }
117
- if (!Array.isArray(keys))
118
- keys = [keys];
119
- keys.forEach(key => {
118
+ (0, array_tools_1.asArray)(keys).forEach(key => {
120
119
  if (filter(key, obj))
121
120
  delete obj[key];
122
121
  });
@@ -11,6 +11,7 @@ type validatorObject<T> = {
11
11
  export declare const tsValidateUnionV3: <T extends unknown>(validatorObject: validatorObject<T>, mandatory?: boolean) => ((input?: T | undefined) => any)[];
12
12
  export declare const tsValidateArray: <T extends any[], I extends ArrayType<T> = ArrayType<T>>(validator: ValidatorTypeResolver<I>, mandatory?: boolean, minimumLength?: number) => Validator<I[]>;
13
13
  export declare const tsValidateString: (length?: number, mandatory?: boolean) => Validator<string>;
14
+ export declare const tsValidateStringMinLength: (length: number, mandatory?: boolean) => Validator<string>;
14
15
  export declare const tsValidateNumber: (mandatory?: boolean) => Validator<number>;
15
16
  export declare const tsValidateEnum: (enumType: TypedMap<number | string>, mandatory?: boolean) => Validator<number | string>;
16
17
  export declare const tsValidateBoolean: (mandatory?: boolean) => Validator<boolean>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tsValidator_ArrayOfObjectsByKey = exports.tsValidator_valueByKey = exports.tsValidateNonMandatoryObject = exports.tsValidateAudit = exports.tsValidateTimestamp = exports.tsValidateRegexp = exports.tsValidateRange = exports.tsValidateIsInRange = exports.tsValidateValue = exports.tsValidateBoolean = exports.tsValidateEnum = exports.tsValidateNumber = exports.tsValidateString = exports.tsValidateArray = exports.tsValidateUnionV3 = exports.tsValidateCustom = exports.tsValidateUnion = exports.tsValidateDynamicObject = void 0;
3
+ exports.tsValidator_ArrayOfObjectsByKey = exports.tsValidator_valueByKey = exports.tsValidateNonMandatoryObject = exports.tsValidateAudit = exports.tsValidateTimestamp = exports.tsValidateRegexp = exports.tsValidateRange = exports.tsValidateIsInRange = exports.tsValidateValue = exports.tsValidateBoolean = exports.tsValidateEnum = exports.tsValidateNumber = exports.tsValidateStringMinLength = exports.tsValidateString = exports.tsValidateArray = exports.tsValidateUnionV3 = exports.tsValidateCustom = exports.tsValidateUnion = exports.tsValidateDynamicObject = void 0;
4
4
  const tools_1 = require("../utils/tools");
5
5
  const validator_core_1 = require("./validator-core");
6
6
  const date_time_tools_1 = require("../utils/date-time-tools");
@@ -85,6 +85,18 @@ const tsValidateString = (length = -1, mandatory = true) => {
85
85
  }];
86
86
  };
87
87
  exports.tsValidateString = tsValidateString;
88
+ const tsValidateStringMinLength = (length, mandatory = true) => {
89
+ return [(0, validator_core_1.tsValidateExists)(mandatory),
90
+ (input) => {
91
+ // noinspection SuspiciousTypeOfGuard
92
+ if (typeof input !== 'string')
93
+ return `input is not a string`;
94
+ if (input.length >= length)
95
+ return;
96
+ return `input has less than ${length} chars`;
97
+ }];
98
+ };
99
+ exports.tsValidateStringMinLength = tsValidateStringMinLength;
88
100
  const tsValidateNumber = (mandatory = true) => {
89
101
  return [(0, validator_core_1.tsValidateExists)(mandatory),
90
102
  (input) => {
@@ -16,7 +16,7 @@ exports.tsValidateOptional = (0, validator_core_1.tsValidateExists)(false);
16
16
  exports.dbIdLength = 32;
17
17
  const tsValidateId = (length, mandatory = true) => (0, type_validators_1.tsValidateRegexp)(new RegExp(`^[0-9a-f]{${length}}$`), mandatory);
18
18
  exports.tsValidateId = tsValidateId;
19
- exports.tsValidateEmail = (0, type_validators_1.tsValidateRegexp)(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
19
+ exports.tsValidateEmail = (0, type_validators_1.tsValidateRegexp)(/[a-z0-9][a-z0-9!#$%&'*+/=?^_`{|}~\-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~\-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/);
20
20
  const tsValidateBucketUrl = (mandatory) => (0, type_validators_1.tsValidateRegexp)(/gs?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/, mandatory);
21
21
  exports.tsValidateBucketUrl = tsValidateBucketUrl;
22
22
  const tsValidateGeneralUrl = (mandatory) => (0, type_validators_1.tsValidateRegexp)(/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/, mandatory);