phecda-core 2.0.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
  };
@@ -346,6 +396,7 @@ function Pipe(v) {
346
396
  __name(Pipe, "Pipe");
347
397
  function Tag(tag) {
348
398
  return (target) => {
399
+ init(target.prototype);
349
400
  target.prototype.__TAG__ = tag;
350
401
  };
351
402
  }
@@ -382,6 +433,7 @@ function Global(target) {
382
433
  }
383
434
  __name(Global, "Global");
384
435
  function Empty(_target) {
436
+ init(_target.prototype);
385
437
  }
386
438
  __name(Empty, "Empty");
387
439
  var DataMap = {};
@@ -398,114 +450,16 @@ function Inject(key) {
398
450
  return DataMap[key] || EmptyProxy;
399
451
  }
400
452
  __name(Inject, "Inject");
401
-
402
- // src/utils.ts
403
- async function validate(p, v) {
404
- if (typeof p === "string" || typeof p === "number") {
405
- if (v === p)
406
- return true;
407
- }
408
- if (typeof p === "function")
409
- return p(v);
410
- if (p instanceof RegExp)
411
- return p.test(v);
412
- return false;
413
- }
414
- __name(validate, "validate");
415
- function getTag(Model) {
416
- return Model.prototype?.__TAG__;
417
- }
418
- __name(getTag, "getTag");
419
-
420
- // src/helper.ts
421
- function getBind(Model) {
422
- const instance = new Model();
423
- const keys = getModelState(instance);
424
- const ret = {};
425
- for (const item of keys) {
426
- const state = getState(instance, item);
427
- if (state.value)
428
- ret[item] = state.value;
429
- }
430
- return ret;
431
- }
432
- __name(getBind, "getBind");
433
- async function plainToClass(Model, input, options = {}) {
434
- const data = new Model();
435
- const err = [];
436
- const stateVars = getModelState(data);
437
- for (const item of stateVars) {
438
- data[item] = input[item];
439
- const handlers = getHandler(data, item);
440
- if (handlers) {
441
- if (options.collectError !== false) {
442
- for (const handler of handlers) {
443
- const rule = handler.rule;
444
- const ret = await validate(rule, data[item]);
445
- if (ret === "ok")
446
- break;
447
- if (rule && !ret) {
448
- err.push(typeof handler.info === "function" ? handler.info(item) : handler.info);
449
- if (!options.collectError)
450
- break;
451
- }
452
- }
453
- }
454
- if (err.length > 0 && !options.transform)
455
- return {
456
- err,
457
- data
458
- };
459
- if (options.transform !== false) {
460
- for (const handler of handlers)
461
- await handler.pipe?.(data);
462
- }
463
- }
464
- }
465
- return {
466
- data,
467
- err
468
- };
469
- }
470
- __name(plainToClass, "plainToClass");
471
- function classToValue(instance) {
472
- const data = {};
473
- const exposeVars = getExposeKey(instance);
474
- for (const item of exposeVars)
475
- data[item] = instance[item];
476
- return data;
477
- }
478
- __name(classToValue, "classToValue");
479
- function to(task, oldTasks) {
480
- const tasks = oldTasks || [];
481
- tasks.push(task);
482
- return {
483
- to: (task2) => to(task2, tasks),
484
- value: tasks
485
- };
486
- }
487
- __name(to, "to");
488
- function snapShot(data) {
489
- const snap = {};
490
- for (const i in data)
491
- snap[i] = data[i];
492
- return {
493
- data,
494
- clear() {
495
- for (const i in snap)
496
- delete data[i];
497
- },
498
- apply() {
499
- for (const i in snap)
500
- data[i] = snap[i];
501
- }
502
- };
503
- }
504
- __name(snapShot, "snapShot");
505
- function addDecoToClass(c, key, handler, type = "normal") {
506
- 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
+ });
507
461
  }
508
- __name(addDecoToClass, "addDecoToClass");
462
+ __name(Nested, "Nested");
509
463
 
510
464
  // src/namespace.ts
511
465
  var activeInstance = {};
@@ -597,87 +551,6 @@ function Storage(storeKey) {
597
551
  };
598
552
  }
599
553
  __name(Storage, "Storage");
