@beecode/msh-util 1.2.1 → 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 (145) hide show
  1. package/dist/array-util.d.ts +23 -0
  2. package/dist/array-util.d.ts.map +1 -0
  3. package/dist/array-util.js +27 -0
  4. package/{src/class-factory-pattern.ts → dist/class-factory-pattern.d.ts} +5 -9
  5. package/dist/class-factory-pattern.d.ts.map +1 -0
  6. package/dist/class-factory-pattern.js +26 -0
  7. package/{src/express/error-handler.ts → dist/express/error-handler.d.ts} +2 -10
  8. package/dist/express/error-handler.d.ts.map +1 -0
  9. package/dist/express/error-handler.js +26 -0
  10. package/dist/index.d.ts +17 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +14 -0
  13. package/dist/joi-util.d.ts +47 -0
  14. package/dist/joi-util.d.ts.map +1 -0
  15. package/dist/joi-util.js +62 -0
  16. package/{src/memoize-factory.ts → dist/memoize-factory.d.ts} +3 -14
  17. package/dist/memoize-factory.d.ts.map +1 -0
  18. package/dist/memoize-factory.js +24 -0
  19. package/dist/object-util.d.ts +72 -0
  20. package/dist/object-util.d.ts.map +1 -0
  21. package/dist/object-util.js +115 -0
  22. package/dist/regex-util.d.ts +12 -0
  23. package/dist/regex-util.d.ts.map +1 -0
  24. package/dist/regex-util.js +12 -0
  25. package/dist/single-threshold-promise.d.ts +31 -0
  26. package/dist/single-threshold-promise.d.ts.map +1 -0
  27. package/dist/single-threshold-promise.js +46 -0
  28. package/dist/singleton/async.d.ts +50 -0
  29. package/dist/singleton/async.d.ts.map +1 -0
  30. package/dist/singleton/async.js +75 -0
  31. package/{src/singleton/pattern.ts → dist/singleton/pattern.d.ts} +3 -13
  32. package/dist/singleton/pattern.d.ts.map +1 -0
  33. package/dist/singleton/pattern.js +41 -0
  34. package/dist/string-util.d.ts +10 -0
  35. package/dist/string-util.d.ts.map +1 -0
  36. package/dist/string-util.js +18 -0
  37. package/dist/time-util.d.ts +74 -0
  38. package/dist/time-util.d.ts.map +1 -0
  39. package/dist/time-util.js +90 -0
  40. package/dist/time-zone.d.ts +467 -0
  41. package/dist/time-zone.d.ts.map +1 -0
  42. package/dist/time-zone.js +468 -0
  43. package/{src/timeout.ts → dist/timeout.d.ts} +2 -3
  44. package/dist/timeout.d.ts.map +1 -0
  45. package/dist/timeout.js +17 -0
  46. package/dist/type-util.d.ts +50 -0
  47. package/dist/type-util.d.ts.map +1 -0
  48. package/dist/type-util.js +54 -0
  49. package/lib/array-util.d.ts.map +1 -1
  50. package/lib/array-util.js +30 -28
  51. package/lib/class-factory-pattern.d.ts.map +1 -1
  52. package/lib/class-factory-pattern.js +17 -8
  53. package/lib/express/error-handler.d.ts.map +1 -1
  54. package/lib/express/error-handler.js +15 -11
  55. package/lib/index.d.ts +16 -13
  56. package/lib/index.d.ts.map +1 -1
  57. package/lib/index.js +107 -29
  58. package/lib/joi-util.d.ts.map +1 -1
  59. package/lib/joi-util.js +66 -22
  60. package/lib/memoize-factory.d.ts +1 -1
  61. package/lib/memoize-factory.d.ts.map +1 -1
  62. package/lib/memoize-factory.js +19 -13
  63. package/lib/object-util.d.ts.map +1 -1
  64. package/lib/object-util.js +110 -55
  65. package/lib/package.json +1 -0
  66. package/lib/regex-util.js +15 -13
  67. package/lib/single-threshold-promise.d.ts +1 -1
  68. package/lib/single-threshold-promise.d.ts.map +1 -1
  69. package/lib/single-threshold-promise.js +74 -28
  70. package/lib/singleton/async.d.ts +1 -1
  71. package/lib/singleton/async.d.ts.map +1 -1
  72. package/lib/singleton/async.js +105 -45
  73. package/lib/singleton/pattern.d.ts +1 -1
  74. package/lib/singleton/pattern.d.ts.map +1 -1
  75. package/lib/singleton/pattern.js +13 -12
  76. package/lib/string-util.js +21 -19
  77. package/lib/time-util.js +69 -39
  78. package/lib/time-zone.d.ts +467 -0
  79. package/lib/time-zone.d.ts.map +1 -0
  80. package/lib/time-zone.js +473 -0
  81. package/lib/timeout.js +9 -6
  82. package/lib/type-util.js +57 -55
  83. package/lib/types/global.d.js +5 -0
  84. package/lib/types/types.d.js +3 -0
  85. package/package.json +188 -134
  86. package/lib/array-util.js.map +0 -1
  87. package/lib/class-factory-pattern.js.map +0 -1
  88. package/lib/express/error-handler.js.map +0 -1
  89. package/lib/index.js.map +0 -1
  90. package/lib/joi-util.js.map +0 -1
  91. package/lib/memoize-factory.js.map +0 -1
  92. package/lib/object-util.js.map +0 -1
  93. package/lib/regex-util.js.map +0 -1
  94. package/lib/single-threshold-promise.js.map +0 -1
  95. package/lib/singleton/async.js.map +0 -1
  96. package/lib/singleton/pattern.js.map +0 -1
  97. package/lib/string-util.js.map +0 -1
  98. package/lib/time-util.js.map +0 -1
  99. package/lib/timeout.js.map +0 -1
  100. package/lib/type-util.js.map +0 -1
  101. package/lib/types/any-function/index.d.ts +0 -2
  102. package/lib/types/any-function/index.d.ts.map +0 -1
  103. package/lib/types/any-function/index.js +0 -3
  104. package/lib/types/any-function/index.js.map +0 -1
  105. package/lib/types/any-function/no-params.d.ts +0 -2
  106. package/lib/types/any-function/no-params.d.ts.map +0 -1
  107. package/lib/types/any-function/no-params.js +0 -3
  108. package/lib/types/any-function/no-params.js.map +0 -1
  109. package/lib/types/any-function/promise-no-params.d.ts +0 -2
  110. package/lib/types/any-function/promise-no-params.d.ts.map +0 -1
  111. package/lib/types/any-function/promise-no-params.js +0 -3
  112. package/lib/types/any-function/promise-no-params.js.map +0 -1
  113. package/lib/types/any-function/promise.d.ts +0 -2
  114. package/lib/types/any-function/promise.d.ts.map +0 -1
  115. package/lib/types/any-function/promise.js +0 -3
  116. package/lib/types/any-function/promise.js.map +0 -1
  117. package/src/array-util.test.ts +0 -50
  118. package/src/array-util.ts +0 -26
  119. package/src/class-factory-pattern.test.ts +0 -39
  120. package/src/express/error-handler.test.ts +0 -44
  121. package/src/index.ts +0 -25
  122. package/src/joi-util.test.ts +0 -192
  123. package/src/joi-util.ts +0 -65
  124. package/src/memoize-factory.test.ts +0 -40
  125. package/src/object-util.test.ts +0 -360
  126. package/src/object-util.ts +0 -127
  127. package/src/regex-util.test.ts +0 -25
  128. package/src/regex-util.ts +0 -11
  129. package/src/single-threshold-promise.test.ts +0 -91
  130. package/src/single-threshold-promise.ts +0 -56
  131. package/src/singleton/async.test.ts +0 -122
  132. package/src/singleton/async.ts +0 -90
  133. package/src/singleton/pattern.test.ts +0 -16
  134. package/src/string-util.test.ts +0 -18
  135. package/src/string-util.ts +0 -18
  136. package/src/time-util.test.ts +0 -89
  137. package/src/time-util.ts +0 -98
  138. package/src/timeout.test.ts +0 -65
  139. package/src/type-util.test.ts +0 -20
  140. package/src/type-util.ts +0 -54
  141. package/src/types/any-function/index.ts +0 -1
  142. package/src/types/any-function/no-params.ts +0 -1
  143. package/src/types/any-function/promise-no-params.ts +0 -1
  144. package/src/types/any-function/promise.ts +0 -1
  145. package/src/types/types.d.ts +0 -2
