phecda-core 1.0.0 → 1.0.2

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,20 +1,12 @@
1
- import * as mitt from 'mitt';
2
- import { EventType, Emitter, Handler } from 'mitt';
3
- import * as vue from 'vue';
4
- import { Ref, UnwrapNestedRefs, App } from 'vue';
5
-
6
1
  interface PhecdaNameSpace {
7
2
  [name: string]: Phecda;
8
3
  }
9
- interface PhecdaEvents extends Record<EventType, unknown> {
10
- [key: EventType]: any;
11
- }
12
4
  interface UsePipeOptions {
13
5
  transform: boolean;
14
6
  collectError: boolean;
15
7
  }
16
8
  interface PhecdaHandler {
17
- init?: (instance: Phecda, isFirstTime: boolean) => any;
9
+ init?: (instance: Phecda) => any;
18
10
  validate?: (instance: Phecda) => string | void;
19
11
  pipe?: (instance: Phecda) => void;
20
12
  error?: any;
@@ -26,35 +18,19 @@ interface PhecdaHandler {
26
18
  beforeUnload?: (instance: Phecda) => any;
27
19
  beforeLoad?: (instance: Phecda, v: any) => any;
28
20
  }
29
- type Vret<I> = {
30
- [P in keyof I]: I[P] extends Function ? I[P] : Ref<I[P]>;
31
- };
32
- type ClassValue<I> = {
33
- [P in keyof I]: I[P] extends Function ? undefined : I[P];
34
- };
35
- type SchemaToObj<S> = {
36
- [P in keyof S]: S[P] extends object ? SchemaToObj<S[P]> : (S[P] extends string ? any : S[P]);
37
- };
38
-
39
- declare class Phecda {
40
- protected static __PHECDA_EMIT__: Emitter<PhecdaEvents>;
41
- protected static __PHECDA_NAMESPACE__: PhecdaNameSpace;
42
- protected static __PHECDA_MODEL__: string[];
43
- protected _symbol: string;
44
- protected _namespace: {
21
+ interface Phecda {
22
+ _namespace: {
23
+ __TAG__: string;
45
24
  __INIT_EVENT__: Set<PropertyKey>;
25
+ __EXPOSE_VAR__: Set<PropertyKey>;
26
+ __IGNORE_VAR__: Set<PropertyKey>;
46
27
  __STATE_VAR__: Set<PropertyKey>;
47
- __STATE_HANDLER__: Map<PropertyKey, PhecdaHandler>;
28
+ __STATE_HANDLER__: Map<PropertyKey, PhecdaHandler[]>;
48
29
  };
49
- constructor();
50
- on<Key extends keyof PhecdaEvents>(type: Key, handler: Handler<PhecdaEvents[Key]>): void;
51
- emit(type: keyof PhecdaEvents, event: PhecdaEvents[keyof PhecdaEvents]): void;
52
- off<Key extends keyof PhecdaEvents>(type: Key, handler?: Handler<PhecdaEvents[Key]>): void;
53
30
  }
54
- declare class PhecdaWeb extends Phecda {
55
- constructor();
56
- }
57
- declare function register(instance: any): void;
31
+ type ClassValue<I> = {
32
+ [P in keyof I]: I[P] extends Function ? undefined : I[P];
33
+ };
58
34
 
59
35
  declare function plainToClass<M extends new (...args: any) => any, Data extends Record<PropertyKey, any>>(Model: M, input: Data, options?: Partial<UsePipeOptions>): Promise<{
60
36
  err: string[];
@@ -95,86 +71,55 @@ declare function to<T extends (...args: any) => any>(task: T, oldTasks?: Functio
95
71
  };
96
72
  value: Function[];
97
73
  };
98
- declare function snapShot<T extends new (...args: any) => any>(data: UnwrapNestedRefs<InstanceType<T>>): {
99
- data: UnwrapNestedRefs<InstanceType<T>>;
74
+ declare function snapShot<T extends new (...args: any) => any>(data: InstanceType<T>): {
75
+ data: InstanceType<T>;
100
76
  clear(): void;
101
77
  apply(): void;
102
78
  };
79
+ 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;
103
80
 
104
- declare function regisInitEvent(target: any, key: string): void;
105
- declare function getInitEvent(target: any): string[];
106
- declare function setModalState(target: any, key: PropertyKey): void;
107
- declare function setExposeKey(target: any, key: PropertyKey): void;
108
- declare function setIgnoreKey(target: any, key: PropertyKey): void;
109
- declare function getModelState(target: any): string[];
110
- declare function getExposeKey(target: any): string[];
111
- declare function getIgnoreKey(target: any): string[];
112
- declare function regisHandler(target: any, key: PropertyKey, handler: PhecdaHandler): void;
113
- declare function getHandler(target: any, key: PropertyKey): any;
114
- declare function Init(target: any, key: string): void;
115
- declare function Rule(rule: RegExp | string | Function | number, info: string, meta?: any): (obj: any, key: string) => void;
116
- declare function Ignore(target: any, key: string): void;
117
- declare function Clear(target: any, key: string): void;
81
+ declare function Init(target: any, key: PropertyKey): void;
82
+ declare function Rule(rule: RegExp | string | Function | number, info: string, meta?: any): (obj: any, key: PropertyKey) => void;
83
+ declare function Ignore(target: any, key: PropertyKey): void;
84
+ declare function Clear(target: any, key: PropertyKey): void;
118
85
  declare function Err<Fn extends (...args: any) => any>(cb: Fn): (target: any, key: PropertyKey) => void;
119
- declare function Get(target: any, key: string): void;
120
- declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: string) => void;
121
- declare function Watcher(eventName: string): (obj: any, key: string) => void;
86
+ declare function Get(target: any, key: PropertyKey): void;
87
+ declare function Pipe(v: ReturnType<typeof to>): (obj: any, key: PropertyKey) => void;
122
88
  declare function Tag(tag: string): (target: any) => void;
123
89
  declare function Storage(target: any): void;
124
90
 
125
- declare const EXPRESS_RE: RegExp;
126
- declare const FN_RE: RegExp;
127
- declare function createFilter<Data extends Record<string, any>>(initState?: Object, option?: {
128
- expressionRE?: RegExp;
129
- fnRE?: RegExp;
130
- exclude?: string[];
131
- }): {
132
- filter: <Schema>(obj: Schema) => SchemaToObj<Schema>;
133
- data: [Data] extends [vue.Ref<any>] ? Data : vue.Ref<vue.UnwrapRef<Data>>;
134
- init: (params?: Data) => void;
135
- setState: <Key extends string>(key: Key, value: Data[Key]) => void;
136
- storeState: (key: string, params?: Data) => void;
137
- store: {
138
- [key: string]: Data;
139
- };
140
- applyStore: (key: string) => void;
141
- dispose: () => void;
142
- clearStore: (key: string) => void;
143
- };
144
-
145
91
  declare function validate(p: RegExp | string | Function | Object | Number, v: any): Promise<any>;
146
- declare function isAsyncFunc(fn: Function): boolean;
147
92
 
148
- declare const emitter: mitt.Emitter<PhecdaEvents>;
149
- declare const emit: {
150
- <Key extends keyof PhecdaEvents>(type: Key, event: PhecdaEvents[Key]): void;
151
- <Key_1 extends keyof PhecdaEvents>(type: undefined extends PhecdaEvents[Key_1] ? Key_1 : never): void;
152
- };
93
+ declare function init(target: Phecda): void;
94
+ declare function regisInitEvent(target: Phecda, key: string): void;
95
+ declare function getInitEvent(target: Phecda): string[];
96
+ declare function setModalState(target: Phecda, key: PropertyKey): void;
97
+ declare function setExposeKey(target: Phecda, key: PropertyKey): void;
98
+ declare function setIgnoreKey(target: Phecda, key: PropertyKey): void;
99
+ declare function getModelState(target: Phecda): string[];
100
+ declare function getExposeKey(target: Phecda): string[];
101
+ declare function getIgnoreKey(target: Phecda): string[];
102
+ declare function regisHandler(target: Phecda, key: PropertyKey, handler: PhecdaHandler): void;
103
+ declare function getHandler(target: Phecda, key: PropertyKey): PhecdaHandler[];
104
+ declare function register(instance: Phecda): void;
153
105
 
154
- declare const phecdaSymbol: unique symbol;
155
- declare function createPhecda(): vue.Raw<{
156
- install(app: App): void;
157
- vProxyMap: WeakMap<object, any>;
158
- vMap: WeakMap<object, any>;
159
- }>;
160
- interface PhecdaInstance {
161
- vProxyMap: WeakMap<any, any>;
162
- vMap: WeakMap<any, any>;
163
- }
164
- declare function setActivePhecda(phecda: PhecdaInstance): void;
165
- declare function getActivePhecda(): PhecdaInstance;
166
-
167
- type _DeepPartial<T> = {
168
- [K in keyof T]?: _DeepPartial<T[K]>;
169
- };
106
+ declare function isArray(info?: string): (obj: any, key: PropertyKey) => void;
107
+ declare function isBoolean(info?: string): (obj: any, key: PropertyKey) => void;
108
+ declare function isNumber(info?: string): (obj: any, key: PropertyKey) => void;
109
+ declare function isString(info?: string): (obj: any, key: PropertyKey) => void;
110
+ declare function isObject(info?: string): (obj: any, key: PropertyKey) => void;
111
+ declare function isMobile(info?: string): (obj: any, key: PropertyKey) => void;
112
+ declare function isLandline(info?: string): (obj: any, key: PropertyKey) => void;
113
+ declare function isMailBox(info?: string): (obj: any, key: PropertyKey) => void;
114
+ declare function isIdCard(info?: string): (obj: any, key: PropertyKey) => void;
115
+ declare function isCnName(info?: string): (obj: any, key: PropertyKey) => void;
116
+ declare function isEnName(info?: string): (obj: any, key: PropertyKey) => void;
117
+ declare function isDate(info?: string): (obj: any, key: PropertyKey) => void;
118
+ declare function isWechat(info?: string): (obj: any, key: PropertyKey) => void;
119
+ declare function isHexColor(info?: string): (obj: any, key: PropertyKey) => void;
120
+ declare function isPostalCode(info?: string): (obj: any, key: PropertyKey) => void;
170
121
 
171
- declare function useR<T extends new (...args: any) => any>(Model: T): UnwrapNestedRefs<InstanceType<T>>;
172
- declare function usePatch<T extends new (...args: any) => any>(Model: T, Data: _DeepPartial<InstanceType<T>>): void;
173
- declare function useListen(eventName: Parameters<typeof emitter['on']>[0], cb: Parameters<typeof emitter['on']>[1]): () => void;
174
- declare function useV<T extends new (...args: any) => any>(Model: T): Vret<InstanceType<T>>;
175
- declare function cloneV<Instance extends Object>(instance: Instance): Instance;
176
- declare function initalize<M extends new (...args: any) => any>(Model: M): InstanceType<M> | void;
177
- declare function clearStorage<M extends new (...args: any) => any>(Model: M, isForceUpdate?: boolean): void;
178
- declare function deleteStorage(tag: string): void;
122
+ declare function toNumber(): (obj: any, key: PropertyKey) => void;
123
+ declare function toString(): (obj: any, key: PropertyKey) => void;
179
124
 
180
- export { ClassValue, Clear, EXPRESS_RE, Err, FN_RE, Get, Ignore, Init, Phecda, PhecdaEvents, PhecdaHandler, PhecdaNameSpace, PhecdaWeb, Pipe, Rule, SchemaToObj, Storage, Tag, UsePipeOptions, Vret, Watcher, classToValue, clearStorage, cloneV, createFilter, createPhecda, deleteStorage, emit, emitter, getActivePhecda, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, initalize, isAsyncFunc, phecdaSymbol, plainToClass, regisHandler, regisInitEvent, register, setActivePhecda, setExposeKey, setIgnoreKey, setModalState, snapShot, to, useListen, usePatch, useR, useV, validate };
125
+ export { ClassValue, Clear, Err, Get, Ignore, Init, Phecda, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, init, isArray, isBoolean, isCnName, isDate, isEnName, isHexColor, isIdCard, isLandline, isMailBox, isMobile, isNumber, isObject, isPostalCode, isString, isWechat, plainToClass, regisHandler, regisInitEvent, register, setExposeKey, setIgnoreKey, setModalState, snapShot, to, toNumber, toString, validate };
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,73 +15,63 @@ var __copyProps = (to2, from, except, desc) => {
17
15
  }
18
16
  return to2;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
19
 
26
20
  // src/index.ts
27
21
  var src_exports = {};
28
22
  __export(src_exports, {
29
23
  Clear: () => Clear,
30
- EXPRESS_RE: () => EXPRESS_RE,
31
24
  Err: () => Err,
32
- FN_RE: () => FN_RE,
33
25
  Get: () => Get,
34
26
  Ignore: () => Ignore,
35
27
  Init: () => Init,
36
- Phecda: () => Phecda,
37
- PhecdaWeb: () => PhecdaWeb,
38
28
  Pipe: () => Pipe,
39
29
  Rule: () => Rule,
40
30
  Storage: () => Storage,
41
31
  Tag: () => Tag,
42
- Watcher: () => Watcher,
32
+ addDecoToClass: () => addDecoToClass,
43
33
  classToValue: () => classToValue,
44
- clearStorage: () => clearStorage,
45
- cloneV: () => cloneV,
46
- createFilter: () => createFilter,
47
- createPhecda: () => createPhecda,
48
- deleteStorage: () => deleteStorage,
49
- emit: () => emit,
50
- emitter: () => emitter,
51
- getActivePhecda: () => getActivePhecda,
52
34
  getExposeKey: () => getExposeKey,
53
35
  getHandler: () => getHandler,
54
36
  getIgnoreKey: () => getIgnoreKey,
55
37
  getInitEvent: () => getInitEvent,
56
38
  getModelState: () => getModelState,
57
- initalize: () => initalize,
58
- isAsyncFunc: () => isAsyncFunc,
59
- phecdaSymbol: () => phecdaSymbol,
39
+ init: () => init,
40
+ isArray: () => isArray,
41
+ isBoolean: () => isBoolean,
42
+ isCnName: () => isCnName,
43
+ isDate: () => isDate,
44
+ isEnName: () => isEnName,
45
+ isHexColor: () => isHexColor,
46
+ isIdCard: () => isIdCard,
47
+ isLandline: () => isLandline,
48
+ isMailBox: () => isMailBox,
49
+ isMobile: () => isMobile,
50
+ isNumber: () => isNumber,
51
+ isObject: () => isObject,
52
+ isPostalCode: () => isPostalCode,
53
+ isString: () => isString,
54
+ isWechat: () => isWechat,
60
55
  plainToClass: () => plainToClass,
61
56
  regisHandler: () => regisHandler,
62
57
  regisInitEvent: () => regisInitEvent,
63
58
  register: () => register,
64
- setActivePhecda: () => setActivePhecda,
65
59
  setExposeKey: () => setExposeKey,
66
60
  setIgnoreKey: () => setIgnoreKey,
67
61
  setModalState: () => setModalState,
68
62
  snapShot: () => snapShot,
69
63
  to: () => to,
70
- useListen: () => useListen,
71
- usePatch: () => usePatch,
72
- useR: () => useR,
73
- useV: () => useV,
64
+ toNumber: () => toNumber,
65
+ toString: () => toString,
74
66
  validate: () => validate
75
67
  });
76
68
  module.exports = __toCommonJS(src_exports);
77
69
 
78
- // src/emitter.ts
79
- var import_mitt = __toESM(require("mitt"));
80
- var emitter = (0, import_mitt.default)();
81
- var emit = emitter.emit.bind(emitter);
82
-
83
- // src/decorators.ts
70
+ // src/core.ts
84
71
  function init(target) {
85
72
  if (!target._namespace) {
86
73
  target._namespace = {
74
+ __TAG__: "",
87
75
  __INIT_EVENT__: /* @__PURE__ */ new Set(),
88
76
  __EXPOSE_VAR__: /* @__PURE__ */ new Set(),
89
77
  __IGNORE_VAR__: /* @__PURE__ */ new Set(),
@@ -115,7 +103,7 @@ function setIgnoreKey(target, key) {
115
103
  }
116
104
  function getModelState(target) {
117
105
  init(target);
118
- return [...target._namespace.__STATE_VAR__].reverse();
106
+ return [...target._namespace.__STATE_VAR__];
119
107
  }
120
108
  function getExposeKey(target) {
121
109
  init(target);
@@ -135,6 +123,16 @@ function regisHandler(target, key, handler) {
135
123
  function getHandler(target, key) {
136
124
  return target._namespace.__STATE_HANDLER__.get(key) || [];
137
125
  }
126
+ function register(instance) {
127
+ const stateVars = getModelState(instance);
128
+ for (const item of stateVars) {
129
+ const handlers = getHandler(instance, item);
130
+ for (const hanlder of handlers)
131
+ hanlder.init?.(instance);
132
+ }
133
+ }
134
+
135
+ // src/decorators.ts
138
136
  function Init(target, key) {
139
137
  setModalState(target, key);
140
138
  regisHandler(target, key, {
@@ -187,24 +185,17 @@ function Pipe(v) {
187
185
  });
188
186
  };
189
187
  }
190
- function Watcher(eventName) {
191
- return (obj, key) => {
192
- setModalState(obj, key);
193
- regisHandler(obj, key, {
194
- init(instance) {
195
- emitter.on(eventName, instance[key].bind(instance));
196
- }
197
- });
198
- };
199
- }
200
188
  function Tag(tag) {
201
189
  return (target) => {
202
- target.prototype._symbol = tag;
190
+ init(target.prototype);
191
+ target.prototype._namespace.__TAG__ = tag;
203
192
  };
204
193
  }
205
194
  function Storage(target) {
206
195
  init(target.prototype);
207
- const tag = target.prototype._symbol;
196
+ const tag = target.prototype._namespace.__TAG__;
197
+ if (tag === "")
198
+ throw new Error("miss tag");
208
199
  const uniTag = Symbol(tag);
209
200
  setModalState(target.prototype, uniTag);
210
201
  regisHandler(target.prototype, uniTag, {
@@ -223,46 +214,6 @@ function Storage(target) {
223
214
  });
224
215
  }
225
216
 
226
- // src/model.ts
227
- var Phecda = class {
228
- constructor() {
229
- if (!this._symbol)
230
- throw new Error("phecda miss tag");
231
- if (!Phecda.__PHECDA_MODEL__) {
232
- Phecda.__PHECDA_MODEL__ = [];
233
- Phecda.__PHECDA_NAMESPACE__ = {};
234
- Phecda.__PHECDA_EMIT__ = emitter;
235
- }
236
- }
237
- on(type, handler) {
238
- Phecda.__PHECDA_EMIT__.on(type, handler);
239
- }
240
- emit(type, event) {
241
- Phecda.__PHECDA_EMIT__.emit(type, event);
242
- }
243
- off(type, handler) {
244
- Phecda.__PHECDA_EMIT__.off(type, handler);
245
- }
246
- };
247
- var PhecdaWeb = class extends Phecda {
248
- constructor() {
249
- super();
250
- if (!window.__PHECDA_EMIT__) {
251
- window.__PHECDA_EMIT__ = Phecda.__PHECDA_EMIT__;
252
- window.__PHECDA_MODEL__ = Phecda.__PHECDA_MODEL__;
253
- window.__PHECDA_NAMESPACE__ = Phecda.__PHECDA_NAMESPACE__;
254
- }
255
- }
256
- };
257
- function register(instance) {
258
- const stateVars = getModelState(instance);
259
- for (const item of stateVars) {
260
- const handlers = getHandler(instance, item);
261
- for (const hanlder of handlers)
262
- hanlder.init?.(instance);
263
- }
264
- }
265
-
266
217
  // src/utils.ts
267
218
  async function validate(p, v) {
268
219
  if (typeof p === "string" || typeof p === "number") {
@@ -275,11 +226,8 @@ async function validate(p, v) {
275
226
  return p.test(v);
276
227
  return false;
277
228
  }
278
- function isAsyncFunc(fn) {
279
- return fn[Symbol.toStringTag] === "AsyncFunction";
280
- }
281
229
 
282
- // src/composable.ts
230
+ // src/helper.ts
283
231
  async function plainToClass(Model, input, options = {}) {
284
232
  const data = new Model();
285
233
  const resolvedOptions = {
@@ -341,269 +289,140 @@ function snapShot(data) {
341
289
  }
342
290
  };
343
291
  }
292
+ function addDecoToClass(c, key, handler, type = "normal") {
293
+ handler(type === "normal" ? c.prototype : c, key);
294
+ }
344
295
 
345
- // src/filter.ts
346
- var import_vue = require("vue");
347
- var EXPRESS_RE = /^{{(.*)}}$/;
348
- var FN_RE = /^\[\[(.*)\]\]$/;
349
- function createFilter(initState = {}, option = {}) {
350
- const resolveOption = Object.assign(
351
- {
352
- expressionRE: EXPRESS_RE,
353
- fnRE: FN_RE,
354
- exclude: []
355
- },
356
- option
296
+ // src/preset/rule.ts
297
+ function isArray(info) {
298
+ return Rule((param) => Array.isArray(param), info || "it should be an array");
299
+ }
300
+ function isBoolean(info) {
301
+ return Rule((param) => [true, false].includes(param), info || "it should be true or false");
302
+ }
303
+ function isNumber(info) {
304
+ return Rule((param) => typeof param === "number", info || "it should be true or false");
305
+ }
306
+ function isString(info) {
307
+ return Rule((param) => typeof param === "string", info || "it should be a string");
308
+ }
309
+ function isObject(info) {
310
+ return Rule((param) => {
311
+ return Object.prototype.toString.call(param) === "[object Object]";
312
+ }, info || "it should be an object");
313
+ }
314
+ function isMobile(info) {
315
+ return Rule(
316
+ /^((\+|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}$/,
317
+ info || "it should be a mobile phone number"
357
318
  );
358
- let data = (0, import_vue.ref)(initState);
359
- let store = {};
360
- function traverse(obj) {
361
- for (const i in obj) {
362
- if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
363
- continue;
364
- if (typeof obj[i] === "object" && obj[i])
365
- traverse(obj[i]);
366
- if (typeof obj[i] === "string") {
367
- if (resolveOption.expressionRE.test(obj[i])) {
368
- const body = obj[i].match(resolveOption.expressionRE)[1];
369
- Object.defineProperty(obj, i, {
370
- get() {
371
- return new Function(...Object.keys(data.value), `return ${body}`)(
372
- ...Object.values(data.value)
373
- );
374
- }
375
- });
376
- }
377
- if (resolveOption.fnRE.test(obj[i])) {
378
- const body = obj[i].match(resolveOption.fnRE)[1];
379
- Object.defineProperty(obj, i, {
380
- get() {
381
- return new Function(...Object.keys(data.value), `${body}`)(
382
- ...Object.values(data.value)
383
- );
384
- }
385
- });
386
- }
387
- }
388
- }
389
- }
390
- function filter(obj) {
391
- obj = (0, import_vue.reactive)(obj);
392
- traverse(obj);
393
- return obj;
394
- }
395
- function setState(key, value) {
396
- data.value[key] = value;
397
- }
398
- function storeState(key, params) {
399
- store[key] = data.value;
400
- init2(params);
401
- }
402
- function applyStore(key) {
403
- if (!store[key])
404
- return;
405
- data.value = store[key];
406
- }
407
- function init2(params) {
408
- data.value = params || initState || {};
409
- }
410
- function clearStore(key) {
411
- delete store[key];
412
- }
413
- function dispose() {
414
- data = null;
415
- store = null;
416
- }
417
- return { filter, data, init: init2, setState, storeState, store, applyStore, dispose, clearStore };
418
319
  }
419
-
420
- // src/vue/phecda.ts
421
- var import_vue2 = require("vue");
422
- var phecdaSymbol = Symbol("phecda");
423
- function createPhecda() {
424
- const phecda = (0, import_vue2.markRaw)({
425
- install(app) {
426
- app.provide(phecdaSymbol, phecda);
427
- app.config.globalProperties.$phecda = phecda;
428
- },
429
- vProxyMap: /* @__PURE__ */ new WeakMap(),
430
- vMap: /* @__PURE__ */ new WeakMap()
431
- });
432
- return phecda;
320
+ function isLandline(info) {
321
+ return Rule(
322
+ /\d{3}-\d{8}|\d{4}-\d{7}/,
323
+ info || "it should be a mobile phone number"
324
+ );
433
325
  }
434
- var activePhecda = {
435
- vProxyMap: /* @__PURE__ */ new WeakMap(),
436
- vMap: /* @__PURE__ */ new WeakMap()
437
- };
438
- function setActivePhecda(phecda) {
439
- activePhecda = phecda;
326
+ function isMailBox(info) {
327
+ return Rule(
328
+ /^[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])?)*$/,
329
+ info || "it should be a mailbox number"
330
+ );
440
331
  }
441
- function getActivePhecda() {
442
- return activePhecda;
332
+ function isIdCard(info) {
333
+ return Rule(
334
+ /(^\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)$)/,
335
+ info || "it should be an identity card number"
336
+ );
443
337
  }
444
-
445
- // src/vue/composable.ts
446
- var import_vue4 = require("vue");
447
-
448
- // src/vue/utils.ts
449
- var import_vue3 = require("vue");
450
- function isObject(o) {
451
- return Object.prototype.toString.call(o) === "[object Object]";
452
- }
453
- function mergeReactiveObjects(target, patchToApply) {
454
- for (const key in patchToApply) {
455
- if (!patchToApply.hasOwnProperty(key))
456
- continue;
457
- const subPatch = patchToApply[key];
458
- const targetValue = target[key];
459
- if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !(0, import_vue3.isRef)(subPatch) && !(0, import_vue3.isReactive)(subPatch)) {
460
- target[key] = mergeReactiveObjects(targetValue, subPatch);
461
- } else {
462
- target[key] = subPatch;
463
- }
464
- }
465
- return target;
338
+ function isCnName(info) {
339
+ return Rule(
340
+ /^([\u4E00-\u9FA5·]{2,16})$/,
341
+ info || "\u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57"
342
+ );
466
343
  }
467
-
468
- // src/vue/composable.ts
469
- function useR(Model) {
470
- if ((0, import_vue4.getCurrentInstance)()) {
471
- const cur = (0, import_vue4.inject)(phecdaSymbol, null);
472
- if (cur)
473
- setActivePhecda(cur);
474
- }
475
- const { vMap } = getActivePhecda();
476
- if (!vMap.has(Model)) {
477
- const instance = (0, import_vue4.reactive)(new Model());
478
- vMap.set(Model, instance);
479
- register(instance);
480
- }
481
- return vMap.get(Model);
344
+ function isEnName(info) {
345
+ return Rule(
346
+ /(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/,
347
+ info || "it should be a valid en-name"
348
+ );
482
349
  }
483
- function usePatch(Model, Data) {
484
- useR(Model);
485
- const { vMap } = getActivePhecda();
486
- const target = vMap.get(Model);
487
- mergeReactiveObjects(target, Data);
350
+ function isDate(info) {
351
+ return Rule(
352
+ /^(?:(?!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)$/,
353
+ info || "it should be a valid date"
354
+ );
488
355
  }
489
- function useListen(eventName, cb) {
490
- (0, import_vue4.onUnmounted)(() => {
491
- emitter.off(eventName, cb);
492
- });
493
- emitter.on(eventName, cb);
494
- return () => emitter.off(eventName, cb);
495
- }
496
- function useV(Model) {
497
- useR(Model);
498
- const { vProxyMap, vMap } = getActivePhecda();
499
- if (vProxyMap.has(Model))
500
- return vProxyMap.get(Model);
501
- const instance = vMap.get(Model);
502
- const proxy = new Proxy(instance, {
503
- get(target, key) {
504
- if (typeof target[key] === "function") {
505
- const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
506
- if (!errorHandler)
507
- return target[key].bind(target);
508
- if (isAsyncFunc(target[key])) {
509
- return (...args) => {
510
- return target[key].apply(target, args).catch(errorHandler);
511
- };
512
- } else {
513
- return (...args) => {
514
- try {
515
- return target[key].apply(target, args);
516
- } catch (e) {
517
- return errorHandler(e);
518
- }
519
- };
520
- }
521
- }
522
- return (0, import_vue4.computed)({
523
- get() {
524
- return target[key];
525
- },
526
- set(v) {
527
- return target[key] = v;
528
- }
529
- });
530
- },
531
- set(target, key, v) {
532
- target[key] = v;
533
- return true;
534
- }
535
- });
536
- vProxyMap.set(Model, proxy);
537
- return proxy;
538
- }
539
- function cloneV(instance) {
540
- const newInstance = {};
541
- for (const key in instance) {
542
- if (instance.hasOwnProperty(key))
543
- newInstance[key] = instance[key];
544
- }
545
- return newInstance;
356
+ function isWechat(info) {
357
+ return Rule(
358
+ /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
359
+ info || "it should be a valid date"
360
+ );
546
361
  }
547
- function initalize(Model) {
548
- const instance = useR(Model);
549
- if (instance) {
550
- Object.assign(instance, new Model());
551
- return instance;
552
- }
362
+ function isHexColor(info) {
363
+ return Rule(
364
+ /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
365
+ info || "it should be a valid hex-color"
366
+ );
553
367
  }
554
- function clearStorage(Model, isForceUpdate = true) {
555
- localStorage.removeItem(`_phecda_${useR(Model)._symbol}`);
556
- isForceUpdate && initalize(Model);
368
+ function isPostalCode(info) {
369
+ return Rule(
370
+ /^(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}$/,
371
+ info || "it should be a valid postal code"
372
+ );
373
+ }
374
+
375
+ // src/preset/pipe.ts
376
+ function toNumber() {
377
+ return Pipe(to((param) => Number(param)));
557
378
  }
558
- function deleteStorage(tag) {
559
- localStorage.removeItem(`_phecda_${tag}`);
379
+ function toString() {
380
+ return Pipe(to((param) => String(param)));
560
381
  }
561
382
  // Annotate the CommonJS export names for ESM import in node:
562
383
  0 && (module.exports = {
563
384
  Clear,
564
- EXPRESS_RE,
565
385
  Err,
566
- FN_RE,
567
386
  Get,
568
387
  Ignore,
569
388
  Init,
570
- Phecda,
571
- PhecdaWeb,
572
389
  Pipe,
573
390
  Rule,
574
391
  Storage,
575
392
  Tag,
576
- Watcher,
393
+ addDecoToClass,
577
394
  classToValue,
578
- clearStorage,
579
- cloneV,
580
- createFilter,
581
- createPhecda,
582
- deleteStorage,
583
- emit,
584
- emitter,
585
- getActivePhecda,
586
395
  getExposeKey,
587
396
  getHandler,
588
397
  getIgnoreKey,
589
398
  getInitEvent,
590
399
  getModelState,
591
- initalize,
592
- isAsyncFunc,
593
- phecdaSymbol,
400
+ init,
401
+ isArray,
402
+ isBoolean,
403
+ isCnName,
404
+ isDate,
405
+ isEnName,
406
+ isHexColor,
407
+ isIdCard,
408
+ isLandline,
409
+ isMailBox,
410
+ isMobile,
411
+ isNumber,
412
+ isObject,
413
+ isPostalCode,
414
+ isString,
415
+ isWechat,
594
416
  plainToClass,
595
417
  regisHandler,
596
418
  regisInitEvent,
597
419
  register,
598
- setActivePhecda,
599
420
  setExposeKey,
600
421
  setIgnoreKey,
601
422
  setModalState,
602
423
  snapShot,
603
424
  to,
604
- useListen,
605
- usePatch,
606
- useR,
607
- useV,
425
+ toNumber,
426
+ toString,
608
427
  validate
609
428
  });
package/dist/index.mjs CHANGED
@@ -1,12 +1,8 @@
1
- // src/emitter.ts
2
- import mitt from "mitt";
3
- var emitter = mitt();
4
- var emit = emitter.emit.bind(emitter);
5
-
6
- // src/decorators.ts
1
+ // src/core.ts
7
2
  function init(target) {
8
3
  if (!target._namespace) {
9
4
  target._namespace = {
5
+ __TAG__: "",
10
6
  __INIT_EVENT__: /* @__PURE__ */ new Set(),
11
7
  __EXPOSE_VAR__: /* @__PURE__ */ new Set(),
12
8
  __IGNORE_VAR__: /* @__PURE__ */ new Set(),
@@ -38,7 +34,7 @@ function setIgnoreKey(target, key) {
38
34
  }
39
35
  function getModelState(target) {
40
36
  init(target);
41
- return [...target._namespace.__STATE_VAR__].reverse();
37
+ return [...target._namespace.__STATE_VAR__];
42
38
  }
43
39
  function getExposeKey(target) {
44
40
  init(target);
@@ -58,6 +54,16 @@ function regisHandler(target, key, handler) {
58
54
  function getHandler(target, key) {
59
55
  return target._namespace.__STATE_HANDLER__.get(key) || [];
60
56
  }
57
+ function register(instance) {
58
+ const stateVars = getModelState(instance);
59
+ for (const item of stateVars) {
60
+ const handlers = getHandler(instance, item);
61
+ for (const hanlder of handlers)
62
+ hanlder.init?.(instance);
63
+ }
64
+ }
65
+
66
+ // src/decorators.ts
61
67
  function Init(target, key) {
62
68
  setModalState(target, key);
63
69
  regisHandler(target, key, {
@@ -110,24 +116,17 @@ function Pipe(v) {
110
116
  });
111
117
  };
112
118
  }
113
- function Watcher(eventName) {
114
- return (obj, key) => {
115
- setModalState(obj, key);
116
- regisHandler(obj, key, {
117
- init(instance) {
118
- emitter.on(eventName, instance[key].bind(instance));
119
- }
120
- });
121
- };
122
- }
123
119
  function Tag(tag) {
124
120
  return (target) => {
125
- target.prototype._symbol = tag;
121
+ init(target.prototype);
122
+ target.prototype._namespace.__TAG__ = tag;
126
123
  };
127
124
  }
128
125
  function Storage(target) {
129
126
  init(target.prototype);
130
- const tag = target.prototype._symbol;
127
+ const tag = target.prototype._namespace.__TAG__;
128
+ if (tag === "")
129
+ throw new Error("miss tag");
131
130
  const uniTag = Symbol(tag);
132
131
  setModalState(target.prototype, uniTag);
133
132
  regisHandler(target.prototype, uniTag, {
@@ -146,46 +145,6 @@ function Storage(target) {
146
145
  });
147
146
  }
148
147
 
149
- // src/model.ts
150
- var Phecda = class {
151
- constructor() {
152
- if (!this._symbol)
153
- throw new Error("phecda miss tag");
154
- if (!Phecda.__PHECDA_MODEL__) {
155
- Phecda.__PHECDA_MODEL__ = [];
156
- Phecda.__PHECDA_NAMESPACE__ = {};
157
- Phecda.__PHECDA_EMIT__ = emitter;
158
- }
159
- }
160
- on(type, handler) {
161
- Phecda.__PHECDA_EMIT__.on(type, handler);
162
- }
163
- emit(type, event) {
164
- Phecda.__PHECDA_EMIT__.emit(type, event);
165
- }
166
- off(type, handler) {
167
- Phecda.__PHECDA_EMIT__.off(type, handler);
168
- }
169
- };
170
- var PhecdaWeb = class extends Phecda {
171
- constructor() {
172
- super();
173
- if (!window.__PHECDA_EMIT__) {
174
- window.__PHECDA_EMIT__ = Phecda.__PHECDA_EMIT__;
175
- window.__PHECDA_MODEL__ = Phecda.__PHECDA_MODEL__;
176
- window.__PHECDA_NAMESPACE__ = Phecda.__PHECDA_NAMESPACE__;
177
- }
178
- }
179
- };
180
- function register(instance) {
181
- const stateVars = getModelState(instance);
182
- for (const item of stateVars) {
183
- const handlers = getHandler(instance, item);
184
- for (const hanlder of handlers)
185
- hanlder.init?.(instance);
186
- }
187
- }
188
-
189
148
  // src/utils.ts
190
149
  async function validate(p, v) {
191
150
  if (typeof p === "string" || typeof p === "number") {
@@ -198,11 +157,8 @@ async function validate(p, v) {
198
157
  return p.test(v);
199
158
  return false;
200
159
  }
201
- function isAsyncFunc(fn) {
202
- return fn[Symbol.toStringTag] === "AsyncFunction";
203
- }
204
160
 
205
- // src/composable.ts
161
+ // src/helper.ts
206
162
  async function plainToClass(Model, input, options = {}) {
207
163
  const data = new Model();
208
164
  const resolvedOptions = {
@@ -264,268 +220,139 @@ function snapShot(data) {
264
220
  }
265
221
  };
266
222
  }
223
+ function addDecoToClass(c, key, handler, type = "normal") {
224
+ handler(type === "normal" ? c.prototype : c, key);
225
+ }
267
226
 
268
- // src/filter.ts
269
- import { reactive, ref } from "vue";
270
- var EXPRESS_RE = /^{{(.*)}}$/;
271
- var FN_RE = /^\[\[(.*)\]\]$/;
272
- function createFilter(initState = {}, option = {}) {
273
- const resolveOption = Object.assign(
274
- {
275
- expressionRE: EXPRESS_RE,
276
- fnRE: FN_RE,
277
- exclude: []
278
- },
279
- option
227
+ // src/preset/rule.ts
228
+ function isArray(info) {
229
+ return Rule((param) => Array.isArray(param), info || "it should be an array");
230
+ }
231
+ function isBoolean(info) {
232
+ return Rule((param) => [true, false].includes(param), info || "it should be true or false");
233
+ }
234
+ function isNumber(info) {
235
+ return Rule((param) => typeof param === "number", info || "it should be true or false");
236
+ }
237
+ function isString(info) {
238
+ return Rule((param) => typeof param === "string", info || "it should be a string");
239
+ }
240
+ function isObject(info) {
241
+ return Rule((param) => {
242
+ return Object.prototype.toString.call(param) === "[object Object]";
243
+ }, info || "it should be an object");
244
+ }
245
+ function isMobile(info) {
246
+ return Rule(
247
+ /^((\+|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}$/,
248
+ info || "it should be a mobile phone number"
280
249
  );
281
- let data = ref(initState);
282
- let store = {};
283
- function traverse(obj) {
284
- for (const i in obj) {
285
- if (Array.isArray(obj[i]) || resolveOption.exclude.includes(i))
286
- continue;
287
- if (typeof obj[i] === "object" && obj[i])
288
- traverse(obj[i]);
289
- if (typeof obj[i] === "string") {
290
- if (resolveOption.expressionRE.test(obj[i])) {
291
- const body = obj[i].match(resolveOption.expressionRE)[1];
292
- Object.defineProperty(obj, i, {
293
- get() {
294
- return new Function(...Object.keys(data.value), `return ${body}`)(
295
- ...Object.values(data.value)
296
- );
297
- }
298
- });
299
- }
300
- if (resolveOption.fnRE.test(obj[i])) {
301
- const body = obj[i].match(resolveOption.fnRE)[1];
302
- Object.defineProperty(obj, i, {
303
- get() {
304
- return new Function(...Object.keys(data.value), `${body}`)(
305
- ...Object.values(data.value)
306
- );
307
- }
308
- });
309
- }
310
- }
311
- }
312
- }
313
- function filter(obj) {
314
- obj = reactive(obj);
315
- traverse(obj);
316
- return obj;
317
- }
318
- function setState(key, value) {
319
- data.value[key] = value;
320
- }
321
- function storeState(key, params) {
322
- store[key] = data.value;
323
- init2(params);
324
- }
325
- function applyStore(key) {
326
- if (!store[key])
327
- return;
328
- data.value = store[key];
329
- }
330
- function init2(params) {
331
- data.value = params || initState || {};
332
- }
333
- function clearStore(key) {
334
- delete store[key];
335
- }
336
- function dispose() {
337
- data = null;
338
- store = null;
339
- }
340
- return { filter, data, init: init2, setState, storeState, store, applyStore, dispose, clearStore };
341
250
  }
342
-
343
- // src/vue/phecda.ts
344
- import { markRaw } from "vue";
345
- var phecdaSymbol = Symbol("phecda");
346
- function createPhecda() {
347
- const phecda = markRaw({
348
- install(app) {
349
- app.provide(phecdaSymbol, phecda);
350
- app.config.globalProperties.$phecda = phecda;
351
- },
352
- vProxyMap: /* @__PURE__ */ new WeakMap(),
353
- vMap: /* @__PURE__ */ new WeakMap()
354
- });
355
- return phecda;
251
+ function isLandline(info) {
252
+ return Rule(
253
+ /\d{3}-\d{8}|\d{4}-\d{7}/,
254
+ info || "it should be a mobile phone number"
255
+ );
356
256
  }
357
- var activePhecda = {
358
- vProxyMap: /* @__PURE__ */ new WeakMap(),
359
- vMap: /* @__PURE__ */ new WeakMap()
360
- };
361
- function setActivePhecda(phecda) {
362
- activePhecda = phecda;
257
+ function isMailBox(info) {
258
+ return Rule(
259
+ /^[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])?)*$/,
260
+ info || "it should be a mailbox number"
261
+ );
363
262
  }
364
- function getActivePhecda() {
365
- return activePhecda;
263
+ function isIdCard(info) {
264
+ return Rule(
265
+ /(^\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)$)/,
266
+ info || "it should be an identity card number"
267
+ );
366
268
  }
367
-
368
- // src/vue/composable.ts
369
- import { computed, getCurrentInstance, inject, onUnmounted, reactive as reactive2 } from "vue";
370
-
371
- // src/vue/utils.ts
372
- import { isReactive, isRef } from "vue";
373
- function isObject(o) {
374
- return Object.prototype.toString.call(o) === "[object Object]";
375
- }
376
- function mergeReactiveObjects(target, patchToApply) {
377
- for (const key in patchToApply) {
378
- if (!patchToApply.hasOwnProperty(key))
379
- continue;
380
- const subPatch = patchToApply[key];
381
- const targetValue = target[key];
382
- if (isObject(targetValue) && isObject(subPatch) && target.hasOwnProperty(key) && !isRef(subPatch) && !isReactive(subPatch)) {
383
- target[key] = mergeReactiveObjects(targetValue, subPatch);
384
- } else {
385
- target[key] = subPatch;
386
- }
387
- }
388
- return target;
269
+ function isCnName(info) {
270
+ return Rule(
271
+ /^([\u4E00-\u9FA5·]{2,16})$/,
272
+ info || "\u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57"
273
+ );
389
274
  }
390
-
391
- // src/vue/composable.ts
392
- function useR(Model) {
393
- if (getCurrentInstance()) {
394
- const cur = inject(phecdaSymbol, null);
395
- if (cur)
396
- setActivePhecda(cur);
397
- }
398
- const { vMap } = getActivePhecda();
399
- if (!vMap.has(Model)) {
400
- const instance = reactive2(new Model());
401
- vMap.set(Model, instance);
402
- register(instance);
403
- }
404
- return vMap.get(Model);
275
+ function isEnName(info) {
276
+ return Rule(
277
+ /(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/,
278
+ info || "it should be a valid en-name"
279
+ );
405
280
  }
406
- function usePatch(Model, Data) {
407
- useR(Model);
408
- const { vMap } = getActivePhecda();
409
- const target = vMap.get(Model);
410
- mergeReactiveObjects(target, Data);
281
+ function isDate(info) {
282
+ return Rule(
283
+ /^(?:(?!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)$/,
284
+ info || "it should be a valid date"
285
+ );
411
286
  }
412
- function useListen(eventName, cb) {
413
- onUnmounted(() => {
414
- emitter.off(eventName, cb);
415
- });
416
- emitter.on(eventName, cb);
417
- return () => emitter.off(eventName, cb);
418
- }
419
- function useV(Model) {
420
- useR(Model);
421
- const { vProxyMap, vMap } = getActivePhecda();
422
- if (vProxyMap.has(Model))
423
- return vProxyMap.get(Model);
424
- const instance = vMap.get(Model);
425
- const proxy = new Proxy(instance, {
426
- get(target, key) {
427
- if (typeof target[key] === "function") {
428
- const errorHandler = getHandler(target, key).find((item) => item.error)?.error;
429
- if (!errorHandler)
430
- return target[key].bind(target);
431
- if (isAsyncFunc(target[key])) {
432
- return (...args) => {
433
- return target[key].apply(target, args).catch(errorHandler);
434
- };
435
- } else {
436
- return (...args) => {
437
- try {
438
- return target[key].apply(target, args);
439
- } catch (e) {
440
- return errorHandler(e);
441
- }
442
- };
443
- }
444
- }
445
- return computed({
446
- get() {
447
- return target[key];
448
- },
449
- set(v) {
450
- return target[key] = v;
451
- }
452
- });
453
- },
454
- set(target, key, v) {
455
- target[key] = v;
456
- return true;
457
- }
458
- });
459
- vProxyMap.set(Model, proxy);
460
- return proxy;
461
- }
462
- function cloneV(instance) {
463
- const newInstance = {};
464
- for (const key in instance) {
465
- if (instance.hasOwnProperty(key))
466
- newInstance[key] = instance[key];
467
- }
468
- return newInstance;
287
+ function isWechat(info) {
288
+ return Rule(
289
+ /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
290
+ info || "it should be a valid date"
291
+ );
469
292
  }
470
- function initalize(Model) {
471
- const instance = useR(Model);
472
- if (instance) {
473
- Object.assign(instance, new Model());
474
- return instance;
475
- }
293
+ function isHexColor(info) {
294
+ return Rule(
295
+ /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
296
+ info || "it should be a valid hex-color"
297
+ );
476
298
  }
477
- function clearStorage(Model, isForceUpdate = true) {
478
- localStorage.removeItem(`_phecda_${useR(Model)._symbol}`);
479
- isForceUpdate && initalize(Model);
299
+ function isPostalCode(info) {
300
+ return Rule(
301
+ /^(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}$/,
302
+ info || "it should be a valid postal code"
303
+ );
304
+ }
305
+
306
+ // src/preset/pipe.ts
307
+ function toNumber() {
308
+ return Pipe(to((param) => Number(param)));
480
309
  }
481
- function deleteStorage(tag) {
482
- localStorage.removeItem(`_phecda_${tag}`);
310
+ function toString() {
311
+ return Pipe(to((param) => String(param)));
483
312
  }
484
313
  export {
485
314
  Clear,
486
- EXPRESS_RE,
487
315
  Err,
488
- FN_RE,
489
316
  Get,
490
317
  Ignore,
491
318
  Init,
492
- Phecda,
493
- PhecdaWeb,
494
319
  Pipe,
495
320
  Rule,
496
321
  Storage,
497
322
  Tag,
498
- Watcher,
323
+ addDecoToClass,
499
324
  classToValue,
500
- clearStorage,
501
- cloneV,
502
- createFilter,
503
- createPhecda,
504
- deleteStorage,
505
- emit,
506
- emitter,
507
- getActivePhecda,
508
325
  getExposeKey,
509
326
  getHandler,
510
327
  getIgnoreKey,
511
328
  getInitEvent,
512
329
  getModelState,
513
- initalize,
514
- isAsyncFunc,
515
- phecdaSymbol,
330
+ init,
331
+ isArray,
332
+ isBoolean,
333
+ isCnName,
334
+ isDate,
335
+ isEnName,
336
+ isHexColor,
337
+ isIdCard,
338
+ isLandline,
339
+ isMailBox,
340
+ isMobile,
341
+ isNumber,
342
+ isObject,
343
+ isPostalCode,
344
+ isString,
345
+ isWechat,
516
346
  plainToClass,
517
347
  regisHandler,
518
348
  regisInitEvent,
519
349
  register,
520
- setActivePhecda,
521
350
  setExposeKey,
522
351
  setIgnoreKey,
523
352
  setModalState,
524
353
  snapShot,
525
354
  to,
526
- useListen,
527
- usePatch,
528
- useR,
529
- useV,
355
+ toNumber,
356
+ toString,
530
357
  validate
531
358
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -11,10 +11,6 @@
11
11
  "dist"
12
12
  ],
13
13
  "license": "MIT",
14
- "dependencies": {
15
- "mitt": "^3.0.0",
16
- "vue": "^3.2.45"
17
- },
18
14
  "devDependencies": {
19
15
  "tsup": "^6.5.0"
20
16
  },