600
-
601
- // src/preset/rule.ts
602
- function isOption() {
603
- return Rule((param) => param === void 0 ? "ok" : true, "");
604
- }
605
- __name(isOption, "isOption");
606
- function isArray(info) {
607
- return Rule((param) => Array.isArray(param), info || ((k) => `'${k}' should be an array`));
608
- }
609
- __name(isArray, "isArray");
610
- function isBoolean(info) {
611
- return Rule((param) => [
612
- true,
613
- false
614
- ].includes(param), info || ((k) => `'${k}' should be boolean`));
615
- }
616
- __name(isBoolean, "isBoolean");
617
- function isNumber(info) {
618
- return Rule((param) => typeof param === "number", info || ((k) => `'${k}' should be number`));
619
- }
620
- __name(isNumber, "isNumber");
621
- function isString(info) {
622
- return Rule((param) => typeof param === "string", info || ((k) => `'${k}' should be a string`));
623
- }
624
- __name(isString, "isString");
625
- function isObject(info) {
626
- return Rule((param) => {
627
- return Object.prototype.toString.call(param) === "[object Object]";
628
- }, info || ((k) => `'${k}' should be an object`));
629
- }
630
- __name(isObject, "isObject");
631
- function isMobile(info) {
632
- 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`));
633
- }
634
- __name(isMobile, "isMobile");
635
- function isLandline(info) {
636
- return Rule(/\d{3}-\d{8}|\d{4}-\d{7}/, info || ((k) => `'${k}' should be a landline`));
637
- }
638
- __name(isLandline, "isLandline");
639
- function isMailBox(info) {
640
- 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`));
641
- }
642
- __name(isMailBox, "isMailBox");
643
- function isIdCard(info) {
644
- 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`));
645
- }
646
- __name(isIdCard, "isIdCard");
647
- function isCnName(info) {
648
- return Rule(/^([\u4E00-\u9FA5·]{2,16})$/, info || ((k) => `'${k}' \u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57`));
649
- }
650
- __name(isCnName, "isCnName");
651
- function isEnName(info) {
652
- 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`));
653
- }
654
- __name(isEnName, "isEnName");
655
- function isDate(info) {
656
- 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`));
657
- }
658
- __name(isDate, "isDate");
659
- function isWechat(info) {
660
- return Rule(/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/, info || ((k) => `'${k}' should be a valid wechat`));
661
- }
662
- __name(isWechat, "isWechat");
663
- function isHexColor(info) {
664
- return Rule(/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, info || ((k) => `'${k}' should be a valid hex-color`));
665
- }
666
- __name(isHexColor, "isHexColor");
667
- function isPostalCode(info) {
668
- 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`));
669
- }
670
- __name(isPostalCode, "isPostalCode");
671
-
672
- // src/preset/pipe.ts
673
- function toNumber() {
674
- return Pipe(to((param) => Number(param)));
675
- }
676
- __name(toNumber, "toNumber");
677
- function toString() {
678
- return Pipe(to((param) => String(param)));
679
- }
680
- __name(toString, "toString");
681
554
  // Annotate the CommonJS export names for ESM import in node:
