Total Complexity | 5 |
Complexity/F | 1.67 |
Lines of Code | 37 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import type {DefaultsAndExamplesFromSchema, SchemaWithDefaultsAndExamples} from "./ts-swiss.types" |
||
2 | |||
3 | const {keys: $keys} = Object |
||
4 | , {random: $random} = Math |
||
5 | |||
6 | export { |
||
7 | regexpize, |
||
8 | extractDefaults, |
||
9 | randomString |
||
10 | } |
||
11 | |||
12 | function regexpize(source: string|RegExp, flags = "") { |
||
13 | return typeof source === "string" |
||
14 | ? new RegExp(source, flags) |
||
15 | : source |
||
16 | } |
||
17 | |||
18 | function extractDefaults<S extends SchemaWithDefaultsAndExamples>({properties}: S) { |
||
19 | const keys: Array<keyof typeof properties> = $keys(properties) |
||
20 | , {length} = keys |
||
21 | , defaults: Partial<DefaultsAndExamplesFromSchema<S>> = {} |
||
22 | |||
23 | for (let i = length; i--;) { |
||
24 | const key = keys[i] |
||
25 | //@ts-ignore |
||
26 | |||
27 | defaults[key] = properties[key].default |
||
28 | } |
||
29 | |||
30 | return defaults as DefaultsAndExamplesFromSchema<S> |
||
31 | } |
||
32 | |||
33 | function randomString() { |
||
34 | return $random().toString(36) |
||
35 | .slice(2) |
||
36 | } |
||
37 |