magic-structure 1.0.7 → 1.1.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.
- package/index.ts +1 -1
- package/lib/decorators.ts +10 -6
- package/lib/mappers.ts +8 -7
- package/lib/types.ts +11 -9
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { field, fieldName, schema,
|
|
1
|
+
export { field, fieldName, schema, from, to, value, skip, skipIf } from './lib/decorators'
|
|
2
2
|
export { fromJson, toJson } from './lib/mappers'
|
|
3
3
|
export { type FieldOptions, type Mapper, type SkipMapper } from './lib/types'
|
package/lib/decorators.ts
CHANGED
|
@@ -11,22 +11,26 @@ function fieldName(fieldName: string, reconstruct = false) {
|
|
|
11
11
|
return field({ fieldName, reconstruct })
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
function
|
|
15
|
-
return field({
|
|
14
|
+
function from<Value, Record extends object>(from: Mapper<Value, Record>) {
|
|
15
|
+
return field({ from })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function to<Value, Record extends object>(to: Mapper<Value, Record>) {
|
|
19
|
+
return field({ to })
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
function schema(schema: FieldOptions['schema'], isArray: FieldOptions['isArray'] = false) {
|
|
19
23
|
return field({ schema, isArray })
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
function value(value: any) {
|
|
23
|
-
return field({ value })
|
|
26
|
+
function value(value: any, excludeNull = false) {
|
|
27
|
+
return field({ value, excludeNull })
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
function skipIf(skipMapper: SkipMapper) {
|
|
30
|
+
function skipIf<Value, Record extends object>(skipMapper: SkipMapper<Value, Record>) {
|
|
27
31
|
return field({ skip: skipMapper })
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
const skip = field({ skip: true })
|
|
31
35
|
|
|
32
|
-
export { field, fieldName,
|
|
36
|
+
export { field, fieldName, from, to, schema, value, skipIf, skip }
|
package/lib/mappers.ts
CHANGED
|
@@ -22,7 +22,7 @@ export function fromJson<Schema extends object, Json extends object>(
|
|
|
22
22
|
continue
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const { fieldName, value,
|
|
25
|
+
const { fieldName, value, from, excludeNull, schema, isArray } = options as FieldOptions
|
|
26
26
|
const dataKey = fieldName !== undefined ? fieldName : key
|
|
27
27
|
const data = json[dataKey as keyof Json]
|
|
28
28
|
|
|
@@ -31,10 +31,11 @@ export function fromJson<Schema extends object, Json extends object>(
|
|
|
31
31
|
continue
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
if (
|
|
35
|
-
result[key] =
|
|
34
|
+
if (from) {
|
|
35
|
+
result[key] = from(data, json)
|
|
36
36
|
} else {
|
|
37
|
-
|
|
37
|
+
const isNullish = excludeNull ? data == null : data === undefined
|
|
38
|
+
result[key] = isNullish ? value : data
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -57,7 +58,7 @@ export function toJson<Json extends object>(json: Json): Json {
|
|
|
57
58
|
continue
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
const {
|
|
61
|
+
const { to, schema, isArray, fieldName, reconstruct, skip } = options as FieldOptions
|
|
61
62
|
const data = json[key as keyof Json]
|
|
62
63
|
|
|
63
64
|
if (skip !== undefined) {
|
|
@@ -72,8 +73,8 @@ export function toJson<Json extends object>(json: Json): Json {
|
|
|
72
73
|
continue
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
if (
|
|
76
|
-
result[resultKey] =
|
|
76
|
+
if (to) {
|
|
77
|
+
result[resultKey] = to(data, json)
|
|
77
78
|
} else {
|
|
78
79
|
result[resultKey] = data
|
|
79
80
|
}
|
package/lib/types.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
export interface Mapper<Record extends object = any> {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export interface Mapper<Value, Record extends object = any> {
|
|
2
|
+
(value: Value, record: Record): any
|
|
3
|
+
(value: Value, record: Record): any
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
export interface SkipMapper {
|
|
7
|
-
|
|
6
|
+
export interface SkipMapper<Value, Record> {
|
|
7
|
+
(value: Value, record: Record): boolean
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export interface FieldOptions<Record extends object = any> {
|
|
11
|
-
skip?: boolean | SkipMapper
|
|
10
|
+
export interface FieldOptions<Value = any, Record extends object = any> {
|
|
11
|
+
skip?: boolean | SkipMapper<Value, Record>
|
|
12
12
|
value?: any
|
|
13
|
-
|
|
13
|
+
excludeNull?: boolean
|
|
14
|
+
from?: Mapper<Value, Record>
|
|
15
|
+
to?: Mapper<Value, Record>
|
|
14
16
|
fieldName?: string
|
|
15
|
-
isArray?: boolean
|
|
16
17
|
reconstruct?: boolean
|
|
17
18
|
schema?: new () => object
|
|
19
|
+
isArray?: boolean
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
export interface MetaInfo {
|