682
555
  0 && (module.exports = {
683
556
  Assign,
@@ -692,9 +565,9 @@ __name(toString, "toString");
692
565
  Ignore,
693
566
  Init,
694
567
  Inject,
568
+ Nested,
695
569
  Pipe,
696
570
  Provide,
697
- Rule,
698
571
  Storage,
699
572
  Tag,
700
573
  Watcher,
@@ -714,26 +587,11 @@ __name(toString, "toString");
714
587
  getOwnState,
715
588
  getProperty,
716
589
  getState,
590
+ getSymbol,
717
591
  getTag,
718
592
  init,
719
593
  injectProperty,
720
- isArray,
721
- isBoolean,
722
- isCnName,
723
- isDate,
724
- isEnName,
725
- isHexColor,
726
- isIdCard,
727
- isLandline,
728
- isMailBox,
729
- isMobile,
730
- isNumber,
731
- isObject,
732
- isOption,
733
594
  isPhecda,
734
- isPostalCode,
735
- isString,
736
- isWechat,
737
595
  plainToClass,
738
596
  regisHandler,
739
597
  regisInitEvent,
@@ -744,8 +602,5 @@ __name(toString, "toString");
744
602
  setModelVar,
745
603
  setState,
746
604
  snapShot,
747
- to,
748
- toNumber,
749
- toString,
750
- validate
605
+ transformClass
751
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
  };
@@ -255,6 +323,7 @@ function Pipe(v) {
255
323
  __name(Pipe, "Pipe");
256
324
  function Tag(tag) {
257
325
  return (target) => {
326
+ init(target.prototype);
258
327
  target.prototype.__TAG__ = tag;
259
328
  };
260
329
  }
@@ -291,6 +360,7 @@ function Global(target) {
291
360
  }
292
361
  __name(Global, "Global");
293
362
  function Empty(_target) {
363
+ init(_target.prototype);
294
364
  }
295
365
  __name(Empty, "Empty");
296
366
  var DataMap = {};
@@ -307,114 +377,16 @@ function Inject(key) {
307
377
  return DataMap[key] || EmptyProxy;
308
378
  }
309
379
  __name(Inject, "Inject");
310
-
311
- // src/utils.ts
312
- async function validate(p, v) {
313
- if (typeof p === "string" || typeof p === "number") {
314
- if (v === p)
315
- return true;
316
- }
317
- if (typeof p === "function")
318
- return p(v);
319
- if (p instanceof RegExp)
320
- return p.test(v);
321
- return false;
322
- }
323
- __name(validate, "validate");
324
- function getTag(Model) {
325
- return Model.prototype?.__TAG__;
326
- }
327
- __name(getTag, "getTag");
328
-
329
- // src/helper.ts
330
- function getBind(Model) {
331
- const instance = new Model();
332
- const keys = getModelState(instance);
333
- const ret = {};
334
- for (const item of keys) {
335
- const state = getState(instance, item);
336
- if (state.value)
337
- ret[item] = state.value;
338
- }
339
- return ret;
340
- }
341
- __name(getBind, "getBind");
342
- async function plainToClass(Model, input, options = {}) {
343
- const data = new Model();
344
- const err = [];
345
- const stateVars = getModelState(data);
346
- for (const item of stateVars) {
347
- data[item] = input[item];
348
- const handlers = getHandler(data, item);
349
- if (handlers) {
350
- if (options.collectError !== false) {
351
- for (const handler of handlers) {
352
- const rule = handler.rule;
353
- const ret = await validate(rule, data[item]);
354
- if (ret === "ok")
355
- break;
356
- if (rule && !ret) {
357
- err.push(typeof handler.info === "function" ? handler.info(item) : handler.info);
358
- if (!options.collectError)
359
- break;
360
- }
361
- }
362
- }
363
- if (err.length > 0 && !options.transform)
364
- return {
365
- err,
366
- data
367
- };
368
- if (options.transform !== false) {
369
- for (const handler of handlers)
370
- await handler.pipe?.(data);
371
- }
372
- }
373
- }
374
- return {
375
- data,
376
- err
377
- };
378
- }
379
- __name(plainToClass, "plainToClass");
380
- function classToValue(instance) {
381
- const data = {};
382
- const exposeVars = getExposeKey(instance);
383
- for (const item of exposeVars)
384
- data[item] = instance[item];
385
- return data;
386
- }
387
- __name(classToValue, "classToValue");
388
- function to(task, oldTasks) {
389
- const tasks = oldTasks || [];
390
- tasks.push(task);
391
- return {
392
- to: (task2) => to(task2, tasks),
393
- value: tasks
394
- };
395
- }
396
- __name(to, "to");
397
- function snapShot(data) {
398
- const snap = {};
399
- for (const i in data)
400
- snap[i] = data[i];
401
- return {
402
- data,
403
- clear() {
404
- for (const i in snap)
405
- delete data[i];
406
- },
407
- apply() {
408
- for (const i in snap)
409
- data[i] = snap[i];
410
- }
411
- };
412
- }
413
- __name(snapShot, "snapShot");
414
- function addDecoToClass(c, key, handler, type = "normal") {
415
- 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
+ });
416
388
  }
417
- __name(addDecoToClass, "addDecoToClass");
389
+ __name(Nested, "Nested");
418
390
 
419
391
  // src/namespace.ts
420
392
  var activeInstance = {};
@@ -506,87 +478,6 @@ function Storage(storeKey) {
506
478
  };
507
479
  }
508
480
  __name(Storage, "Storage");
