arktype 1.0.16-alpha → 1.0.18-alpha
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 +3 -3
- package/dist/cjs/main.js +38 -0
- package/dist/cjs/nodes/branch.js +140 -0
- package/dist/cjs/nodes/compose.js +157 -0
- package/dist/{nodes → cjs/nodes}/discriminate.js +92 -100
- package/dist/cjs/nodes/node.js +139 -0
- package/dist/cjs/nodes/predicate.js +86 -0
- package/dist/cjs/nodes/rules/class.js +25 -0
- package/dist/cjs/nodes/rules/collapsibleSet.js +48 -0
- package/dist/cjs/nodes/rules/divisor.js +29 -0
- package/dist/cjs/nodes/rules/props.js +160 -0
- package/dist/cjs/nodes/rules/range.js +105 -0
- package/dist/cjs/nodes/rules/regex.js +26 -0
- package/dist/cjs/nodes/rules/rules.js +123 -0
- package/dist/cjs/parse/ast/ast.js +4 -0
- package/dist/cjs/parse/ast/bound.js +16 -0
- package/dist/cjs/parse/ast/config.js +13 -0
- package/dist/cjs/parse/ast/distributableFunction.js +48 -0
- package/dist/cjs/parse/ast/divisor.js +9 -0
- package/dist/cjs/parse/ast/intersection.js +30 -0
- package/dist/cjs/parse/ast/keyof.js +111 -0
- package/dist/cjs/parse/ast/morph.js +58 -0
- package/dist/cjs/parse/ast/narrow.js +22 -0
- package/dist/cjs/parse/ast/node.js +4 -0
- package/dist/cjs/parse/ast/tuple.js +94 -0
- package/dist/cjs/parse/ast/union.js +9 -0
- package/dist/cjs/parse/ast/utils.js +4 -0
- package/dist/cjs/parse/definition.js +61 -0
- package/dist/cjs/parse/record.js +37 -0
- package/dist/cjs/parse/string/reduce/dynamic.js +167 -0
- package/dist/cjs/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/parse/string/reduce/static.js +4 -0
- package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/parse/string/shift/scanner.js +114 -0
- package/dist/cjs/parse/string/string.js +43 -0
- package/dist/cjs/scopes/ark.js +45 -0
- package/dist/cjs/scopes/cache.js +52 -0
- package/dist/cjs/scopes/expressions.js +29 -0
- package/dist/cjs/scopes/jsObjects.js +93 -0
- package/dist/cjs/scopes/scope.js +323 -0
- package/dist/cjs/scopes/tsKeywords.js +113 -0
- package/dist/cjs/scopes/type.js +50 -0
- package/dist/cjs/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/scopes/validation/date.js +62 -0
- package/dist/cjs/scopes/validation/validation.js +108 -0
- package/dist/cjs/traverse/problems.js +281 -0
- package/dist/cjs/traverse/traverse.js +390 -0
- package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/utils/data.js +53 -0
- package/dist/cjs/utils/domains.js +30 -0
- package/dist/cjs/utils/errors.js +26 -0
- package/dist/cjs/utils/freeze.js +15 -0
- package/dist/cjs/utils/generics.js +63 -0
- package/dist/cjs/utils/numericLiterals.js +75 -0
- package/dist/cjs/utils/objectKinds.js +69 -0
- package/dist/cjs/utils/paths.js +32 -0
- package/dist/cjs/utils/serialize.js +74 -0
- package/dist/cjs/utils/unionToTuple.js +4 -0
- package/dist/mjs/main.js +8 -0
- package/dist/mjs/nodes/branch.js +123 -0
- package/dist/mjs/nodes/compose.js +135 -0
- package/dist/mjs/nodes/discriminate.js +247 -0
- package/dist/mjs/nodes/node.js +117 -0
- package/dist/mjs/nodes/predicate.js +69 -0
- package/dist/mjs/nodes/rules/class.js +11 -0
- package/dist/mjs/nodes/rules/collapsibleSet.js +34 -0
- package/dist/mjs/nodes/rules/divisor.js +15 -0
- package/dist/mjs/nodes/rules/props.js +142 -0
- package/dist/mjs/nodes/rules/range.js +86 -0
- package/dist/mjs/nodes/rules/regex.js +11 -0
- package/dist/mjs/nodes/rules/rules.js +106 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/parse/ast/ast.js +1 -0
- package/dist/mjs/parse/ast/bound.js +2 -0
- package/dist/mjs/parse/ast/config.js +9 -0
- package/dist/mjs/parse/ast/distributableFunction.js +34 -0
- package/dist/mjs/parse/ast/divisor.js +1 -0
- package/dist/mjs/parse/ast/intersection.js +16 -0
- package/dist/mjs/parse/ast/keyof.js +103 -0
- package/dist/mjs/parse/ast/morph.js +44 -0
- package/dist/mjs/parse/ast/narrow.js +14 -0
- package/dist/mjs/parse/ast/node.js +1 -0
- package/dist/mjs/parse/ast/tuple.js +80 -0
- package/dist/mjs/parse/ast/union.js +1 -0
- package/dist/mjs/parse/ast/utils.js +1 -0
- package/dist/mjs/parse/definition.js +46 -0
- package/dist/mjs/parse/record.js +29 -0
- package/dist/mjs/parse/string/reduce/dynamic.js +159 -0
- package/dist/mjs/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/parse/string/reduce/static.js +1 -0
- package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/parse/string/shift/operator/operator.js +13 -0
- package/dist/mjs/parse/string/shift/scanner.js +106 -0
- package/dist/mjs/parse/string/string.js +28 -0
- package/dist/mjs/scopes/ark.js +28 -0
- package/dist/mjs/scopes/cache.js +38 -0
- package/dist/{scopes → mjs/scopes}/expressions.js +9 -20
- package/dist/mjs/scopes/jsObjects.js +82 -0
- package/dist/mjs/scopes/scope.js +303 -0
- package/dist/mjs/scopes/tsKeywords.js +116 -0
- package/dist/mjs/scopes/type.js +35 -0
- package/dist/{scopes → mjs/scopes}/validation/creditCard.js +11 -15
- package/dist/{scopes → mjs/scopes}/validation/date.js +16 -21
- package/dist/mjs/scopes/validation/validation.js +112 -0
- package/dist/mjs/traverse/problems.js +260 -0
- package/dist/mjs/traverse/traverse.js +373 -0
- package/dist/{utils → mjs/utils}/chainableNoOpProxy.js +2 -6
- package/dist/mjs/utils/data.js +38 -0
- package/dist/mjs/utils/domains.js +15 -0
- package/dist/mjs/utils/errors.js +10 -0
- package/dist/mjs/utils/freeze.js +7 -0
- package/dist/mjs/utils/generics.js +38 -0
- package/dist/mjs/utils/numericLiterals.js +72 -0
- package/dist/mjs/utils/objectKinds.js +55 -0
- package/dist/{utils → mjs/utils}/paths.js +3 -8
- package/dist/mjs/utils/serialize.js +58 -0
- package/dist/mjs/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/package.json +14 -6
- package/dist/main.js +0 -29
- package/dist/nodes/branch.js +0 -135
- package/dist/nodes/compose.js +0 -99
- package/dist/nodes/node.js +0 -134
- package/dist/nodes/predicate.js +0 -88
- package/dist/nodes/rules/class.js +0 -23
- package/dist/nodes/rules/collapsibleSet.js +0 -34
- package/dist/nodes/rules/divisor.js +0 -19
- package/dist/nodes/rules/props.js +0 -130
- package/dist/nodes/rules/range.js +0 -108
- package/dist/nodes/rules/regex.js +0 -17
- package/dist/nodes/rules/rules.js +0 -102
- package/dist/parse/ast/ast.js +0 -2
- package/dist/parse/ast/bound.js +0 -7
- package/dist/parse/ast/config.js +0 -14
- package/dist/parse/ast/distributableFunction.js +0 -40
- package/dist/parse/ast/divisor.js +0 -5
- package/dist/parse/ast/intersection.js +0 -21
- package/dist/parse/ast/keyof.js +0 -107
- package/dist/parse/ast/morph.js +0 -50
- package/dist/parse/ast/narrow.js +0 -17
- package/dist/parse/ast/node.js +0 -2
- package/dist/parse/ast/tuple.js +0 -72
- package/dist/parse/ast/union.js +0 -5
- package/dist/parse/ast/utils.js +0 -2
- package/dist/parse/definition.js +0 -47
- package/dist/parse/record.js +0 -31
- package/dist/parse/string/reduce/dynamic.js +0 -159
- package/dist/parse/string/reduce/shared.js +0 -13
- package/dist/parse/string/reduce/static.js +0 -2
- package/dist/parse/string/shift/operand/enclosed.js +0 -37
- package/dist/parse/string/shift/operand/operand.js +0 -15
- package/dist/parse/string/shift/operand/unenclosed.js +0 -45
- package/dist/parse/string/shift/operator/bounds.js +0 -83
- package/dist/parse/string/shift/operator/divisor.js +0 -23
- package/dist/parse/string/shift/operator/operator.js +0 -34
- package/dist/parse/string/shift/scanner.js +0 -97
- package/dist/parse/string/string.js +0 -35
- package/dist/scopes/ark.js +0 -27
- package/dist/scopes/cache.js +0 -28
- package/dist/scopes/jsObjects.js +0 -20
- package/dist/scopes/scope.js +0 -184
- package/dist/scopes/tsKeywords.js +0 -49
- package/dist/scopes/type.js +0 -40
- package/dist/scopes/validation/validation.js +0 -74
- package/dist/traverse/problems.js +0 -234
- package/dist/traverse/traverse.js +0 -330
- package/dist/utils/data.js +0 -39
- package/dist/utils/domains.js +0 -27
- package/dist/utils/errors.js +0 -17
- package/dist/utils/freeze.js +0 -16
- package/dist/utils/generics.js +0 -53
- package/dist/utils/numericLiterals.js +0 -88
- package/dist/utils/objectKinds.js +0 -69
- package/dist/utils/serialize.js +0 -71
- package/dist/utils/unionToTuple.js +0 -2
- /package/dist/{package.json → cjs/package.json} +0 -0
- /package/dist/{main.d.ts → types/main.d.ts} +0 -0
- /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
- /package/dist/{parse → types/parse}/definition.d.ts +0 -0
- /package/dist/{parse → types/parse}/record.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
- /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{utils → types/utils}/data.d.ts +0 -0
- /package/dist/{utils → types/utils}/domains.d.ts +0 -0
- /package/dist/{utils → types/utils}/errors.d.ts +0 -0
- /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
- /package/dist/{utils → types/utils}/generics.d.ts +0 -0
- /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
- /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
- /package/dist/{utils → types/utils}/paths.d.ts +0 -0
- /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
- /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.morph = exports.narrow = exports.valueOf = exports.instanceOf = exports.keyOf = exports.arrayOf = exports.union = exports.intersection = void 0;
|
|
4
|
-
const ark_js_1 = require("./ark.js");
|
|
1
|
+
import { scopes } from "./ark.js";
|
|
5
2
|
/**
|
|
6
3
|
* @operator {@link intersection | &}
|
|
7
4
|
* @docgenTable
|
|
@@ -14,8 +11,7 @@ const ark_js_1 = require("./ark.js");
|
|
|
14
11
|
* const tupleIntersection = type(["/@arktype\.io$/", "&", "email"])
|
|
15
12
|
* @example helper
|
|
16
13
|
* const helperIntersection = intersection("/@arktype\.io$/","email")
|
|
17
|
-
*/
|
|
18
|
-
exports.intersection = ark_js_1.scopes.ark.intersection;
|
|
14
|
+
*/ export const intersection = scopes.ark.intersection;
|
|
19
15
|
/**
|
|
20
16
|
* @operator {@link union | |}
|
|
21
17
|
* @docgenTable
|
|
@@ -28,8 +24,7 @@ exports.intersection = ark_js_1.scopes.ark.intersection;
|
|
|
28
24
|
* const tupleUnion = type(["string", "|", "number"])
|
|
29
25
|
* @example helper
|
|
30
26
|
* const helperUnion = union("string", "number")
|
|
31
|
-
*/
|
|
32
|
-
exports.union = ark_js_1.scopes.ark.union;
|
|
27
|
+
*/ export const union = scopes.ark.union;
|
|
33
28
|
/**
|
|
34
29
|
* @operator {@link arrayOf}
|
|
35
30
|
* @docgenTable
|
|
@@ -42,8 +37,7 @@ exports.union = ark_js_1.scopes.ark.union;
|
|
|
42
37
|
* const tupleArray = type(["number", "[]"])
|
|
43
38
|
* @example helper
|
|
44
39
|
* const helperArray = arrayOf("number")
|
|
45
|
-
*/
|
|
46
|
-
exports.arrayOf = ark_js_1.scopes.ark.arrayOf;
|
|
40
|
+
*/ export const arrayOf = scopes.ark.arrayOf;
|
|
47
41
|
/**
|
|
48
42
|
* @operator {@link keyOf}
|
|
49
43
|
* @docgenTable
|
|
@@ -53,8 +47,7 @@ exports.arrayOf = ark_js_1.scopes.ark.arrayOf;
|
|
|
53
47
|
* const tupleKeyOf = type(["keyOf", {a:"string"}])
|
|
54
48
|
* @example helper
|
|
55
49
|
* const helperKeyOf = keyOf({a:"string"})
|
|
56
|
-
*/
|
|
57
|
-
exports.keyOf = ark_js_1.scopes.ark.keyOf;
|
|
50
|
+
*/ export const keyOf = scopes.ark.keyOf;
|
|
58
51
|
/**
|
|
59
52
|
* @operator {@link instanceOf}
|
|
60
53
|
* @docgenTable
|
|
@@ -64,8 +57,7 @@ exports.keyOf = ark_js_1.scopes.ark.keyOf;
|
|
|
64
57
|
* const tupleInstanceOf = type(["instanceOf", Date])
|
|
65
58
|
* @example helper
|
|
66
59
|
* const helperInstanceOf = instanceOf(Date)
|
|
67
|
-
*/
|
|
68
|
-
exports.instanceOf = ark_js_1.scopes.ark.instanceOf;
|
|
60
|
+
*/ export const instanceOf = scopes.ark.instanceOf;
|
|
69
61
|
/**
|
|
70
62
|
* @operator {@link valueOf | ===}
|
|
71
63
|
* @docgenTable
|
|
@@ -75,8 +67,7 @@ exports.instanceOf = ark_js_1.scopes.ark.instanceOf;
|
|
|
75
67
|
* const tupleValueOf = type(["valueOf", {a:"string"}])
|
|
76
68
|
* @example helper
|
|
77
69
|
* const helperValueOf = valueOf({a:"string"})
|
|
78
|
-
*/
|
|
79
|
-
exports.valueOf = ark_js_1.scopes.ark.valueOf;
|
|
70
|
+
*/ export const valueOf = scopes.ark.valueOf;
|
|
80
71
|
/**
|
|
81
72
|
* @operator {@link narrow | =>}
|
|
82
73
|
* @docgenTable
|
|
@@ -85,8 +76,7 @@ exports.valueOf = ark_js_1.scopes.ark.valueOf;
|
|
|
85
76
|
* const narrow = type( ["number", "=>" , (n) => n % 2 === 0])
|
|
86
77
|
* @example
|
|
87
78
|
* const isEven = (x: unknown): x is number => x % 2 === 0
|
|
88
|
-
*/
|
|
89
|
-
exports.narrow = ark_js_1.scopes.ark.narrow;
|
|
79
|
+
*/ export const narrow = scopes.ark.narrow;
|
|
90
80
|
/**
|
|
91
81
|
* @operator {@link morph | |>}
|
|
92
82
|
* @docgenTable
|
|
@@ -96,5 +86,4 @@ exports.narrow = ark_js_1.scopes.ark.narrow;
|
|
|
96
86
|
* const tupleMorph = type( ["string", "|>" , (data) => `morphed ${data}`])
|
|
97
87
|
* @example helper
|
|
98
88
|
* const helperMorph = morph("string", (data) => `morphed ${data}`)
|
|
99
|
-
*/
|
|
100
|
-
exports.morph = ark_js_1.scopes.ark.morph;
|
|
89
|
+
*/ export const morph = scopes.ark.morph;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { scope } from "./scope.js";
|
|
2
|
+
/**
|
|
3
|
+
* @docgenScope
|
|
4
|
+
* @docgenTable
|
|
5
|
+
*/ export const jsObjectsScope = scope({
|
|
6
|
+
Function: [
|
|
7
|
+
"node",
|
|
8
|
+
{
|
|
9
|
+
object: {
|
|
10
|
+
class: Function
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
Date: [
|
|
15
|
+
"node",
|
|
16
|
+
{
|
|
17
|
+
object: {
|
|
18
|
+
class: Date
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
Error: [
|
|
23
|
+
"node",
|
|
24
|
+
{
|
|
25
|
+
object: {
|
|
26
|
+
class: Error
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
Map: [
|
|
31
|
+
"node",
|
|
32
|
+
{
|
|
33
|
+
object: {
|
|
34
|
+
class: Map
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
RegExp: [
|
|
39
|
+
"node",
|
|
40
|
+
{
|
|
41
|
+
object: {
|
|
42
|
+
class: RegExp
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
Set: [
|
|
47
|
+
"node",
|
|
48
|
+
{
|
|
49
|
+
object: {
|
|
50
|
+
class: Set
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
WeakMap: [
|
|
55
|
+
"node",
|
|
56
|
+
{
|
|
57
|
+
object: {
|
|
58
|
+
class: WeakMap
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
WeakSet: [
|
|
63
|
+
"node",
|
|
64
|
+
{
|
|
65
|
+
object: {
|
|
66
|
+
class: WeakSet
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
Promise: [
|
|
71
|
+
"node",
|
|
72
|
+
{
|
|
73
|
+
object: {
|
|
74
|
+
class: Promise
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
}, {
|
|
79
|
+
name: "jsObjects",
|
|
80
|
+
standard: false
|
|
81
|
+
});
|
|
82
|
+
export const jsObjects = jsObjectsScope.compile();
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
2
|
+
if (privateCollection.has(obj)) {
|
|
3
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function _classApplyDescriptorGet(receiver, descriptor) {
|
|
7
|
+
if (descriptor.get) {
|
|
8
|
+
return descriptor.get.call(receiver);
|
|
9
|
+
}
|
|
10
|
+
return descriptor.value;
|
|
11
|
+
}
|
|
12
|
+
function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
13
|
+
if (descriptor.set) {
|
|
14
|
+
descriptor.set.call(receiver, value);
|
|
15
|
+
} else {
|
|
16
|
+
if (!descriptor.writable) {
|
|
17
|
+
throw new TypeError("attempted to set read only private field");
|
|
18
|
+
}
|
|
19
|
+
descriptor.value = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
23
|
+
if (!privateMap.has(receiver)) {
|
|
24
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
25
|
+
}
|
|
26
|
+
return privateMap.get(receiver);
|
|
27
|
+
}
|
|
28
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
29
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
|
|
30
|
+
return _classApplyDescriptorGet(receiver, descriptor);
|
|
31
|
+
}
|
|
32
|
+
function _classPrivateFieldInit(obj, privateMap, value) {
|
|
33
|
+
_checkPrivateRedeclaration(obj, privateMap);
|
|
34
|
+
privateMap.set(obj, value);
|
|
35
|
+
}
|
|
36
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
37
|
+
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
|
|
38
|
+
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) {
|
|
42
|
+
if (!privateSet.has(receiver)) {
|
|
43
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
44
|
+
}
|
|
45
|
+
return fn;
|
|
46
|
+
}
|
|
47
|
+
function _classPrivateMethodInit(obj, privateSet) {
|
|
48
|
+
_checkPrivateRedeclaration(obj, privateSet);
|
|
49
|
+
privateSet.add(obj);
|
|
50
|
+
}
|
|
51
|
+
function _defineProperty(obj, key, value) {
|
|
52
|
+
if (key in obj) {
|
|
53
|
+
Object.defineProperty(obj, key, {
|
|
54
|
+
value: value,
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
obj[key] = value;
|
|
61
|
+
}
|
|
62
|
+
return obj;
|
|
63
|
+
}
|
|
64
|
+
import { flattenType, isConfigNode } from "../nodes/node.js";
|
|
65
|
+
import { parseDefinition } from "../parse/definition.js";
|
|
66
|
+
import { compileProblemWriters } from "../traverse/problems.js";
|
|
67
|
+
import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
|
|
68
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
69
|
+
import { deepFreeze } from "../utils/freeze.js";
|
|
70
|
+
import { hasKeys } from "../utils/generics.js";
|
|
71
|
+
import { Path } from "../utils/paths.js";
|
|
72
|
+
import { Cache, FreezingCache } from "./cache.js";
|
|
73
|
+
import { initializeType } from "./type.js";
|
|
74
|
+
export const compileScopeOptions = (opts)=>({
|
|
75
|
+
codes: compileProblemWriters(opts.codes),
|
|
76
|
+
keys: opts.keys ?? "loose"
|
|
77
|
+
});
|
|
78
|
+
let anonymousScopeCount = 0;
|
|
79
|
+
const scopeRegistry = {};
|
|
80
|
+
const spaceRegistry = {};
|
|
81
|
+
export const isConfigTuple = (def)=>Array.isArray(def) && def[1] === ":";
|
|
82
|
+
var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
|
|
83
|
+
export class Scope {
|
|
84
|
+
getAnonymousQualifiedName(base) {
|
|
85
|
+
let increment = 0;
|
|
86
|
+
let id = base;
|
|
87
|
+
while(this.isResolvable(id)){
|
|
88
|
+
id = `${base}${increment++}`;
|
|
89
|
+
}
|
|
90
|
+
return `${this.name}.${id}`;
|
|
91
|
+
}
|
|
92
|
+
addAnonymousTypeReference(referencedType, ctx) {
|
|
93
|
+
var _ctx_type;
|
|
94
|
+
(_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = referencedType.includesMorph);
|
|
95
|
+
return referencedType.node;
|
|
96
|
+
}
|
|
97
|
+
get infer() {
|
|
98
|
+
return chainableNoOpProxy;
|
|
99
|
+
}
|
|
100
|
+
compile() {
|
|
101
|
+
if (!spaceRegistry[this.name]) {
|
|
102
|
+
for(const name in this.aliases){
|
|
103
|
+
this.resolve(name);
|
|
104
|
+
}
|
|
105
|
+
spaceRegistry[this.name] = _classPrivateFieldGet(this, _exports).root;
|
|
106
|
+
}
|
|
107
|
+
return _classPrivateFieldGet(this, _exports).root;
|
|
108
|
+
}
|
|
109
|
+
addParsedReferenceIfResolvable(name, ctx) {
|
|
110
|
+
var _ctx_type;
|
|
111
|
+
const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", [
|
|
112
|
+
name
|
|
113
|
+
]);
|
|
114
|
+
if (!resolution) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
(_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = resolution.includesMorph);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
resolve(name) {
|
|
121
|
+
return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", [
|
|
122
|
+
name
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
125
|
+
resolveNode(node) {
|
|
126
|
+
return typeof node === "string" ? this.resolveNode(this.resolve(node).node) : node;
|
|
127
|
+
}
|
|
128
|
+
resolveTypeNode(node) {
|
|
129
|
+
const resolution = this.resolveNode(node);
|
|
130
|
+
return isConfigNode(resolution) ? resolution.node : resolution;
|
|
131
|
+
}
|
|
132
|
+
isResolvable(name) {
|
|
133
|
+
return _classPrivateFieldGet(this, _resolutions).has(name) || this.aliases[name];
|
|
134
|
+
}
|
|
135
|
+
constructor(aliases, opts = {}){
|
|
136
|
+
_classPrivateMethodInit(this, _register);
|
|
137
|
+
_classPrivateMethodInit(this, _cacheSpaces);
|
|
138
|
+
_classPrivateMethodInit(this, _initializeContext);
|
|
139
|
+
_classPrivateMethodInit(this, _resolveRecurse);
|
|
140
|
+
_defineProperty(this, "aliases", void 0);
|
|
141
|
+
_defineProperty(this, "name", void 0);
|
|
142
|
+
_defineProperty(this, "config", void 0);
|
|
143
|
+
_defineProperty(this, "parseCache", void 0);
|
|
144
|
+
_classPrivateFieldInit(this, _resolutions, {
|
|
145
|
+
writable: true,
|
|
146
|
+
value: void 0
|
|
147
|
+
});
|
|
148
|
+
_classPrivateFieldInit(this, _exports, {
|
|
149
|
+
writable: true,
|
|
150
|
+
value: void 0
|
|
151
|
+
});
|
|
152
|
+
_defineProperty(this, "expressions", void 0);
|
|
153
|
+
_defineProperty(this, "intersection", void 0);
|
|
154
|
+
_defineProperty(this, "union", void 0);
|
|
155
|
+
_defineProperty(this, "arrayOf", void 0);
|
|
156
|
+
_defineProperty(this, "keyOf", void 0);
|
|
157
|
+
_defineProperty(this, "valueOf", void 0);
|
|
158
|
+
_defineProperty(this, "instanceOf", void 0);
|
|
159
|
+
_defineProperty(this, "narrow", void 0);
|
|
160
|
+
_defineProperty(this, "morph", void 0);
|
|
161
|
+
_defineProperty(this, "type", void 0);
|
|
162
|
+
this.aliases = aliases;
|
|
163
|
+
this.parseCache = new FreezingCache();
|
|
164
|
+
_classPrivateFieldSet(this, _resolutions, new Cache());
|
|
165
|
+
_classPrivateFieldSet(this, _exports, new Cache());
|
|
166
|
+
this.expressions = {
|
|
167
|
+
intersection: (l, r, opts)=>this.type([
|
|
168
|
+
l,
|
|
169
|
+
"&",
|
|
170
|
+
r
|
|
171
|
+
], opts),
|
|
172
|
+
union: (l, r, opts)=>this.type([
|
|
173
|
+
l,
|
|
174
|
+
"|",
|
|
175
|
+
r
|
|
176
|
+
], opts),
|
|
177
|
+
arrayOf: (def, opts)=>this.type([
|
|
178
|
+
def,
|
|
179
|
+
"[]"
|
|
180
|
+
], opts),
|
|
181
|
+
keyOf: (def, opts)=>this.type([
|
|
182
|
+
"keyof",
|
|
183
|
+
def
|
|
184
|
+
], opts),
|
|
185
|
+
node: (def, opts)=>this.type([
|
|
186
|
+
"node",
|
|
187
|
+
def
|
|
188
|
+
], opts),
|
|
189
|
+
instanceOf: (def, opts)=>this.type([
|
|
190
|
+
"instanceof",
|
|
191
|
+
def
|
|
192
|
+
], opts),
|
|
193
|
+
valueOf: (def, opts)=>this.type([
|
|
194
|
+
"===",
|
|
195
|
+
def
|
|
196
|
+
], opts),
|
|
197
|
+
narrow: (def, fn, opts)=>this.type([
|
|
198
|
+
def,
|
|
199
|
+
"=>",
|
|
200
|
+
fn
|
|
201
|
+
], opts),
|
|
202
|
+
morph: (def, fn, opts)=>this.type([
|
|
203
|
+
def,
|
|
204
|
+
"|>",
|
|
205
|
+
fn
|
|
206
|
+
], opts)
|
|
207
|
+
};
|
|
208
|
+
this.intersection = this.expressions.intersection;
|
|
209
|
+
this.union = this.expressions.union;
|
|
210
|
+
this.arrayOf = this.expressions.arrayOf;
|
|
211
|
+
this.keyOf = this.expressions.keyOf;
|
|
212
|
+
this.valueOf = this.expressions.valueOf;
|
|
213
|
+
this.instanceOf = this.expressions.instanceOf;
|
|
214
|
+
this.narrow = this.expressions.narrow;
|
|
215
|
+
this.morph = this.expressions.morph;
|
|
216
|
+
this.type = Object.assign((def, config = {})=>{
|
|
217
|
+
const t = initializeType("λtype", def, config, this);
|
|
218
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
|
|
219
|
+
const root = parseDefinition(def, ctx);
|
|
220
|
+
t.node = deepFreeze(hasKeys(config) ? {
|
|
221
|
+
config,
|
|
222
|
+
node: this.resolveTypeNode(root)
|
|
223
|
+
} : root);
|
|
224
|
+
t.flat = deepFreeze(flattenType(t));
|
|
225
|
+
return t;
|
|
226
|
+
}, {
|
|
227
|
+
from: this.expressions.node
|
|
228
|
+
});
|
|
229
|
+
this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
|
|
230
|
+
if (opts.standard !== false) {
|
|
231
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
|
|
232
|
+
spaceRegistry["standard"]
|
|
233
|
+
], "imports");
|
|
234
|
+
}
|
|
235
|
+
if (opts.imports) {
|
|
236
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.imports, "imports");
|
|
237
|
+
}
|
|
238
|
+
if (opts.includes) {
|
|
239
|
+
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
|
|
240
|
+
}
|
|
241
|
+
this.config = compileScopeOptions(opts);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function register(opts) {
|
|
245
|
+
const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `scope${++anonymousScopeCount}`;
|
|
246
|
+
scopeRegistry[name] = this;
|
|
247
|
+
return name;
|
|
248
|
+
}
|
|
249
|
+
function cacheSpaces(spaces, kind) {
|
|
250
|
+
for (const space of spaces){
|
|
251
|
+
for(const name in space){
|
|
252
|
+
if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
|
|
253
|
+
throwParseError(writeDuplicateAliasesMessage(name));
|
|
254
|
+
}
|
|
255
|
+
_classPrivateFieldGet(this, _resolutions).set(name, space[name]);
|
|
256
|
+
if (kind === "includes") {
|
|
257
|
+
_classPrivateFieldGet(this, _exports).set(name, space[name]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function initializeContext(type) {
|
|
263
|
+
return {
|
|
264
|
+
type,
|
|
265
|
+
path: new Path()
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function resolveRecurse(name, onUnresolvable, seen) {
|
|
269
|
+
const maybeCacheResult = _classPrivateFieldGet(this, _resolutions).get(name);
|
|
270
|
+
if (maybeCacheResult) {
|
|
271
|
+
return maybeCacheResult;
|
|
272
|
+
}
|
|
273
|
+
const aliasDef = this.aliases[name];
|
|
274
|
+
if (!aliasDef) {
|
|
275
|
+
return onUnresolvable === "throw" ? throwInternalError(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
|
|
276
|
+
}
|
|
277
|
+
const t = initializeType(name, aliasDef, {}, this);
|
|
278
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
|
|
279
|
+
_classPrivateFieldGet(this, _resolutions).set(name, t);
|
|
280
|
+
_classPrivateFieldGet(this, _exports).set(name, t);
|
|
281
|
+
let node = parseDefinition(aliasDef, ctx);
|
|
282
|
+
if (typeof node === "string") {
|
|
283
|
+
if (seen.includes(node)) {
|
|
284
|
+
return throwParseError(writeShallowCycleErrorMessage(name, seen));
|
|
285
|
+
}
|
|
286
|
+
seen.push(node);
|
|
287
|
+
node = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, node, "throw", seen).node;
|
|
288
|
+
}
|
|
289
|
+
t.node = deepFreeze(node);
|
|
290
|
+
t.flat = deepFreeze(flattenType(t));
|
|
291
|
+
return t;
|
|
292
|
+
}
|
|
293
|
+
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
294
|
+
export const rootScope = scope({}, {
|
|
295
|
+
name: "root",
|
|
296
|
+
standard: false
|
|
297
|
+
});
|
|
298
|
+
export const rootType = rootScope.type;
|
|
299
|
+
export const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
|
|
300
|
+
...seen,
|
|
301
|
+
name
|
|
302
|
+
].join("=>")}`;
|
|
303
|
+
export const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { scope } from "./scope.js";
|
|
2
|
+
const always = {
|
|
3
|
+
bigint: true,
|
|
4
|
+
boolean: true,
|
|
5
|
+
null: true,
|
|
6
|
+
number: true,
|
|
7
|
+
object: true,
|
|
8
|
+
string: true,
|
|
9
|
+
symbol: true,
|
|
10
|
+
undefined: true
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @keywords keywords: {"any": "any",
|
|
14
|
+
"bigint": "a bigint",
|
|
15
|
+
"boolean": "a boolean",
|
|
16
|
+
"false": "false",
|
|
17
|
+
"never": "never",
|
|
18
|
+
"null": "null",
|
|
19
|
+
"number": "a number",
|
|
20
|
+
"object": "an object",
|
|
21
|
+
"string": "a string",
|
|
22
|
+
"symbol": "a symbol",
|
|
23
|
+
"true": "true",
|
|
24
|
+
"unknown": "unknown",
|
|
25
|
+
"void": "void",
|
|
26
|
+
"undefined": "undefined"}
|
|
27
|
+
* @docgenScope
|
|
28
|
+
* @docgenTable
|
|
29
|
+
*/ export const tsKeywordsScope = scope({
|
|
30
|
+
any: [
|
|
31
|
+
"node",
|
|
32
|
+
always
|
|
33
|
+
],
|
|
34
|
+
bigint: [
|
|
35
|
+
"node",
|
|
36
|
+
{
|
|
37
|
+
bigint: true
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
boolean: [
|
|
41
|
+
"node",
|
|
42
|
+
{
|
|
43
|
+
boolean: true
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
false: [
|
|
47
|
+
"node",
|
|
48
|
+
{
|
|
49
|
+
boolean: {
|
|
50
|
+
value: false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
never: [
|
|
55
|
+
"node",
|
|
56
|
+
{}
|
|
57
|
+
],
|
|
58
|
+
null: [
|
|
59
|
+
"node",
|
|
60
|
+
{
|
|
61
|
+
null: true
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
number: [
|
|
65
|
+
"node",
|
|
66
|
+
{
|
|
67
|
+
number: true
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
object: [
|
|
71
|
+
"node",
|
|
72
|
+
{
|
|
73
|
+
object: true
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
string: [
|
|
77
|
+
"node",
|
|
78
|
+
{
|
|
79
|
+
string: true
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
symbol: [
|
|
83
|
+
"node",
|
|
84
|
+
{
|
|
85
|
+
symbol: true
|
|
86
|
+
}
|
|
87
|
+
],
|
|
88
|
+
true: [
|
|
89
|
+
"node",
|
|
90
|
+
{
|
|
91
|
+
boolean: {
|
|
92
|
+
value: true
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
unknown: [
|
|
97
|
+
"node",
|
|
98
|
+
always
|
|
99
|
+
],
|
|
100
|
+
void: [
|
|
101
|
+
"node",
|
|
102
|
+
{
|
|
103
|
+
undefined: true
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
undefined: [
|
|
107
|
+
"node",
|
|
108
|
+
{
|
|
109
|
+
undefined: true
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
}, {
|
|
113
|
+
name: "ts",
|
|
114
|
+
standard: false
|
|
115
|
+
});
|
|
116
|
+
export const tsKeywords = tsKeywordsScope.compile();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { traverseRoot } from "../traverse/traverse.js";
|
|
2
|
+
import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
|
|
3
|
+
export const initializeType = (name, definition, config, scope)=>{
|
|
4
|
+
const root = {
|
|
5
|
+
// temporarily initialize node/flat to aliases that will be included in
|
|
6
|
+
// the final type in case of cyclic resolutions
|
|
7
|
+
node: name,
|
|
8
|
+
flat: [
|
|
9
|
+
[
|
|
10
|
+
"alias",
|
|
11
|
+
name
|
|
12
|
+
]
|
|
13
|
+
],
|
|
14
|
+
allows: (data)=>!namedTraverse(data).problems,
|
|
15
|
+
assert: (data)=>{
|
|
16
|
+
const result = namedTraverse(data);
|
|
17
|
+
return result.problems ? result.problems.throw() : result.data;
|
|
18
|
+
},
|
|
19
|
+
infer: chainableNoOpProxy,
|
|
20
|
+
inferIn: chainableNoOpProxy,
|
|
21
|
+
qualifiedName: isAnonymousName(name) ? scope.getAnonymousQualifiedName(name) : `${scope.name}.${name}`,
|
|
22
|
+
definition,
|
|
23
|
+
scope,
|
|
24
|
+
includesMorph: false,
|
|
25
|
+
config
|
|
26
|
+
};
|
|
27
|
+
// define within a key to dynamically assign a name to the function
|
|
28
|
+
const namedTraverse = {
|
|
29
|
+
[name]: (data)=>traverseRoot(namedTraverse, data)
|
|
30
|
+
}[name];
|
|
31
|
+
const t = Object.assign(namedTraverse, root);
|
|
32
|
+
return t;
|
|
33
|
+
};
|
|
34
|
+
export const isType = (value)=>value?.infer === chainableNoOpProxy;
|
|
35
|
+
export const isAnonymousName = (name)=>name[0] === "λ";
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.creditCard = exports.isLuhnValid = void 0;
|
|
4
|
-
const scope_js_1 = require("../scope.js");
|
|
1
|
+
import { rootType } from "../scope.js";
|
|
5
2
|
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
|
|
6
|
-
const isLuhnValid = (creditCardInput)
|
|
3
|
+
export const isLuhnValid = (creditCardInput)=>{
|
|
7
4
|
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
8
5
|
let sum = 0;
|
|
9
6
|
let digit;
|
|
10
7
|
let tmpNum;
|
|
11
8
|
let shouldDouble;
|
|
12
|
-
for
|
|
9
|
+
for(let i = sanitized.length - 1; i >= 0; i--){
|
|
13
10
|
digit = sanitized.substring(i, i + 1);
|
|
14
11
|
tmpNum = parseInt(digit, 10);
|
|
15
12
|
if (shouldDouble) {
|
|
16
13
|
tmpNum *= 2;
|
|
17
14
|
if (tmpNum >= 10) {
|
|
18
|
-
sum +=
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
15
|
+
sum += tmpNum % 10 + 1;
|
|
16
|
+
} else {
|
|
21
17
|
sum += tmpNum;
|
|
22
18
|
}
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
19
|
+
} else {
|
|
25
20
|
sum += tmpNum;
|
|
26
21
|
}
|
|
27
22
|
shouldDouble = !shouldDouble;
|
|
28
23
|
}
|
|
29
24
|
return !!(sum % 10 === 0 ? sanitized : false);
|
|
30
25
|
};
|
|
31
|
-
exports.isLuhnValid = isLuhnValid;
|
|
32
26
|
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
|
|
33
27
|
const creditCardMatcher = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
|
|
34
|
-
|
|
28
|
+
export const creditCard = rootType([
|
|
35
29
|
creditCardMatcher,
|
|
36
30
|
"=>",
|
|
37
|
-
(s, problems)
|
|
38
|
-
], {
|
|
31
|
+
(s, problems)=>isLuhnValid(s) || !problems.mustBe("a valid credit card number")
|
|
32
|
+
], {
|
|
33
|
+
mustBe: "a valid credit card number"
|
|
34
|
+
});
|