@data7expressions/typ3s 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,1339 @@
1
+ # typ3s
2
+
3
+ [![language typescript](https://img.shields.io/badge/language-typescript-blue)](https://www.npmjs.com/package/lambdaorm)
4
+ [![npm version](https://img.shields.io/badge/npm-10.2.5-green)](https://www.npmjs.com/package/@data7expressions/typ3s)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
7
+ [![Github CI](https://img.shields.io/badge/Github-CI-red.svg)](https://github.com/lambda-orm/lambdaorm/actions?query=workflow%3A%22publish%22)
8
+
9
+ The objective of this library is to obtain the type of an object, array, function, etc. \
10
+ Also be able to obtain information about the cardinality of the data.
11
+
12
+ ## Primitive
13
+
14
+ - string
15
+ - integer
16
+ - decimal
17
+ - number
18
+ - boolean
19
+ - date
20
+ - dateTime
21
+ - time
22
+ - any
23
+ - void
24
+
25
+ ## Array / Tuples
26
+
27
+ []
28
+
29
+ ## Object
30
+
31
+ {}
32
+
33
+ ## Function
34
+
35
+ ()
36
+
37
+ ## Modifiers
38
+
39
+ |Modifier |description |
40
+ |:------------------|:-----------------------|
41
+ | ! | Nullable |
42
+ | ? | Undefinable |
43
+ | # | Unique |
44
+ | ~ | Async |
45
+
46
+ ## Examples
47
+
48
+ |type |description |
49
+ |:--------------------------------|:----------------------------------------------------------|
50
+ | [string] | array of string |
51
+ | [string, integer] | tuple with string and integer properties |
52
+ | {name:string, nro:integer} | object with string and integer properties |
53
+ | [{name:string, today:date}] | array of object |
54
+ | (>any) | function without params and return value |
55
+ | (list:[integer]>integer) | function with array integer as params and return integer |
56
+ | (today:date,nro:?integer>date) | function with undefinable integer param |
57
+ | {name:string,nro:?integer} | object of properties string and undefinable integer |
58
+ | {name:string,nro:!integer} | object of properties string and nullable integer |
59
+ | ([integer]>~integer) | function with return async of integer |
60
+
61
+ ## Methods
62
+
63
+ | method |description |
64
+ |:--------------|:----------------------------------------------|
65
+ | type | get the type |
66
+ | stringify | converts the type to a string |
67
+ | parse | converts the string to type |
68
+ | serialize | serialize type |
69
+ | deserialized | deserialize type |
70
+ | validate | validate data with type |
71
+
72
+ ## Type method
73
+
74
+ The type method allows us to obtain the type of an object, array, function, etc. \
75
+ Identifying primitive types, arrays, objects, functions, etc. \
76
+ In the case of Array, it will evaluate the type of the array elements. \
77
+ And in the case of Object it will evaluate the type of the object's properties. \
78
+
79
+ The type method has as its second optional parameter "options" which can receive the following properties:
80
+
81
+ | property |description |
82
+ |:----------|:--------------------------------|
83
+ | info | get the info of the type |
84
+ | describes | get the description of the type |
85
+ | enums | get the enums of the type |
86
+
87
+ **In case of setting info:**
88
+
89
+ ```ts
90
+ const type = Type.type(data, { info: true })
91
+ ```
92
+
93
+ The following information will be added to the type:
94
+
95
+ | property |description |
96
+ |:--------------|:----------------------------------|
97
+ | indefinite | number of undefined values |
98
+ | nullables | number of null values |
99
+ | repeated | number of repeated values |
100
+ | distinctCount | number of distinct values |
101
+ | count | number of total values |
102
+ | repeatRate | percentage of repeated values |
103
+ | nullable | if type is null |
104
+ | undefinable | if type is undefined |
105
+ | unique | if the type is unique |
106
+
107
+ **In the case of setting, describe:**
108
+
109
+ ```ts
110
+ const type = Type.type(data, { describe: true })
111
+ ```
112
+
113
+ The following information will be added to the type:
114
+
115
+ | property |description |
116
+ |:----------|:------------------------|
117
+ | percent10 | repeating value 10% |
118
+ | percent25 | repeating value 25% |
119
+ | percent50 | value that repeats 50% |
120
+ | percent75 | value that repeats 75% |
121
+ | percent90 | value that repeats 90% |
122
+ | max | maximum value |
123
+ | min | minimum value |
124
+ | maxLen | maximum length |
125
+ | minLen | minimum length |
126
+ | sum | sum of values |
127
+ | mean | average |
128
+ | std | standard deviation |
129
+
130
+ **In the case of setting enums:**
131
+
132
+ ```ts
133
+ const type = Type.type(data, { enums: true })
134
+ ```
135
+
136
+ **The following information will be added to the type:**
137
+
138
+ | property |description |
139
+ |:----------|:----------------|
140
+ | enums | enum values |
141
+
142
+ ## stringify method
143
+
144
+ The stringify method allows us to convert the type to a string.
145
+
146
+ ```ts
147
+ const type = Type.type(data)
148
+ const stringified = Type.stringify(type)
149
+ ```
150
+
151
+ ## Parse method
152
+
153
+ The parse method allows us to convert a string to a type.
154
+
155
+ ```ts
156
+ const type = Type.type(data)
157
+ const stringified = Type.stringify(type)
158
+ const type2 = Type.parse(stringified)
159
+ ```
160
+
161
+ ## Serialize method
162
+
163
+ The serialize method allows us to serialize a type.
164
+
165
+ ```ts
166
+ const type = Type.type(data)
167
+ const serialized = Type.serialize(type)
168
+ ```
169
+
170
+ ## Deserialize method
171
+
172
+ The deserialize method allows us to deserialize a type.
173
+
174
+ ```ts
175
+ const type = Type.type(data)
176
+ const serialized = Type.serialize(type)
177
+ const deserialized = Type.deserialize(serialized)
178
+ ```
179
+
180
+ ## Validate method
181
+
182
+ The validate method allows us to validate data with a type.
183
+
184
+ ```ts
185
+ let [isValid, message] = Type.validate(data, '[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}]')
186
+ if (!isValid) {
187
+ console.log(message)
188
+ } else {
189
+ console.log('Valid!')
190
+ }
191
+ ```
192
+
193
+ ## Example
194
+
195
+ This example shows how you can obtain the type of an array of objects, convert it to a string, parse it, serialize it, and deserialize it.
196
+
197
+ ```ts
198
+ import { Type } from 'typ3s'
199
+ const data = [
200
+ {
201
+ name: 'Spain',
202
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
203
+ languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
204
+ phoneCode: 34,
205
+ religion: 'Roman Catholicism'
206
+ },
207
+ {
208
+ name: 'United Kingdom',
209
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
210
+ languages: ['English'],
211
+ phoneCode: 44,
212
+ religion: 'Christianity'
213
+ },
214
+ {
215
+ name: 'Italy',
216
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
217
+ languages: ['Italian'],
218
+ phoneCode: 39,
219
+ religion: 'Roman Catholicism'
220
+ },
221
+ {
222
+ name: 'France',
223
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
224
+ languages: ['French'],
225
+ phoneCode: 33,
226
+ religion: 'Roman Catholicism'
227
+ },
228
+ {
229
+ name: 'Argentina',
230
+ region: { name: 'South America', code: 'SA', description: 'South America' },
231
+ languages: ['Spanish'],
232
+ phoneCode: 54,
233
+ religion: 'Roman Catholicism'
234
+ },
235
+ {
236
+ name: 'Germany',
237
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
238
+ languages: ['German'],
239
+ phoneCode: 49,
240
+ religion: 'Christianity'
241
+ },
242
+ {
243
+ name: 'Portugal',
244
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
245
+ languages: ['Portuguese'],
246
+ phoneCode: 351,
247
+ religion: 'Roman Catholicism'
248
+ },
249
+ {
250
+ name: 'United States',
251
+ region: { name: 'North America', code: 'NA', description: 'North America' },
252
+ languages: ['English'],
253
+ phoneCode: 1,
254
+ religion: 'Christianity'
255
+ }
256
+ ]
257
+ const type = Type.type(data)
258
+ const stringified = Type.stringify(type)
259
+ const type2 = Type.parse(stringified)
260
+ const serialized = Type.serialize(type2, 2)
261
+ const deserialized = Type.deserialize(serialized)
262
+ const serialize2 = Type.serialize(deserialized)
263
+ console.log(stringified)
264
+ console.log(serialized)
265
+ console.log(serialize2)
266
+ ```
267
+
268
+ **stringify result:**
269
+
270
+ ```javascript
271
+ [{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}]
272
+ ```
273
+
274
+ **serialized result:**
275
+
276
+ ```json
277
+ {
278
+ "primitive": "list",
279
+ "list": {
280
+ "items": {
281
+ "primitive": "obj",
282
+ "obj": {
283
+ "properties": [
284
+ {
285
+ "name": "name",
286
+ "type": {
287
+ "primitive": "string"
288
+ }
289
+ },
290
+ {
291
+ "name": "region",
292
+ "type": {
293
+ "primitive": "obj",
294
+ "obj": {
295
+ "properties": [
296
+ {
297
+ "name": "name",
298
+ "type": {
299
+ "primitive": "string"
300
+ }
301
+ },
302
+ {
303
+ "name": "code",
304
+ "type": {
305
+ "primitive": "string"
306
+ }
307
+ },
308
+ {
309
+ "name": "description",
310
+ "type": {
311
+ "primitive": "string"
312
+ }
313
+ }
314
+ ]
315
+ }
316
+ }
317
+ },
318
+ {
319
+ "name": "languages",
320
+ "type": {
321
+ "primitive": "list",
322
+ "list": {
323
+ "items": {
324
+ "primitive": "string"
325
+ }
326
+ }
327
+ }
328
+ },
329
+ {
330
+ "name": "phoneCode",
331
+ "type": {
332
+ "primitive": "integer"
333
+ }
334
+ },
335
+ {
336
+ "name": "religion",
337
+ "type": {
338
+ "primitive": "string"
339
+ }
340
+ }
341
+ ]
342
+ }
343
+ }
344
+ }
345
+ }
346
+ ```
347
+
348
+ **serialized result 2:**
349
+
350
+ ```json
351
+ {"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"region","type":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"code","type":{"primitive":"string"}},{"name":"description","type":{"primitive":"string"}}]}}},{"name":"languages","type":{"primitive":"list","list":{"items":{"primitive":"string"}}}},{"name":"phoneCode","type":{"primitive":"integer"}},{"name":"religion","type":{"primitive":"string"}}]}}}}
352
+ ```
353
+
354
+ ## Info example
355
+
356
+ This example shows how you can obtain the info of an array of objects. \
357
+ To use the info method it is necessary to pass the info: true parameter in the type method.
358
+
359
+ ```ts
360
+ import { Type } from 'typ3s'
361
+ const data = [
362
+ {
363
+ name: 'Spain',
364
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
365
+ languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
366
+ phoneCode: 34,
367
+ religion: 'Roman Catholicism'
368
+ },
369
+ {
370
+ name: 'United Kingdom',
371
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
372
+ languages: ['English'],
373
+ phoneCode: 44,
374
+ religion: 'Christianity'
375
+ },
376
+ {
377
+ name: 'Italy',
378
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
379
+ languages: ['Italian'],
380
+ phoneCode: 39,
381
+ religion: 'Roman Catholicism'
382
+ },
383
+ {
384
+ name: 'France',
385
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
386
+ languages: ['French'],
387
+ phoneCode: 33,
388
+ religion: 'Roman Catholicism'
389
+ },
390
+ {
391
+ name: 'Argentina',
392
+ region: { name: 'South America', code: 'SA', description: 'South America' },
393
+ languages: ['Spanish'],
394
+ phoneCode: 54,
395
+ religion: 'Roman Catholicism'
396
+ },
397
+ {
398
+ name: 'Germany',
399
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
400
+ languages: ['German'],
401
+ phoneCode: 49,
402
+ religion: 'Christianity'
403
+ },
404
+ {
405
+ name: 'Portugal',
406
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
407
+ languages: ['Portuguese'],
408
+ phoneCode: 351,
409
+ religion: 'Roman Catholicism'
410
+ },
411
+ {
412
+ name: 'United States',
413
+ region: { name: 'North America', code: 'NA', description: 'North America' },
414
+ languages: ['English'],
415
+ phoneCode: 1,
416
+ religion: 'Christianity'
417
+ }
418
+ ]
419
+ const type = Type.type(data, { info: true })
420
+ const stringified = Type.stringify(type)
421
+ const serialized = Type.serialize(type, 1)
422
+ console.log(stringified)
423
+ console.log(serialized)
424
+ ```
425
+
426
+ **stringified:**
427
+
428
+ ```javascript
429
+ [#{languages:[string],name:#string,phoneCode:#integer,region:{code:string,description:string,name:string},religion:string}]
430
+ ```
431
+
432
+ **serialized:**
433
+
434
+ ```json
435
+ {
436
+ "primitive": "list",
437
+ "list": {
438
+ "items": {
439
+ "primitive": "obj",
440
+ "obj": {
441
+ "properties": [
442
+ {
443
+ "name": "languages",
444
+ "type": {
445
+ "primitive": "list",
446
+ "list": {
447
+ "items": {
448
+ "primitive": "string",
449
+ "indefinite": 0,
450
+ "nullables": 0,
451
+ "repeated": 2,
452
+ "distinctCount": 9,
453
+ "count": 11,
454
+ "repeatRate": 0.03636363636363636,
455
+ "nullable": false,
456
+ "undefinable": false,
457
+ "unique": false
458
+ }
459
+ },
460
+ "indefinite": 0,
461
+ "nullables": 0,
462
+ "repeated": 2,
463
+ "count": 11,
464
+ "repeatRate": 0.03636363636363636,
465
+ "nullable": false,
466
+ "undefinable": false,
467
+ "unique": false,
468
+ "distinctCount": 9
469
+ }
470
+ },
471
+ {
472
+ "name": "name",
473
+ "type": {
474
+ "primitive": "string",
475
+ "onParentDistinctRepeated": 0,
476
+ "distinctCount": 8,
477
+ "onParentDistinctRepeatedRate": 0,
478
+ "onParentDistinctUnique": true,
479
+ "indefinite": 0,
480
+ "nullables": 0,
481
+ "repeated": 0,
482
+ "count": 8,
483
+ "repeatRate": 0,
484
+ "nullable": false,
485
+ "undefinable": false,
486
+ "unique": true
487
+ }
488
+ },
489
+ {
490
+ "name": "phoneCode",
491
+ "type": {
492
+ "primitive": "integer",
493
+ "onParentDistinctRepeated": 0,
494
+ "distinctCount": 8,
495
+ "onParentDistinctRepeatedRate": 0,
496
+ "onParentDistinctUnique": true,
497
+ "indefinite": 0,
498
+ "nullables": 0,
499
+ "repeated": 0,
500
+ "count": 8,
501
+ "repeatRate": 0,
502
+ "nullable": false,
503
+ "undefinable": false,
504
+ "unique": true
505
+ }
506
+ },
507
+ {
508
+ "name": "region",
509
+ "type": {
510
+ "primitive": "obj",
511
+ "obj": {
512
+ "properties": [
513
+ {
514
+ "name": "code",
515
+ "type": {
516
+ "primitive": "string",
517
+ "onParentDistinctRepeated": 0,
518
+ "distinctCount": 3,
519
+ "onParentDistinctRepeatedRate": 0,
520
+ "onParentDistinctUnique": true,
521
+ "indefinite": 0,
522
+ "nullables": 0,
523
+ "repeated": 15,
524
+ "count": 8,
525
+ "repeatRate": 0.5357142857142857,
526
+ "nullable": false,
527
+ "undefinable": false,
528
+ "unique": false
529
+ }
530
+ },
531
+ {
532
+ "name": "description",
533
+ "type": {
534
+ "primitive": "string",
535
+ "onParentDistinctRepeated": 0,
536
+ "distinctCount": 3,
537
+ "onParentDistinctRepeatedRate": 0,
538
+ "onParentDistinctUnique": true,
539
+ "indefinite": 0,
540
+ "nullables": 0,
541
+ "repeated": 15,
542
+ "count": 8,
543
+ "repeatRate": 0.5357142857142857,
544
+ "nullable": false,
545
+ "undefinable": false,
546
+ "unique": false
547
+ }
548
+ },
549
+ {
550
+ "name": "name",
551
+ "type": {
552
+ "primitive": "string",
553
+ "onParentDistinctRepeated": 0,
554
+ "distinctCount": 3,
555
+ "onParentDistinctRepeatedRate": 0,
556
+ "onParentDistinctUnique": true,
557
+ "indefinite": 0,
558
+ "nullables": 0,
559
+ "repeated": 15,
560
+ "count": 8,
561
+ "repeatRate": 0.5357142857142857,
562
+ "nullable": false,
563
+ "undefinable": false,
564
+ "unique": false
565
+ }
566
+ }
567
+ ]
568
+ },
569
+ "indefinite": 0,
570
+ "nullables": 0,
571
+ "repeated": 15,
572
+ "distinctCount": 3,
573
+ "count": 8,
574
+ "repeatRate": 0.5357142857142857,
575
+ "nullable": false,
576
+ "undefinable": false,
577
+ "unique": false
578
+ }
579
+ },
580
+ {
581
+ "name": "religion",
582
+ "type": {
583
+ "primitive": "string",
584
+ "onParentDistinctRepeated": 13,
585
+ "distinctCount": 2,
586
+ "onParentDistinctRepeatedRate": 0.4642857142857143,
587
+ "onParentDistinctUnique": false,
588
+ "indefinite": 0,
589
+ "nullables": 0,
590
+ "repeated": 13,
591
+ "count": 8,
592
+ "repeatRate": 0.4642857142857143,
593
+ "nullable": false,
594
+ "undefinable": false,
595
+ "unique": false
596
+ }
597
+ }
598
+ ]
599
+ },
600
+ "repeated": 0,
601
+ "nullables": 0,
602
+ "indefinite": 0,
603
+ "distinctCount": 8,
604
+ "count": 8,
605
+ "repeatRate": 0,
606
+ "nullable": false,
607
+ "undefinable": false,
608
+ "unique": true
609
+ }
610
+ }
611
+ }
612
+ ```
613
+
614
+ ## Describe example
615
+
616
+ This example shows how you can obtain the description of an array of objects. \
617
+ To use the describe method it is necessary to pass the describe: true parameter in the type method.
618
+
619
+ ```ts
620
+ import { Type } from 'typ3s'
621
+ const data = [
622
+ {
623
+ name: 'Spain',
624
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
625
+ languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
626
+ phoneCode: 34,
627
+ religion: 'Roman Catholicism'
628
+ },
629
+ {
630
+ name: 'United Kingdom',
631
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
632
+ languages: ['English'],
633
+ phoneCode: 44,
634
+ religion: 'Christianity'
635
+ },
636
+ {
637
+ name: 'Italy',
638
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
639
+ languages: ['Italian'],
640
+ phoneCode: 39,
641
+ religion: 'Roman Catholicism'
642
+ },
643
+ {
644
+ name: 'France',
645
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
646
+ languages: ['French'],
647
+ phoneCode: 33,
648
+ religion: 'Roman Catholicism'
649
+ },
650
+ {
651
+ name: 'Argentina',
652
+ region: { name: 'South America', code: 'SA', description: 'South America' },
653
+ languages: ['Spanish'],
654
+ phoneCode: 54,
655
+ religion: 'Roman Catholicism'
656
+ },
657
+ {
658
+ name: 'Germany',
659
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
660
+ languages: ['German'],
661
+ phoneCode: 49,
662
+ religion: 'Christianity'
663
+ },
664
+ {
665
+ name: 'Portugal',
666
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
667
+ languages: ['Portuguese'],
668
+ phoneCode: 351,
669
+ religion: 'Roman Catholicism'
670
+ },
671
+ {
672
+ name: 'United States',
673
+ region: { name: 'North America', code: 'NA', description: 'North America' },
674
+ languages: ['English'],
675
+ phoneCode: 1,
676
+ religion: 'Christianity'
677
+ }
678
+ ]
679
+ const type = Type.type(data, { describe: true })
680
+ const serialized = Type.serialize(type, 1)
681
+ console.log(serialized)
682
+ ```
683
+
684
+ **Result:**
685
+
686
+ ```json
687
+ {
688
+ "primitive": "list",
689
+ "list": {
690
+ "items": {
691
+ "primitive": "obj",
692
+ "obj": {
693
+ "properties": [
694
+ {
695
+ "name": "languages",
696
+ "type": {
697
+ "primitive": "list",
698
+ "list": {
699
+ "items": {
700
+ "primitive": "string",
701
+ "indefinite": 0,
702
+ "nullables": 0,
703
+ "repeated": 2,
704
+ "distinctCount": 9,
705
+ "count": 11,
706
+ "repeatRate": 0.03636363636363636,
707
+ "nullable": false,
708
+ "undefinable": false,
709
+ "unique": false,
710
+ "percent10": "Catalan",
711
+ "percent25": "Galician",
712
+ "percent50": "Italian",
713
+ "percent75": "German",
714
+ "percent90": "Portuguese",
715
+ "max": "Spanish",
716
+ "min": "Basque",
717
+ "maxLen": 10,
718
+ "minLen": 6
719
+ }
720
+ },
721
+ "indefinite": 0,
722
+ "nullables": 0,
723
+ "repeated": 2,
724
+ "count": 11,
725
+ "repeatRate": 0.03636363636363636,
726
+ "nullable": false,
727
+ "undefinable": false,
728
+ "unique": false,
729
+ "distinctCount": 9
730
+ }
731
+ },
732
+ {
733
+ "name": "name",
734
+ "type": {
735
+ "primitive": "string",
736
+ "onParentDistinctRepeated": 0,
737
+ "distinctCount": 8,
738
+ "onParentDistinctRepeatedRate": 0,
739
+ "onParentDistinctUnique": true,
740
+ "indefinite": 0,
741
+ "nullables": 0,
742
+ "repeated": 0,
743
+ "count": 8,
744
+ "repeatRate": 0,
745
+ "nullable": false,
746
+ "undefinable": false,
747
+ "unique": true,
748
+ "percent10": "Spain",
749
+ "percent25": "Italy",
750
+ "percent50": "Argentina",
751
+ "percent75": "Portugal",
752
+ "percent90": "United States",
753
+ "max": "United States",
754
+ "min": "Argentina",
755
+ "maxLen": 14,
756
+ "minLen": 5
757
+ }
758
+ },
759
+ {
760
+ "name": "phoneCode",
761
+ "type": {
762
+ "primitive": "integer",
763
+ "onParentDistinctRepeated": 0,
764
+ "distinctCount": 8,
765
+ "onParentDistinctRepeatedRate": 0,
766
+ "onParentDistinctUnique": true,
767
+ "indefinite": 0,
768
+ "nullables": 0,
769
+ "repeated": 0,
770
+ "count": 8,
771
+ "repeatRate": 0,
772
+ "nullable": false,
773
+ "undefinable": false,
774
+ "unique": true,
775
+ "percent10": 1,
776
+ "percent25": 34,
777
+ "percent50": 44,
778
+ "percent75": 54,
779
+ "percent90": 351,
780
+ "sum": 605,
781
+ "max": 351,
782
+ "min": 1,
783
+ "mean": 75.625,
784
+ "std": 105.15932852105894
785
+ }
786
+ },
787
+ {
788
+ "name": "region",
789
+ "type": {
790
+ "primitive": "obj",
791
+ "obj": {
792
+ "properties": [
793
+ {
794
+ "name": "code",
795
+ "type": {
796
+ "primitive": "string",
797
+ "onParentDistinctRepeated": 0,
798
+ "distinctCount": 3,
799
+ "onParentDistinctRepeatedRate": 0,
800
+ "onParentDistinctUnique": true,
801
+ "indefinite": 0,
802
+ "nullables": 0,
803
+ "repeated": 15,
804
+ "count": 8,
805
+ "repeatRate": 0.5357142857142857,
806
+ "nullable": false,
807
+ "undefinable": false,
808
+ "unique": false,
809
+ "percent10": "EU",
810
+ "percent25": "EU",
811
+ "percent50": "SA",
812
+ "percent75": "EU",
813
+ "percent90": "NA",
814
+ "max": "SA",
815
+ "min": "EU",
816
+ "maxLen": 2,
817
+ "minLen": 2
818
+ }
819
+ },
820
+ {
821
+ "name": "description",
822
+ "type": {
823
+ "primitive": "string",
824
+ "onParentDistinctRepeated": 0,
825
+ "distinctCount": 3,
826
+ "onParentDistinctRepeatedRate": 0,
827
+ "onParentDistinctUnique": true,
828
+ "indefinite": 0,
829
+ "nullables": 0,
830
+ "repeated": 15,
831
+ "count": 8,
832
+ "repeatRate": 0.5357142857142857,
833
+ "nullable": false,
834
+ "undefinable": false,
835
+ "unique": false,
836
+ "percent10": "European Union",
837
+ "percent25": "European Union",
838
+ "percent50": "South America",
839
+ "percent75": "European Union",
840
+ "percent90": "North America",
841
+ "max": "South America",
842
+ "min": "European Union",
843
+ "maxLen": 14,
844
+ "minLen": 13
845
+ }
846
+ },
847
+ {
848
+ "name": "name",
849
+ "type": {
850
+ "primitive": "string",
851
+ "onParentDistinctRepeated": 0,
852
+ "distinctCount": 3,
853
+ "onParentDistinctRepeatedRate": 0,
854
+ "onParentDistinctUnique": true,
855
+ "indefinite": 0,
856
+ "nullables": 0,
857
+ "repeated": 15,
858
+ "count": 8,
859
+ "repeatRate": 0.5357142857142857,
860
+ "nullable": false,
861
+ "undefinable": false,
862
+ "unique": false,
863
+ "percent10": "Europe",
864
+ "percent25": "Europe",
865
+ "percent50": "South America",
866
+ "percent75": "Europe",
867
+ "percent90": "North America",
868
+ "max": "South America",
869
+ "min": "Europe",
870
+ "maxLen": 13,
871
+ "minLen": 6
872
+ }
873
+ }
874
+ ]
875
+ },
876
+ "indefinite": 0,
877
+ "nullables": 0,
878
+ "repeated": 15,
879
+ "distinctCount": 3,
880
+ "count": 8,
881
+ "repeatRate": 0.5357142857142857,
882
+ "nullable": false,
883
+ "undefinable": false,
884
+ "unique": false
885
+ }
886
+ },
887
+ {
888
+ "name": "religion",
889
+ "type": {
890
+ "primitive": "string",
891
+ "onParentDistinctRepeated": 13,
892
+ "distinctCount": 2,
893
+ "onParentDistinctRepeatedRate": 0.4642857142857143,
894
+ "onParentDistinctUnique": false,
895
+ "indefinite": 0,
896
+ "nullables": 0,
897
+ "repeated": 13,
898
+ "count": 8,
899
+ "repeatRate": 0.4642857142857143,
900
+ "nullable": false,
901
+ "undefinable": false,
902
+ "unique": false,
903
+ "percent10": "Roman Catholicism",
904
+ "percent25": "Roman Catholicism",
905
+ "percent50": "Roman Catholicism",
906
+ "percent75": "Roman Catholicism",
907
+ "percent90": "Christianity",
908
+ "max": "Roman Catholicism",
909
+ "min": "Christianity",
910
+ "maxLen": 17,
911
+ "minLen": 12
912
+ }
913
+ }
914
+ ]
915
+ },
916
+ "repeated": 0,
917
+ "nullables": 0,
918
+ "indefinite": 0,
919
+ "distinctCount": 8,
920
+ "count": 8,
921
+ "repeatRate": 0,
922
+ "nullable": false,
923
+ "undefinable": false,
924
+ "unique": true
925
+ }
926
+ }
927
+ }
928
+ ```
929
+
930
+ ## Enums example
931
+
932
+ This example shows how you can obtain the enums of an array of objects. \
933
+ To use the enums method it is necessary to pass the enums: true parameter in the type method.
934
+
935
+ ```ts
936
+ import { Type } from 'typ3s'
937
+ const data = [
938
+ {
939
+ name: 'Spain',
940
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
941
+ languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
942
+ phoneCode: 34,
943
+ religion: 'Roman Catholicism'
944
+ },
945
+ {
946
+ name: 'United Kingdom',
947
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
948
+ languages: ['English'],
949
+ phoneCode: 44,
950
+ religion: 'Christianity'
951
+ },
952
+ {
953
+ name: 'Italy',
954
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
955
+ languages: ['Italian'],
956
+ phoneCode: 39,
957
+ religion: 'Roman Catholicism'
958
+ },
959
+ {
960
+ name: 'France',
961
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
962
+ languages: ['French'],
963
+ phoneCode: 33,
964
+ religion: 'Roman Catholicism'
965
+ },
966
+ {
967
+ name: 'Argentina',
968
+ region: { name: 'South America', code: 'SA', description: 'South America' },
969
+ languages: ['Spanish'],
970
+ phoneCode: 54,
971
+ religion: 'Roman Catholicism'
972
+ },
973
+ {
974
+ name: 'Germany',
975
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
976
+ languages: ['German'],
977
+ phoneCode: 49,
978
+ religion: 'Christianity'
979
+ },
980
+ {
981
+ name: 'Portugal',
982
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
983
+ languages: ['Portuguese'],
984
+ phoneCode: 351,
985
+ religion: 'Roman Catholicism'
986
+ },
987
+ {
988
+ name: 'United States',
989
+ region: { name: 'North America', code: 'NA', description: 'North America' },
990
+ languages: ['English'],
991
+ phoneCode: 1,
992
+ religion: 'Christianity'
993
+ }
994
+ ]
995
+ const type = Type.type(data, { enums: true })
996
+ const serialized = Type.serialize(type, 1)
997
+ console.log(serialized)
998
+ ```
999
+
1000
+ **Enum Result:**
1001
+
1002
+ In the event that it detects a field that is repeated and there are few options, it is considered an enum. \
1003
+ In this case, detect that the religion field is repeated and there are only two options, so it is considered an enum. \
1004
+ Giving information on the values and the number of times it is repeated.
1005
+
1006
+ ```json
1007
+ ...
1008
+ {
1009
+ "name": "religion",
1010
+ "type": {
1011
+ "primitive": "string",
1012
+ "onParentDistinctRepeated": 13,
1013
+ "distinctCount": 2,
1014
+ "onParentDistinctRepeatedRate": 0.4642857142857143,
1015
+ "onParentDistinctUnique": false,
1016
+ "indefinite": 0,
1017
+ "nullables": 0,
1018
+ "repeated": 13,
1019
+ "count": 8,
1020
+ "repeatRate": 0.4642857142857143,
1021
+ "nullable": false,
1022
+ "undefinable": false,
1023
+ "unique": false,
1024
+ "enums": [
1025
+ {
1026
+ "value": "Roman Catholicism",
1027
+ "count": 5
1028
+ },
1029
+ {
1030
+ "value": "Christianity",
1031
+ "count": 3
1032
+ }
1033
+ ]
1034
+ }
1035
+ }
1036
+ ...
1037
+ ```
1038
+
1039
+ **Complete Result:**
1040
+
1041
+ ```json
1042
+ {
1043
+ "primitive": "list",
1044
+ "list": {
1045
+ "items": {
1046
+ "primitive": "obj",
1047
+ "obj": {
1048
+ "properties": [
1049
+ {
1050
+ "name": "languages",
1051
+ "type": {
1052
+ "primitive": "list",
1053
+ "list": {
1054
+ "items": {
1055
+ "primitive": "string",
1056
+ "indefinite": 0,
1057
+ "nullables": 0,
1058
+ "repeated": 2,
1059
+ "distinctCount": 9,
1060
+ "count": 11,
1061
+ "repeatRate": 0.03636363636363636,
1062
+ "nullable": false,
1063
+ "undefinable": false,
1064
+ "unique": false
1065
+ }
1066
+ },
1067
+ "indefinite": 0,
1068
+ "nullables": 0,
1069
+ "repeated": 2,
1070
+ "count": 11,
1071
+ "repeatRate": 0.03636363636363636,
1072
+ "nullable": false,
1073
+ "undefinable": false,
1074
+ "unique": false,
1075
+ "distinctCount": 9
1076
+ }
1077
+ },
1078
+ {
1079
+ "name": "name",
1080
+ "type": {
1081
+ "primitive": "string",
1082
+ "onParentDistinctRepeated": 0,
1083
+ "distinctCount": 8,
1084
+ "onParentDistinctRepeatedRate": 0,
1085
+ "onParentDistinctUnique": true,
1086
+ "indefinite": 0,
1087
+ "nullables": 0,
1088
+ "repeated": 0,
1089
+ "count": 8,
1090
+ "repeatRate": 0,
1091
+ "nullable": false,
1092
+ "undefinable": false,
1093
+ "unique": true
1094
+ }
1095
+ },
1096
+ {
1097
+ "name": "phoneCode",
1098
+ "type": {
1099
+ "primitive": "integer",
1100
+ "onParentDistinctRepeated": 0,
1101
+ "distinctCount": 8,
1102
+ "onParentDistinctRepeatedRate": 0,
1103
+ "onParentDistinctUnique": true,
1104
+ "indefinite": 0,
1105
+ "nullables": 0,
1106
+ "repeated": 0,
1107
+ "count": 8,
1108
+ "repeatRate": 0,
1109
+ "nullable": false,
1110
+ "undefinable": false,
1111
+ "unique": true
1112
+ }
1113
+ },
1114
+ {
1115
+ "name": "region",
1116
+ "type": {
1117
+ "primitive": "obj",
1118
+ "obj": {
1119
+ "properties": [
1120
+ {
1121
+ "name": "code",
1122
+ "type": {
1123
+ "primitive": "string",
1124
+ "onParentDistinctRepeated": 0,
1125
+ "distinctCount": 3,
1126
+ "onParentDistinctRepeatedRate": 0,
1127
+ "onParentDistinctUnique": true,
1128
+ "indefinite": 0,
1129
+ "nullables": 0,
1130
+ "repeated": 15,
1131
+ "count": 8,
1132
+ "repeatRate": 0.5357142857142857,
1133
+ "nullable": false,
1134
+ "undefinable": false,
1135
+ "unique": false
1136
+ }
1137
+ },
1138
+ {
1139
+ "name": "description",
1140
+ "type": {
1141
+ "primitive": "string",
1142
+ "onParentDistinctRepeated": 0,
1143
+ "distinctCount": 3,
1144
+ "onParentDistinctRepeatedRate": 0,
1145
+ "onParentDistinctUnique": true,
1146
+ "indefinite": 0,
1147
+ "nullables": 0,
1148
+ "repeated": 15,
1149
+ "count": 8,
1150
+ "repeatRate": 0.5357142857142857,
1151
+ "nullable": false,
1152
+ "undefinable": false,
1153
+ "unique": false
1154
+ }
1155
+ },
1156
+ {
1157
+ "name": "name",
1158
+ "type": {
1159
+ "primitive": "string",
1160
+ "onParentDistinctRepeated": 0,
1161
+ "distinctCount": 3,
1162
+ "onParentDistinctRepeatedRate": 0,
1163
+ "onParentDistinctUnique": true,
1164
+ "indefinite": 0,
1165
+ "nullables": 0,
1166
+ "repeated": 15,
1167
+ "count": 8,
1168
+ "repeatRate": 0.5357142857142857,
1169
+ "nullable": false,
1170
+ "undefinable": false,
1171
+ "unique": false
1172
+ }
1173
+ }
1174
+ ]
1175
+ },
1176
+ "indefinite": 0,
1177
+ "nullables": 0,
1178
+ "repeated": 15,
1179
+ "distinctCount": 3,
1180
+ "count": 8,
1181
+ "repeatRate": 0.5357142857142857,
1182
+ "nullable": false,
1183
+ "undefinable": false,
1184
+ "unique": false
1185
+ }
1186
+ },
1187
+ {
1188
+ "name": "religion",
1189
+ "type": {
1190
+ "primitive": "string",
1191
+ "onParentDistinctRepeated": 13,
1192
+ "distinctCount": 2,
1193
+ "onParentDistinctRepeatedRate": 0.4642857142857143,
1194
+ "onParentDistinctUnique": false,
1195
+ "indefinite": 0,
1196
+ "nullables": 0,
1197
+ "repeated": 13,
1198
+ "count": 8,
1199
+ "repeatRate": 0.4642857142857143,
1200
+ "nullable": false,
1201
+ "undefinable": false,
1202
+ "unique": false,
1203
+ "enums": [
1204
+ {
1205
+ "value": "Roman Catholicism",
1206
+ "count": 5
1207
+ },
1208
+ {
1209
+ "value": "Christianity",
1210
+ "count": 3
1211
+ }
1212
+ ]
1213
+ }
1214
+ }
1215
+ ]
1216
+ },
1217
+ "repeated": 0,
1218
+ "nullables": 0,
1219
+ "indefinite": 0,
1220
+ "distinctCount": 8,
1221
+ "count": 8,
1222
+ "repeatRate": 0,
1223
+ "nullable": false,
1224
+ "undefinable": false,
1225
+ "unique": true
1226
+ }
1227
+ }
1228
+ }
1229
+ ```
1230
+
1231
+ ## Validate example
1232
+
1233
+ This example shows how you can validate an object with a type.
1234
+
1235
+ ```ts
1236
+ import { Type } from 'typ3s'
1237
+ const data = [
1238
+ {
1239
+ name: 'Spain',
1240
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
1241
+ languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
1242
+ phoneCode: 34,
1243
+ religion: 'Roman Catholicism'
1244
+ },
1245
+ {
1246
+ name: 'United Kingdom',
1247
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
1248
+ languages: ['English'],
1249
+ phoneCode: 44,
1250
+ religion: 'Christianity'
1251
+ }
1252
+ ]
1253
+ const withoutLanguages = [
1254
+ {
1255
+ name: 'Italy',
1256
+ region: { name: 'Europe', code: 'EU', description: 'European Union' },
1257
+ phoneCode: 39,
1258
+ religion: 'Roman Catholicism'
1259
+ }
1260
+ ]
1261
+ const withoutRegionCode = [
1262
+ {
1263
+ name: 'France',
1264
+ region: { name: 'Europe', description: 'European Union' },
1265
+ languages: ['French'],
1266
+ phoneCode: 33,
1267
+ religion: 'Roman Catholicism'
1268
+ }
1269
+ ]
1270
+ const incorrectPhoneCode = [
1271
+ {
1272
+ name: 'Argentina',
1273
+ region: { name: 'South America', code: 'SA', description: 'South America' },
1274
+ languages: ['Spanish'],
1275
+ phoneCode: '54',
1276
+ religion: 'Roman Catholicism'
1277
+ }
1278
+ ]
1279
+ const type = Type.type(data)
1280
+ const stringified = Type.stringify(type)
1281
+ let [isValid, message] = Type.validate(data, type)
1282
+ if (!isValid) {
1283
+ console.log(message)
1284
+ } else {
1285
+ console.log('Valid!')
1286
+ }
1287
+ [isValid, message] = Type.validate(withoutLanguages, type)
1288
+ if (!isValid) {
1289
+ console.log(message)
1290
+ } else {
1291
+ console.log('Valid!')
1292
+ }
1293
+ [isValid, message] = Type.validate(withoutRegionCode, stringified)
1294
+ if (!isValid) {
1295
+ console.log(message)
1296
+ } else {
1297
+ console.log('Valid!')
1298
+ }
1299
+ [isValid, message] = Type.validate(incorrectPhoneCode, stringified)
1300
+ if (!isValid) {
1301
+ console.log(message)
1302
+ } else {
1303
+ console.log('Valid!')
1304
+ }
1305
+ ```
1306
+
1307
+ **Data Result:**
1308
+
1309
+ ```sh
1310
+ Valid!
1311
+ ```
1312
+
1313
+ **WithoutLanguages Result:**
1314
+
1315
+ ```sh
1316
+ [{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property languages [string] value: undefined is not an array
1317
+ ```
1318
+
1319
+ **WithoutRegionCode Result:**
1320
+
1321
+ ```sh
1322
+ [{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property region {name:string,code:string,description:string} property code Value undefined is not a string
1323
+ ```
1324
+
1325
+ **IncorrectPhoneCode Result:**
1326
+
1327
+ ```sh
1328
+ [{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property phoneCode Value 54 is not an integer
1329
+ ```
1330
+
1331
+ ## Source documentation
1332
+
1333
+ - Source documentation is available in [Source Code Documentation](https://github.com/data7expressions/typ3s/tree/main/doc/source)
1334
+
1335
+ ## Related projects
1336
+
1337
+ - [json-light](https://www.npmjs.com/package/@data7expressions/json-light)
1338
+ - [3xpr](https://www.npmjs.com/package/@data7expressions/3xpr)
1339
+ - [jexp](https://www.npmjs.com/package/@data7expressions/jexp)