@markuplint/types 1.0.0-dev.20211213.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/LICENSE +21 -0
- package/README.md +117 -0
- package/extends.json +12 -0
- package/gen/specific-schema.json +98 -0
- package/gen/types.ts +66 -0
- package/lib/check-multi-types.d.ts +2 -0
- package/lib/check-multi-types.js +29 -0
- package/lib/check.d.ts +8 -0
- package/lib/check.js +49 -0
- package/lib/check.spec.d.ts +1 -0
- package/lib/check.spec.js +57 -0
- package/lib/css-syntax.d.ts +2 -0
- package/lib/css-syntax.js +141 -0
- package/lib/css-syntax.spec.d.ts +1 -0
- package/lib/css-syntax.spec.js +157 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.js +6 -0
- package/lib/defs.d.ts +3 -0
- package/lib/defs.js +752 -0
- package/lib/enum.d.ts +3 -0
- package/lib/enum.js +26 -0
- package/lib/get-candicate.d.ts +3 -0
- package/lib/get-candicate.js +26 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/keyword-type.d.ts +3 -0
- package/lib/keyword-type.js +46 -0
- package/lib/list.d.ts +3 -0
- package/lib/list.js +29 -0
- package/lib/list.spec.d.ts +1 -0
- package/lib/list.spec.js +129 -0
- package/lib/match-result.d.ts +9 -0
- package/lib/match-result.js +33 -0
- package/lib/number.d.ts +3 -0
- package/lib/number.js +41 -0
- package/lib/primitive/index.d.ts +7 -0
- package/lib/primitive/index.js +17 -0
- package/lib/primitive/index.spec.d.ts +1 -0
- package/lib/primitive/index.spec.js +62 -0
- package/lib/primitive/is-float.d.ts +6 -0
- package/lib/primitive/is-float.js +12 -0
- package/lib/primitive/is-int.d.ts +6 -0
- package/lib/primitive/is-int.js +12 -0
- package/lib/primitive/is-non-zero-uint.d.ts +6 -0
- package/lib/primitive/is-non-zero-uint.js +12 -0
- package/lib/primitive/is-quantity.d.ts +7 -0
- package/lib/primitive/is-quantity.js +41 -0
- package/lib/primitive/is-uint.d.ts +8 -0
- package/lib/primitive/is-uint.js +19 -0
- package/lib/primitive/range.d.ts +8 -0
- package/lib/primitive/range.js +18 -0
- package/lib/primitive/split-unit.d.ts +9 -0
- package/lib/primitive/split-unit.js +24 -0
- package/lib/rfc/is-bcp-47.d.ts +5 -0
- package/lib/rfc/is-bcp-47.js +14 -0
- package/lib/rfc/is-bcp-47.spec.d.ts +1 -0
- package/lib/rfc/is-bcp-47.spec.js +19 -0
- package/lib/token/index.d.ts +2 -0
- package/lib/token/index.js +7 -0
- package/lib/token/token-collection.d.ts +62 -0
- package/lib/token/token-collection.js +383 -0
- package/lib/token/token-collection.spec.d.ts +1 -0
- package/lib/token/token-collection.spec.js +144 -0
- package/lib/token/token.d.ts +55 -0
- package/lib/token/token.js +128 -0
- package/lib/types.d.ts +78 -0
- package/lib/types.js +2 -0
- package/lib/types.schema.d.ts +54 -0
- package/lib/types.schema.js +8 -0
- package/lib/w3c/check-serialized-permissions-policy.d.ts +15 -0
- package/lib/w3c/check-serialized-permissions-policy.js +251 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +1 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.js +26 -0
- package/lib/whatwg/check-autocomplete.d.ts +5 -0
- package/lib/whatwg/check-autocomplete.js +282 -0
- package/lib/whatwg/check-autocomplete.spec.d.ts +1 -0
- package/lib/whatwg/check-autocomplete.spec.js +370 -0
- package/lib/whatwg/check-datetime/date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/date-string.js +31 -0
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +3 -0
- package/lib/whatwg/check-datetime/datetime-tokens.js +509 -0
- package/lib/whatwg/check-datetime/duration-string.d.ts +6 -0
- package/lib/whatwg/check-datetime/duration-string.js +322 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +66 -0
- package/lib/whatwg/check-datetime/index.d.ts +5 -0
- package/lib/whatwg/check-datetime/index.js +35 -0
- package/lib/whatwg/check-datetime/index.spec.d.ts +1 -0
- package/lib/whatwg/check-datetime/index.spec.js +318 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +9 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +116 -0
- package/lib/whatwg/check-datetime/month-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/month-string.js +20 -0
- package/lib/whatwg/check-datetime/time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/time-string.js +35 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +7 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +75 -0
- package/lib/whatwg/check-datetime/week-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/week-string.js +29 -0
- package/lib/whatwg/check-datetime/year-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/year-string.js +20 -0
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/yearless-date-string.js +27 -0
- package/lib/whatwg/check-mime-type.d.ts +11 -0
- package/lib/whatwg/check-mime-type.js +42 -0
- package/lib/whatwg/check-mime-type.spec.d.ts +1 -0
- package/lib/whatwg/check-mime-type.spec.js +58 -0
- package/lib/whatwg/is-abs-url.d.ts +15 -0
- package/lib/whatwg/is-abs-url.js +34 -0
- package/lib/whatwg/is-abs-url.spec.d.ts +1 -0
- package/lib/whatwg/is-abs-url.spec.js +8 -0
- package/lib/whatwg/is-browser-context-name.d.ts +11 -0
- package/lib/whatwg/is-browser-context-name.js +18 -0
- package/lib/whatwg/is-custom-element-name.d.ts +22 -0
- package/lib/whatwg/is-custom-element-name.js +77 -0
- package/lib/whatwg/is-itemprop-name.d.ts +12 -0
- package/lib/whatwg/is-itemprop-name.js +19 -0
- package/package.json +33 -0
- package/src/check-multi-types.ts +35 -0
- package/src/check.spec.ts +62 -0
- package/src/check.ts +48 -0
- package/src/css-syntax.spec.ts +167 -0
- package/src/css-syntax.ts +177 -0
- package/src/debug.ts +3 -0
- package/src/defs.ts +810 -0
- package/src/enum.ts +25 -0
- package/src/get-candicate.ts +24 -0
- package/src/index.ts +3 -0
- package/src/keyword-type.ts +52 -0
- package/src/list.spec.ts +133 -0
- package/src/list.ts +34 -0
- package/src/match-result.ts +49 -0
- package/src/number.ts +46 -0
- package/src/primitive/index.spec.ts +65 -0
- package/src/primitive/index.ts +7 -0
- package/src/primitive/is-float.ts +8 -0
- package/src/primitive/is-int.ts +8 -0
- package/src/primitive/is-non-zero-uint.ts +8 -0
- package/src/primitive/is-quantity.ts +38 -0
- package/src/primitive/is-uint.ts +15 -0
- package/src/primitive/range.ts +14 -0
- package/src/primitive/split-unit.ts +20 -0
- package/src/rfc/is-bcp-47.spec.ts +23 -0
- package/src/rfc/is-bcp-47.ts +13 -0
- package/src/token/index.ts +2 -0
- package/src/token/token-collection.spec.ts +147 -0
- package/src/token/token-collection.ts +444 -0
- package/src/token/token.ts +144 -0
- package/src/types.schema.ts +1059 -0
- package/src/types.ts +118 -0
- package/src/w3c/check-serialized-permissions-policy.spec.ts +27 -0
- package/src/w3c/check-serialized-permissions-policy.ts +287 -0
- package/src/whatwg/check-autocomplete.spec.ts +378 -0
- package/src/whatwg/check-autocomplete.ts +324 -0
- package/src/whatwg/check-datetime/date-string.ts +44 -0
- package/src/whatwg/check-datetime/datetime-tokens.ts +600 -0
- package/src/whatwg/check-datetime/duration-string.ts +399 -0
- package/src/whatwg/check-datetime/global-date-and-time-string.ts +96 -0
- package/src/whatwg/check-datetime/index.spec.ts +333 -0
- package/src/whatwg/check-datetime/index.ts +36 -0
- package/src/whatwg/check-datetime/local-date-and-time-string.ts +163 -0
- package/src/whatwg/check-datetime/month-string.ts +31 -0
- package/src/whatwg/check-datetime/time-string.ts +49 -0
- package/src/whatwg/check-datetime/time-zone-offset-string.ts +93 -0
- package/src/whatwg/check-datetime/week-string.ts +41 -0
- package/src/whatwg/check-datetime/year-string.ts +26 -0
- package/src/whatwg/check-datetime/yearless-date-string.ts +38 -0
- package/src/whatwg/check-mime-type.spec.ts +59 -0
- package/src/whatwg/check-mime-type.ts +46 -0
- package/src/whatwg/is-abs-url.spec.ts +8 -0
- package/src/whatwg/is-abs-url.ts +31 -0
- package/src/whatwg/is-browser-context-name.ts +16 -0
- package/src/whatwg/is-custom-element-name.ts +76 -0
- package/src/whatwg/is-itemprop-name.ts +17 -0
- package/tsconfig.test.json +3 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types.schema.json +1092 -0
package/lib/enum.d.ts
ADDED
package/lib/enum.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkEnum = void 0;
|
|
4
|
+
const match_result_1 = require("./match-result");
|
|
5
|
+
function checkEnum(value, type, ref) {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
const disallowToSurroundBySpaces = (_a = type.disallowToSurroundBySpaces) !== null && _a !== void 0 ? _a : true;
|
|
8
|
+
const caseInsensitive = (_b = type.caseInsensitive) !== null && _b !== void 0 ? _b : true;
|
|
9
|
+
if (!disallowToSurroundBySpaces) {
|
|
10
|
+
value = value.trim();
|
|
11
|
+
}
|
|
12
|
+
let values = type.enum;
|
|
13
|
+
if (caseInsensitive) {
|
|
14
|
+
value = value.toLowerCase();
|
|
15
|
+
values = type.enum.map(v => v.toLowerCase());
|
|
16
|
+
}
|
|
17
|
+
const res = values.includes(value);
|
|
18
|
+
if (res) {
|
|
19
|
+
return (0, match_result_1.matched)();
|
|
20
|
+
}
|
|
21
|
+
return (0, match_result_1.unmatched)(value, 'doesnt-exist-in-enum', {
|
|
22
|
+
ref,
|
|
23
|
+
expects: type.enum.map(value => ({ type: 'const', value })),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.checkEnum = checkEnum;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCandicate = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
|
|
6
|
+
function getCandicate(value, ...candicates) {
|
|
7
|
+
if (!value) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const list = candicates.flat(2).filter((s) => !!s);
|
|
11
|
+
let candicate;
|
|
12
|
+
let maxRatio = 0;
|
|
13
|
+
list.forEach(word => {
|
|
14
|
+
const dist = (0, leven_1.default)(value.trim().toLowerCase(), word.trim().toLowerCase());
|
|
15
|
+
const ratio = 1 - dist / word.length;
|
|
16
|
+
if (0.5 <= ratio && maxRatio < ratio) {
|
|
17
|
+
candicate = word;
|
|
18
|
+
}
|
|
19
|
+
maxRatio = Math.max(ratio, maxRatio);
|
|
20
|
+
});
|
|
21
|
+
if (value === candicate) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
return candicate;
|
|
25
|
+
}
|
|
26
|
+
exports.getCandicate = getCandicate;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./whatwg/is-custom-element-name"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./check"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./types"), exports);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkKeywordType = void 0;
|
|
4
|
+
const check_1 = require("./check");
|
|
5
|
+
const css_syntax_1 = require("./css-syntax");
|
|
6
|
+
const debug_1 = require("./debug");
|
|
7
|
+
const defs_1 = require("./defs");
|
|
8
|
+
const resultCache = new Map();
|
|
9
|
+
const CACHE_KEY_PREFIX = '@markuplint/types/checkKeywordType/cache:::';
|
|
10
|
+
function checkKeywordType(value, type, cache = true) {
|
|
11
|
+
const key = `${CACHE_KEY_PREFIX}${value}${type}`;
|
|
12
|
+
if (cache) {
|
|
13
|
+
const cachedResult = resultCache.get(key);
|
|
14
|
+
if (cachedResult) {
|
|
15
|
+
(0, debug_1.log)('Restore cache: %s / %s', value, type);
|
|
16
|
+
return cachedResult;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const result = _checkKeywordType(value, type);
|
|
20
|
+
if (cache) {
|
|
21
|
+
resultCache.set(key, result);
|
|
22
|
+
(0, debug_1.log)('Cache checking: %O', {
|
|
23
|
+
input: value,
|
|
24
|
+
type,
|
|
25
|
+
key,
|
|
26
|
+
result: result,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
exports.checkKeywordType = checkKeywordType;
|
|
32
|
+
function _checkKeywordType(value, type) {
|
|
33
|
+
const def = defs_1.types[type];
|
|
34
|
+
if (!def) {
|
|
35
|
+
return (0, css_syntax_1.cssSyntaxMatch)(value, type);
|
|
36
|
+
}
|
|
37
|
+
const matched = (0, check_1.isCSSSyntax)(def) ? (0, css_syntax_1.cssSyntaxMatch)(value, def) : def.is(value);
|
|
38
|
+
if (!matched.matched) {
|
|
39
|
+
return {
|
|
40
|
+
...matched,
|
|
41
|
+
ref: matched.ref || def.ref,
|
|
42
|
+
expects: matched.expects || def.expects,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return matched;
|
|
46
|
+
}
|
package/lib/list.d.ts
ADDED
package/lib/list.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkList = void 0;
|
|
4
|
+
const check_1 = require("./check");
|
|
5
|
+
const match_result_1 = require("./match-result");
|
|
6
|
+
const token_1 = require("./token");
|
|
7
|
+
function checkList(value, type, ref, cache = true) {
|
|
8
|
+
const tokens = new token_1.TokenCollection(value, type);
|
|
9
|
+
const matches = tokens.check({ ref });
|
|
10
|
+
if (!matches.matched) {
|
|
11
|
+
return matches;
|
|
12
|
+
}
|
|
13
|
+
const identTokens = tokens.getIdentTokens();
|
|
14
|
+
for (const token of identTokens) {
|
|
15
|
+
const res = (0, check_1.check)(token.value, type.token, ref, cache);
|
|
16
|
+
if (!res.matched) {
|
|
17
|
+
const { offset, line, column } = token_1.Token.shiftLocation(token, res.offset);
|
|
18
|
+
return {
|
|
19
|
+
...res,
|
|
20
|
+
partName: res.partName || 'the content of the list',
|
|
21
|
+
offset,
|
|
22
|
+
line,
|
|
23
|
+
column,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return (0, match_result_1.matched)();
|
|
28
|
+
}
|
|
29
|
+
exports.checkList = checkList;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/list.spec.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const list_1 = require("./list");
|
|
4
|
+
test('Zero space', () => {
|
|
5
|
+
const type = { token: 'Zero', separator: 'space' };
|
|
6
|
+
expect((0, list_1.checkList)('0', type).matched).toBe(true);
|
|
7
|
+
expect((0, list_1.checkList)(' 0 ', type).matched).toBe(true);
|
|
8
|
+
expect((0, list_1.checkList)(' 0 0', type).matched).toBe(true);
|
|
9
|
+
expect((0, list_1.checkList)(' 0 0 ', type).matched).toBe(true);
|
|
10
|
+
expect((0, list_1.checkList)('0 0 0 ', type).matched).toBe(true);
|
|
11
|
+
expect((0, list_1.checkList)('0 0 0 0', type).matched).toBe(true);
|
|
12
|
+
expect((0, list_1.checkList)('0 0 0 0', type).matched).toBe(true);
|
|
13
|
+
expect((0, list_1.checkList)('1', type).matched).toBe(false);
|
|
14
|
+
expect((0, list_1.checkList)(' 1 ', type).matched).toBe(false);
|
|
15
|
+
expect((0, list_1.checkList)(' 1 0', type).matched).toBe(false);
|
|
16
|
+
expect((0, list_1.checkList)(' 1 0 ', type).matched).toBe(false);
|
|
17
|
+
expect((0, list_1.checkList)('0 1 0 ', type).matched).toBe(false);
|
|
18
|
+
expect((0, list_1.checkList)('0 1 0 0', type).matched).toBe(false);
|
|
19
|
+
expect((0, list_1.checkList)('0 1 0 0', type).matched).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
test('Zero comma', () => {
|
|
22
|
+
const type = { token: 'Zero', separator: 'comma' };
|
|
23
|
+
expect((0, list_1.checkList)('0', type).matched).toBe(true);
|
|
24
|
+
expect((0, list_1.checkList)(' 0 ', type).matched).toBe(true);
|
|
25
|
+
expect((0, list_1.checkList)(' 0 0', type).matched).toBe(false);
|
|
26
|
+
expect((0, list_1.checkList)(' 0,0', type).matched).toBe(true);
|
|
27
|
+
expect((0, list_1.checkList)(' 0,,0 ', type).matched).toBe(false);
|
|
28
|
+
expect((0, list_1.checkList)('0 0 0 ', type).matched).toBe(false);
|
|
29
|
+
expect((0, list_1.checkList)(' 0,0,0,0 ', type).matched).toBe(true);
|
|
30
|
+
expect((0, list_1.checkList)('0, 0, 0 , 0', type).matched).toBe(true);
|
|
31
|
+
expect((0, list_1.checkList)('0,0,0,0,', type).matched).toBe(false);
|
|
32
|
+
expect((0, list_1.checkList)(',0,0,0,0', type).matched).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
test('Zero comma (disallowToSurroundBySpaces)', () => {
|
|
35
|
+
const type = { token: 'Zero', separator: 'comma', disallowToSurroundBySpaces: true };
|
|
36
|
+
expect((0, list_1.checkList)('0', type).matched).toBe(true);
|
|
37
|
+
expect((0, list_1.checkList)(' 0 ', type).matched).toBe(false);
|
|
38
|
+
expect((0, list_1.checkList)(' 0 0', type).matched).toBe(false);
|
|
39
|
+
expect((0, list_1.checkList)(' 0,0', type).matched).toBe(false);
|
|
40
|
+
expect((0, list_1.checkList)(' 0,,0 ', type).matched).toBe(false);
|
|
41
|
+
expect((0, list_1.checkList)('0 0 0 ', type).matched).toBe(false);
|
|
42
|
+
expect((0, list_1.checkList)(' 0,0,0,0 ', type).matched).toBe(false);
|
|
43
|
+
expect((0, list_1.checkList)('0, 0, 0 , 0', type).matched).toBe(false);
|
|
44
|
+
expect((0, list_1.checkList)('0,0,0,0', type).matched).toBe(true);
|
|
45
|
+
expect((0, list_1.checkList)('0,0,0,0,', type).matched).toBe(false);
|
|
46
|
+
expect((0, list_1.checkList)(',0,0,0,0', type).matched).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
test('Location of the token', () => {
|
|
49
|
+
const type = { token: 'Accept', separator: 'comma' };
|
|
50
|
+
expect((0, list_1.checkList)('x/y;a=b', type)).toStrictEqual({
|
|
51
|
+
candicate: 'x/y',
|
|
52
|
+
column: 4,
|
|
53
|
+
expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
|
|
54
|
+
length: 4,
|
|
55
|
+
line: 1,
|
|
56
|
+
matched: false,
|
|
57
|
+
offset: 3,
|
|
58
|
+
raw: ';a=b',
|
|
59
|
+
reason: 'extra-token',
|
|
60
|
+
partName: 'the content of the list',
|
|
61
|
+
ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
|
|
62
|
+
});
|
|
63
|
+
expect((0, list_1.checkList)('x/y,x/y;a=b', type)).toStrictEqual({
|
|
64
|
+
candicate: 'x/y',
|
|
65
|
+
column: 8,
|
|
66
|
+
expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
|
|
67
|
+
length: 4,
|
|
68
|
+
line: 1,
|
|
69
|
+
matched: false,
|
|
70
|
+
offset: 7,
|
|
71
|
+
raw: ';a=b',
|
|
72
|
+
reason: 'extra-token',
|
|
73
|
+
partName: 'the content of the list',
|
|
74
|
+
ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
|
|
75
|
+
});
|
|
76
|
+
expect((0, list_1.checkList)('x/y\n,\nx/y;a=b', type)).toStrictEqual({
|
|
77
|
+
candicate: 'x/y',
|
|
78
|
+
column: 4,
|
|
79
|
+
expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
|
|
80
|
+
length: 4,
|
|
81
|
+
line: 3,
|
|
82
|
+
matched: false,
|
|
83
|
+
offset: 9,
|
|
84
|
+
raw: ';a=b',
|
|
85
|
+
reason: 'extra-token',
|
|
86
|
+
partName: 'the content of the list',
|
|
87
|
+
ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
test('Expected comma', () => {
|
|
91
|
+
const type = { token: 'Accept', separator: 'comma' };
|
|
92
|
+
expect((0, list_1.checkList)('x/y,', type)).toStrictEqual({
|
|
93
|
+
column: 4,
|
|
94
|
+
expects: undefined,
|
|
95
|
+
length: 1,
|
|
96
|
+
line: 1,
|
|
97
|
+
matched: false,
|
|
98
|
+
offset: 3,
|
|
99
|
+
raw: ',',
|
|
100
|
+
reason: 'extra-token',
|
|
101
|
+
ref: null,
|
|
102
|
+
});
|
|
103
|
+
expect((0, list_1.checkList)('x/y,,x/y', type)).toStrictEqual({
|
|
104
|
+
column: 5,
|
|
105
|
+
expects: undefined,
|
|
106
|
+
length: 1,
|
|
107
|
+
line: 1,
|
|
108
|
+
matched: false,
|
|
109
|
+
offset: 4,
|
|
110
|
+
raw: ',',
|
|
111
|
+
reason: 'unexpected-comma',
|
|
112
|
+
ref: null,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
test('Missing comma', () => {
|
|
116
|
+
const type = { token: 'Accept', separator: 'comma' };
|
|
117
|
+
expect((0, list_1.checkList)('a/b,x/y a/z', type)).toStrictEqual({
|
|
118
|
+
column: 9,
|
|
119
|
+
candicate: ',a/z',
|
|
120
|
+
expects: undefined,
|
|
121
|
+
length: 3,
|
|
122
|
+
line: 1,
|
|
123
|
+
matched: false,
|
|
124
|
+
offset: 8,
|
|
125
|
+
raw: 'a/z',
|
|
126
|
+
reason: 'missing-comma',
|
|
127
|
+
ref: null,
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FormattedPrimitiveTypeCheck, MatchedResult, UnmatchedResult, UnmatchedResultOptions, UnmatchedResultReason } from './types';
|
|
2
|
+
export declare function matches(checker: FormattedPrimitiveTypeCheck, options?: UnmatchedResultOptions & {
|
|
3
|
+
ref?: string;
|
|
4
|
+
reason?: UnmatchedResultReason;
|
|
5
|
+
}): (value: string) => UnmatchedResult | MatchedResult;
|
|
6
|
+
export declare function matched(): MatchedResult;
|
|
7
|
+
export declare function unmatched(value: string, reason?: UnmatchedResultReason, options?: UnmatchedResultOptions & {
|
|
8
|
+
ref?: string;
|
|
9
|
+
}): UnmatchedResult;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unmatched = exports.matched = exports.matches = void 0;
|
|
4
|
+
function matches(checker, options) {
|
|
5
|
+
return (value) => {
|
|
6
|
+
const valid = checker(value);
|
|
7
|
+
if (!valid && !value) {
|
|
8
|
+
return unmatched(value, 'empty-token', options);
|
|
9
|
+
}
|
|
10
|
+
return valid ? matched() : unmatched(value, options === null || options === void 0 ? void 0 : options.reason, options);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.matches = matches;
|
|
14
|
+
function matched() {
|
|
15
|
+
return {
|
|
16
|
+
matched: true,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.matched = matched;
|
|
20
|
+
function unmatched(value, reason, options) {
|
|
21
|
+
return {
|
|
22
|
+
...options,
|
|
23
|
+
matched: false,
|
|
24
|
+
ref: (options === null || options === void 0 ? void 0 : options.ref) || null,
|
|
25
|
+
raw: value,
|
|
26
|
+
offset: 0,
|
|
27
|
+
length: value.length,
|
|
28
|
+
line: 1,
|
|
29
|
+
column: 1,
|
|
30
|
+
reason: reason !== null && reason !== void 0 ? reason : 'syntax-error',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.unmatched = unmatched;
|
package/lib/number.d.ts
ADDED
package/lib/number.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkNumber = void 0;
|
|
4
|
+
const match_result_1 = require("./match-result");
|
|
5
|
+
const primitive_1 = require("./primitive");
|
|
6
|
+
function checkNumber(value, type, ref) {
|
|
7
|
+
if (!value) {
|
|
8
|
+
return (0, match_result_1.unmatched)(value, 'empty-token');
|
|
9
|
+
}
|
|
10
|
+
const syntaxMatched = type.type === 'float' ? (0, primitive_1.isFloat)(value) : (0, primitive_1.isInt)(value);
|
|
11
|
+
if (syntaxMatched) {
|
|
12
|
+
const n = parseFloat(value);
|
|
13
|
+
if (!isFinite(n)) {
|
|
14
|
+
return (0, match_result_1.unmatched)(value, 'unexpected-token');
|
|
15
|
+
}
|
|
16
|
+
const clampable = type.clampable;
|
|
17
|
+
const isInt = type.type === 'integer';
|
|
18
|
+
if (type.gt != null && type.gt < n) {
|
|
19
|
+
return (0, match_result_1.unmatched)(value, 'out-of-range', {
|
|
20
|
+
candicate: clampable && isInt ? `${type.gt + 1}` : undefined,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (type.gte != null && type.gte <= n) {
|
|
24
|
+
return (0, match_result_1.unmatched)(value, 'out-of-range', {
|
|
25
|
+
candicate: clampable ? `${type.gte}` : undefined,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (type.lt != null && n < type.lt) {
|
|
29
|
+
return (0, match_result_1.unmatched)(value, 'out-of-range', {
|
|
30
|
+
candicate: clampable && isInt ? `${type.lt - 1}` : undefined,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (type.lte != null && n <= type.lte) {
|
|
34
|
+
return (0, match_result_1.unmatched)(value, 'out-of-range', {
|
|
35
|
+
candicate: clampable ? `${type.lte}` : undefined,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return syntaxMatched ? (0, match_result_1.matched)() : (0, match_result_1.unmatched)(value, 'unexpected-token');
|
|
40
|
+
}
|
|
41
|
+
exports.checkNumber = checkNumber;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { isInt } from './is-int';
|
|
2
|
+
export { isUint } from './is-uint';
|
|
3
|
+
export { isFloat } from './is-float';
|
|
4
|
+
export { isNonZeroUint } from './is-non-zero-uint';
|
|
5
|
+
export { isQuantity } from './is-quantity';
|
|
6
|
+
export { splitUnit } from './split-unit';
|
|
7
|
+
export { range } from './range';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.range = exports.splitUnit = exports.isQuantity = exports.isNonZeroUint = exports.isFloat = exports.isUint = exports.isInt = void 0;
|
|
4
|
+
var is_int_1 = require("./is-int");
|
|
5
|
+
Object.defineProperty(exports, "isInt", { enumerable: true, get: function () { return is_int_1.isInt; } });
|
|
6
|
+
var is_uint_1 = require("./is-uint");
|
|
7
|
+
Object.defineProperty(exports, "isUint", { enumerable: true, get: function () { return is_uint_1.isUint; } });
|
|
8
|
+
var is_float_1 = require("./is-float");
|
|
9
|
+
Object.defineProperty(exports, "isFloat", { enumerable: true, get: function () { return is_float_1.isFloat; } });
|
|
10
|
+
var is_non_zero_uint_1 = require("./is-non-zero-uint");
|
|
11
|
+
Object.defineProperty(exports, "isNonZeroUint", { enumerable: true, get: function () { return is_non_zero_uint_1.isNonZeroUint; } });
|
|
12
|
+
var is_quantity_1 = require("./is-quantity");
|
|
13
|
+
Object.defineProperty(exports, "isQuantity", { enumerable: true, get: function () { return is_quantity_1.isQuantity; } });
|
|
14
|
+
var split_unit_1 = require("./split-unit");
|
|
15
|
+
Object.defineProperty(exports, "splitUnit", { enumerable: true, get: function () { return split_unit_1.splitUnit; } });
|
|
16
|
+
var range_1 = require("./range");
|
|
17
|
+
Object.defineProperty(exports, "range", { enumerable: true, get: function () { return range_1.range; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
4
|
+
test('int', () => {
|
|
5
|
+
expect((0, _1.isInt)('0')).toBe(true);
|
|
6
|
+
expect((0, _1.isInt)('1')).toBe(true);
|
|
7
|
+
expect((0, _1.isInt)('-0')).toBe(true);
|
|
8
|
+
expect((0, _1.isInt)('-1')).toBe(true);
|
|
9
|
+
expect((0, _1.isInt)('10')).toBe(true);
|
|
10
|
+
expect((0, _1.isInt)('100')).toBe(true);
|
|
11
|
+
expect((0, _1.isInt)('1.00')).toBe(false);
|
|
12
|
+
expect((0, _1.isInt)('.001')).toBe(false);
|
|
13
|
+
expect((0, _1.isInt)(' 1 ')).toBe(false);
|
|
14
|
+
expect((0, _1.isInt)('- 1')).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
test('uint', () => {
|
|
17
|
+
expect((0, _1.isUint)('0')).toBe(true);
|
|
18
|
+
expect((0, _1.isUint)('1')).toBe(true);
|
|
19
|
+
expect((0, _1.isUint)('10')).toBe(true);
|
|
20
|
+
expect((0, _1.isUint)('100')).toBe(true);
|
|
21
|
+
expect((0, _1.isUint)('-0')).toBe(false);
|
|
22
|
+
expect((0, _1.isUint)('-1')).toBe(false);
|
|
23
|
+
expect((0, _1.isUint)('1.00')).toBe(false);
|
|
24
|
+
expect((0, _1.isUint)('.001')).toBe(false);
|
|
25
|
+
expect((0, _1.isUint)(' 1 ')).toBe(false);
|
|
26
|
+
expect((0, _1.isUint)('- 1')).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
test('float', () => {
|
|
29
|
+
expect((0, _1.isFloat)('0')).toBe(true);
|
|
30
|
+
expect((0, _1.isFloat)('1')).toBe(true);
|
|
31
|
+
expect((0, _1.isFloat)('10')).toBe(true);
|
|
32
|
+
expect((0, _1.isFloat)('100')).toBe(true);
|
|
33
|
+
expect((0, _1.isFloat)('-0')).toBe(true);
|
|
34
|
+
expect((0, _1.isFloat)('-1')).toBe(true);
|
|
35
|
+
expect((0, _1.isFloat)('1.00')).toBe(true);
|
|
36
|
+
expect((0, _1.isFloat)('.001')).toBe(true);
|
|
37
|
+
expect((0, _1.isFloat)(' 1 ')).toBe(false);
|
|
38
|
+
expect((0, _1.isFloat)('- 1')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
test('nonZeroUint', () => {
|
|
41
|
+
expect((0, _1.isNonZeroUint)('0')).toBe(false);
|
|
42
|
+
expect((0, _1.isNonZeroUint)('1')).toBe(true);
|
|
43
|
+
expect((0, _1.isNonZeroUint)('10')).toBe(true);
|
|
44
|
+
expect((0, _1.isNonZeroUint)('100')).toBe(true);
|
|
45
|
+
expect((0, _1.isNonZeroUint)('-0')).toBe(false);
|
|
46
|
+
expect((0, _1.isNonZeroUint)('-1')).toBe(false);
|
|
47
|
+
expect((0, _1.isNonZeroUint)('1.00')).toBe(false);
|
|
48
|
+
expect((0, _1.isNonZeroUint)('.001')).toBe(false);
|
|
49
|
+
expect((0, _1.isNonZeroUint)(' 1 ')).toBe(false);
|
|
50
|
+
expect((0, _1.isNonZeroUint)('- 1')).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
test('quantity', () => {
|
|
53
|
+
expect((0, _1.isQuantity)('0px', ['px', 'em'])).toBe(true);
|
|
54
|
+
expect((0, _1.isQuantity)('.5px', ['px', 'em'])).toBe(true);
|
|
55
|
+
expect((0, _1.isQuantity)('1.5em', ['px', 'em'])).toBe(true);
|
|
56
|
+
expect((0, _1.isQuantity)('1.5cm', ['px', 'em'])).toBe(false);
|
|
57
|
+
expect((0, _1.isQuantity)('1.5px', ['px', 'em'], 'int')).toBe(false);
|
|
58
|
+
expect((0, _1.isQuantity)('-5px', ['px', 'em'], 'int')).toBe(true);
|
|
59
|
+
expect((0, _1.isQuantity)('-5px', ['px', 'em'], 'uint')).toBe(false);
|
|
60
|
+
expect((0, _1.isQuantity)('1.12e+21px', ['px', 'em'], 'float')).toBe(true);
|
|
61
|
+
expect((0, _1.isQuantity)('1.12e+21px', ['px', 'em'], 'int')).toBe(false);
|
|
62
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFloat = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#floating-point-numbers
|
|
6
|
+
*
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
function isFloat(value) {
|
|
10
|
+
return value === value.trim() && Number.isFinite(parseFloat(value));
|
|
11
|
+
}
|
|
12
|
+
exports.isFloat = isFloat;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isInt = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#signed-integers
|
|
6
|
+
*
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
function isInt(value) {
|
|
10
|
+
return /^-?[0-9]+$/.test(value);
|
|
11
|
+
}
|
|
12
|
+
exports.isInt = isInt;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNonZeroUint = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Non-negative integer greater than zero
|
|
6
|
+
*
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
function isNonZeroUint(value) {
|
|
10
|
+
return /^[0-9]+$/.test(value) && !/^0+$/.test(value);
|
|
11
|
+
}
|
|
12
|
+
exports.isNonZeroUint = isNonZeroUint;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isQuantity = void 0;
|
|
4
|
+
const is_float_1 = require("./is-float");
|
|
5
|
+
const is_int_1 = require("./is-int");
|
|
6
|
+
const is_uint_1 = require("./is-uint");
|
|
7
|
+
const split_unit_1 = require("./split-unit");
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param value
|
|
11
|
+
* @param units
|
|
12
|
+
* @param numberType
|
|
13
|
+
*/
|
|
14
|
+
function isQuantity(value, units, numberType = 'float') {
|
|
15
|
+
const { num, unit } = (0, split_unit_1.splitUnit)(value);
|
|
16
|
+
if (!units.includes(unit.toLowerCase())) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
switch (numberType) {
|
|
20
|
+
case 'int': {
|
|
21
|
+
if (!(0, is_int_1.isInt)(num)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'uint': {
|
|
27
|
+
if (!(0, is_uint_1.isUint)(num)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'float': {
|
|
33
|
+
if (!(0, is_float_1.isFloat)(num)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
exports.isQuantity = isQuantity;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isUint = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#non-negative-integers
|
|
6
|
+
*
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
function isUint(value, opions) {
|
|
10
|
+
const matched = /^[0-9]+$/.test(value);
|
|
11
|
+
if (matched && opions) {
|
|
12
|
+
const n = parseInt(value, 10);
|
|
13
|
+
if (opions.gt != null) {
|
|
14
|
+
return isFinite(n) && opions.gt < n;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return matched;
|
|
18
|
+
}
|
|
19
|
+
exports.isUint = isUint;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.range = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* It is in the range between `from` and `to`.
|
|
6
|
+
*
|
|
7
|
+
* @param value
|
|
8
|
+
* @param from
|
|
9
|
+
* @param to
|
|
10
|
+
*/
|
|
11
|
+
function range(value, from, to) {
|
|
12
|
+
const num = parseFloat(value);
|
|
13
|
+
if (isNaN(num)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return from <= num && num <= to;
|
|
17
|
+
}
|
|
18
|
+
exports.range = range;
|