509
-
510
- // src/preset/rule.ts
511
- function isOption() {
512
- return Rule((param) => param === void 0 ? "ok" : true, "");
513
- }
514
- __name(isOption, "isOption");
515
- function isArray(info) {
516
- return Rule((param) => Array.isArray(param), info || ((k) => `'${k}' should be an array`));
517
- }
518
- __name(isArray, "isArray");
519
- function isBoolean(info) {
520
- return Rule((param) => [
521
- true,
522
- false
523
- ].includes(param), info || ((k) => `'${k}' should be boolean`));
524
- }
525
- __name(isBoolean, "isBoolean");
526
- function isNumber(info) {
527
- return Rule((param) => typeof param === "number", info || ((k) => `'${k}' should be number`));
528
- }
529
- __name(isNumber, "isNumber");
530
- function isString(info) {
531
- return Rule((param) => typeof param === "string", info || ((k) => `'${k}' should be a string`));
532
- }
533
- __name(isString, "isString");
534
- function isObject(info) {
535
- return Rule((param) => {
536
- return Object.prototype.toString.call(param) === "[object Object]";
537
- }, info || ((k) => `'${k}' should be an object`));
538
- }
539
- __name(isObject, "isObject");
540
- function isMobile(info) {
541
- 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`));
542
- }
543
- __name(isMobile, "isMobile");
544
- function isLandline(info) {
545
- return Rule(/\d{3}-\d{8}|\d{4}-\d{7}/, info || ((k) => `'${k}' should be a landline`));
546
- }
547
- __name(isLandline, "isLandline");
548
- function isMailBox(info) {
549
- 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`));
550
- }
551
- __name(isMailBox, "isMailBox");
552
- function isIdCard(info) {
553
- 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`));
554
- }
555
- __name(isIdCard, "isIdCard");
556
- function isCnName(info) {
557
- return Rule(/^([\u4E00-\u9FA5·]{2,16})$/, info || ((k) => `'${k}' \u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57`));
558
- }
559
- __name(isCnName, "isCnName");
560
- function isEnName(info) {
561
- 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`));
562
- }
563
- __name(isEnName, "isEnName");
564
- function isDate(info) {
565
- 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`));
566
- }
567
- __name(isDate, "isDate");
568
- function isWechat(info) {
569
- return Rule(/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/, info || ((k) => `'${k}' should be a valid wechat`));
570
- }
571
- __name(isWechat, "isWechat");
572
- function isHexColor(info) {
573
- return Rule(/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, info || ((k) => `'${k}' should be a valid hex-color`));
574
- }
575
- __name(isHexColor, "isHexColor");
576
- function isPostalCode(info) {
577
- 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`));
578
- }
579
- __name(isPostalCode, "isPostalCode");
580
-
581
- // src/preset/pipe.ts
582
- function toNumber() {
583
- return Pipe(to((param) => Number(param)));
584
- }
585
- __name(toNumber, "toNumber");
586
- function toString() {
587
- return Pipe(to((param) => String(param)));
588
- }
589
- __name(toString, "toString");
590
481
  export {
591
482
  Assign,
592
483
  Bind,
@@ -600,9 +491,9 @@ export {
600
491
  Ignore,
601
492
  Init,
602
493
  Inject,
494
+ Nested,
603
495
  Pipe,
604
496
  Provide,
605
- Rule,
606
497
  Storage,
607
498
  Tag,
608
499
  Watcher,
@@ -622,26 +513,11 @@ export {
622
513
  getOwnState,
623
514
  getProperty,
624
515
  getState,
516
+ getSymbol,
625
517
  getTag,
626
518
  init,
627
519
  injectProperty,
628
- isArray,
629
- isBoolean,
630
- isCnName,
631
- isDate,
632
- isEnName,
633
- isHexColor,
634
- isIdCard,
635
- isLandline,
636
- isMailBox,
637
- isMobile,
638
- isNumber,
639
- isObject,
640
- isOption,
641
520
  isPhecda,
642
- isPostalCode,
643
- isString,
644
- isWechat,
645
521
  plainToClass,
646
522
  regisHandler,
647
523
  regisInitEvent,
@@ -652,8 +528,5 @@ export {
652
528
  setModelVar,
653
529
  setState,
654
530
  snapShot,
655
- to,
656
- toNumber,
657
- toString,
658
- validate
531
+ transformClass
659
532
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "2.0.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",