phecda-core 2.0.1-alpha.0 → 2.1.0-alpha.1

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/dist/index.d.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  interface NameSpace {
2
2
  [name: string]: Phecda;
3
3
  }
4
- interface UsePipeOptions {
5
- transform?: boolean;
6
- collectError?: boolean;
7
- }
8
4
  interface InjectData {
9
5
  [key: string]: any;
10
6
  }
@@ -28,61 +24,13 @@ type ClassValue<I> = {
28
24
  interface Events {
29
25
  }
30
26
 
31
- declare function getBind<M extends new (...args: any) => any>(Model: M): any;
32
- declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(Model: M, input: Data, options?: UsePipeOptions): Promise<{
33
- err: string[];
34
- data: InstanceType<M>;
35
- }>;
36
- declare function classToValue<M>(instance: M): ClassValue<M>;
37
- declare function to<T extends (...args: any) => any>(task: T, oldTasks?: Function[]): {
38
- to: <R extends (arg: ReturnType<T>) => any>(task: R) => {
39
- to: <R_1 extends (arg: ReturnType<R>) => any>(task: R_1) => {
40
- to: <R_2 extends (arg: ReturnType<R_1>) => any>(task: R_2) => {
41
- to: <R_3 extends (arg: ReturnType<R_2>) => any>(task: R_3) => {
42
- to: <R_4 extends (arg: ReturnType<R_3>) => any>(task: R_4) => {
43
- to: <R_5 extends (arg: ReturnType<R_4>) => any>(task: R_5) => {
44
- to: <R_6 extends (arg: ReturnType<R_5>) => any>(task: R_6) => {
45
- to: <R_7 extends (arg: ReturnType<R_6>) => any>(task: R_7) => {
46
- to: <R_8 extends (arg: ReturnType<R_7>) => any>(task: R_8) => {
47
- to: <R_9 extends (arg: ReturnType<R_8>) => any>(task: R_9) => {
48
- to: <R_10 extends (arg: ReturnType<R_9>) => any>(task: R_10) => any;
49
- value: Function[];
50
- };
51
- value: Function[];
52
- };
53
- value: Function[];
54
- };
55
- value: Function[];
56
- };
57
- value: Function[];
58
- };
59
- value: Function[];
60
- };
61
- value: Function[];
62
- };
63
- value: Function[];
64
- };
65
- value: Function[];
66
- };
67
- value: Function[];
68
- };
69
- value: Function[];
70
- };
71
- declare function snapShot<T extends new (...args: any) => any>(data: InstanceType<T>): {
72
- data: InstanceType<T>;
73
- clear(): void;
74
- apply(): void;
75
- };
76
- declare function addDecoToClass<M extends new (...args: any) => any>(c: M, key: keyof InstanceType<M> | string, handler: ((target: any, key: PropertyKey) => void), type?: 'static' | 'class' | 'normal'): void;
77
-
78
27
  declare function Init(target: any, key: PropertyKey): void;
79
28
  declare function Bind(value: any): (target: any, k: PropertyKey) => void;
80
- declare function Rule(rule: RegExp | string | ((arg: any) => boolean | 'ok') | number, info: string | ((k: string) => string), meta?: any): (obj: any, key: PropertyKey) => void;
81
29
  declare function Ignore(target: any, key: PropertyKey): void;
82
30
  declare function Clear(target: any, key: PropertyKey): void;
83
31
  declare function Err<Fn extends (...args: any) => any>(cb: Fn): (target: any, key: PropertyKey) => void;
84
32
  declare function Expose(target: any, key: PropertyKey): void;
85
- declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: PropertyKey) => void;
33
+ declare function Pipe(cb: (arg: any, instance: any, key: string) => any): (obj: any, key: PropertyKey) => void;
86
34
  declare function Tag(tag: string): (target: any) => void;
87
35
  declare function Assign(cb: (instance?: any) => any): (target: any) => void;
88
36
  declare function Global(target: any): void;
@@ -90,9 +38,20 @@ declare function Empty(_target: any): void;
90
38
  declare const DataMap: InjectData;
91
39
  declare function Provide<K extends keyof InjectData>(key: K, value: InjectData[K]): void;
92
40
  declare function Inject<K extends keyof InjectData>(key: K): InjectData[K];
41
+ declare function Nested<M extends new (...args: any) => any>(Model: M): (obj: any, key: PropertyKey) => void;
93
42
 
94
- declare function validate(p: RegExp | string | Function | Object | Number, v: any): Promise<any>;
95
43
  declare function getTag<M extends new (...args: any) => any>(Model: M): any;
