core-services-sdk 1.3.22 → 1.3.24
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/package.json +5 -2
- package/src/core/index.js +1 -0
- package/src/core/normalize-premitives-types-or-default.js +215 -0
- package/src/index.js +1 -0
- package/src/util/index.js +12 -0
- package/src/util/mask-sensitive.js +78 -0
- package/tests/core/normalize-premitives-types-or-default.unit.test.js +72 -0
- package/tests/rabbit-mq/rabbit-mq.test.js +1 -0
- package/tests/util/mask-sensitive.unit.test.js +79 -0
- package/tsconfig.json +9 -0
- package/types/core/combine-unique-arrays.d.ts +1 -0
- package/types/core/index.d.ts +8 -0
- package/types/core/normalize-min-max.d.ts +10 -0
- package/types/core/normalize-phone-number.d.ts +48 -0
- package/types/core/normalize-premitives-types-or-default.d.ts +172 -0
- package/types/core/normalize-to-array.d.ts +1 -0
- package/types/core/otp-generators.d.ts +56 -0
- package/types/core/regex-utils.d.ts +1 -0
- package/types/core/sanitize-objects.d.ts +4 -0
- package/types/crypto/crypto.d.ts +18 -0
- package/types/crypto/encryption.d.ts +6 -0
- package/types/crypto/index.d.ts +2 -0
- package/types/fastify/error-codes.d.ts +29 -0
- package/types/fastify/error-handlers/with-error-handling.d.ts +15 -0
- package/types/fastify/index.d.ts +2 -0
- package/types/http/HttpError.d.ts +82 -0
- package/types/http/http-method.d.ts +7 -0
- package/types/http/http.d.ts +36 -0
- package/types/http/index.d.ts +4 -0
- package/types/http/responseType.d.ts +20 -0
- package/types/ids/generators.d.ts +10 -0
- package/types/ids/index.d.ts +2 -0
- package/types/ids/prefixes.d.ts +34 -0
- package/types/index.d.ts +11 -0
- package/types/logger/get-logger.d.ts +23 -0
- package/types/logger/index.d.ts +1 -0
- package/types/mailer/index.d.ts +2 -0
- package/types/mailer/mailer.service.d.ts +21 -0
- package/types/mailer/transport.factory.d.ts +48 -0
- package/types/mongodb/connect.d.ts +4 -0
- package/types/mongodb/index.d.ts +3 -0
- package/types/mongodb/initialize-mongodb.d.ts +13 -0
- package/types/mongodb/validate-mongo-uri.d.ts +15 -0
- package/types/rabbit-mq/index.d.ts +1 -0
- package/types/rabbit-mq/rabbit-mq.d.ts +40 -0
- package/types/templates/index.d.ts +1 -0
- package/types/templates/template-loader.d.ts +3 -0
- package/types/util/index.d.ts +7 -0
- package/types/util/mask-sensitive.d.ts +2 -0
- package/vitest.config.js +13 -3
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "core-services-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.24",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"lint": "eslint .",
|
|
8
9
|
"lint:fix": "eslint . --fix",
|
|
9
10
|
"test": "vitest run --coverage",
|
|
10
11
|
"format": "prettier --write .",
|
|
11
|
-
"bump": "node ./scripts/bump-version.js"
|
|
12
|
+
"bump": "node ./scripts/bump-version.js",
|
|
13
|
+
"build:types": "tsc --declaration --allowJs --emitDeclarationOnly --outDir types ./src/index.js"
|
|
12
14
|
},
|
|
13
15
|
"repository": {
|
|
14
16
|
"type": "git",
|
|
@@ -45,6 +47,7 @@
|
|
|
45
47
|
"eslint-plugin-prettier": "^5.5.1",
|
|
46
48
|
"path": "^0.12.7",
|
|
47
49
|
"prettier": "^3.6.2",
|
|
50
|
+
"typescript": "^5.9.2",
|
|
48
51
|
"url": "^0.11.4",
|
|
49
52
|
"vitest": "^3.2.4"
|
|
50
53
|
}
|
package/src/core/index.js
CHANGED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize Utilities
|
|
3
|
+
*
|
|
4
|
+
* Small helpers to guarantee stable, predictable values from user/config inputs.
|
|
5
|
+
* When an incoming value is missing, malformed, or in a different-but-supported
|
|
6
|
+
* representation (e.g., number/boolean as string), these utilities either accept
|
|
7
|
+
* it (after safe normalization) or return a default you control.
|
|
8
|
+
*
|
|
9
|
+
* Design highlights:
|
|
10
|
+
* - Keep call sites compact and intention-revealing.
|
|
11
|
+
* - Be strict for strings (no implicit type coercion).
|
|
12
|
+
* - Be permissive for number/boolean when the input is a string in an accepted form.
|
|
13
|
+
* - Fast, side-effect free, no deep cloning — values are returned by reference when valid.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Generic predicate-based normalization.
|
|
18
|
+
*
|
|
19
|
+
* Purpose:
|
|
20
|
+
* Ensure a value conforms to a caller-provided predicate; otherwise return a provided default.
|
|
21
|
+
* Useful when you need a single reusable pattern for custom shapes or policies
|
|
22
|
+
* (e.g., "must be a non-empty array of strings", "must be a plain object", etc.).
|
|
23
|
+
*
|
|
24
|
+
* Behavior:
|
|
25
|
+
* - Calls `isValid(value)`.
|
|
26
|
+
* - If the predicate returns true → returns `value` (as-is).
|
|
27
|
+
* - Otherwise → returns `defaultValue` (as-is).
|
|
28
|
+
*
|
|
29
|
+
* Performance & Safety:
|
|
30
|
+
* - O(1) aside from your predicate cost.
|
|
31
|
+
* - No cloning or sanitization is performed.
|
|
32
|
+
* - Ensure `isValid` is pure and fast; avoid throwing inside it.
|
|
33
|
+
*
|
|
34
|
+
* @template T
|
|
35
|
+
* @param {any} value
|
|
36
|
+
* Candidate input to validate.
|
|
37
|
+
* @param {(v:any)=>boolean} isValid
|
|
38
|
+
* Validation predicate. Return true iff the input is acceptable.
|
|
39
|
+
* @param {T} defaultValue
|
|
40
|
+
* Fallback to return when `value` fails validation.
|
|
41
|
+
* @returns {T}
|
|
42
|
+
* The original `value` when valid; otherwise `defaultValue`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // Ensure a finite number
|
|
46
|
+
* normalizeOrDefault(5, v => typeof v === 'number' && Number.isFinite(v), 0) // → 5
|
|
47
|
+
* normalizeOrDefault('x', v => typeof v === 'number', 0) // → 0
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // Ensure an object (non-null, non-array)
|
|
51
|
+
* const cfg = normalizeOrDefault(
|
|
52
|
+
* maybeCfg,
|
|
53
|
+
* v => v && typeof v === 'object' && !Array.isArray(v),
|
|
54
|
+
* {}
|
|
55
|
+
* )
|
|
56
|
+
*/
|
|
57
|
+
export function normalizeOrDefault(value, isValid, defaultValue) {
|
|
58
|
+
return isValid(value) ? value : defaultValue
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Normalize a value to a non-empty, trimmed string; otherwise return a default (also trimmed).
|
|
63
|
+
*
|
|
64
|
+
* Purpose:
|
|
65
|
+
* Guarantee that downstream code receives a usable, non-empty string
|
|
66
|
+
* without performing implicit type coercion.
|
|
67
|
+
*
|
|
68
|
+
* Acceptance Criteria:
|
|
69
|
+
* - Accept only actual strings whose `trim()` length is > 0.
|
|
70
|
+
* - Return `value.trim()` when valid.
|
|
71
|
+
* - Otherwise return `defaultValue.trim()`.
|
|
72
|
+
*
|
|
73
|
+
* Why strict for strings?
|
|
74
|
+
* - Silent coercion from non-strings to string can hide bugs.
|
|
75
|
+
* - If you need to stringify other types, do it explicitly at the call site.
|
|
76
|
+
*
|
|
77
|
+
* Edge Cases:
|
|
78
|
+
* - If `defaultValue` is empty or whitespace-only, the function returns an empty string.
|
|
79
|
+
* Prefer providing a meaningful, non-empty default for clarity.
|
|
80
|
+
*
|
|
81
|
+
* @param {any} value
|
|
82
|
+
* Candidate to normalize (must be a string to be accepted).
|
|
83
|
+
* @param {string} defaultValue
|
|
84
|
+
* Fallback used when `value` is not a non-empty string. Will be `trim()`ed.
|
|
85
|
+
* @returns {string}
|
|
86
|
+
* A trimmed non-empty string when `value` is valid; otherwise `defaultValue.trim()`.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* normalizeStringOrDefault(' user-roles-management:edit ', 'fallback')
|
|
90
|
+
* // → 'user-roles-management:edit'
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* normalizeStringOrDefault('', 'user-roles-management:edit')
|
|
94
|
+
* // → 'user-roles-management:edit'
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* normalizeStringOrDefault(42, 'user-roles-management:edit')
|
|
98
|
+
* // → 'user-roles-management:edit'
|
|
99
|
+
*/
|
|
100
|
+
export function normalizeStringOrDefault(value, defaultValue) {
|
|
101
|
+
const def = typeof defaultValue === 'string' ? defaultValue.trim() : ''
|
|
102
|
+
if (typeof value === 'string') {
|
|
103
|
+
const trimmed = value.trim()
|
|
104
|
+
if (trimmed.length > 0) {
|
|
105
|
+
return trimmed
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return def
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Normalize a value to a valid number (with safe string coercion); otherwise return a default.
|
|
113
|
+
*
|
|
114
|
+
* Purpose:
|
|
115
|
+
* Accept numeric inputs that may arrive as strings (e.g., from env vars or config files)
|
|
116
|
+
* while keeping semantics explicit and predictable.
|
|
117
|
+
*
|
|
118
|
+
* Acceptance Criteria:
|
|
119
|
+
* - Accepts finite numbers (`typeof value === 'number' && Number.isFinite(value)`).
|
|
120
|
+
* - Accepts strings that become a finite number via `Number(trimmed)`.
|
|
121
|
+
* Examples: "42", " 3.14 ", "1e3", "-7", "0x10" (JS Number semantics).
|
|
122
|
+
* - Rejects non-numeric strings (e.g., "", " ", "abc") and non-number types.
|
|
123
|
+
* - Returns `defaultValue` when not acceptable.
|
|
124
|
+
*
|
|
125
|
+
* Parsing Semantics:
|
|
126
|
+
* - Uses `Number(s)` which requires the whole trimmed string to be numeric.
|
|
127
|
+
* - Honors JavaScript numeric literal rules (including hex and scientific notation).
|
|
128
|
+
* - If you want base-10 only or looser parsing, do it explicitly before calling.
|
|
129
|
+
*
|
|
130
|
+
* @param {any} value
|
|
131
|
+
* Candidate to normalize (number or numeric string).
|
|
132
|
+
* @param {number} defaultValue
|
|
133
|
+
* Fallback used when `value` is neither a finite number nor a numeric string.
|
|
134
|
+
* @returns {number}
|
|
135
|
+
* A finite number derived from `value`, or `defaultValue`.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* normalizeNumberOrDefault(42, 0) // → 42
|
|
139
|
+
* normalizeNumberOrDefault(' 3.14 ', 0) // → 3.14
|
|
140
|
+
* normalizeNumberOrDefault('1e3', 0) // → 1000
|
|
141
|
+
* normalizeNumberOrDefault('-7', 0) // → -7
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* normalizeNumberOrDefault('abc', 7) // → 7
|
|
145
|
+
* normalizeNumberOrDefault(NaN, 7) // → 7
|
|
146
|
+
* normalizeNumberOrDefault({}, 7) // → 7
|
|
147
|
+
*/
|
|
148
|
+
export function normalizeNumberOrDefault(value, defaultValue) {
|
|
149
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
150
|
+
return value
|
|
151
|
+
}
|
|
152
|
+
if (typeof value === 'string') {
|
|
153
|
+
const s = value.trim()
|
|
154
|
+
if (s.length > 0) {
|
|
155
|
+
const n = Number(s)
|
|
156
|
+
if (Number.isFinite(n)) {
|
|
157
|
+
return n
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return defaultValue
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Normalize a value to a boolean (with "true"/"false" string support); otherwise return a default.
|
|
166
|
+
*
|
|
167
|
+
* Purpose:
|
|
168
|
+
* Stabilize feature flags and binary config values that might be provided as either booleans
|
|
169
|
+
* or as canonical strings.
|
|
170
|
+
*
|
|
171
|
+
* Acceptance Criteria:
|
|
172
|
+
* - Accepts actual booleans (`true` / `false`) → returned as-is.
|
|
173
|
+
* - Accepts strings equal to "true" or "false" (case-insensitive, trimmed).
|
|
174
|
+
* "true" → true
|
|
175
|
+
* "false" → false
|
|
176
|
+
* - Rejects other strings ("yes", "1", "0", etc.) and other types → returns `defaultValue`.
|
|
177
|
+
*
|
|
178
|
+
* Rationale:
|
|
179
|
+
* - Limiting string forms to the canonical words avoids accidental truthiness/falseyness.
|
|
180
|
+
* - If you need to accept "1"/"0" or other variants, coerce at the call site so intent is explicit.
|
|
181
|
+
*
|
|
182
|
+
* @param {any} value
|
|
183
|
+
* Candidate to normalize (boolean or "true"/"false" string).
|
|
184
|
+
* @param {boolean} defaultValue
|
|
185
|
+
* Fallback used when `value` is neither a boolean nor an accepted string form.
|
|
186
|
+
* @returns {boolean}
|
|
187
|
+
* A boolean derived from `value`, or `defaultValue`.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* normalizeBooleanOrDefault(true, false) // → true
|
|
191
|
+
* normalizeBooleanOrDefault(false, true) // → false
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* normalizeBooleanOrDefault('true', false) // → true
|
|
195
|
+
* normalizeBooleanOrDefault(' FALSE ', true) // → false
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* normalizeBooleanOrDefault('yes', false) // → false (rejected → default)
|
|
199
|
+
* normalizeBooleanOrDefault(1, true) // → true (rejected → default)
|
|
200
|
+
*/
|
|
201
|
+
export function normalizeBooleanOrDefault(value, defaultValue) {
|
|
202
|
+
if (typeof value === 'boolean') {
|
|
203
|
+
return value
|
|
204
|
+
}
|
|
205
|
+
if (typeof value === 'string') {
|
|
206
|
+
const s = value.trim().toLowerCase()
|
|
207
|
+
if (s === 'true') {
|
|
208
|
+
return true
|
|
209
|
+
}
|
|
210
|
+
if (s === 'false') {
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return defaultValue
|
|
215
|
+
}
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mask middle of a primitive value while keeping left/right edges.
|
|
3
|
+
* @param {string|number|boolean|null|undefined} value
|
|
4
|
+
* @param {string} [fill='•']
|
|
5
|
+
* @param {number} [maskLen=3]
|
|
6
|
+
* @param {number} [left=4]
|
|
7
|
+
* @param {number} [right=4]
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export const maskSingle = (
|
|
11
|
+
value,
|
|
12
|
+
fill = '•',
|
|
13
|
+
maskLen = 3,
|
|
14
|
+
left = 4,
|
|
15
|
+
right = 4,
|
|
16
|
+
) => {
|
|
17
|
+
if (value == null) {
|
|
18
|
+
return ''
|
|
19
|
+
}
|
|
20
|
+
const str = String(value)
|
|
21
|
+
if (str.length === 0) {
|
|
22
|
+
return ''
|
|
23
|
+
}
|
|
24
|
+
const m = Math.max(1, maskLen)
|
|
25
|
+
|
|
26
|
+
if (str.length <= left + right) {
|
|
27
|
+
if (str.length === 1) {
|
|
28
|
+
return fill
|
|
29
|
+
}
|
|
30
|
+
if (str.length === 2) {
|
|
31
|
+
return str[0] + fill.repeat(2) // "ab" -> "a••"
|
|
32
|
+
}
|
|
33
|
+
return str.slice(0, 1) + fill.repeat(m) + str.slice(-1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return str.slice(0, left) + fill.repeat(m) + str.slice(-right)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Recursively mask values in strings, numbers, booleans, arrays, and objects.
|
|
41
|
+
* @param {string|number|boolean|Array|Object|null|undefined} value
|
|
42
|
+
* @param {string} [fill='•']
|
|
43
|
+
* @param {number} [maskLen=3]
|
|
44
|
+
* @param {number} [left=4]
|
|
45
|
+
* @param {number} [right=4]
|
|
46
|
+
* @returns {string|Array|Object}
|
|
47
|
+
*/
|
|
48
|
+
export const mask = (value, fill = '•', maskLen = 3, left = 4, right = 4) => {
|
|
49
|
+
const type = typeof value
|
|
50
|
+
|
|
51
|
+
if (value instanceof Date) {
|
|
52
|
+
const isoDate = value.toISOString()
|
|
53
|
+
return maskSingle(isoDate, '', isoDate.length, 0, 0)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (value == null || (value && ['string', 'number'].includes(type))) {
|
|
57
|
+
return maskSingle(value, fill, maskLen, left, right)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (type === 'boolean') {
|
|
61
|
+
return maskSingle(value, '', 0, 0, 0)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (Array.isArray(value)) {
|
|
65
|
+
return value.map((aValue) => mask(aValue, fill, maskLen, left, right))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeof value === 'object') {
|
|
69
|
+
return Object.fromEntries(
|
|
70
|
+
Object.entries(value).map(([prop, propValue]) => [
|
|
71
|
+
prop,
|
|
72
|
+
mask(propValue, fill, maskLen, left, right),
|
|
73
|
+
]),
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return value
|
|
78
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
normalizeOrDefault,
|
|
5
|
+
normalizeStringOrDefault,
|
|
6
|
+
normalizeNumberOrDefault,
|
|
7
|
+
normalizeBooleanOrDefault,
|
|
8
|
+
} from '../../src/core/normalize-premitives-types-or-default.js'
|
|
9
|
+
|
|
10
|
+
describe('normalizeOrDefault (generic)', () => {
|
|
11
|
+
it('returns value if predicate is true', () => {
|
|
12
|
+
const out = normalizeOrDefault(5, (v) => typeof v === 'number', 0)
|
|
13
|
+
expect(out).toBe(5)
|
|
14
|
+
})
|
|
15
|
+
it('returns default if predicate is false', () => {
|
|
16
|
+
const out = normalizeOrDefault('x', (v) => typeof v === 'number', 0)
|
|
17
|
+
expect(out).toBe(0)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('normalizeStringOrDefault (strict, no coercion)', () => {
|
|
22
|
+
it('keeps a non-empty trimmed string', () => {
|
|
23
|
+
expect(normalizeStringOrDefault(' hello ', 'fallback')).toBe('hello')
|
|
24
|
+
})
|
|
25
|
+
it('returns default for empty / whitespace-only', () => {
|
|
26
|
+
expect(normalizeStringOrDefault('', 'fallback')).toBe('fallback')
|
|
27
|
+
expect(normalizeStringOrDefault(' ', 'fallback')).toBe('fallback')
|
|
28
|
+
})
|
|
29
|
+
it('returns default for non-strings', () => {
|
|
30
|
+
expect(normalizeStringOrDefault(123, 'fallback')).toBe('fallback')
|
|
31
|
+
expect(normalizeStringOrDefault(null, 'fallback')).toBe('fallback')
|
|
32
|
+
})
|
|
33
|
+
it('trims default defensively', () => {
|
|
34
|
+
expect(normalizeStringOrDefault('', ' fallback ')).toBe('fallback')
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('normalizeNumberOrDefault (coercive for strings)', () => {
|
|
39
|
+
it('keeps valid number', () => {
|
|
40
|
+
expect(normalizeNumberOrDefault(42, 0)).toBe(42)
|
|
41
|
+
})
|
|
42
|
+
it('coerces valid numeric string', () => {
|
|
43
|
+
expect(normalizeNumberOrDefault('42', 0)).toBe(42)
|
|
44
|
+
expect(normalizeNumberOrDefault(' 3.14 ', 0)).toBe(3.14)
|
|
45
|
+
expect(normalizeNumberOrDefault('1e3', 0)).toBe(1000)
|
|
46
|
+
expect(normalizeNumberOrDefault('-7', 0)).toBe(-7)
|
|
47
|
+
})
|
|
48
|
+
it('returns default for invalid number inputs', () => {
|
|
49
|
+
expect(normalizeNumberOrDefault(NaN, 7)).toBe(7)
|
|
50
|
+
expect(normalizeNumberOrDefault(' ', 7)).toBe(7)
|
|
51
|
+
expect(normalizeNumberOrDefault('abc', 7)).toBe(7)
|
|
52
|
+
expect(normalizeNumberOrDefault({}, 7)).toBe(7)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('normalizeBooleanOrDefault (coercive for "true"/"false" strings)', () => {
|
|
57
|
+
it('keeps actual booleans', () => {
|
|
58
|
+
expect(normalizeBooleanOrDefault(true, false)).toBe(true)
|
|
59
|
+
expect(normalizeBooleanOrDefault(false, true)).toBe(false)
|
|
60
|
+
})
|
|
61
|
+
it('coerces "true"/"false" strings (case-insensitive)', () => {
|
|
62
|
+
expect(normalizeBooleanOrDefault('true', false)).toBe(true)
|
|
63
|
+
expect(normalizeBooleanOrDefault('FALSE', true)).toBe(false)
|
|
64
|
+
expect(normalizeBooleanOrDefault(' TrUe ', false)).toBe(true)
|
|
65
|
+
})
|
|
66
|
+
it('returns default for other strings / types', () => {
|
|
67
|
+
expect(normalizeBooleanOrDefault('yes', false)).toBe(false)
|
|
68
|
+
expect(normalizeBooleanOrDefault('0', true)).toBe(true)
|
|
69
|
+
expect(normalizeBooleanOrDefault(1, false)).toBe(false)
|
|
70
|
+
expect(normalizeBooleanOrDefault(null, true)).toBe(true)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { mask, maskSingle } from '../../src/util/index.js'
|
|
4
|
+
|
|
5
|
+
describe('maskSingle', () => {
|
|
6
|
+
it('masks middle of a regular string (length == left+right)', () => {
|
|
7
|
+
expect(maskSingle('abcdefgh')).toBe('a•••h')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('masks numbers with default settings', () => {
|
|
11
|
+
expect(maskSingle(12345678)).toBe('1•••8')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('masks booleans', () => {
|
|
15
|
+
expect(maskSingle(true)).toBe('t•••e')
|
|
16
|
+
expect(maskSingle(false)).toBe('f•••e')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('masks very short strings correctly', () => {
|
|
20
|
+
expect(maskSingle('ab')).toBe('a••b'.slice(0, 3)) // will produce 'a••'
|
|
21
|
+
expect(maskSingle('a')).toBe('•')
|
|
22
|
+
expect(maskSingle('')).toBe('')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('respects custom fill and mask length', () => {
|
|
26
|
+
expect(maskSingle('abcdefgh', '*', 5)).toBe('a*****h')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('ensures maskLen is at least 1', () => {
|
|
30
|
+
expect(maskSingle('abcdefgh', '*', 0)).toBe('a*h')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('returns empty string for null/undefined', () => {
|
|
34
|
+
expect(maskSingle(null)).toBe('')
|
|
35
|
+
expect(maskSingle(undefined)).toBe('')
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('mask', () => {
|
|
40
|
+
it('masks primitives (string, number, boolean)', () => {
|
|
41
|
+
expect(mask('abcdefgh')).toBe('a•••h')
|
|
42
|
+
expect(mask(12345678)).toBe('1•••8')
|
|
43
|
+
expect(mask(true)).toBe('true')
|
|
44
|
+
expect(mask(false)).toBe('false')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('returns empty string for null/undefined', () => {
|
|
48
|
+
expect(mask(null)).toBe('')
|
|
49
|
+
expect(mask(undefined)).toBe('')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('masks arrays recursively', () => {
|
|
53
|
+
expect(mask(['abcdefgh', 12345678])).toEqual(['a•••h', '1•••8'])
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('masks objects recursively', () => {
|
|
57
|
+
expect(mask({ a: 'abcdefgh', b: 12345678 })).toEqual({
|
|
58
|
+
a: 'a•••h',
|
|
59
|
+
b: '1•••8',
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('masks nested objects/arrays recursively', () => {
|
|
64
|
+
const input = { arr: ['abcdefgh', { num: 12345678 }] }
|
|
65
|
+
const expected = { arr: ['a•••h', { num: '1•••8' }] }
|
|
66
|
+
expect(mask(input)).toEqual(expected)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('handles Date instances by returning full ISO string', () => {
|
|
70
|
+
const d = new Date('2025-08-15T12:34:56.789Z')
|
|
71
|
+
expect(mask(d)).toBe(d.toISOString())
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('respects custom fill and mask length in recursive calls', () => {
|
|
75
|
+
const input = { val: 'abcdefgh' }
|
|
76
|
+
const expected = { val: 'a*****h' }
|
|
77
|
+
expect(mask(input, '*', 5)).toEqual(expected)
|
|
78
|
+
})
|
|
79
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function combineUniqueArrays(...lists: Array<any>[]): Array<any>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./regex-utils.js";
|
|
2
|
+
export * from "./otp-generators.js";
|
|
3
|
+
export * from "./sanitize-objects.js";
|
|
4
|
+
export * from "./normalize-min-max.js";
|
|
5
|
+
export * from "./normalize-to-array.js";
|
|
6
|
+
export * from "./combine-unique-arrays.js";
|
|
7
|
+
export * from "./normalize-phone-number.js";
|
|
8
|
+
export * from "./normalize-premitives-types-or-default.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Resolve libphonenumber regardless of interop shape */
|
|
2
|
+
export function getLib(): any;
|
|
3
|
+
export function phoneUtil(): any;
|
|
4
|
+
/**
|
|
5
|
+
* Parse & validate an international number (must start with '+').
|
|
6
|
+
* @param {string} input
|
|
7
|
+
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
8
|
+
* @throws {Error} If the number is invalid
|
|
9
|
+
*/
|
|
10
|
+
export function normalizePhoneOrThrowIntl(input: string): {
|
|
11
|
+
e164: string;
|
|
12
|
+
national: string;
|
|
13
|
+
international: string;
|
|
14
|
+
regionCode: string | undefined;
|
|
15
|
+
type: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Parse & validate a national number using a region hint.
|
|
19
|
+
* @param {string} input
|
|
20
|
+
* @param {string} defaultRegion
|
|
21
|
+
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
22
|
+
* @throws {Error} If the number is invalid
|
|
23
|
+
*/
|
|
24
|
+
export function normalizePhoneOrThrowWithRegion(input: string, defaultRegion: string): {
|
|
25
|
+
e164: string;
|
|
26
|
+
national: string;
|
|
27
|
+
international: string;
|
|
28
|
+
regionCode: string | undefined;
|
|
29
|
+
type: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Smart normalization:
|
|
33
|
+
* - If input starts with '+', parse as international.
|
|
34
|
+
* - Otherwise require a defaultRegion and parse as national.
|
|
35
|
+
* @param {string} input
|
|
36
|
+
* @param {{ defaultRegion?: string }} [opts]
|
|
37
|
+
* @returns {{e164:string,national:string,international:string,regionCode:string|undefined,type:number}}
|
|
38
|
+
* @throws {Error} If invalid or defaultRegion is missing for non-international input
|
|
39
|
+
*/
|
|
40
|
+
export function normalizePhoneOrThrow(input: string, opts?: {
|
|
41
|
+
defaultRegion?: string;
|
|
42
|
+
}): {
|
|
43
|
+
e164: string;
|
|
44
|
+
national: string;
|
|
45
|
+
international: string;
|
|
46
|
+
regionCode: string | undefined;
|
|
47
|
+
type: number;
|
|
48
|
+
};
|