@oscarpalmer/jhunal 0.2.0 → 0.3.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.
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ function getTypes(value) {
6
+ return (Array.isArray(value) ? value : [value]).filter(
7
+ (item) => types.has(item)
8
+ );
9
+ }
10
+ const types = /* @__PURE__ */ new Set([
11
+ "array",
12
+ "bigint",
13
+ "boolean",
14
+ "date",
15
+ "function",
16
+ "null",
17
+ "number",
18
+ "object",
19
+ "string",
20
+ "symbol",
21
+ "undefined"
22
+ ]);
23
+
24
+ exports.getTypes = getTypes;
@@ -0,0 +1,20 @@
1
+ function getTypes(value) {
2
+ return (Array.isArray(value) ? value : [value]).filter(
3
+ (item) => types.has(item)
4
+ );
5
+ }
6
+ const types = /* @__PURE__ */ new Set([
7
+ "array",
8
+ "bigint",
9
+ "boolean",
10
+ "date",
11
+ "function",
12
+ "null",
13
+ "number",
14
+ "object",
15
+ "string",
16
+ "symbol",
17
+ "undefined"
18
+ ]);
19
+
20
+ export { getTypes };
package/dist/index.cjs CHANGED
@@ -2,110 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const types = /* @__PURE__ */ new Set([
6
- "array",
7
- "bigint",
8
- "boolean",
9
- "date",
10
- "function",
11
- "null",
12
- "number",
13
- "object",
14
- "string",
15
- "symbol",
16
- "undefined"
17
- ]);
18
- const validators = {
19
- array: Array.isArray,
20
- bigint: (value) => typeof value === "bigint",
21
- boolean: (value) => typeof value === "boolean",
22
- date: (value) => value instanceof Date,
23
- function: (value) => typeof value === "function",
24
- null: (value) => value === null,
25
- number: (value) => typeof value === "number",
26
- object: (value) => typeof value === "object" && value !== null,
27
- string: (value) => typeof value === "string",
28
- symbol: (value) => typeof value === "symbol",
29
- undefined: (value) => value === void 0
30
- };
31
- function getTypes(value) {
32
- return (Array.isArray(value) ? value : [value]).filter(
33
- (item) => types.has(item)
34
- );
35
- }
36
- function getValidatedSchema(schema) {
37
- const validated = {
38
- keys: [],
39
- length: 0,
40
- properties: {}
41
- };
42
- if (typeof schema !== "object" || schema === null) {
43
- return validated;
44
- }
45
- const keys = Object.keys(schema);
46
- const { length } = keys;
47
- for (let index = 0; index < length; index += 1) {
48
- const key = keys[index];
49
- const value = schema[key];
50
- let required = true;
51
- let valueTypes;
52
- if (Array.isArray(value)) {
53
- valueTypes = getTypes(value);
54
- } else if (typeof value === "object" && value !== null) {
55
- if (typeof value.required === "boolean") {
56
- required = value.required;
57
- }
58
- valueTypes = getTypes(value.type);
59
- } else {
60
- valueTypes = getTypes(value);
61
- }
62
- if (valueTypes.length > 0) {
63
- if (!required && !valueTypes.includes("undefined")) {
64
- valueTypes.push("undefined");
65
- }
66
- validated.keys.push(key);
67
- validated.properties[key] = {
68
- required,
69
- types: valueTypes
70
- };
71
- validated.length += 1;
72
- }
73
- }
74
- return validated;
75
- }
76
- function schematic(schema) {
77
- const validated = getValidatedSchema(schema);
78
- const canValidate = validated.length > 0;
79
- return Object.freeze({
80
- is: (value) => canValidate && validate(validated, value)
81
- });
82
- }
83
- function validate(validated, obj) {
84
- if (typeof obj !== "object" || obj === null) {
85
- return false;
86
- }
87
- outer: for (let index = 0; index < validated.length; index += 1) {
88
- const key = validated.keys[index];
89
- const property = validated.properties[key];
90
- const value = obj[key];
91
- if (value === void 0 && property.required && !property.types.includes("undefined")) {
92
- return false;
93
- }
94
- const typesLength = property.types.length;
95
- if (typesLength === 1) {
96
- if (!validators[property.types[0]](value)) {
97
- return false;
98
- }
99
- continue;
100
- }
101
- for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
102
- if (validators[property.types[typeIndex]](value)) {
103
- continue outer;
104
- }
105
- }
106
- return false;
107
- }
108
- return true;
109
- }
5
+ const schematic = require('./schematic.cjs');
110
6
 
