@beecode/msh-util 2.0.0-alpha → 2.0.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.
Files changed (52) hide show
  1. package/lib/array-util.d.ts +23 -0
  2. package/lib/array-util.d.ts.map +1 -0
  3. package/lib/array-util.js +32 -0
  4. package/lib/class-factory-pattern.d.ts +25 -0
  5. package/lib/class-factory-pattern.d.ts.map +1 -0
  6. package/lib/class-factory-pattern.js +39 -0
  7. package/lib/express/error-handler.d.ts +17 -0
  8. package/lib/express/error-handler.d.ts.map +1 -0
  9. package/lib/express/error-handler.js +32 -0
  10. package/lib/index.d.ts +17 -0
  11. package/lib/index.d.ts.map +1 -0
  12. package/lib/index.js +108 -0
  13. package/lib/joi-util.d.ts +47 -0
  14. package/lib/joi-util.d.ts.map +1 -0
  15. package/lib/joi-util.js +107 -0
  16. package/lib/memoize-factory.d.ts +16 -0
  17. package/lib/memoize-factory.d.ts.map +1 -0
  18. package/lib/memoize-factory.js +34 -0
  19. package/lib/object-util.d.ts +72 -0
  20. package/lib/object-util.d.ts.map +1 -0
  21. package/lib/object-util.js +173 -0
  22. package/lib/package.json +1 -0
  23. package/lib/regex-util.d.ts +12 -0
  24. package/lib/regex-util.d.ts.map +1 -0
  25. package/lib/regex-util.js +17 -0
  26. package/lib/single-threshold-promise.d.ts +31 -0
  27. package/lib/single-threshold-promise.d.ts.map +1 -0
  28. package/lib/single-threshold-promise.js +95 -0
  29. package/lib/singleton/async.d.ts +50 -0
  30. package/lib/singleton/async.d.ts.map +1 -0
  31. package/lib/singleton/async.js +138 -0
  32. package/lib/singleton/pattern.d.ts +34 -0
  33. package/lib/singleton/pattern.d.ts.map +1 -0
  34. package/lib/singleton/pattern.js +46 -0
  35. package/lib/string-util.d.ts +10 -0
  36. package/lib/string-util.d.ts.map +1 -0
  37. package/lib/string-util.js +23 -0
  38. package/lib/time-util.d.ts +74 -0
  39. package/lib/time-util.d.ts.map +1 -0
  40. package/lib/time-util.js +127 -0
  41. package/lib/time-zone.d.ts +467 -0
  42. package/lib/time-zone.d.ts.map +1 -0
  43. package/lib/time-zone.js +473 -0
  44. package/lib/timeout.d.ts +15 -0
  45. package/lib/timeout.d.ts.map +1 -0
  46. package/lib/timeout.js +24 -0
  47. package/lib/type-util.d.ts +50 -0
  48. package/lib/type-util.d.ts.map +1 -0
  49. package/lib/type-util.js +59 -0
  50. package/lib/types/global.d.js +5 -0
  51. package/lib/types/types.d.js +3 -0
  52. package/package.json +2 -2