@@ -1,44 +0,0 @@
1
- import { expressErrorHandler } from 'src/express/error-handler'
2
-
3
- describe('expressErrorHandler', () => {
4
- const fake_fn = jest.fn()
5
- const fake_req = jest.fn()
6
- const fake_res = jest.fn()
7
- const fake_next = jest.fn()
8
-
9
- class FakeExpressController {
10
- @expressErrorHandler
11
- async login(_req: any, _res: any, _next: any): Promise<any> {
12
- return await fake_fn()
13
- }
14
- }
15
- const fakeExpressControllerInstance = new FakeExpressController()
16
-
17
- afterEach(() => jest.resetAllMocks())
18
-
19
- it('should call next with error if async function throws error', async () => {
20
- const error = new Error()
21
- fake_fn.mockRejectedValue(error)
22
-
23
- await fakeExpressControllerInstance.login(fake_req, fake_res, fake_next)
24
-
25
- expect(fake_fn).toHaveBeenCalledTimes(1)
26
- expect(fake_req).not.toHaveBeenCalled()
27
- expect(fake_res).not.toHaveBeenCalled()
28
- expect(fake_next).toHaveBeenCalledTimes(1)
29
- expect(fake_next).toHaveBeenCalledOnceWith(error)
30
- })
31
-
32
- it('should work as usual if async function resolves promise', async () => {
33
- const expected = { successful: true }
34
- fake_fn.mockResolvedValue(expected)
35
-
36
- const result = await fakeExpressControllerInstance.login(fake_req, fake_res, fake_next)
37
-
38
- expect(fake_fn).toHaveBeenCalledTimes(1)
39
- expect(fake_req).not.toHaveBeenCalled()
40
- expect(fake_res).not.toHaveBeenCalled()
41
- expect(fake_next).not.toHaveBeenCalled()
42
- expect(result).toBe(expected)
43
- })
44
- })
package/src/index.ts DELETED
@@ -1,25 +0,0 @@
1
- export * from 'src/express/error-handler'
2
-
3
- export * from 'src/singleton/async'
4
-
5
- export * from 'src/singleton/pattern'
6
-
7
- export * from 'src/class-factory-pattern'
8
-
9
- export * from 'src/joi-util'
10
-
11
- export * from 'src/memoize-factory'
12
-
13
- export * from 'src/object-util'
14
-
15
- export * from 'src/regex-util'
16
-
17
- export * from 'src/single-threshold-promise'
18
-
19
- export * from 'src/string-util'
20
-
21
- export * from 'src/time-util'
22
-
23
- export * from 'src/timeout'
24
-
25
- export * from 'src/type-util'
@@ -1,192 +0,0 @@
1
- import Joi from 'joi'
2
- import { JoiUtil } from 'src/joi-util'
3
-
4
- describe('JoiUtil', () => {
5
- const joiUtil = new JoiUtil()
6
-
7
- const validObject = { a: 'string', b: true, c: 123, d: new Date() }
8
- const invalidObject = { invalid: true }
9
-
10
- const joiSchema = Joi.object().keys({
11
- a: Joi.string().required(),
12
- b: Joi.boolean().required(),
13
- c: Joi.number().required(),
14
- d: Joi.date().required(),
15
- })
16
-
17
- describe('sanitize', () => {
18
- it('should return valid object', () => {
19
- const result = joiUtil.sanitize(validObject, joiSchema)
20
- expect(result).toEqual(validObject)
21
- })
22
- it('should return valid object and remove properties that are not defined in schema', () => {
23
- const result = joiUtil.sanitize({ ...validObject, test: true }, joiSchema)
24
- expect(result).toEqual(validObject)
25
- expect(result.test).toBeUndefined()
26
- })
27
- it('should throw error if object is not valid, return first error', () => {
28
- try {
29
- joiUtil.sanitize(invalidObject, joiSchema)
30
- expect.fail('test failed')
31
- } catch (err: any) {
32
- expect(err.message).toEqual("'a' is required")
33
- expect(err.payload.details).toEqual([
34
- {
35
- context: {
36
- key: 'a',
37
- label: 'a',
38
- },
39
- message: '"a" is required',
40
- path: ['a'],
41
- type: 'any.required',
42
- },
43
- ])
44
- }
45
- })
46
- it('should throw error if object is not valid, return all errors', () => {
47
- try {
48
- joiUtil.sanitize(invalidObject, joiSchema, { abortEarly: false })
49
- expect.fail('test failed')
50
- } catch (err: any) {
51
- expect(err.message).toEqual("'a' is required. 'b' is required. 'c' is required. 'd' is required")
52
- expect(err.payload.details).toEqual([
53
- {
54
- context: {
55
- key: 'a',
56
- label: 'a',
57
- },
58
- message: '"a" is required',
59
- path: ['a'],
60
- type: 'any.required',
61
- },
62
- {
63
- context: {
64
- key: 'b',
65
- label: 'b',
66
- },
67
- message: '"b" is required',
68
- path: ['b'],
69
- type: 'any.required',
70
- },
71
- {
72
- context: {
73
- key: 'c',
74
- label: 'c',
75
- },
76
- message: '"c" is required',
77
- path: ['c'],
78
- type: 'any.required',
79
- },
80
- {
81
- context: {
82
- key: 'd',
83
- label: 'd',
84
- },
85
- message: '"d" is required',
86
- path: ['d'],
87
- type: 'any.required',
88
- },
89
- ])
90
- }
91
- })
92
- })
93
-
94
- describe('validate', () => {
95
- it('should return valid object', () => {
96
- const result = joiUtil.validate(validObject, joiSchema)
97
- expect(result).toEqual(validObject)
98
- })
99
-
100
- it('should throw error if there are unknown properties', () => {
101
- try {
102
- joiUtil.validate({ ...validObject, test: true }, joiSchema)
103
- expect.fail('test failed')
104
- } catch (err: any) {
105
- expect(err.message).toEqual("'test' is not allowed")
106
- }
107
- })
108
- it('should throw error if object is not valid, return first error', () => {
109
- try {
110
- joiUtil.validate(invalidObject, joiSchema)
111
- expect.fail('test failed')
112
- } catch (err: any) {
113
- expect(err.message).toEqual("'a' is required")
114
- expect(err.payload.details).toEqual([
115
- {
116
- context: {
117
- key: 'a',
118
- label: 'a',
119
- },
120
- message: '"a" is required',
121
- path: ['a'],
122
- type: 'any.required',
123
- },
124
- ])
125
- }
126
- })
127
- it('should throw error if object is not valid, return all errors', () => {
128
- try {
129
- joiUtil.validate(invalidObject, joiSchema, { abortEarly: false })
130
- expect.fail('test failed')
131
- } catch (err: any) {
132
- expect(err.message).toEqual(
133
- "'a' is required. 'b' is required. 'c' is required. 'd' is required. 'invalid' is not allowed"
134
- )
135
- expect(err.payload.details).toEqual([
136
- {
137
- context: {
138
- key: 'a',
139
- label: 'a',
140
- },
141
- message: '"a" is required',
142
- path: ['a'],
143
- type: 'any.required',
144
- },
145
- {
146
- context: {
147
- key: 'b',
148
- label: 'b',
149
- },
150
- message: '"b" is required',
151
- path: ['b'],
152
- type: 'any.required',
153
- },
154
- {
155
- context: {
156
- key: 'c',
157
- label: 'c',
158
- },
159
- message: '"c" is required',
160
- path: ['c'],
161
- type: 'any.required',
162
- },
163
- {
164
- context: {
165
- key: 'd',
166
- label: 'd',
167
- },
168
- message: '"d" is required',
169
- path: ['d'],
170
- type: 'any.required',
171
- },
172
- {
173
- context: {
174
- child: 'invalid',
175
- key: 'invalid',
176
- label: 'invalid',
177
- value: true,
178
- },
179
- message: '"invalid" is not allowed',
180
- path: ['invalid'],
181
- type: 'object.unknown',
182
- },
183
- ])
184
- }
185
- })
186
-
187
- it('should allow unknown if flag is set and call logger with warn message', () => {
188
- const result = joiUtil.validate({ ...validObject, unknownProp: 'test' }, joiSchema, { allowUnknown: true })
189
- expect(result).toEqual({ ...validObject, unknownProp: 'test' })
190
- })
191
- })
192
- })
package/src/joi-util.ts DELETED
@@ -1,65 +0,0 @@
1
- import { ObjectSchema, Schema, ValidationOptions } from 'joi'
2
-
3
- export class ErrorWithPayload<T> extends Error {
4
- payload: T
5
-
6
- constructor(message: string, payload: T) {
7
- super(message)
8
- this.payload = payload
9
- }
10
- }
11
-
12
- /**
13
- * This is a simple wrapper around Joi validation with two functions exposed validate and sanitize. If object is not valid function throws an error.
14
- * @example
15
- * type SomeType = {
16
- * a: string
17
- * b: number
18
- * }
19
- * const someSchema = Joi.object<SomeType>().keys({
20
- * a: Joi.string().required(),
21
- * b: Joi.number().required(),
22
- * }).required()
23
- *
24
- * const joiUtil = new JoiUtil()
25
- *
26
- * // using
27
- * const invalidObject = joiUtil.validate({}, someSchema) // validate throws an error
28
- * const validObject = joiUtil.validate({ a: 'a', b: 1 }, someSchema)
29
- */
30
- export class JoiUtil {
31
- /**
32
- * Validate and clean object
33
- * @template T
34
- * @template Joi
35
- * @param {any} objectToValidate
36
- * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
37
- * @param {validationOptions} [validationOptions]
38
- * @returns {T} expected object after validation
39
- */
40
- sanitize<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T {
41
- return this._validate<T>(objectToValidate, schema, { ...validationOptions, stripUnknown: true })
42
- }
43
-
44
- /**
45
- * Only validate properties specified in validation schema
46
- * @template T
47
- * @template Joi
48
- * @param {any} objectToValidate
49
- * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
50
- * @param {validationOptions} [validationOptions]
51
- * @returns {T} expected object after validation
52
- */
53
- validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T {
54
- return this._validate<T>(objectToValidate, schema, validationOptions)
55
- }
56
-
57
- protected _validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T {
58
- const { error: validationError, value } = schema.validate(objectToValidate, validationOptions)
59
- if (validationError) {
60
- throw new ErrorWithPayload(validationError.message.split('"').join("'"), validationError)
61
- }
62
-
63
- return value as T
64
- }
65
- }
@@ -1,40 +0,0 @@
1
- import { memoizeFactory } from 'src/memoize-factory'
2
-
3
- describe('memoizeFactory', () => {
4
- const fake_factoryFn = jest.fn()
5
-
6
- beforeEach(() => {
7
- fake_factoryFn.mockImplementation((a: number, b: number): number => {
8
- return a + b
9
- })
10
- })
11
-
12
- afterEach(() => {
13
- jest.resetAllMocks()
14
- })
15
-
16
- it('should only call factory function once if the same argument is passed', () => {
17
- const memoizedImplementation = memoizeFactory(fake_factoryFn)
18
- expect(fake_factoryFn).not.toHaveBeenCalled()
19
-
20
- expect(memoizedImplementation(1, 2)).toEqual(3)
21
- expect(fake_factoryFn).toHaveBeenCalledTimes(1)
22
-
23
- expect(memoizedImplementation(1, 2)).toEqual(3)
24
- expect(fake_factoryFn).toHaveBeenCalledTimes(1)
25
- })
26
-
27
- it('should call factory as many times as it is called if the arguments are different', () => {
28
- const memoizedImplementation = memoizeFactory(fake_factoryFn)
29
- expect(fake_factoryFn).not.toHaveBeenCalled()
30
-
31
- expect(memoizedImplementation(1, 2)).toEqual(3)
32
- expect(fake_factoryFn).toHaveBeenCalledTimes(1)
33
-
34
- expect(memoizedImplementation(1, 3)).toEqual(4)
35
- expect(fake_factoryFn).toHaveBeenCalledTimes(2)
36
-
37
- expect(memoizedImplementation(1, 4)).toEqual(5)
38
- expect(fake_factoryFn).toHaveBeenCalledTimes(3)
39
- })
40
- })