111
- exports.schematic = schematic;
7
+
8
+
9
+ exports.schematic = schematic.schematic;
package/dist/index.js CHANGED
@@ -1,107 +1 @@
1
- const types = /* @__PURE__ */ new Set([
2
- "array",
3
- "bigint",
4
- "boolean",
5
- "date",
6
- "function",
7
- "null",
8
- "number",
9
- "object",
10
- "string",
11
- "symbol",
12
- "undefined"
13
- ]);
14
- const validators = {
15
- array: Array.isArray,
16
- bigint: (value) => typeof value === "bigint",
17
- boolean: (value) => typeof value === "boolean",
18
- date: (value) => value instanceof Date,
19
- function: (value) => typeof value === "function",
20
- null: (value) => value === null,
21
- number: (value) => typeof value === "number",
22
- object: (value) => typeof value === "object" && value !== null,
23
- string: (value) => typeof value === "string",
24
- symbol: (value) => typeof value === "symbol",
25
- undefined: (value) => value === void 0
26
- };
27
- function getTypes(value) {
28
- return (Array.isArray(value) ? value : [value]).filter(
29
- (item) => types.has(item)
30
- );
31
- }
32
- function getValidatedSchema(schema) {
33
- const validated = {
34
- keys: [],
35
- length: 0,
36
- properties: {}
37
- };
38
- if (typeof schema !== "object" || schema === null) {
39
- return validated;
40
- }
41
- const keys = Object.keys(schema);
42
- const { length } = keys;
43
- for (let index = 0; index < length; index += 1) {
44
- const key = keys[index];
45
- const value = schema[key];
46
- let required = true;
47
- let valueTypes;
48
- if (Array.isArray(value)) {
49
- valueTypes = getTypes(value);
50
- } else if (typeof value === "object" && value !== null) {
51
- if (typeof value.required === "boolean") {
52
- required = value.required;
53
- }
54
- valueTypes = getTypes(value.type);
55
- } else {
56
- valueTypes = getTypes(value);
57
- }
58
- if (valueTypes.length > 0) {
59
- if (!required && !valueTypes.includes("undefined")) {
60
- valueTypes.push("undefined");
61
- }
62
- validated.keys.push(key);
63
- validated.properties[key] = {
64
- required,
65
- types: valueTypes
66
- };
67
- validated.length += 1;
68
- }
69
- }
70
- return validated;
71
- }
72
- function schematic(schema) {
73
- const validated = getValidatedSchema(schema);
74
- const canValidate = validated.length > 0;
75
- return Object.freeze({
76
- is: (value) => canValidate && validate(validated, value)
77
- });
78
- }
79
- function validate(validated, obj) {
80
- if (typeof obj !== "object" || obj === null) {
81
- return false;
82
- }
83
- outer: for (let index = 0; index < validated.length; index += 1) {
84
- const key = validated.keys[index];
85
- const property = validated.properties[key];
86
- const value = obj[key];
87
- if (value === void 0 && property.required && !property.types.includes("undefined")) {
88
- return false;
89
- }
90
- const typesLength = property.types.length;
91
- if (typesLength === 1) {
92
- if (!validators[property.types[0]](value)) {
93
- return false;
94
- }
95
- continue;
96
- }
97
- for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
98
- if (validators[property.types[typeIndex]](value)) {
99
- continue outer;
100
- }
101
- }
102
- return false;
103
- }
104
- return true;
105
- }
106
-
107
- export { schematic };
1
+ export { schematic } from './schematic.js';
package/dist/model.cjs ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
package/dist/model.js ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const validation = require('./validation.cjs');
6
+
7
+ function schematic(schema) {
8
+ const validated = validation.getValidatedSchema(schema);
9
+ const canValidate = validated.length > 0;
10
+ return Object.freeze({
11
+ is: (value) => canValidate && validation.validate(validated, value)
12
+ });
13
+ }
14
+
15
+ exports.schematic = schematic;
@@ -0,0 +1,11 @@
1
+ import { getValidatedSchema, validate } from './validation.js';
2
+
3
+ function schematic(schema) {
4
+ const validated = getValidatedSchema(schema);
5
+ const canValidate = validated.length > 0;
6
+ return Object.freeze({
7
+ is: (value) => canValidate && validate(validated, value)
8
+ });
9
+ }
10
+
11
+ export { schematic };
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const helpers = require('./helpers.cjs');
6
+
7
+ function getValidatedSchema(schema) {
8
+ const validated = {
9
+ keys: [],
10
+ length: 0,
11
+ properties: {}
12
+ };
13
+ if (typeof schema !== "object" || schema === null) {
14
+ return validated;
15
+ }
16
+ const keys = Object.keys(schema);
17
+ const { length } = keys;
18
+ for (let index = 0; index < length; index += 1) {
19
+ const key = keys[index];
20
+ const value = schema[key];
21
+ let required = true;
22
+ let valueTypes;
23
+ if (Array.isArray(value)) {
24
+ valueTypes = helpers.getTypes(value);
25
+ } else if (typeof value === "object" && value !== null) {
26
+ if (typeof value.required === "boolean") {
27
+ required = value.required;
28
+ }
29
+ valueTypes = helpers.getTypes(value.type);
30
+ } else {
31
+ valueTypes = helpers.getTypes(value);
32
+ }
33
+ if (valueTypes.length > 0) {
34
+ if (!required && !valueTypes.includes("undefined")) {
35
+ valueTypes.push("undefined");
36
+ }
37
+ validated.keys.push(key);
38
+ validated.properties[key] = {
39
+ required,
40
+ types: valueTypes
41
+ };
42
+ validated.length += 1;
43
+ }
44
+ }
45
+ return validated;
46
+ }
47
+ function validate(validated, obj) {
48
+ if (typeof obj !== "object" || obj === null) {
49
+ return false;
50
+ }
51
+ outer: for (let index = 0; index < validated.length; index += 1) {
52
+ const key = validated.keys[index];
53
+ const property = validated.properties[key];
54
+ const value = obj[key];
55
+ if (value === void 0 && property.required && !property.types.includes("undefined")) {
56
+ return false;
57
+ }
58
+ const typesLength = property.types.length;
59
+ if (typesLength === 1) {
60
+ if (!validators[property.types[0]](value)) {
61
+ return false;
62
+ }
63
+ continue;
64
+ }
65
+ for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
66
+ if (validators[property.types[typeIndex]](value)) {
67
+ continue outer;
68
+ }
69
+ }
70
+ return false;
71
+ }
72
+ return true;
73
+ }
74
+ const validators = {
75
+ array: Array.isArray,
76
+ bigint: (value) => typeof value === "bigint",
77
+ boolean: (value) => typeof value === "boolean",
78
+ date: (value) => value instanceof Date,
79
+ function: (value) => typeof value === "function",
80
+ null: (value) => value === null,
81
+ number: (value) => typeof value === "number",
82
+ object: (value) => typeof value === "object" && value !== null,
83
+ string: (value) => typeof value === "string",
84
+ symbol: (value) => typeof value === "symbol",
85
+ undefined: (value) => value === void 0
86
+ };
87
+
88
+ exports.getValidatedSchema = getValidatedSchema;
89
+ exports.validate = validate;
@@ -0,0 +1,84 @@
1
+ import { getTypes } from './helpers.js';
2
+
3
+ function getValidatedSchema(schema) {
4
+ const validated = {
5
+ keys: [],
6
+ length: 0,
7
+ properties: {}
8
+ };
9
+ if (typeof schema !== "object" || schema === null) {
10
+ return validated;
11
+ }
12
+ const keys = Object.keys(schema);
13
+ const { length } = keys;
14
+ for (let index = 0; index < length; index += 1) {
15
+ const key = keys[index];
16
+ const value = schema[key];
17
+ let required = true;
18
+ let valueTypes;
19
+ if (Array.isArray(value)) {
20
+ valueTypes = getTypes(value);
21
+ } else if (typeof value === "object" && value !== null) {
22
+ if (typeof value.required === "boolean") {
23
+ required = value.required;
24
+ }
25
+ valueTypes = getTypes(value.type);
26
+ } else {
27
+ valueTypes = getTypes(value);
28
+ }
29
+ if (valueTypes.length > 0) {
30
+ if (!required && !valueTypes.includes("undefined")) {
31
+ valueTypes.push("undefined");
32
+ }
33
+ validated.keys.push(key);
34
+ validated.properties[key] = {
35
+ required,
36
+ types: valueTypes
37
+ };
38
+ validated.length += 1;
39
+ }
40
+ }
41
+ return validated;
42
+ }
43
+ function validate(validated, obj) {
44
+ if (typeof obj !== "object" || obj === null) {
45
+ return false;
46
+ }
47
+ outer: for (let index = 0; index < validated.length; index += 1) {
48
+ const key = validated.keys[index];
49
+ const property = validated.properties[key];
50
+ const value = obj[key];
51
+ if (value === void 0 && property.required && !property.types.includes("undefined")) {
52
+ return false;
53
+ }
54
+ const typesLength = property.types.length;
55
+ if (typesLength === 1) {
56
+ if (!validators[property.types[0]](value)) {
57
+ return false;
58
+ }
59
+ continue;
60
+ }
61
+ for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
62
+ if (validators[property.types[typeIndex]](value)) {
63
+ continue outer;
64
+ }
65
+ }
66
+ return false;
67
+ }
68
+ return true;
69
+ }
70
+ const validators = {
71
+ array: Array.isArray,
72
+ bigint: (value) => typeof value === "bigint",
73
+ boolean: (value) => typeof value === "boolean",
74
+ date: (value) => value instanceof Date,
75
+ function: (value) => typeof value === "function",
76
+ null: (value) => value === null,
77
+ number: (value) => typeof value === "number",
78
+ object: (value) => typeof value === "object" && value !== null,
79
+ string: (value) => typeof value === "string",
80
+ symbol: (value) => typeof value === "symbol",
81
+ undefined: (value) => value === void 0
82
+ };
83
+
84
+ export { getValidatedSchema, validate };
package/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  },
51
51
  "type": "module",
52
52
  "types": "./types/index.d.cts",
53
- "version": "0.2.0"
53
+ "version": "0.3.0"
54
54
  }
package/src/helpers.ts ADDED
@@ -0,0 +1,21 @@
1
+ import type {Values} from './model';
2
+
3
+ export function getTypes(value: unknown): (keyof Values)[] {
4
+ return (Array.isArray(value) ? value : [value]).filter(item =>
5
+ types.has(item),
6
+ );
7
+ }
8
+
9
+ const types = new Set<keyof Values>([
10
+ 'array',
11
+ 'bigint',
12
+ 'boolean',
13
+ 'date',
14
+ 'function',
15
+ 'null',
16
+ 'number',
17
+ 'object',
18
+ 'string',
19
+ 'symbol',
20
+ 'undefined',
21
+ ]);