44
+ declare function getSymbol<M extends new (...args: any) => any>(instance: InstanceType<M>): any;
45
+ declare function getBind<M extends new (...args: any) => any>(Model: M): any;
46
+ declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(Model: M, input: Data): InstanceType<M>;
47
+ declare function transformClass<M extends new (...args: any) => any>(instance: InstanceType<M>, force?: boolean): Promise<string[]>;
48
+ declare function classToValue<M>(instance: M): ClassValue<M>;
49
+ declare function snapShot<T extends new (...args: any) => any>(data: InstanceType<T>): {
50
+ data: InstanceType<T>;
51
+ clear(): void;
52
+ apply(): void;
53
+ };
54
+ declare function addDecoToClass<M extends new (...args: any) => any>(c: M, key: keyof InstanceType<M> | string, handler: ((target: any, key: PropertyKey) => void), type?: 'static' | 'class' | 'normal'): void;
96
55
 
97
56
  declare function isPhecda(target: any): any;
98
57
  declare function init(target: Phecda): void;
@@ -126,24 +85,4 @@ declare function Watcher(eventName: keyof Events, options?: {
126
85
  declare function Effect(eventName: string, options?: any): (obj: any, key: string) => void;
127
86
  declare function Storage(storeKey?: string): (target: any, key?: PropertyKey) => void;
128
87
 
129
- declare function isOption(): (obj: any, key: PropertyKey) => void;
130
- declare function isArray(info?: string): (obj: any, key: PropertyKey) => void;
131
- declare function isBoolean(info?: string): (obj: any, key: PropertyKey) => void;
132
- declare function isNumber(info?: string): (obj: any, key: PropertyKey) => void;
133
- declare function isString(info?: string): (obj: any, key: PropertyKey) => void;
134
- declare function isObject(info?: string): (obj: any, key: PropertyKey) => void;
135
- declare function isMobile(info?: string): (obj: any, key: PropertyKey) => void;
136
- declare function isLandline(info?: string): (obj: any, key: PropertyKey) => void;
137
- declare function isMailBox(info?: string): (obj: any, key: PropertyKey) => void;
138
- declare function isIdCard(info?: string): (obj: any, key: PropertyKey) => void;
139
- declare function isCnName(info?: string): (obj: any, key: PropertyKey) => void;
140
- declare function isEnName(info?: string): (obj: any, key: PropertyKey) => void;
141
- declare function isDate(info?: string): (obj: any, key: PropertyKey) => void;
142
- declare function isWechat(info?: string): (obj: any, key: PropertyKey) => void;
143
- declare function isHexColor(info?: string): (obj: any, key: PropertyKey) => void;
144
- declare function isPostalCode(info?: string): (obj: any, key: PropertyKey) => void;
145
-
146
- declare function toNumber(): (obj: any, key: PropertyKey) => void;
147
- declare function toString(): (obj: any, key: PropertyKey) => void;
148
-
149
- export { Assign, Bind, ClassValue, Clear, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, NameSpace, Phecda, Pipe, Provide, Rule, Storage, Tag, UsePipeOptions, Watcher, activeInstance, addDecoToClass, classToValue, getBind, getExposeKey, getHandler, getInitEvent, getModelState, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnInitEvent, getOwnModelState, getOwnState, getProperty, getState, getTag, init, injectProperty, isArray, isBoolean, isCnName, isDate, isEnName, isHexColor, isIdCard, isLandline, isMailBox, isMobile, isNumber, isObject, isOption, isPhecda, isPostalCode, isString, isWechat, plainToClass, regisHandler, regisInitEvent, register, registerAsync, setExposeKey, setIgnoreKey, setModelVar, setState, snapShot, to, toNumber, toString, validate };
88
+ export { Assign, Bind, ClassValue, Clear, DataMap, Effect, Empty, Err, Events, Expose, Global, Handler, Ignore, Init, Inject, InjectData, NameSpace, Nested, Phecda, Pipe, Provide, Storage, Tag, Watcher, activeInstance, addDecoToClass, classToValue, getBind, getExposeKey, getHandler, getInitEvent, getModelState, getOwnExposeKey, getOwnHandler, getOwnIgnoreKey, getOwnInitEvent, getOwnModelState, getOwnState, getProperty, getState, getSymbol, getTag, init, injectProperty, isPhecda, plainToClass, regisHandler, regisInitEvent, register, registerAsync, setExposeKey, setIgnoreKey, setModelVar, setState, snapShot, transformClass };
package/dist/index.js CHANGED
@@ -8,13 +8,13 @@ var __export = (target, all) => {
8
8
  for (var name in all)
9
9
  __defProp(target, name, { get: all[name], enumerable: true });
10
10
  };
11
- var __copyProps = (to2, from, except, desc) => {
11
+ var __copyProps = (to, from, except, desc) => {
12
12
  if (from && typeof from === "object" || typeof from === "function") {
13
13
  for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to2, key) && key !== except)
15
- __defProp(to2, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
16
  }
17
- return to2;
17
+ return to;
18
18
  };
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
 
@@ -33,9 +33,9 @@ __export(src_exports, {
33
33
  Ignore: () => Ignore,
34
34
  Init: () => Init,
35
35
  Inject: () => Inject,
36
+ Nested: () => Nested,
36
37
  Pipe: () => Pipe,
37
38
  Provide: () => Provide,
38
- Rule: () => Rule,
39
39
  Storage: () => Storage,
40
40
  Tag: () => Tag,
41
41
  Watcher: () => Watcher,
@@ -55,26 +55,11 @@ __export(src_exports, {
55
55
  getOwnState: () => getOwnState,
56
56
  getProperty: () => getProperty,
57
57
  getState: () => getState,
58
+ getSymbol: () => getSymbol,
58
59
  getTag: () => getTag,
59
60
  init: () => init,
60
61
  injectProperty: () => injectProperty,
61
- isArray: () => isArray,
62
- isBoolean: () => isBoolean,
63
- isCnName: () => isCnName,
64
- isDate: () => isDate,
65
- isEnName: () => isEnName,
66
- isHexColor: () => isHexColor,
67
- isIdCard: () => isIdCard,
68
- isLandline: () => isLandline,
69
- isMailBox: () => isMailBox,
70
- isMobile: () => isMobile,
71
- isNumber: () => isNumber,
72
- isObject: () => isObject,
73
- isOption: () => isOption,
74
62
  isPhecda: () => isPhecda,
75
- isPostalCode: () => isPostalCode,
76
- isString: () => isString,
77
- isWechat: () => isWechat,
78
63
  plainToClass: () => plainToClass,
79
64
  regisHandler: () => regisHandler,
80
65
  regisInitEvent: () => regisInitEvent,
@@ -85,10 +70,7 @@ __export(src_exports, {
85
70
  setModelVar: () => setModelVar,
86
71
  setState: () => setState,
87
72
  snapShot: () => snapShot,
88
- to: () => to,
89
- toNumber: () => toNumber,
90
- toString: () => toString,
91
- validate: () => validate
73
+ transformClass: () => transformClass
92
74
  });
93
75
  module.exports = __toCommonJS(src_exports);
94
76
 
@@ -274,6 +256,87 @@ async function registerAsync(instance) {
274
256
  }
275
257
  __name(registerAsync, "registerAsync");
276
258
 
259
+ // src/helper.ts
260
+ function getTag(Model) {
261
+ return Model.prototype?.__TAG__;
262
+ }
263
+ __name(getTag, "getTag");
264
+ function getSymbol(instance) {
265
+ const Model = instance.constructor;
266
+ return getTag(Model) || Model.name;
267
+ }
268
+ __name(getSymbol, "getSymbol");
269
+ function getBind(Model) {
270
+ const instance = new Model();
271
+ const keys = getModelState(instance);
272
+ const ret = {};
273
+ for (const item of keys) {
274
+ const state = getState(instance, item);
275
+ if (state.value)
276
+ ret[item] = state.value;
277
+ }
278
+ return ret;
279
+ }
280
+ __name(getBind, "getBind");
281
+ function plainToClass(Model, input) {
282
+ const instance = new Model();
283
+ const keys = getExposeKey(instance);
284
+ for (const item of keys)
285
+ instance[item] = input[item];
286
+ return instance;
287
+ }
288
+ __name(plainToClass, "plainToClass");
289
+ async function transformClass(instance, force = false) {
290
+ const err = [];
291
+ const stateVars = getModelState(instance);
292
+ for (const item of stateVars) {
293
+ const handlers = getHandler(instance, item);
294
+ if (handlers) {
295
+ for (const handler of handlers) {
296
+ const pipe = handler.pipe;
297
+ try {
298
+ await pipe(instance);
299
+ } catch (e) {
300
+ err.push(e.message);
301
+ if (!force)
302
+ return err;
303
+ }
304
+ }
305
+ }
306
+ }
307
+ return err;
308
+ }
309
+ __name(transformClass, "transformClass");
310
+ function classToValue(instance) {
311
+ const data = {};
312
+ const exposeVars = getExposeKey(instance);
313
+ for (const item of exposeVars)
314
+ data[item] = instance[item];
315
+ return data;
316
+ }
317
+ __name(classToValue, "classToValue");
318
+ function snapShot(data) {
319
+ const snap = {};
320
+ for (const i in data)
321
+ snap[i] = data[i];
322
+ return {
323
+ data,
324
+ clear() {
325
+ for (const i in snap)
326
+ delete data[i];
327
+ },
328
+ apply() {
329
+ for (const i in snap)
330
+ data[i] = snap[i];
331
+ }
332
+ };
333
+ }
334
+ __name(snapShot, "snapShot");
335
+ function addDecoToClass(c, key, handler, type = "normal") {
336
+ handler(type === "normal" ? c.prototype : c, key);
337
+ }
338
+ __name(addDecoToClass, "addDecoToClass");
339
+
277
340
  // src/decorators.ts
278
341
  function Init(target, key) {
279
342
  setModelVar(target, key);
@@ -293,17 +356,6 @@ function Bind(value) {
293
356
  };
294
357
  }
295
358
  __name(Bind, "Bind");
296
- function Rule(rule, info, meta) {
297
- return (obj, key) => {
298
- setModelVar(obj, key);
299
- regisHandler(obj, key, {
300
- rule,
301
- info,
302
- meta
303
- });
304
- };
305
- }
306
- __name(Rule, "Rule");
307
359
  function Ignore(target, key) {
308
360
  setIgnoreKey(target, key);
309
361
  }
@@ -331,14 +383,12 @@ function Expose(target, key) {
331
383
  setExposeKey(target, key);
332
384
  }
333
385
  __name(Expose, "Expose");
334
- function Pipe(v) {
386
+ function Pipe(cb) {
335
387
  return (obj, key) => {
336
388
  setModelVar(obj, key);
337
389
  regisHandler(obj, key, {
338
390
  async pipe(instance) {
339
- const tasks = v.value;
340
- for (const task of tasks)
341
- instance[key] = await task(instance[key]);
391
+ instance[key] = await cb(instance[key], instance, key);
342
392
  }
343
393
  });
344
394
  };
@@ -400,114 +450,16 @@ function Inject(key) {
400
450
  return DataMap[key] || EmptyProxy;
401
451
  }
402
452
  __name(Inject, "Inject");
403
-
404
- // src/utils.ts
405
- async function validate(p, v) {
406
- if (typeof p === "string" || typeof p === "number") {
407
- if (v === p)
408
- return true;
409
- }
410
- if (typeof p === "function")
411
- return p(v);
412
- if (p instanceof RegExp)
413
- return p.test(v);
414
- return false;
415
- }
416
- __name(validate, "validate");
417
- function getTag(Model) {
418
- return Model.prototype?.__TAG__;
419
- }
420
- __name(getTag, "getTag");
421
-
422
- // src/helper.ts
423
- function getBind(Model) {
424
- const instance = new Model();
425
- const keys = getModelState(instance);
426
- const ret = {};
427
- for (const item of keys) {
428
- const state = getState(instance, item);
429
- if (state.value)
430
- ret[item] = state.value;
431
- }
432
- return ret;
433
- }
434
- __name(getBind, "getBind");
435
- async function plainToClass(Model, input, options = {}) {
436
- const data = new Model();
437
- const err = [];
438
- const stateVars = getModelState(data);
439
- for (const item of stateVars) {
440
- data[item] = input[item];
441
- const handlers = getHandler(data, item);
442
- if (handlers) {
443
- if (options.collectError !== false) {
444
- for (const handler of handlers) {
445
- const rule = handler.rule;
446
- const ret = await validate(rule, data[item]);
447
- if (ret === "ok")
448
- break;
449
- if (rule && !ret) {
450
- err.push(typeof handler.info === "function" ? handler.info(item) : handler.info);
451
- if (!options.collectError)
452
- break;
453
- }
454
- }
455
- }
456
- if (err.length > 0 && !options.transform)
457
- return {
458
- err,
459
- data
460
- };
461
- if (options.transform !== false) {
462
- for (const handler of handlers)
463
- await handler.pipe?.(data);
464
- }
465
- }
466
- }
467
- return {
468
- data,
469
- err
470
- };
471
- }
472
- __name(plainToClass, "plainToClass");
473
- function classToValue(instance) {
474
- const data = {};
475
- const exposeVars = getExposeKey(instance);
476
- for (const item of exposeVars)
477
- data[item] = instance[item];
478
- return data;
479
- }
480
- __name(classToValue, "classToValue");
481
- function to(task, oldTasks) {
482
- const tasks = oldTasks || [];
483
- tasks.push(task);
484
- return {
485
- to: (task2) => to(task2, tasks),
486
- value: tasks
487
- };
488
- }
489
- __name(to, "to");
490
- function snapShot(data) {
491
- const snap = {};
492
- for (const i in data)
493
- snap[i] = data[i];
494
- return {
495
- data,
496
- clear() {
497
- for (const i in snap)
498
- delete data[i];
499
- },
500
- apply() {
501
- for (const i in snap)
502
- data[i] = snap[i];
503
- }
504
- };
505
- }
506
- __name(snapShot, "snapShot");
507
- function addDecoToClass(c, key, handler, type = "normal") {
508
- handler(type === "normal" ? c.prototype : c, key);
453
+ function Nested(Model) {
454
+ return Pipe(async (property) => {
455
+ const instance = plainToClass(Model, property);
456
+ const err = await transformClass(instance);
457
+ if (err.length > 0)
458
+ throw new Error(err[0]);
459
+ return instance;
460
+ });
509
461
  }
510
- __name(addDecoToClass, "addDecoToClass");
462
+ __name(Nested, "Nested");
511
463
 
512
464
  // src/namespace.ts
513
465
  var activeInstance = {};
@@ -599,87 +551,6 @@ function Storage(storeKey) {
599
551
  };
600
552
  }
601
553
  __name(Storage, "Storage");
602
-
603
- // src/preset/rule.ts
604
- function isOption() {
605
- return Rule((param) => param === void 0 ? "ok" : true, "");
606
- }
607
- __name(isOption, "isOption");
608
- function isArray(info) {
609
- return Rule((param) => Array.isArray(param), info || ((k) => `'${k}' should be an array`));
610
- }
611
- __name(isArray, "isArray");
612
- function isBoolean(info) {
613
- return Rule((param) => [
614
- true,
615
- false
616
- ].includes(param), info || ((k) => `'${k}' should be boolean`));
617
- }
618
- __name(isBoolean, "isBoolean");
619
- function isNumber(info) {
620
- return Rule((param) => typeof param === "number", info || ((k) => `'${k}' should be number`));
621
- }
622
- __name(isNumber, "isNumber");
623
- function isString(info) {
624
- return Rule((param) => typeof param === "string", info || ((k) => `'${k}' should be a string`));
625
- }
626
- __name(isString, "isString");
627
- function isObject(info) {
628
- return Rule((param) => {
629
- return Object.prototype.toString.call(param) === "[object Object]";
630
- }, info || ((k) => `'${k}' should be an object`));
631
- }
632
- __name(isObject, "isObject");
633
- function isMobile(info) {
634
- return Rule(/^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/, info || ((k) => `'${k}' should be a mobile phone number`));
635
- }
636
- __name(isMobile, "isMobile");
637
- function isLandline(info) {
638
- return Rule(/\d{3}-\d{8}|\d{4}-\d{7}/, info || ((k) => `'${k}' should be a landline`));
639
- }
640
- __name(isLandline, "isLandline");
641
- function isMailBox(info) {
642
- return Rule(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, info || ((k) => `'${k}' should be a mailbox`));
643
- }
644
- __name(isMailBox, "isMailBox");
645
- function isIdCard(info) {
646
- return Rule(/(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/, info || ((k) => `'${k}' should be an identity card number`));
647
- }
648
- __name(isIdCard, "isIdCard");
649
- function isCnName(info) {
650
- return Rule(/^([\u4E00-\u9FA5·]{2,16})$/, info || ((k) => `'${k}' \u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57`));
651
- }
652
- __name(isCnName, "isCnName");
653
- function isEnName(info) {
654
- return Rule(/(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/, info || ((k) => `'${k}' should be a valid en-name`));
655
- }
656
- __name(isEnName, "isEnName");
657
- function isDate(info) {
658
- return Rule(/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/, info || ((k) => `'${k}' should be a valid date`));
659
- }
660
- __name(isDate, "isDate");
661
- function isWechat(info) {
662
- return Rule(/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/, info || ((k) => `'${k}' should be a valid wechat`));
663
- }
664
- __name(isWechat, "isWechat");
665
- function isHexColor(info) {
666
- return Rule(/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, info || ((k) => `'${k}' should be a valid hex-color`));
667
- }
668
- __name(isHexColor, "isHexColor");
669
- function isPostalCode(info) {
670
- return Rule(/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/, info || ((k) => `'${k}' should be a valid postal code`));
671
- }
672
- __name(isPostalCode, "isPostalCode");
673
-
674
- // src/preset/pipe.ts
675
- function toNumber() {
676
- return Pipe(to((param) => Number(param)));
677
- }
678
- __name(toNumber, "toNumber");
679
- function toString() {
680
- return Pipe(to((param) => String(param)));
681
- }
682
- __name(toString, "toString");
683
554
  // Annotate the CommonJS export names for ESM import in node:
684
555
  0 && (module.exports = {
685
556
  Assign,
@@ -694,9 +565,9 @@ __name(toString, "toString");
694
565
  Ignore,
695
566
  Init,
696
567
  Inject,
568
+ Nested,
697
569
  Pipe,
698
570
  Provide,
699
- Rule,
700
571
  Storage,
701
572
  Tag,
702
573
  Watcher,
@@ -716,26 +587,11 @@ __name(toString, "toString");
716
587
  getOwnState,
717
588
  getProperty,
718
589
  getState,
590
+ getSymbol,
719
591
  getTag,
720
592
  init,
721
593
  injectProperty,
722
- isArray,
723
- isBoolean,
724
- isCnName,
725
- isDate,
726
- isEnName,
727
- isHexColor,
728
- isIdCard,
729
- isLandline,
730
- isMailBox,
731
- isMobile,
732
- isNumber,
733
- isObject,
734
- isOption,
735
594
  isPhecda,
736
- isPostalCode,
737
- isString,
738
- isWechat,
739
595
  plainToClass,
740
596
  regisHandler,
741
597
  regisInitEvent,
@@ -746,8 +602,5 @@ __name(toString, "toString");
746
602
  setModelVar,
747
603
  setState,
748
604
  snapShot,
749
- to,
750
- toNumber,
751
- toString,
752
- validate
605
+ transformClass
753
606
  });
package/dist/index.mjs CHANGED
@@ -183,6 +183,87 @@ async function registerAsync(instance) {
183
183
  }
184
184
  __name(registerAsync, "registerAsync");
185
185
 
186
+ // src/helper.ts
187
+ function getTag(Model) {
188
+ return Model.prototype?.__TAG__;
189
+ }
190
+ __name(getTag, "getTag");
191
+ function getSymbol(instance) {
192
+ const Model = instance.constructor;
193
+ return getTag(Model) || Model.name;
194
+ }
195
+ __name(getSymbol, "getSymbol");
196
+ function getBind(Model) {
197
+ const instance = new Model();
198
+ const keys = getModelState(instance);
199
+ const ret = {};
200
+ for (const item of keys) {
201
+ const state = getState(instance, item);
202
+ if (state.value)
203
+ ret[item] = state.value;
204
+ }
205
+ return ret;
206
+ }
207
+ __name(getBind, "getBind");
208
+ function plainToClass(Model, input) {
209
+ const instance = new Model();
210
+ const keys = getExposeKey(instance);
211
+ for (const item of keys)
212
+ instance[item] = input[item];
213
+ return instance;
214
+ }
215
+ __name(plainToClass, "plainToClass");
216
+ async function transformClass(instance, force = false) {
217
+ const err = [];
218
+ const stateVars = getModelState(instance);
219
+ for (const item of stateVars) {
220
+ const handlers = getHandler(instance, item);
221
+ if (handlers) {
222
+ for (const handler of handlers) {
223
+ const pipe = handler.pipe;
224
+ try {
225
+ await pipe(instance);
226
+ } catch (e) {
227
+ err.push(e.message);
228
+ if (!force)
229
+ return err;
230
+ }
231
+ }
232
+ }
233
+ }
234
+ return err;
235
+ }
236
+ __name(transformClass, "transformClass");
237
+ function classToValue(instance) {
238
+ const data = {};
239
+ const exposeVars = getExposeKey(instance);
240
+ for (const item of exposeVars)
241
+ data[item] = instance[item];
242
+ return data;
243
+ }
244
+ __name(classToValue, "classToValue");
245
+ function snapShot(data) {
246
+ const snap = {};
247
+ for (const i in data)
248
+ snap[i] = data[i];
249
+ return {
250
+ data,
251
+ clear() {
252
+ for (const i in snap)
253
+ delete data[i];
254
+ },
255
+ apply() {
256
+ for (const i in snap)
257
+ data[i] = snap[i];
258
+ }
259
+ };
260
+ }
261
+ __name(snapShot, "snapShot");
262
+ function addDecoToClass(c, key, handler, type = "normal") {
263
+ handler(type === "normal" ? c.prototype : c, key);
264
+ }
265
+ __name(addDecoToClass, "addDecoToClass");
266
+
186
267
  // src/decorators.ts
187
268
  function Init(target, key) {
188
269
  setModelVar(target, key);
@@ -202,17 +283,6 @@ function Bind(value) {
202
283
  };
203
284
  }
204
285
  __name(Bind, "Bind");
205
- function Rule(rule, info, meta) {
206
- return (obj, key) => {
207
- setModelVar(obj, key);
208
- regisHandler(obj, key, {
209
- rule,
210
- info,
211
- meta
212
- });
213
- };
214
- }
215
- __name(Rule, "Rule");
216
286
  function Ignore(target, key) {
217
287
  setIgnoreKey(target, key);
218
288
  }
@@ -240,14 +310,12 @@ function Expose(target, key) {
240
310
  setExposeKey(target, key);
241
311
  }
242
312
  __name(Expose, "Expose");
243
- function Pipe(v) {
313
+ function Pipe(cb) {
244
314
  return (obj, key) => {
245
315
  setModelVar(obj, key);
246
316
  regisHandler(obj, key, {
247
317
  async pipe(instance) {
248
- const tasks = v.value;
249
- for (const task of tasks)
250
- instance[key] = await task(instance[key]);
318
+ instance[key] = await cb(instance[key], instance, key);
251
319
  }
252
320
  });
253
321
  };
@@ -309,114 +377,16 @@ function Inject(key) {
309
377
  return DataMap[key] || EmptyProxy;
310
378
  }
311
379
  __name(Inject, "Inject");
312
-
313
- // src/utils.ts
314
- async function validate(p, v) {
315
- if (typeof p === "string" || typeof p === "number") {
316
- if (v === p)
317
- return true;
318
- }
319
- if (typeof p === "function")
320
- return p(v);
321
- if (p instanceof RegExp)
322
- return p.test(v);
323
- return false;
324
- }
325
- __name(validate, "validate");
326
- function getTag(Model) {
327
- return Model.prototype?.__TAG__;
328
- }
329
- __name(getTag, "getTag");
330
-
331
- // src/helper.ts
332
- function getBind(Model) {
333
- const instance = new Model();
334
- const keys = getModelState(instance);
335
- const ret = {};
336
- for (const item of keys) {
337
- const state = getState(instance, item);
338
- if (state.value)
339
- ret[item] = state.value;
340
- }
341
- return ret;
342
- }
343
- __name(getBind, "getBind");
344
- async function plainToClass(Model, input, options = {}) {
345
- const data = new Model();
346
- const err = [];
347
- const stateVars = getModelState(data);
348
- for (const item of stateVars) {
349
- data[item] = input[item];
350
- const handlers = getHandler(data, item);
351
- if (handlers) {
352
- if (options.collectError !== false) {
353
- for (const handler of handlers) {
354
- const rule = handler.rule;
355
- const ret = await validate(rule, data[item]);
356
- if (ret === "ok")
357
- break;
358
- if (rule && !ret) {
359
- err.push(typeof handler.info === "function" ? handler.info(item) : handler.info);
360
- if (!options.collectError)
361
- break;
362
- }
363
- }
364
- }
365
- if (err.length > 0 && !options.transform)
366
- return {
367
- err,
368
- data
369
- };
370
- if (options.transform !== false) {
371
- for (const handler of handlers)
372
- await handler.pipe?.(data);
373
- }
374
- }
375
- }
376
- return {
377
- data,
378
- err
379
- };
380
- }
381
- __name(plainToClass, "plainToClass");
382
- function classToValue(instance) {
383
- const data = {};
384
- const exposeVars = getExposeKey(instance);
385
- for (const item of exposeVars)
386
- data[item] = instance[item];
387
- return data;
388
- }
389
- __name(classToValue, "classToValue");
390
- function to(task, oldTasks) {
391
- const tasks = oldTasks || [];
392
- tasks.push(task);
393
- return {
394
- to: (task2) => to(task2, tasks),
395
- value: tasks
396
- };
397
- }
398
- __name(to, "to");
399
- function snapShot(data) {
400
- const snap = {};
401
- for (const i in data)
402
- snap[i] = data[i];
403
- return {
404
- data,
405
- clear() {
406
- for (const i in snap)
407
- delete data[i];
408
- },
409
- apply() {
410
- for (const i in snap)
411
- data[i] = snap[i];
412
- }
413
- };
414
- }
415
- __name(snapShot, "snapShot");
416
- function addDecoToClass(c, key, handler, type = "normal") {
417
- handler(type === "normal" ? c.prototype : c, key);
380
+ function Nested(Model) {
381
+ return Pipe(async (property) => {
382
+ const instance = plainToClass(Model, property);
383
+ const err = await transformClass(instance);
384
+ if (err.length > 0)
385
+ throw new Error(err[0]);
386
+ return instance;
387
+ });
418
388
  }
419
- __name(addDecoToClass, "addDecoToClass");
389
+ __name(Nested, "Nested");
420
390
 
421
391
  // src/namespace.ts
422
392
  var activeInstance = {};
@@ -508,87 +478,6 @@ function Storage(storeKey) {
508
478
  };
509
479
  }
510
480
  __name(Storage, "Storage");
511
-
512
- // src/preset/rule.ts
513
- function isOption() {
514
- return Rule((param) => param === void 0 ? "ok" : true, "");
515
- }
516
- __name(isOption, "isOption");
517
- function isArray(info) {
518
- return Rule((param) => Array.isArray(param), info || ((k) => `'${k}' should be an array`));
519
- }
520
- __name(isArray, "isArray");
521
- function isBoolean(info) {
522
- return Rule((param) => [
523
- true,
524
- false
525
- ].includes(param), info || ((k) => `'${k}' should be boolean`));
526
- }
527
- __name(isBoolean, "isBoolean");
528
- function isNumber(info) {
529
- return Rule((param) => typeof param === "number", info || ((k) => `'${k}' should be number`));
530
- }
531
- __name(isNumber, "isNumber");
532
- function isString(info) {
533
- return Rule((param) => typeof param === "string", info || ((k) => `'${k}' should be a string`));
534
- }
535
- __name(isString, "isString");
536
- function isObject(info) {
537
- return Rule((param) => {
538
- return Object.prototype.toString.call(param) === "[object Object]";
539
- }, info || ((k) => `'${k}' should be an object`));
540
- }
541
- __name(isObject, "isObject");
542
- function isMobile(info) {
543
- return Rule(/^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/, info || ((k) => `'${k}' should be a mobile phone number`));
544
- }
545
- __name(isMobile, "isMobile");
546
- function isLandline(info) {
547
- return Rule(/\d{3}-\d{8}|\d{4}-\d{7}/, info || ((k) => `'${k}' should be a landline`));
548
- }
549
- __name(isLandline, "isLandline");
550
- function isMailBox(info) {
551
- return Rule(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, info || ((k) => `'${k}' should be a mailbox`));
552
- }
553
- __name(isMailBox, "isMailBox");
554
- function isIdCard(info) {
555
- return Rule(/(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/, info || ((k) => `'${k}' should be an identity card number`));
556
- }
557
- __name(isIdCard, "isIdCard");
558
- function isCnName(info) {
559
- return Rule(/^([\u4E00-\u9FA5·]{2,16})$/, info || ((k) => `'${k}' \u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57`));
560
- }
561
- __name(isCnName, "isCnName");
562
- function isEnName(info) {
563
- return Rule(/(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/, info || ((k) => `'${k}' should be a valid en-name`));
564
- }
565
- __name(isEnName, "isEnName");
566
- function isDate(info) {
567
- return Rule(/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/, info || ((k) => `'${k}' should be a valid date`));
568
- }
569
- __name(isDate, "isDate");
570
- function isWechat(info) {
571
- return Rule(/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/, info || ((k) => `'${k}' should be a valid wechat`));
572
- }
573
- __name(isWechat, "isWechat");
574
- function isHexColor(info) {
575
- return Rule(/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, info || ((k) => `'${k}' should be a valid hex-color`));
576
- }
577
- __name(isHexColor, "isHexColor");
578
- function isPostalCode(info) {
579
- return Rule(/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/, info || ((k) => `'${k}' should be a valid postal code`));
580
- }
581
- __name(isPostalCode, "isPostalCode");
582
-
583
- // src/preset/pipe.ts
584
- function toNumber() {
585
- return Pipe(to((param) => Number(param)));
586
- }
587
- __name(toNumber, "toNumber");
588
- function toString() {
589
- return Pipe(to((param) => String(param)));
590
- }
591
- __name(toString, "toString");
592
481
  export {
593
482
  Assign,
594
483
  Bind,
@@ -602,9 +491,9 @@ export {
602
491
  Ignore,
603
492
  Init,
604
493
  Inject,
494
+ Nested,
605
495
  Pipe,
606
496
  Provide,
607
- Rule,
608
497
  Storage,
609
498
  Tag,
610
499
  Watcher,
@@ -624,26 +513,11 @@ export {
624
513
  getOwnState,
625
514
  getProperty,
626
515
  getState,
516
+ getSymbol,
627
517
  getTag,
628
518
  init,
629
519
  injectProperty,
630
- isArray,
631
- isBoolean,
632
- isCnName,
633
- isDate,
634
- isEnName,
635
- isHexColor,
636
- isIdCard,
637
- isLandline,
638
- isMailBox,
639
- isMobile,
640
- isNumber,
641
- isObject,
642
- isOption,
643
520
  isPhecda,
644
- isPostalCode,
645
- isString,
646
- isWechat,
647
521
  plainToClass,
648
522
  regisHandler,
649
523
  regisInitEvent,
@@ -654,8 +528,5 @@ export {
654
528
  setModelVar,
655
529
  setState,
656
530
  snapShot,
657
- to,
658
- toNumber,
659
- toString,
660
- validate
531
+ transformClass
661
532
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "2.0.1-alpha.0",
3
+ "version": "2.1.0-alpha.1",
4
4
  "description": "provide base function and abstract limit to other phecda module ",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",