phecda-core 1.0.1 → 1.0.4

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
@@ -6,17 +6,13 @@ interface UsePipeOptions {
6
6
  collectError: boolean;
7
7
  }
8
8
  interface PhecdaHandler {
9
- init?: (instance: Phecda) => any;
10
- validate?: (instance: Phecda) => string | void;
11
- pipe?: (instance: Phecda) => void;
12
- error?: any;
13
- watch?: any;
14
- rule?: any;
15
- info?: any;
16
- ignore?: boolean;
9
+ init?: (instance: any) => any;
10
+ pipe?: (instance: any) => void;
11
+ rule?: RegExp | string | Function | number;
12
+ info?: string;
17
13
  meta?: any;
18
- beforeUnload?: (instance: Phecda) => any;
19
- beforeLoad?: (instance: Phecda, v: any) => any;
14
+ error?: any;
15
+ http?: any;
20
16
  }
21
17
  interface Phecda {
22
18
  _namespace: {
@@ -89,6 +85,7 @@ declare function Tag(tag: string): (target: any) => void;
89
85
  declare function Storage(target: any): void;
90
86
 
91
87
  declare function validate(p: RegExp | string | Function | Object | Number, v: any): Promise<any>;
88
+ declare function getTag<M extends new (...args: any) => any>(Model: M): any;
92
89
 
93
90
  declare function init(target: Phecda): void;
94
91
  declare function regisInitEvent(target: Phecda, key: string): void;
@@ -103,4 +100,29 @@ declare function regisHandler(target: Phecda, key: PropertyKey, handler: PhecdaH
103
100
  declare function getHandler(target: Phecda, key: PropertyKey): PhecdaHandler[];
104
101
  declare function register(instance: Phecda): void;
105
102
 
106
- export { ClassValue, Clear, Err, Get, Ignore, Init, Phecda, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, init, plainToClass, regisHandler, regisInitEvent, register, setExposeKey, setIgnoreKey, setModalState, snapShot, to, validate };
103
+ declare function isArray(info?: string): (obj: any, key: PropertyKey) => void;
104
+ declare function isBoolean(info?: string): (obj: any, key: PropertyKey) => void;
105
+ declare function isNumber(info?: string): (obj: any, key: PropertyKey) => void;
106
+ declare function isString(info?: string): (obj: any, key: PropertyKey) => void;
107
+ declare function isObject(info?: string): (obj: any, key: PropertyKey) => void;
108
+ declare function isMobile(info?: string): (obj: any, key: PropertyKey) => void;
109
+ declare function isLandline(info?: string): (obj: any, key: PropertyKey) => void;
110
+ declare function isMailBox(info?: string): (obj: any, key: PropertyKey) => void;
111
+ declare function isIdCard(info?: string): (obj: any, key: PropertyKey) => void;
112
+ declare function isCnName(info?: string): (obj: any, key: PropertyKey) => void;
113
+ declare function isEnName(info?: string): (obj: any, key: PropertyKey) => void;
114
+ declare function isDate(info?: string): (obj: any, key: PropertyKey) => void;
115
+ declare function isWechat(info?: string): (obj: any, key: PropertyKey) => void;
116
+ declare function isHexColor(info?: string): (obj: any, key: PropertyKey) => void;
117
+ declare function isPostalCode(info?: string): (obj: any, key: PropertyKey) => void;
118
+
119
+ declare function toNumber(): (obj: any, key: PropertyKey) => void;
120
+ declare function toString(): (obj: any, key: PropertyKey) => void;
121
+
122
+ declare const activeInstance: Record<string, any>;
123
+ declare function injectProperty(key: string, value: any): Record<string, any>;
124
+ declare function getProperty(key: string): any;
125
+
126
+ declare function Watcher(eventName: string): (obj: any, key: string) => void;
127
+
128
+ export { ClassValue, Clear, Err, Get, Ignore, Init, Phecda, PhecdaHandler, PhecdaNameSpace, Pipe, Rule, Storage, Tag, UsePipeOptions, Watcher, activeInstance, addDecoToClass, classToValue, getExposeKey, getHandler, getIgnoreKey, getInitEvent, getModelState, getProperty, getTag, init, injectProperty, 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
@@ -29,6 +29,8 @@ __export(src_exports, {
29
29
  Rule: () => Rule,
30
30
  Storage: () => Storage,
31
31
  Tag: () => Tag,
32
+ Watcher: () => Watcher,
33
+ activeInstance: () => activeInstance,
32
34
  addDecoToClass: () => addDecoToClass,
33
35
  classToValue: () => classToValue,
34
36
  getExposeKey: () => getExposeKey,
@@ -36,7 +38,25 @@ __export(src_exports, {
36
38
  getIgnoreKey: () => getIgnoreKey,
37
39
  getInitEvent: () => getInitEvent,
38
40
  getModelState: () => getModelState,
41
+ getProperty: () => getProperty,
42
+ getTag: () => getTag,
39
43
  init: () => init,
44
+ injectProperty: () => injectProperty,
45
+ isArray: () => isArray,
46
+ isBoolean: () => isBoolean,
47
+ isCnName: () => isCnName,
48
+ isDate: () => isDate,
49
+ isEnName: () => isEnName,
50
+ isHexColor: () => isHexColor,
51
+ isIdCard: () => isIdCard,
52
+ isLandline: () => isLandline,
53
+ isMailBox: () => isMailBox,
54
+ isMobile: () => isMobile,
55
+ isNumber: () => isNumber,
56
+ isObject: () => isObject,
57
+ isPostalCode: () => isPostalCode,
58
+ isString: () => isString,
59
+ isWechat: () => isWechat,
40
60
  plainToClass: () => plainToClass,
41
61
  regisHandler: () => regisHandler,
42
62
  regisInitEvent: () => regisInitEvent,
@@ -46,6 +66,8 @@ __export(src_exports, {
46
66
  setModalState: () => setModalState,
47
67
  snapShot: () => snapShot,
48
68
  to: () => to,
69
+ toNumber: () => toNumber,
70
+ toString: () => toString,
49
71
  validate: () => validate
50
72
  });
51
73
  module.exports = __toCommonJS(src_exports);
@@ -209,6 +231,9 @@ async function validate(p, v) {
209
231
  return p.test(v);
210
232
  return false;
211
233
  }
234
+ function getTag(Model) {
235
+ return Model.prototype?._namespace?.__TAG__;
236
+ }
212
237
 
213
238
  // src/helper.ts
214
239
  async function plainToClass(Model, input, options = {}) {
@@ -275,6 +300,115 @@ function snapShot(data) {
275
300
  function addDecoToClass(c, key, handler, type = "normal") {
276
301
  handler(type === "normal" ? c.prototype : c, key);
277
302
  }
303
+
304
+ // src/preset/rule.ts
305
+ function isArray(info) {
306
+ return Rule((param) => Array.isArray(param), info || "it should be an array");
307
+ }
308
+ function isBoolean(info) {
309
+ return Rule((param) => [true, false].includes(param), info || "it should be true or false");
310
+ }
311
+ function isNumber(info) {
312
+ return Rule((param) => typeof param === "number", info || "it should be true or false");
313
+ }
314
+ function isString(info) {
315
+ return Rule((param) => typeof param === "string", info || "it should be a string");
316
+ }
317
+ function isObject(info) {
318
+ return Rule((param) => {
319
+ return Object.prototype.toString.call(param) === "[object Object]";
320
+ }, info || "it should be an object");
321
+ }
322
+ function isMobile(info) {
323
+ return Rule(
324
+ /^((\+|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}$/,
325
+ info || "it should be a mobile phone number"
326
+ );
327
+ }
328
+ function isLandline(info) {
329
+ return Rule(
330
+ /\d{3}-\d{8}|\d{4}-\d{7}/,
331
+ info || "it should be a mobile phone number"
332
+ );
333
+ }
334
+ function isMailBox(info) {
335
+ return Rule(
336
+ /^[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])?)*$/,
337
+ info || "it should be a mailbox number"
338
+ );
339
+ }
340
+ function isIdCard(info) {
341
+ return Rule(
342
+ /(^\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)$)/,
343
+ info || "it should be an identity card number"
344
+ );
345
+ }
346
+ function isCnName(info) {
347
+ return Rule(
348
+ /^([\u4E00-\u9FA5·]{2,16})$/,
349
+ info || "\u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57"
350
+ );
351
+ }
352
+ function isEnName(info) {
353
+ return Rule(
354
+ /(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/,
355
+ info || "it should be a valid en-name"
356
+ );
357
+ }
358
+ function isDate(info) {
359
+ return Rule(
360
+ /^(?:(?!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)$/,
361
+ info || "it should be a valid date"
362
+ );
363
+ }
364
+ function isWechat(info) {
365
+ return Rule(
366
+ /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
367
+ info || "it should be a valid date"
368
+ );
369
+ }
370
+ function isHexColor(info) {
371
+ return Rule(
372
+ /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
373
+ info || "it should be a valid hex-color"
374
+ );
375
+ }
376
+ function isPostalCode(info) {
377
+ return Rule(
378
+ /^(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}$/,
379
+ info || "it should be a valid postal code"
380
+ );
381
+ }
382
+
383
+ // src/preset/pipe.ts
384
+ function toNumber() {
385
+ return Pipe(to((param) => Number(param)));
386
+ }
387
+ function toString() {
388
+ return Pipe(to((param) => String(param)));
389
+ }
390
+
391
+ // src/namespace.ts
392
+ var activeInstance = {};
393
+ function injectProperty(key, value) {
394
+ activeInstance[key] = value;
395
+ return activeInstance;
396
+ }
397
+ function getProperty(key) {
398
+ return activeInstance[key];
399
+ }
400
+
401
+ // src/custom/decorator.ts
402
+ function Watcher(eventName) {
403
+ return (obj, key) => {
404
+ setModalState(obj, key);
405
+ regisHandler(obj, key, {
406
+ init(instance) {
407
+ getProperty("watcher")?.({ eventName, instance, key });
408
+ }
409
+ });
410
+ };
411
+ }
278
412
  // Annotate the CommonJS export names for ESM import in node:
279
413
  0 && (module.exports = {
280
414
  Clear,
@@ -286,6 +420,8 @@ function addDecoToClass(c, key, handler, type = "normal") {
286
420
  Rule,
287
421
  Storage,
288
422
  Tag,
423
+ Watcher,
424
+ activeInstance,
289
425
  addDecoToClass,
290
426
  classToValue,
291
427
  getExposeKey,
@@ -293,7 +429,25 @@ function addDecoToClass(c, key, handler, type = "normal") {
293
429
  getIgnoreKey,
294
430
  getInitEvent,
295
431
  getModelState,
432
+ getProperty,
433
+ getTag,
296
434
  init,
435
+ injectProperty,
436
+ isArray,
437
+ isBoolean,
438
+ isCnName,
439
+ isDate,
440
+ isEnName,
441
+ isHexColor,
442
+ isIdCard,
443
+ isLandline,
444
+ isMailBox,
445
+ isMobile,
446
+ isNumber,
447
+ isObject,
448
+ isPostalCode,
449
+ isString,
450
+ isWechat,
297
451
  plainToClass,
298
452
  regisHandler,
299
453
  regisInitEvent,
@@ -303,5 +457,7 @@ function addDecoToClass(c, key, handler, type = "normal") {
303
457
  setModalState,
304
458
  snapShot,
305
459
  to,
460
+ toNumber,
461
+ toString,
306
462
  validate
307
463
  });
package/dist/index.mjs CHANGED
@@ -157,6 +157,9 @@ async function validate(p, v) {
157
157
  return p.test(v);
158
158
  return false;
159
159
  }
160
+ function getTag(Model) {
161
+ return Model.prototype?._namespace?.__TAG__;
162
+ }
160
163
 
161
164
  // src/helper.ts
162
165
  async function plainToClass(Model, input, options = {}) {
@@ -223,6 +226,115 @@ function snapShot(data) {
223
226
  function addDecoToClass(c, key, handler, type = "normal") {
224
227
  handler(type === "normal" ? c.prototype : c, key);
225
228
  }
229
+
230
+ // src/preset/rule.ts
231
+ function isArray(info) {
232
+ return Rule((param) => Array.isArray(param), info || "it should be an array");
233
+ }
234
+ function isBoolean(info) {
235
+ return Rule((param) => [true, false].includes(param), info || "it should be true or false");
236
+ }
237
+ function isNumber(info) {
238
+ return Rule((param) => typeof param === "number", info || "it should be true or false");
239
+ }
240
+ function isString(info) {
241
+ return Rule((param) => typeof param === "string", info || "it should be a string");
242
+ }
243
+ function isObject(info) {
244
+ return Rule((param) => {
245
+ return Object.prototype.toString.call(param) === "[object Object]";
246
+ }, info || "it should be an object");
247
+ }
248
+ function isMobile(info) {
249
+ return Rule(
250
+ /^((\+|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}$/,
251
+ info || "it should be a mobile phone number"
252
+ );
253
+ }
254
+ function isLandline(info) {
255
+ return Rule(
256
+ /\d{3}-\d{8}|\d{4}-\d{7}/,
257
+ info || "it should be a mobile phone number"
258
+ );
259
+ }
260
+ function isMailBox(info) {
261
+ return Rule(
262
+ /^[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])?)*$/,
263
+ info || "it should be a mailbox number"
264
+ );
265
+ }
266
+ function isIdCard(info) {
267
+ return Rule(
268
+ /(^\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)$)/,
269
+ info || "it should be an identity card number"
270
+ );
271
+ }
272
+ function isCnName(info) {
273
+ return Rule(
274
+ /^([\u4E00-\u9FA5·]{2,16})$/,
275
+ info || "\u9700\u8981\u662F\u4E00\u4E2A\u5408\u7406\u7684\u4E2D\u6587\u540D\u5B57"
276
+ );
277
+ }
278
+ function isEnName(info) {
279
+ return Rule(
280
+ /(^[a-zA-Z]{1}[a-zA-Z\s]{0,20}[a-zA-Z]{1}$)/,
281
+ info || "it should be a valid en-name"
282
+ );
283
+ }
284
+ function isDate(info) {
285
+ return Rule(
286
+ /^(?:(?!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)$/,
287
+ info || "it should be a valid date"
288
+ );
289
+ }
290
+ function isWechat(info) {
291
+ return Rule(
292
+ /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
293
+ info || "it should be a valid date"
294
+ );
295
+ }
296
+ function isHexColor(info) {
297
+ return Rule(
298
+ /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
299
+ info || "it should be a valid hex-color"
300
+ );
301
+ }
302
+ function isPostalCode(info) {
303
+ return Rule(
304
+ /^(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}$/,
305
+ info || "it should be a valid postal code"
306
+ );
307
+ }
308
+
309
+ // src/preset/pipe.ts
310
+ function toNumber() {
311
+ return Pipe(to((param) => Number(param)));
312
+ }
313
+ function toString() {
314
+ return Pipe(to((param) => String(param)));
315
+ }
316
+
317
+ // src/namespace.ts
318
+ var activeInstance = {};
319
+ function injectProperty(key, value) {
320
+ activeInstance[key] = value;
321
+ return activeInstance;
322
+ }
323
+ function getProperty(key) {
324
+ return activeInstance[key];
325
+ }
326
+
327
+ // src/custom/decorator.ts
328
+ function Watcher(eventName) {
329
+ return (obj, key) => {
330
+ setModalState(obj, key);
331
+ regisHandler(obj, key, {
332
+ init(instance) {
333
+ getProperty("watcher")?.({ eventName, instance, key });
334
+ }
335
+ });
336
+ };
337
+ }
226
338
  export {
227
339
  Clear,
228
340
  Err,
@@ -233,6 +345,8 @@ export {
233
345
  Rule,
234
346
  Storage,
235
347
  Tag,
348
+ Watcher,
349
+ activeInstance,
236
350
  addDecoToClass,
237
351
  classToValue,
238
352
  getExposeKey,
@@ -240,7 +354,25 @@ export {
240
354
  getIgnoreKey,
241
355
  getInitEvent,
242
356
  getModelState,
357
+ getProperty,
358
+ getTag,
243
359
  init,
360
+ injectProperty,
361
+ isArray,
362
+ isBoolean,
363
+ isCnName,
364
+ isDate,
365
+ isEnName,
366
+ isHexColor,
367
+ isIdCard,
368
+ isLandline,
369
+ isMailBox,
370
+ isMobile,
371
+ isNumber,
372
+ isObject,
373
+ isPostalCode,
374
+ isString,
375
+ isWechat,
244
376
  plainToClass,
245
377
  regisHandler,
246
378
  regisInitEvent,
@@ -250,5 +382,7 @@ export {
250
382
  setModalState,
251
383
  snapShot,
252
384
  to,
385
+ toNumber,
386
+ toString,
253
387
  validate
254
388
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",