ascertain 0.13.0 → 0.13.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.
Files changed (3) hide show
  1. package/.swcrc +16 -0
  2. package/build/index.js +187 -201
  3. package/package.json +10 -12
package/.swcrc ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/swcrc",
3
+ "module": {
4
+ "type": "commonjs",
5
+ "strict": true,
6
+ "strictMode": true
7
+ },
8
+ "jsc": {
9
+ "target": "es2022",
10
+ "parser": {
11
+ "syntax": "ecmascript"
12
+ },
13
+ "externalHelpers": false,
14
+ "keepClassNames": true
15
+ }
16
+ }
package/build/index.js CHANGED
@@ -1,235 +1,221 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- exports.or = exports.optional = exports.fromBase64 = exports.default = exports.ascertain = exports.as = exports.and = exports.$values = exports.$keys = void 0;
7
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
9
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
10
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
11
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
12
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
14
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
16
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
17
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
18
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
19
- var AssertError = /*#__PURE__*/function () {
20
- function AssertError(value, expected, path) {
21
- var subject = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'value';
22
- _classCallCheck(this, AssertError);
23
- this.value = value;
24
- this.expected = expected;
25
- this.path = path;
26
- this.subject = subject;
27
- }
28
- _createClass(AssertError, [{
29
- key: "toString",
30
- value: function toString() {
31
- return "Invalid ".concat(this.subject, " ").concat(JSON.stringify(this.value), " specified by path ").concat(this.path, " expected ").concat(this.expected);
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ $keys: function() {
13
+ return $keys;
14
+ },
15
+ $values: function() {
16
+ return $values;
17
+ },
18
+ and: function() {
19
+ return and;
20
+ },
21
+ as: function() {
22
+ return as;
23
+ },
24
+ ascertain: function() {
25
+ return ascertain;
26
+ },
27
+ default: function() {
28
+ return _default;
29
+ },
30
+ fromBase64: function() {
31
+ return fromBase64;
32
+ },
33
+ optional: function() {
34
+ return optional;
35
+ },
36
+ or: function() {
37
+ return or;
38
+ }
39
+ });
40
+ let AssertError = class AssertError {
41
+ constructor(value, expected, path, subject = 'value'){
42
+ this.value = value;
43
+ this.expected = expected;
44
+ this.path = path;
45
+ this.subject = subject;
32
46
  }
33
- }]);
34
- return AssertError;
35
- }();
47
+ toString() {
48
+ return `Invalid ${this.subject} ${JSON.stringify(this.value)} specified by path ${this.path} expected ${this.expected}`;
49
+ }
50
+ };
36
51
  function findFirstError(array, map) {
37
- for (var i = 0; i < array.length; i++) {
38
- var error = map(array[i], i);
39
- if (error) {
40
- return error;
52
+ for(let i = 0; i < array.length; i++){
53
+ const error = map(array[i], i);
54
+ if (error) {
55
+ return error;
56
+ }
41
57
  }
42
- }
43
- return false;
58
+ return false;
44
59
  }
45
60
  function findNotError(array, map) {
46
- var errors = [];
47
- for (var i = 0; i < array.length; i++) {
48
- var error = map(array[i], i);
49
- if (!error) {
50
- return false;
51
- } else {
52
- errors.push(error);
61
+ const errors = [];
62
+ for(let i = 0; i < array.length; i++){
63
+ const error = map(array[i], i);
64
+ if (!error) {
65
+ return false;
66
+ } else {
67
+ errors.push(error);
68
+ }
53
69
  }
54
- }
55
- return errors.reduce(function (result, error) {
56
- return new AssertError(result.value, [result.expected, error.expected].join(' or '), result.path, result.string);
57
- });
70
+ return errors.reduce((result, error)=>{
71
+ return new AssertError(result.value, [
72
+ result.expected,
73
+ error.expected
74
+ ].join(' or '), result.path, result.string);
75
+ });
58
76
  }
59
77
  function Optional(schema) {
60
- this.schema = schema;
78
+ this.schema = schema;
61
79
  }
62
80
  function And(schema) {
63
- this.schema = schema;
81
+ this.schema = schema;
64
82
  }
65
83
  function Or(schema) {
66
- this.schema = schema;
84
+ this.schema = schema;
67
85
  }
68
- var optional = exports.optional = function optional(schema) {
69
- return new Optional(schema);
70
- };
71
- var and = exports.and = function and() {
72
- for (var _len = arguments.length, schema = new Array(_len), _key = 0; _key < _len; _key++) {
73
- schema[_key] = arguments[_key];
74
- }
75
- return new And(schema);
86
+ const optional = (schema)=>{
87
+ return new Optional(schema);
76
88
  };
77
- var or = exports.or = function or() {
78
- for (var _len2 = arguments.length, schema = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
79
- schema[_key2] = arguments[_key2];
80
- }
81
- return new Or(schema);
89
+ const and = (...schema)=>{
90
+ return new And(schema);
82
91
  };
83
- var $keys = exports.$keys = Symbol.for('@@keys');
84
- var $values = exports.$values = Symbol.for('@@values');
85
- var fromBase64 = exports.fromBase64 = typeof Buffer === 'undefined' ? function (value) {
86
- return atob(value);
87
- } : function (value) {
88
- return Buffer.from(value, 'base64').toString('utf-8');
92
+ const or = (...schema)=>{
93
+ return new Or(schema);
89
94
  };
90
- var MULTIPLIERS = {
91
- ms: 1,
92
- s: 1000,
93
- m: 60000,
94
- h: 3600000,
95
- d: 86400000,
96
- w: 604800000
95
+ const $keys = Symbol.for('@@keys');
96
+ const $values = Symbol.for('@@values');
97
+ const fromBase64 = typeof Buffer === 'undefined' ? (value)=>atob(value) : (value)=>Buffer.from(value, 'base64').toString('utf-8');
98
+ const MULTIPLIERS = {
99
+ ms: 1,
100
+ s: 1000,
101
+ m: 60000,
102
+ h: 3600000,
103
+ d: 86400000,
104
+ w: 604800000
97
105
  };
98
- var as = exports.as = {
99
- string: function string(value) {
100
- return typeof value === 'string' ? value : undefined;
101
- },
102
- number: function number(value) {
103
- var result = parseFloat(value);
104
- return Number.isFinite(result) ? result : undefined;
105
- },
106
- date: function date(value) {
107
- var result = Date.parse(value);
108
- return Number.isFinite(result) ? new Date(result) : undefined;
109
- },
110
- time: function time(value) {
111
- var matches = value === null || value === void 0 ? void 0 : value.match(/^(\d+)(ms|s|m|h|d|w)?$/);
112
- if (matches) {
113
- var _matches = _slicedToArray(matches, 3),
114
- _ = _matches[0],
115
- amount = _matches[1],
116
- _matches$ = _matches[2],
117
- unit = _matches$ === void 0 ? 'ms' : _matches$;
118
- return parseInt(amount, 10) * MULTIPLIERS[unit];
119
- }
120
- return undefined;
121
- },
122
- boolean: function boolean(value) {
123
- return /^(0|1|true|false|enabled|disabled)$/i.test(value) ? /^(1|true|enabled)$/i.test(value) : undefined;
124
- },
125
- array: function array(value, delimiter) {
126
- var _value$split;
127
- return (_value$split = value === null || value === void 0 ? void 0 : value.split(delimiter)) !== null && _value$split !== void 0 ? _value$split : undefined;
128
- },
129
- json: function json(value) {
130
- try {
131
- return JSON.parse(value);
132
- } catch (e) {
133
- return undefined;
134
- }
135
- },
136
- base64: function base64(value) {
137
- try {
138
- return fromBase64(value);
139
- } catch (e) {
140
- return undefined;
106
+ const as = {
107
+ string: (value)=>{
108
+ return typeof value === 'string' ? value : undefined;
109
+ },
110
+ number: (value)=>{
111
+ const result = parseFloat(value);
112
+ return Number.isFinite(result) ? result : undefined;
113
+ },
114
+ date: (value)=>{
115
+ const result = Date.parse(value);
116
+ return Number.isFinite(result) ? new Date(result) : undefined;
117
+ },
118
+ time: (value)=>{
119
+ const matches = value?.match(/^(\d+)(ms|s|m|h|d|w)?$/);
120
+ if (matches) {
121
+ const [_, amount, unit = 'ms'] = matches;
122
+ return parseInt(amount, 10) * MULTIPLIERS[unit];
123
+ }
124
+ return undefined;
125
+ },
126
+ boolean: (value)=>/^(0|1|true|false|enabled|disabled)$/i.test(value) ? /^(1|true|enabled)$/i.test(value) : undefined,
127
+ array: (value, delimiter)=>value?.split(delimiter) ?? undefined,
128
+ json: (value)=>{
129
+ try {
130
+ return JSON.parse(value);
131
+ } catch (e) {
132
+ return undefined;
133
+ }
134
+ },
135
+ base64: (value)=>{
136
+ try {
137
+ return fromBase64(value);
138
+ } catch (e) {
139
+ return undefined;
140
+ }
141
141
  }
142
- }
143
142
  };
144
143
  function certain(target, schema, path, optional) {
145
- if (schema === null || typeof schema === 'undefined') {
146
- return new AssertError(schema, 'any value', path, 'schema value');
147
- }
148
- if (schema instanceof Optional) {
149
- return certain(target, schema.schema, path, true);
150
- }
151
- var isValue = target !== null && typeof target !== 'undefined';
152
- if (!isValue && optional) {
153
- return;
154
- }
155
- if (schema instanceof Or) {
156
- if (!schema.schema.length) {
157
- return new AssertError(target, 'values', path, 'OR schema');
144
+ if (schema === null || typeof schema === 'undefined') {
145
+ return new AssertError(schema, 'any value', path, 'schema value');
158
146
  }
159
- return findNotError(schema.schema, function (schema) {
160
- return certain(target, schema, path);
161
- });
162
- }
163
- if (schema instanceof And) {
164
- if (!schema.schema.length) {
165
- return new AssertError(target, 'values', path, 'AND schema');
147
+ if (schema instanceof Optional) {
148
+ return certain(target, schema.schema, path, true);
166
149
  }
167
- return findFirstError(schema.schema, function (schema) {
168
- return certain(target, schema, path);
169
- });
170
- }
171
- if (typeof schema === 'function') {
172
- if (!isValue) {
173
- return new AssertError(target, schema.name, path);
150
+ const isValue = target !== null && typeof target !== 'undefined';
151
+ if (!isValue && optional) {
152
+ return;
174
153
  }
175
- if (_typeof(target) === 'object' && !(target instanceof schema)) {
176
- return new AssertError(target, schema.name, path);
177
- }
178
- if (_typeof(target) !== 'object' && target.constructor !== schema) {
179
- return new AssertError(target, schema.name, path);
154
+ if (schema instanceof Or) {
155
+ if (!schema.schema.length) {
156
+ return new AssertError(target, 'values', path, 'OR schema');
157
+ }
158
+ return findNotError(schema.schema, (schema)=>certain(target, schema, path));
180
159
  }
181
- } else if (Array.isArray(schema)) {
182
- if (!Array.isArray(target)) {
183
- return new AssertError(target, schema.constructor.name, path);
160
+ if (schema instanceof And) {
161
+ if (!schema.schema.length) {
162
+ return new AssertError(target, 'values', path, 'AND schema');
163
+ }
164
+ return findFirstError(schema.schema, (schema)=>certain(target, schema, path));
184
165
  }
185
- return findFirstError(target, function (value, idx) {
186
- return findNotError(schema, function (itemSchemaType) {
187
- return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
188
- });
189
- });
190
- } else if (_typeof(schema) === 'object') {
191
- if (schema instanceof RegExp) {
192
- if (!schema.test('' + target)) {
193
- return new AssertError(target, "matching /".concat(schema.source, "/"), path);
194
- }
195
- } else {
196
- if (_typeof(target) !== 'object') {
197
- return new AssertError(target, schema.constructor.name, path);
198
- }
199
- if (target === null) {
200
- return new AssertError(target, 'an object', path);
201
- }
202
- if ($keys in schema) {
203
- var targetKeys = Object.keys(target);
204
- var assertError = findFirstError(targetKeys, function (targetKey) {
205
- return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
206
- });
207
- if (assertError) {
208
- return assertError;
166
+ if (typeof schema === 'function') {
167
+ if (!isValue) {
168
+ return new AssertError(target, schema.name, path);
169
+ }
170
+ if (typeof target === 'object' && !(target instanceof schema)) {
171
+ return new AssertError(target, schema.name, path);
209
172
  }
210
- }
211
- if ($values in schema) {
212
- var _targetKeys = Object.keys(target);
213
- var _assertError = findFirstError(_targetKeys, function (targetKey) {
214
- return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
173
+ if (typeof target !== 'object' && target.constructor !== schema) {
174
+ return new AssertError(target, schema.name, path);
175
+ }
176
+ } else if (Array.isArray(schema)) {
177
+ if (!Array.isArray(target)) {
178
+ return new AssertError(target, schema.constructor.name, path);
179
+ }
180
+ return findFirstError(target, (value, idx)=>{
181
+ return findNotError(schema, (itemSchemaType)=>certain(value, itemSchemaType, `${path}.${idx}`));
215
182
  });
216
- if (_assertError) {
217
- return _assertError;
183
+ } else if (typeof schema === 'object') {
184
+ if (schema instanceof RegExp) {
185
+ if (!schema.test('' + target)) {
186
+ return new AssertError(target, `matching /${schema.source}/`, path);
187
+ }
188
+ } else {
189
+ if (typeof target !== 'object') {
190
+ return new AssertError(target, schema.constructor.name, path);
191
+ }
192
+ if (target === null) {
193
+ return new AssertError(target, 'an object', path);
194
+ }
195
+ if ($keys in schema) {
196
+ const targetKeys = Object.keys(target);
197
+ const assertError = findFirstError(targetKeys, (targetKey)=>certain(targetKey, schema[$keys], `${path}.${targetKey}`));
198
+ if (assertError) {
199
+ return assertError;
200
+ }
201
+ }
202
+ if ($values in schema) {
203
+ const targetKeys = Object.keys(target);
204
+ const assertError = findFirstError(targetKeys, (targetKey)=>certain(target[targetKey], schema[$values], `${path}.${targetKey}`));
205
+ if (assertError) {
206
+ return assertError;
207
+ }
208
+ }
209
+ return findFirstError(Object.keys(schema), (key)=>certain(target[key], schema[key], `${path}.${key}`));
218
210
  }
219
- }
220
- return findFirstError(Object.keys(schema), function (key) {
221
- return certain(target[key], schema[key], "".concat(path, ".").concat(key));
222
- });
211
+ } else if (target !== schema) {
212
+ return new AssertError(target, schema, path);
223
213
  }
224
- } else if (target !== schema) {
225
- return new AssertError(target, schema, path);
226
- }
227
214
  }
228
- var ascertain = exports.ascertain = function ascertain(schema, data) {
229
- var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
230
- var result = certain(data, schema, rootName);
231
- if (result instanceof AssertError) {
232
- throw new TypeError(result.toString());
233
- }
215
+ const ascertain = (schema, data, rootName = '[root]')=>{
216
+ const result = certain(data, schema, rootName);
217
+ if (result instanceof AssertError) {
218
+ throw new TypeError(result.toString());
219
+ }
234
220
  };
235
- var _default = exports.default = ascertain;
221
+ const _default = ascertain;
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "ascertain",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
4
4
  "description": "0-Deps, simple, fast, for browser and node js object schema validator",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
7
+ "scripts": {
8
+ "build": "NODE_ENV=production swc src/index.js -d build --ignore **/__tests__,**/__fixtures__ --strip-leading-paths && cp src/index.d.ts build",
9
+ "test": "NODE_ENV=test jest",
10
+ "test:cov": "COVERAGE=1 NODE_ENV=test jest"
11
+ },
7
12
  "repository": {
8
13
  "type": "git",
9
14
  "url": "git+https://github.com/3axap4eHko/ascertain.git"
@@ -28,16 +33,9 @@
28
33
  },
29
34
  "homepage": "https://github.com/3axap4eHko/ascertain#readme",
30
35
  "devDependencies": {
31
- "@babel/cli": "^7.23.9",
32
- "@babel/core": "^7.24.0",
33
- "@babel/preset-env": "^7.24.0",
34
- "babel-jest": "^29.7.0",
36
+ "@swc/cli": "^0.3.10",
37
+ "@swc/core": "^1.4.11",
38
+ "@swc/jest": "^0.2.36",
35
39
  "jest": "^29.7.0"
36
- },
37
- "dependencies": {},
38
- "scripts": {
39
- "build": "NODE_ENV=production babel src --out-dir build --ignore **/__tests__,**/__fixtures__ && cp src/index.d.ts build",
40
- "test": "NODE_ENV=test jest",
41
- "test:cov": "COVERAGE=1 NODE_ENV=test jest"
42
40
  }
43
- }
41
+ }