magic-structure 1.0.6 → 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 CHANGED
@@ -1,2 +1,3 @@
1
- export { field, fieldName, schema, mapper, value, skip, skipIf } from './lib/decorators'
2
- export { fromJson, toJson } from './lib/mappers'
1
+ export { field, fieldName, schema, from, to, value, skip, skipIf } from './lib/decorators'
2
+ export { fromJson, toJson } from './lib/mappers'
3
+ export { type FieldOptions, type Mapper, type SkipMapper } from './lib/types'
package/lib/decorators.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { FieldOptions, Mapper, SkipMapper } from './types.ts'
2
1
  import Meta from './meta'
2
+ import type { FieldOptions, Mapper, SkipMapper } from './types'
3
3
 
4
4
  function field(options: FieldOptions) {
5
5
  return <Target extends object>(target: Target, property: keyof Target) => {
@@ -11,22 +11,26 @@ function fieldName(fieldName: string, reconstruct = false) {
11
11
  return field({ fieldName, reconstruct })
12
12
  }
13
13
 
14
- function mapper<Record extends object>(mapper: Mapper<Record>) {
15
- return field({ mapper })
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, mapper, schema, value, skipIf, skip }
36
+ export { field, fieldName, from, to, schema, value, skipIf, skip }
package/lib/mappers.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { FieldOptions } from './types.ts'
2
1
  import Meta from './meta'
2
+ import type { FieldOptions } from './types'
3
3
 
4
4
  export function fromJson<Schema extends object, Json extends object>(
5
5
  schema: new () => Schema,
@@ -22,7 +22,7 @@ export function fromJson<Schema extends object, Json extends object>(
22
22
  continue
23
23
  }
24
24
 
25
- const { fieldName, value, mapper, schema, isArray } = options as FieldOptions
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 (mapper?.from) {
35
- result[key] = mapper.from(data, json)
34
+ if (from) {
35
+ result[key] = from(data, json)
36
36
  } else {
37
- result[key] = data === undefined ? value : data
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 { mapper, schema, isArray, fieldName, reconstruct, skip } = options as FieldOptions
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 (mapper?.to) {
76
- result[resultKey] = mapper.to(data, json)
76
+ if (to) {
77
+ result[resultKey] = to(data, json)
77
78
  } else {
78
79
  result[resultKey] = data
79
80
  }
package/lib/meta.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { FieldOptions, MetaInfo } from './types.ts'
1
+ import type { FieldOptions, MetaInfo } from './types'
2
2
 
3
3
  const META_KEY = Symbol('meta')
4
4
 
package/lib/types.ts CHANGED
@@ -1,20 +1,22 @@
1
- export interface Mapper<Record extends object = any> {
2
- from?: (field: any, record: Record) => any
3
- to?: (field: any, record: Record) => any
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
- <Record>(field: any, record: Record): boolean
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
- mapper?: Mapper<Record>
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 {
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "magic-structure",
3
- "version": "1.0.6",
3
+ "version": "1.1.0",
4
4
  "author": "Vlad Kramarukha",
5
5
  "description": "TS lib for mapping data",
6
6
  "keywords": ["TS", "mapper", "structure", "json", "magic"],
7
7
  "private": false,
8
- "module": "dist/index.js",
8
+ "module": "index.ts",
9
9
  "type": "module",
10
- "main": "dist/index.js",
11
- "types": "dist/lib/types.d.ts",
12
- "files": ["README.md", "LICENSE", "lib/*", "dist/*", "index.ts"],
10
+ "main": "index.ts",
11
+ "types": "./lib/types.ts",
12
+ "files": ["README.md", "LICENSE", "lib/*", "index.ts"],
13
13
  "license": "Apache-2.0",
14
14
  "exports": {
15
15
  ".": {
16
- "default": "./dist/index.js",
17
- "types": "./dist/lib/types.d.ts"
16
+ "default": "./index.ts",
17
+ "types": "./lib/types.ts"
18
18
  }
19
19
  },
20
20
  "devDependencies": {
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { field, fieldName, schema, mapper, value, skip, skipIf } from './lib/decorators';
2
- export { fromJson, toJson } from './lib/mappers';
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { field, fieldName, schema, mapper, value, skip, skipIf } from './lib/decorators';
2
- export { fromJson, toJson } from './lib/mappers';
@@ -1,9 +0,0 @@
1
- import type { FieldOptions, Mapper, SkipMapper } from './types.ts';
2
- declare function field(options: FieldOptions): <Target extends object>(target: Target, property: keyof Target) => void;
3
- declare function fieldName(fieldName: string, reconstruct?: boolean): <Target extends object>(target: Target, property: keyof Target) => void;
4
- declare function mapper<Record extends object>(mapper: Mapper<Record>): <Target extends object>(target: Target, property: keyof Target) => void;
5
- declare function schema(schema: FieldOptions['schema'], isArray?: FieldOptions['isArray']): <Target extends object>(target: Target, property: keyof Target) => void;
6
- declare function value(value: any): <Target extends object>(target: Target, property: keyof Target) => void;
7
- declare function skipIf(skipMapper: SkipMapper): <Target extends object>(target: Target, property: keyof Target) => void;
8
- declare const skip: <Target extends object>(target: Target, property: keyof Target) => void;
9
- export { field, fieldName, mapper, schema, value, skipIf, skip };
@@ -1,23 +0,0 @@
1
- import Meta from './meta';
2
- function field(options) {
3
- return (target, property) => {
4
- Meta.addMeta(target, property, options);
5
- };
6
- }
7
- function fieldName(fieldName, reconstruct = false) {
8
- return field({ fieldName, reconstruct });
9
- }
10
- function mapper(mapper) {
11
- return field({ mapper });
12
- }
13
- function schema(schema, isArray = false) {
14
- return field({ schema, isArray });
15
- }
16
- function value(value) {
17
- return field({ value });
18
- }
19
- function skipIf(skipMapper) {
20
- return field({ skip: skipMapper });
21
- }
22
- const skip = field({ skip: true });
23
- export { field, fieldName, mapper, schema, value, skipIf, skip };
@@ -1,2 +0,0 @@
1
- export declare function fromJson<Schema extends object, Json extends object>(schema: new () => Schema, json: Json): Schema;
2
- export declare function toJson<Json extends object>(json: Json): Json;
@@ -1,63 +0,0 @@
1
- import Meta from './meta';
2
- export function fromJson(schema, json) {
3
- if (json && typeof json !== 'object') {
4
- return json;
5
- }
6
- const result = {};
7
- const instance = new schema();
8
- const metaInfo = Meta.getMeta(instance);
9
- for (const key of [...Object.keys(metaInfo), ...Object.keys(instance)]) {
10
- const options = metaInfo[key];
11
- if (!options) {
12
- result[key] = json[key];
13
- continue;
14
- }
15
- const { fieldName, value, mapper, schema, isArray } = options;
16
- const dataKey = fieldName !== undefined ? fieldName : key;
17
- const data = json[dataKey];
18
- if (schema && data && typeof data === 'object') {
19
- result[key] = isArray ? data.map((v) => fromJson(schema, v)) : fromJson(schema, data);
20
- continue;
21
- }
22
- if (mapper?.from) {
23
- result[key] = mapper.from(data, json);
24
- }
25
- else {
26
- result[key] = data === undefined ? value : data;
27
- }
28
- }
29
- return Object.assign(instance, result);
30
- }
31
- export function toJson(json) {
32
- if (json && typeof json !== 'object') {
33
- return json;
34
- }
35
- const result = {};
36
- const metaInfo = Meta.getMeta(json);
37
- for (const key of [...Object.keys(metaInfo), ...Object.keys(json)]) {
38
- const options = metaInfo[key];
39
- if (!options) {
40
- result[key] = json[key];
41
- continue;
42
- }
43
- const { mapper, schema, isArray, fieldName, reconstruct, skip } = options;
44
- const data = json[key];
45
- if (skip !== undefined) {
46
- const skipped = typeof skip === 'function' ? skip(data, json) : skip;
47
- if (skipped)
48
- continue;
49
- }
50
- const resultKey = fieldName && reconstruct ? fieldName : key;
51
- if (schema && data && typeof data === 'object') {
52
- result[resultKey] = isArray ? data.map(toJson) : toJson(data);
53
- continue;
54
- }
55
- if (mapper?.to) {
56
- result[resultKey] = mapper.to(data, json);
57
- }
58
- else {
59
- result[resultKey] = data;
60
- }
61
- }
62
- return result;
63
- }
@@ -1,6 +0,0 @@
1
- import type { FieldOptions, MetaInfo } from './types.ts';
2
- export default class Meta {
3
- static isMeta<Target extends object>(target: Target | (Target & MetaInfo)): target is Target & MetaInfo;
4
- static addMeta<Target extends object>(target: Target, property: keyof Target, info: FieldOptions): void;
5
- static getMeta<Target extends object>(target: Target | Target & MetaInfo): MetaInfo;
6
- }
package/dist/lib/meta.js DELETED
@@ -1,20 +0,0 @@
1
- const META_KEY = Symbol('meta');
2
- const pt = (target) => target.constructor.prototype;
3
- const meta = (target) => pt(target)[META_KEY] || {};
4
- export default class Meta {
5
- static isMeta(target) {
6
- return META_KEY in pt(target);
7
- }
8
- static addMeta(target, property, info) {
9
- if (!Meta.isMeta(target)) {
10
- pt(target)[META_KEY] = {};
11
- }
12
- if (!meta(target)[property]) {
13
- meta(target)[property] = {};
14
- }
15
- Object.assign(meta(target)[property], info);
16
- }
17
- static getMeta(target) {
18
- return meta(target);
19
- }
20
- }
@@ -1,19 +0,0 @@
1
- export interface Mapper<Record extends object = any> {
2
- from?: (field: any, record: Record) => any;
3
- to?: (field: any, record: Record) => any;
4
- }
5
- export interface SkipMapper {
6
- <Record>(field: any, record: Record): boolean;
7
- }
8
- export interface FieldOptions<Record extends object = any> {
9
- skip?: boolean | SkipMapper;
10
- value?: any;
11
- mapper?: Mapper<Record>;
12
- fieldName?: string;
13
- isArray?: boolean;
14
- reconstruct?: boolean;
15
- schema?: new () => object;
16
- }
17
- export interface MetaInfo {
18
- [p: string]: FieldOptions;
19
- }
package/dist/lib/types.js DELETED
File without changes