@@ -0,0 +1,23 @@
1
+ export declare const arrayUtil: {
2
+ /**
3
+ * Check if array element is not empty
4
+ * @template T
5
+ * @param {T | null | undefined} value
6
+ * @return {value is T}
7
+ * @example
8
+ * const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
9
+ * console.log(notEmptyArray)// [0, 1, 2, '']
10
+ */
11
+ notEmpty: <T>(value: T | null | undefined) => value is T;
12
+ /**
13
+ * Check if array element is not falsy
14
+ * @template T
15
+ * @param {T | null | undefined} value
16
+ * @return {value is T}
17
+ * @example
18
+ * const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
19
+ * console.log(notFalsyArray)// [1, 2]
20
+ */
21
+ notFalsy: <T_1>(value: T_1 | null | undefined) => value is T_1;
22
+ };
23
+ //# sourceMappingURL=array-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array-util.d.ts","sourceRoot":"","sources":["../src/array-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;IACrB;;;;;;;;OAQG;yBACkB,CAAC,GAAG,IAAI,GAAG,SAAS;IAGzC;;;;;;;;OAQG;;CAIH,CAAA"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.arrayUtil = void 0;
7
+ var arrayUtil = exports.arrayUtil = {
8
+ /**
9
+ * Check if array element is not empty
10
+ * @template T
11
+ * @param {T | null | undefined} value
12
+ * @return {value is T}
13
+ * @example
14
+ * const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
15
+ * console.log(notEmptyArray)// [0, 1, 2, '']
16
+ */
17
+ notEmpty: function notEmpty(value) {
18
+ return value !== null && value !== undefined;
19
+ },
20
+ /**
21
+ * Check if array element is not falsy
22
+ * @template T
23
+ * @param {T | null | undefined} value
24
+ * @return {value is T}
25
+ * @example
26
+ * const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
27
+ * console.log(notFalsyArray)// [1, 2]
28
+ */
29
+ notFalsy: function notFalsy(value) {
30
+ return !!value;
31
+ }
32
+ };
@@ -0,0 +1,25 @@
1
+ export type ClassType<T = object> = new (...args: T extends {
2
+ new (...args: infer P): any;
3
+ } ? P : never[]) => T;
4
+ /**
5
+ * This is a wrapper that easily converts class constructor call (`new className(..constructorParams)`) into function call (`classNameFactory(..constructorParams)`)
6
+ * @param {C} classType
7
+ * @template C
8
+ * @return {(...args: ConstructorParameters<C>) => InstanceType<C>}
9
+ * @example
10
+ * export class SomeClass {
11
+ * protected _a: string
12
+ *
13
+ * constructor(params: { a: string }) {
14
+ * const { a } = params
15
+ * this._a = a
16
+ * }
17
+ * }
18
+ *
19
+ * export const someClassFactory = classFactoryPattern(SomeClass)
20
+ *
21
+ * // using
22
+ * const someClassInstance = someClassFactory({ a: 'test' })
23
+ */
24
+ export declare const classFactoryPattern: <C extends ClassType<object>>(classType: C) => (...args: ConstructorParameters<C>) => InstanceType<C>;
25
+ //# sourceMappingURL=class-factory-pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class-factory-pattern.d.ts","sourceRoot":"","sources":["../src/class-factory-pattern.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,SAAS,CAAC,CAAC,GAAG,MAAM,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC,SAAS;IAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,GAAG,GAAG,CAAA;CAAE,GAAG,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,CAAA;AAE/G;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,mBAAmB,2CACpB,CAAC,eACC,sBAAsB,CAAC,CAAC,KAAK,aAAa,CAAC,CAIxD,CAAA"}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.classFactoryPattern = void 0;
7
+ function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
8
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
9
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+
12
+ /**
13
+ * This is a wrapper that easily converts class constructor call (`new className(..constructorParams)`) into function call (`classNameFactory(..constructorParams)`)
14
+ * @param {C} classType
15
+ * @template C
16
+ * @return {(...args: ConstructorParameters<C>) => InstanceType<C>}
17
+ * @example
18
+ * export class SomeClass {
19
+ * protected _a: string
20
+ *
21
+ * constructor(params: { a: string }) {
22
+ * const { a } = params
23
+ * this._a = a
24
+ * }
25
+ * }
26
+ *
27
+ * export const someClassFactory = classFactoryPattern(SomeClass)
28
+ *
29
+ * // using
30
+ * const someClassInstance = someClassFactory({ a: 'test' })
31
+ */
32
+ var classFactoryPattern = exports.classFactoryPattern = function classFactoryPattern(classType) {
33
+ return function () {
34
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
35
+ args[_key] = arguments[_key];
36
+ }
37
+ return _construct(classType, args);
38
+ };
39
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Wrap async express http request end return promise or call next on catch
3
+ * @param _target
4
+ * @param _key
5
+ * @param descriptor
6
+ * @example
7
+ * export class RootController {
8
+ * /@expressErrorHandler
9
+ * async login(req: Request, res: Response): Promise<void> {
10
+ * const { username, password } = validationUtil().sanitize(req.body, postLoginBodySchema)
11
+ * const result = await authorizationUseCase.login({ username, password })
12
+ * res.json(result)
13
+ * }
14
+ * }
15
+ */
16
+ export declare const expressErrorHandler: (_target: any, _key: string, descriptor: TypedPropertyDescriptor<any>) => any;
17
+ //# sourceMappingURL=error-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../src/express/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,eAAO,MAAM,mBAAmB,YAAa,GAAG,QAAQ,MAAM,cAAc,wBAAwB,GAAG,CAAC,KAAG,GAU1G,CAAA"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.expressErrorHandler = void 0;
7
+ /**
8
+ * Wrap async express http request end return promise or call next on catch
9
+ * @param _target
10
+ * @param _key
11
+ * @param descriptor
12
+ * @example
13
+ * export class RootController {
14
+ * /@expressErrorHandler
15
+ * async login(req: Request, res: Response): Promise<void> {
16
+ * const { username, password } = validationUtil().sanitize(req.body, postLoginBodySchema)
17
+ * const result = await authorizationUseCase.login({ username, password })
18
+ * res.json(result)
19
+ * }
20
+ * }
21
+ */
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ var expressErrorHandler = exports.expressErrorHandler = function expressErrorHandler(_target, _key, descriptor) {
24
+ var originalMethod = descriptor.value;
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ descriptor.value = function () {
27
+ var next = arguments[2]; // eslint-disable-line prefer-rest-params
28
+
29
+ return Promise.resolve(originalMethod.apply(this, arguments))["catch"](next); // eslint-disable-line prefer-rest-params
30
+ };
31
+ return descriptor;
32
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ export { expressErrorHandler } from './express/error-handler';
2
+ export { SingletonAsync } from './singleton/async';
3
+ export { singletonPattern } from './singleton/pattern';
4
+ export { classFactoryPattern } from './class-factory-pattern';
5
+ export type { ClassType } from './class-factory-pattern';
6
+ export { ErrorWithPayload, JoiUtil } from './joi-util';
7
+ export { memoizeFactory } from './memoize-factory';
8
+ export { ObjectUtil } from './object-util';
9
+ export type { ObjectType } from './object-util';
10
+ export { regexUtil } from './regex-util';
11
+ export { SingleThresholdPromise } from './single-threshold-promise';
12
+ export { stringUtil } from './string-util';
13
+ export { DurationUnit, TimeUtil } from './time-util';
14
+ export type { DurationUnitType } from './time-util';
15
+ export { timeout } from './timeout';
16
+ export { typeUtil } from './type-util';
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAEhE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAErD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAEhE,YAAY,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAE3D,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAEzD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAErD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAA;AAEtE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEvD,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "DurationUnit", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _timeUtil.DurationUnit;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "ErrorWithPayload", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _joiUtil.ErrorWithPayload;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "JoiUtil", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _joiUtil.JoiUtil;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "ObjectUtil", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _objectUtil.ObjectUtil;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "SingleThresholdPromise", {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _singleThresholdPromise.SingleThresholdPromise;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "SingletonAsync", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _async.SingletonAsync;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "TimeUtil", {
43
+ enumerable: true,
44
+ get: function get() {
45
+ return _timeUtil.TimeUtil;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "classFactoryPattern", {
49
+ enumerable: true,
50
+ get: function get() {
51
+ return _classFactoryPattern.classFactoryPattern;
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "expressErrorHandler", {
55
+ enumerable: true,
56
+ get: function get() {
57
+ return _errorHandler.expressErrorHandler;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "memoizeFactory", {
61
+ enumerable: true,
62
+ get: function get() {
63
+ return _memoizeFactory.memoizeFactory;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "regexUtil", {
67
+ enumerable: true,
68
+ get: function get() {
69
+ return _regexUtil.regexUtil;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "singletonPattern", {
73
+ enumerable: true,
74
+ get: function get() {
75
+ return _pattern.singletonPattern;
76
+ }
77
+ });
78
+ Object.defineProperty(exports, "stringUtil", {
79
+ enumerable: true,
80
+ get: function get() {
81
+ return _stringUtil.stringUtil;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "timeout", {
85
+ enumerable: true,
86
+ get: function get() {
87
+ return _timeout.timeout;
88
+ }
89
+ });
90
+ Object.defineProperty(exports, "typeUtil", {
91
+ enumerable: true,
92
+ get: function get() {
93
+ return _typeUtil.typeUtil;
94
+ }
95
+ });
96
+ var _errorHandler = require("./express/error-handler");
97
+ var _async = require("./singleton/async");
98
+ var _pattern = require("./singleton/pattern");
99
+ var _classFactoryPattern = require("./class-factory-pattern");
100
+ var _joiUtil = require("./joi-util");
101
+ var _memoizeFactory = require("./memoize-factory");
102
+ var _objectUtil = require("./object-util");
103
+ var _regexUtil = require("./regex-util");
104
+ var _singleThresholdPromise = require("./single-threshold-promise");
105
+ var _stringUtil = require("./string-util");
106
+ var _timeUtil = require("./time-util");
107
+ var _timeout = require("./timeout");
108
+ var _typeUtil = require("./type-util");
@@ -0,0 +1,47 @@
1
+ import { ObjectSchema, Schema, ValidationOptions } from 'joi';
2
+ export declare class ErrorWithPayload<T> extends Error {
3
+ payload: T;
4
+ constructor(message: string, payload: T);
5
+ }
6
+ /**
7
+ * This is a simple wrapper around Joi validation with two functions exposed validate and sanitize. If object is not valid function throws an error.
8
+ * @example
9
+ * type SomeType = {
10
+ * a: string
11
+ * b: number
12
+ * }
13
+ * const someSchema = Joi.object<SomeType>().keys({
14
+ * a: Joi.string().required(),
15
+ * b: Joi.number().required(),
16
+ * }).required()
17
+ *
18
+ * const joiUtil = new JoiUtil()
19
+ *
20
+ * // using
21
+ * const invalidObject = joiUtil.validate({}, someSchema) // validate throws an error
22
+ * const validObject = joiUtil.validate({ a: 'a', b: 1 }, someSchema)
23
+ */
24
+ export declare class JoiUtil {
25
+ /**
26
+ * Validate and clean object
27
+ * @template T
28
+ * @template Joi
29
+ * @param {any} objectToValidate
30
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
31
+ * @param {validationOptions} [validationOptions]
32
+ * @returns {T} expected object after validation
33
+ */
34
+ sanitize<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
35
+ /**
36
+ * Only validate properties specified in validation schema
37
+ * @template T
38
+ * @template Joi
39
+ * @param {any} objectToValidate
40
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
41
+ * @param {validationOptions} [validationOptions]
42
+ * @returns {T} expected object after validation
43
+ */
44
+ validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
45
+ protected _validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
46
+ }
47
+ //# sourceMappingURL=joi-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"joi-util.d.ts","sourceRoot":"","sources":["../src/joi-util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,KAAK,CAAA;AAE7D,qBAAa,gBAAgB,CAAC,CAAC,CAAE,SAAQ,KAAK;IAC7C,OAAO,EAAE,CAAC,CAAA;gBAEE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;CAIvC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,OAAO;IACnB;;;;;;;;OAQG;IAEH,QAAQ,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;IAI9G;;;;;;;;OAQG;IAEH,QAAQ,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;IAK9G,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;CAQzH"}
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.JoiUtil = exports.ErrorWithPayload = void 0;
8
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
12
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
15
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
17
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
18
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
19
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
20
+ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
21
+ function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
22
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
23
+ function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
24
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
25
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
26
+ var ErrorWithPayload = exports.ErrorWithPayload = /*#__PURE__*/function (_Error) {
27
+ function ErrorWithPayload(message, payload) {
28
+ var _this;
29
+ _classCallCheck(this, ErrorWithPayload);
30
+ _this = _callSuper(this, ErrorWithPayload, [message]);
31
+ _this.payload = payload;
32
+ return _this;
33
+ }
34
+ _inherits(ErrorWithPayload, _Error);
35
+ return _createClass(ErrorWithPayload);
36
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
37
+ /**
38
+ * This is a simple wrapper around Joi validation with two functions exposed validate and sanitize. If object is not valid function throws an error.
39
+ * @example
40
+ * type SomeType = {
41
+ * a: string
42
+ * b: number
43
+ * }
44
+ * const someSchema = Joi.object<SomeType>().keys({
45
+ * a: Joi.string().required(),
46
+ * b: Joi.number().required(),
47
+ * }).required()
48
+ *
49
+ * const joiUtil = new JoiUtil()
50
+ *
51
+ * // using
52
+ * const invalidObject = joiUtil.validate({}, someSchema) // validate throws an error
53
+ * const validObject = joiUtil.validate({ a: 'a', b: 1 }, someSchema)
54
+ */
55
+ var JoiUtil = exports.JoiUtil = /*#__PURE__*/function () {
56
+ function JoiUtil() {
57
+ _classCallCheck(this, JoiUtil);
58
+ }
59
+ return _createClass(JoiUtil, [{
60
+ key: "sanitize",
61
+ value:
62
+ /**
63
+ * Validate and clean object
64
+ * @template T
65
+ * @template Joi
66
+ * @param {any} objectToValidate
67
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
68
+ * @param {validationOptions} [validationOptions]
69
+ * @returns {T} expected object after validation
70
+ */
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ function sanitize(objectToValidate, schema, validationOptions) {
73
+ return this._validate(objectToValidate, schema, _objectSpread(_objectSpread({}, validationOptions), {}, {
74
+ stripUnknown: true
75
+ }));
76
+ }
77
+
78
+ /**
79
+ * Only validate properties specified in validation schema
80
+ * @template T
81
+ * @template Joi
82
+ * @param {any} objectToValidate
83
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
84
+ * @param {validationOptions} [validationOptions]
85
+ * @returns {T} expected object after validation
86
+ */
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+ }, {
89
+ key: "validate",
90
+ value: function validate(objectToValidate, schema, validationOptions) {
91
+ return this._validate(objectToValidate, schema, validationOptions);
92
+ }
93
+
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ }, {
96
+ key: "_validate",
97
+ value: function _validate(objectToValidate, schema, validationOptions) {
98
+ var _schema$validate = schema.validate(objectToValidate, validationOptions),
99
+ validationError = _schema$validate.error,
100
+ value = _schema$validate.value;
101
+ if (validationError) {
102
+ throw new ErrorWithPayload(validationError.message.split('"').join("'"), validationError);
103
+ }
104
+ return value;
105
+ }
106
+ }]);
107
+ }();
@@ -0,0 +1,16 @@
1
+ export type AnyFunction<T> = (...args: any[]) => T;
2
+ /**
3
+ * This is a simple implementation of memoize function that caches result against the parameter passed that are passed to the
4
+ * function so it never runs the same calculation twice.
5
+ * @template F
6
+ * @template R
7
+ * @param {F} factoryFn
8
+ * @return {F: AnyFunction<R>}
9
+ * @example
10
+ * export const sumTwoNumbersMemoize = memoizeFactory((a:number, b:number): number => a + b)
11
+ *
12
+ * // using
13
+ * sumTwoNumbersMemoize(5 + 10) // 15
14
+ */
15
+ export declare const memoizeFactory: <F extends AnyFunction<R>, R>(factoryFn: F) => F;
16
+ //# sourceMappingURL=memoize-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoize-factory.d.ts","sourceRoot":"","sources":["../src/memoize-factory.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;AAElD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,2CAA4C,CAAC,KAAG,CAW1E,CAAA"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.memoizeFactory = void 0;
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+
9
+ /**
10
+ * This is a simple implementation of memoize function that caches result against the parameter passed that are passed to the
11
+ * function so it never runs the same calculation twice.
12
+ * @template F
13
+ * @template R
14
+ * @param {F} factoryFn
15
+ * @return {F: AnyFunction<R>}
16
+ * @example
17
+ * export const sumTwoNumbersMemoize = memoizeFactory((a:number, b:number): number => a + b)
18
+ *
19
+ * // using
20
+ * sumTwoNumbersMemoize(5 + 10) // 15
21
+ */
22
+ var memoizeFactory = exports.memoizeFactory = function memoizeFactory(factoryFn) {
23
+ var cache = {};
24
+ return function () {
25
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26
+ args[_key] = arguments[_key];
27
+ }
28
+ var key = JSON.stringify(args);
29
+ if (key in cache) {
30
+ return cache[key];
31
+ }
32
+ return cache[key] = factoryFn.apply(void 0, args);
33
+ };
34
+ };
@@ -0,0 +1,72 @@
1
+ export type ObjectType = Record<string, any>;
2
+ export declare class ObjectUtil {
3
+ /**
4
+ * Deep clone object. Returned object will have no references to the object passed through params
5
+ * @template T
6
+ * @param {T} objectToClone
7
+ * @return {T}
8
+ */
9
+ deepClone<T extends ObjectType>(objectToClone: T): T;
10
+ /**
11
+ * Pick only properties from the property list. It is only allowed to pick properties from the first level
12
+ * @template T
13
+ * @template L
14
+ * @param {T} obj
15
+ * @param {L[]} keyList
16
+ * @return {Pick<T, L>}
17
+ */
18
+ pickByList<T extends object, L extends keyof T>(obj: T, keyList: (L | string)[]): Pick<T, L>;
19
+ /**
20
+ * Pick objects properties using keys from the second object.
21
+ * @template T
22
+ * @template L
23
+ * @param {T} obj
24
+ * @param {Partial<T>} objWithPickKeys
25
+ * @return {Pick<T, L>}
26
+ */
27
+ pickByObjectKeys<T extends object, L extends keyof T>(obj: T, objWithPickKeys: Partial<T> | ObjectType): Pick<T, L>;
28
+ /**
29
+ * This function will do stringify deeper that JSON.stringify.
30
+ * @param {any} entity - entity thant needs to be stringify
31
+ * @param {object} [options] - available options
32
+ * @param {boolean} [options.isSortable=false] - if object property should be sorted
33
+ * @param {boolean} [options.isPrettyPrinted=false] - if object and array properties should be printed in a new row
34
+ * @param {number} [options.prettyPrintCompactLevel=0] - if pretty print is on define the level of deepest children that are not
35
+ * going to be pretty. It doesn't matter if the siblings doesn't have the same depth.
36
+ * @return {string} - strung result
37
+ * @example
38
+ * console.log(new ObjectUtil().deepStringify(null)) // 'null'
39
+ * console.log(new ObjectUtil().deepStringify(undefined)) // 'undefined'
40
+ * console.log(new ObjectUtil().deepStringify({ a: 1 })) // '{\n\ta: 1\n}'
41
+ * // `{
42
+ * // a:1
43
+ * // }`
44
+ * console.log(new ObjectUtil().deepStringify({ b: 1, a: 2 }, {isSorted:true, compact: true})) // { a: 2, b: 1 }
45
+ */
46
+ deepStringify(entity: any, options?: {
47
+ isSorted?: boolean;
48
+ isPrettyPrinted?: boolean;
49
+ prettyPrintCompactLevel?: number;
50
+ }): string;
51
+ protected _deepStringifyCompact(params: {
52
+ isPrettyPrinted: boolean;
53
+ prettyPrintCompactLevel: number;
54
+ }): number | boolean;
55
+ /**
56
+ * We are converting objects to string (or null or undefined) and comparing if the result is equal
57
+ * @param a
58
+ * @param b
59
+ * @return {boolean}
60
+ */
61
+ deepEqual(a: any, b: any): boolean;
62
+ /**
63
+ * This function is going to convert any null to undefined in the object that is passed to it.
64
+ * @template T
65
+ * @param {T} objectWithNulls
66
+ * @return {T}
67
+ * @example
68
+ * console.log(new ObjectUtil().deepNullToUndefined({ a: null, b: { c: null } })) // { a: undefined, b: { c: undefined } }
69
+ */
70
+ deepNullToUndefined<T extends ObjectType>(objectWithNulls: T): T;
71
+ }
72
+ //# sourceMappingURL=object-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-util.d.ts","sourceRoot":"","sources":["../src/object-util.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAE5C,qBAAa,UAAU;IACtB;;;;;OAKG;IACH,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,aAAa,EAAE,CAAC,GAAG,CAAC;IAIpD;;;;;;;OAOG;IACH,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IAa5F;;;;;;;OAOG;IACH,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IAMnH;;;;;;;;;;;;;;;;;OAiBG;IACH,aAAa,CAEZ,MAAM,EAAE,GAAG,EACX,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,eAAe,CAAC,EAAE,OAAO,CAAC;QAAC,uBAAuB,CAAC,EAAE,MAAM,CAAA;KAAE,GAC3F,MAAM;IAeT,SAAS,CAAC,qBAAqB,CAAC,MAAM,EAAE;QAAE,eAAe,EAAE,OAAO,CAAC;QAAC,uBAAuB,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,GAAG,OAAO;IAUxH;;;;;OAKG;IAEH,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,OAAO;IAIlC;;;;;;;OAOG;IACH,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAAE,eAAe,EAAE,CAAC,GAAG,CAAC;CAehE"}