@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.
Files changed (177) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +117 -0
  3. package/extends.json +12 -0
  4. package/gen/specific-schema.json +98 -0
  5. package/gen/types.ts +66 -0
  6. package/lib/check-multi-types.d.ts +2 -0
  7. package/lib/check-multi-types.js +29 -0
  8. package/lib/check.d.ts +8 -0
  9. package/lib/check.js +49 -0
  10. package/lib/check.spec.d.ts +1 -0
  11. package/lib/check.spec.js +57 -0
  12. package/lib/css-syntax.d.ts +2 -0
  13. package/lib/css-syntax.js +141 -0
  14. package/lib/css-syntax.spec.d.ts +1 -0
  15. package/lib/css-syntax.spec.js +157 -0
  16. package/lib/debug.d.ts +2 -0
  17. package/lib/debug.js +6 -0
  18. package/lib/defs.d.ts +3 -0
  19. package/lib/defs.js +752 -0
  20. package/lib/enum.d.ts +3 -0
  21. package/lib/enum.js +26 -0
  22. package/lib/get-candicate.d.ts +3 -0
  23. package/lib/get-candicate.js +26 -0
  24. package/lib/index.d.ts +3 -0
  25. package/lib/index.js +6 -0
  26. package/lib/keyword-type.d.ts +3 -0
  27. package/lib/keyword-type.js +46 -0
  28. package/lib/list.d.ts +3 -0
  29. package/lib/list.js +29 -0
  30. package/lib/list.spec.d.ts +1 -0
  31. package/lib/list.spec.js +129 -0
  32. package/lib/match-result.d.ts +9 -0
  33. package/lib/match-result.js +33 -0
  34. package/lib/number.d.ts +3 -0
  35. package/lib/number.js +41 -0
  36. package/lib/primitive/index.d.ts +7 -0
  37. package/lib/primitive/index.js +17 -0
  38. package/lib/primitive/index.spec.d.ts +1 -0
  39. package/lib/primitive/index.spec.js +62 -0
  40. package/lib/primitive/is-float.d.ts +6 -0
  41. package/lib/primitive/is-float.js +12 -0
  42. package/lib/primitive/is-int.d.ts +6 -0
  43. package/lib/primitive/is-int.js +12 -0
  44. package/lib/primitive/is-non-zero-uint.d.ts +6 -0
  45. package/lib/primitive/is-non-zero-uint.js +12 -0
  46. package/lib/primitive/is-quantity.d.ts +7 -0
  47. package/lib/primitive/is-quantity.js +41 -0
  48. package/lib/primitive/is-uint.d.ts +8 -0
  49. package/lib/primitive/is-uint.js +19 -0
  50. package/lib/primitive/range.d.ts +8 -0
  51. package/lib/primitive/range.js +18 -0
  52. package/lib/primitive/split-unit.d.ts +9 -0
  53. package/lib/primitive/split-unit.js +24 -0
  54. package/lib/rfc/is-bcp-47.d.ts +5 -0
  55. package/lib/rfc/is-bcp-47.js +14 -0
  56. package/lib/rfc/is-bcp-47.spec.d.ts +1 -0
  57. package/lib/rfc/is-bcp-47.spec.js +19 -0
  58. package/lib/token/index.d.ts +2 -0
  59. package/lib/token/index.js +7 -0
  60. package/lib/token/token-collection.d.ts +62 -0
  61. package/lib/token/token-collection.js +383 -0
  62. package/lib/token/token-collection.spec.d.ts +1 -0
  63. package/lib/token/token-collection.spec.js +144 -0
  64. package/lib/token/token.d.ts +55 -0
  65. package/lib/token/token.js +128 -0
  66. package/lib/types.d.ts +78 -0
  67. package/lib/types.js +2 -0
  68. package/lib/types.schema.d.ts +54 -0
  69. package/lib/types.schema.js +8 -0
  70. package/lib/w3c/check-serialized-permissions-policy.d.ts +15 -0
  71. package/lib/w3c/check-serialized-permissions-policy.js +251 -0
  72. package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +1 -0
  73. package/lib/w3c/check-serialized-permissions-policy.spec.js +26 -0
  74. package/lib/whatwg/check-autocomplete.d.ts +5 -0
  75. package/lib/whatwg/check-autocomplete.js +282 -0
  76. package/lib/whatwg/check-autocomplete.spec.d.ts +1 -0
  77. package/lib/whatwg/check-autocomplete.spec.js +370 -0
  78. package/lib/whatwg/check-datetime/date-string.d.ts +5 -0
  79. package/lib/whatwg/check-datetime/date-string.js +31 -0
  80. package/lib/whatwg/check-datetime/datetime-tokens.d.ts +3 -0
  81. package/lib/whatwg/check-datetime/datetime-tokens.js +509 -0
  82. package/lib/whatwg/check-datetime/duration-string.d.ts +6 -0
  83. package/lib/whatwg/check-datetime/duration-string.js +322 -0
  84. package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +5 -0
  85. package/lib/whatwg/check-datetime/global-date-and-time-string.js +66 -0
  86. package/lib/whatwg/check-datetime/index.d.ts +5 -0
  87. package/lib/whatwg/check-datetime/index.js +35 -0
  88. package/lib/whatwg/check-datetime/index.spec.d.ts +1 -0
  89. package/lib/whatwg/check-datetime/index.spec.js +318 -0
  90. package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +9 -0
  91. package/lib/whatwg/check-datetime/local-date-and-time-string.js +116 -0
  92. package/lib/whatwg/check-datetime/month-string.d.ts +5 -0
  93. package/lib/whatwg/check-datetime/month-string.js +20 -0
  94. package/lib/whatwg/check-datetime/time-string.d.ts +5 -0
  95. package/lib/whatwg/check-datetime/time-string.js +35 -0
  96. package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +7 -0
  97. package/lib/whatwg/check-datetime/time-zone-offset-string.js +75 -0
  98. package/lib/whatwg/check-datetime/week-string.d.ts +5 -0
  99. package/lib/whatwg/check-datetime/week-string.js +29 -0
  100. package/lib/whatwg/check-datetime/year-string.d.ts +5 -0
  101. package/lib/whatwg/check-datetime/year-string.js +20 -0
  102. package/lib/whatwg/check-datetime/yearless-date-string.d.ts +5 -0
  103. package/lib/whatwg/check-datetime/yearless-date-string.js +27 -0
  104. package/lib/whatwg/check-mime-type.d.ts +11 -0
  105. package/lib/whatwg/check-mime-type.js +42 -0
  106. package/lib/whatwg/check-mime-type.spec.d.ts +1 -0
  107. package/lib/whatwg/check-mime-type.spec.js +58 -0
  108. package/lib/whatwg/is-abs-url.d.ts +15 -0
  109. package/lib/whatwg/is-abs-url.js +34 -0
  110. package/lib/whatwg/is-abs-url.spec.d.ts +1 -0
  111. package/lib/whatwg/is-abs-url.spec.js +8 -0
  112. package/lib/whatwg/is-browser-context-name.d.ts +11 -0
  113. package/lib/whatwg/is-browser-context-name.js +18 -0
  114. package/lib/whatwg/is-custom-element-name.d.ts +22 -0
  115. package/lib/whatwg/is-custom-element-name.js +77 -0
  116. package/lib/whatwg/is-itemprop-name.d.ts +12 -0
  117. package/lib/whatwg/is-itemprop-name.js +19 -0
  118. package/package.json +33 -0
  119. package/src/check-multi-types.ts +35 -0
  120. package/src/check.spec.ts +62 -0
  121. package/src/check.ts +48 -0
  122. package/src/css-syntax.spec.ts +167 -0
  123. package/src/css-syntax.ts +177 -0
  124. package/src/debug.ts +3 -0
  125. package/src/defs.ts +810 -0
  126. package/src/enum.ts +25 -0
  127. package/src/get-candicate.ts +24 -0
  128. package/src/index.ts +3 -0
  129. package/src/keyword-type.ts +52 -0
  130. package/src/list.spec.ts +133 -0
  131. package/src/list.ts +34 -0
  132. package/src/match-result.ts +49 -0
  133. package/src/number.ts +46 -0
  134. package/src/primitive/index.spec.ts +65 -0
  135. package/src/primitive/index.ts +7 -0
  136. package/src/primitive/is-float.ts +8 -0
  137. package/src/primitive/is-int.ts +8 -0
  138. package/src/primitive/is-non-zero-uint.ts +8 -0
  139. package/src/primitive/is-quantity.ts +38 -0
  140. package/src/primitive/is-uint.ts +15 -0
  141. package/src/primitive/range.ts +14 -0
  142. package/src/primitive/split-unit.ts +20 -0
  143. package/src/rfc/is-bcp-47.spec.ts +23 -0
  144. package/src/rfc/is-bcp-47.ts +13 -0
  145. package/src/token/index.ts +2 -0
  146. package/src/token/token-collection.spec.ts +147 -0
  147. package/src/token/token-collection.ts +444 -0
  148. package/src/token/token.ts +144 -0
  149. package/src/types.schema.ts +1059 -0
  150. package/src/types.ts +118 -0
  151. package/src/w3c/check-serialized-permissions-policy.spec.ts +27 -0
  152. package/src/w3c/check-serialized-permissions-policy.ts +287 -0
  153. package/src/whatwg/check-autocomplete.spec.ts +378 -0
  154. package/src/whatwg/check-autocomplete.ts +324 -0
  155. package/src/whatwg/check-datetime/date-string.ts +44 -0
  156. package/src/whatwg/check-datetime/datetime-tokens.ts +600 -0
  157. package/src/whatwg/check-datetime/duration-string.ts +399 -0
  158. package/src/whatwg/check-datetime/global-date-and-time-string.ts +96 -0
  159. package/src/whatwg/check-datetime/index.spec.ts +333 -0
  160. package/src/whatwg/check-datetime/index.ts +36 -0
  161. package/src/whatwg/check-datetime/local-date-and-time-string.ts +163 -0
  162. package/src/whatwg/check-datetime/month-string.ts +31 -0
  163. package/src/whatwg/check-datetime/time-string.ts +49 -0
  164. package/src/whatwg/check-datetime/time-zone-offset-string.ts +93 -0
  165. package/src/whatwg/check-datetime/week-string.ts +41 -0
  166. package/src/whatwg/check-datetime/year-string.ts +26 -0
  167. package/src/whatwg/check-datetime/yearless-date-string.ts +38 -0
  168. package/src/whatwg/check-mime-type.spec.ts +59 -0
  169. package/src/whatwg/check-mime-type.ts +46 -0
  170. package/src/whatwg/is-abs-url.spec.ts +8 -0
  171. package/src/whatwg/is-abs-url.ts +31 -0
  172. package/src/whatwg/is-browser-context-name.ts +16 -0
  173. package/src/whatwg/is-custom-element-name.ts +76 -0
  174. package/src/whatwg/is-itemprop-name.ts +17 -0
  175. package/tsconfig.test.json +3 -0
  176. package/tsconfig.tsbuildinfo +1 -0
  177. package/types.schema.json +1092 -0
package/lib/enum.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { Result } from './types';
2
+ import type { Enum } from './types.schema';
3
+ export declare function checkEnum(value: string, type: Enum, ref?: string): Result;
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,3 @@
1
+ declare type NullableString = string | null | undefined;
2
+ export declare function getCandicate(value: NullableString, ...candicates: (NullableString | NullableString[])[]): string | undefined;
3
+ export {};
@@ -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
@@ -0,0 +1,3 @@
1
+ export * from './whatwg/is-custom-element-name';
2
+ export * from './check';
3
+ export * from './types';
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,3 @@
1
+ import type { Result } from './types';
2
+ import type { KeywordDefinedType } from './types.schema';
3
+ export declare function checkKeywordType(value: string, type: KeywordDefinedType, cache?: boolean): Result;
@@ -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
@@ -0,0 +1,3 @@
1
+ import type { Result } from './types';
2
+ import type { List } from './types.schema';
3
+ export declare function checkList(value: string, type: List, ref?: string, cache?: boolean): Result;
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 {};
@@ -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;
@@ -0,0 +1,3 @@
1
+ import type { Result } from './types';
2
+ import type { Number } from './types.schema';
3
+ export declare function checkNumber(value: string, type: Number, ref?: string): Result;
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,6 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#floating-point-numbers
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function isFloat(value: string): boolean;
@@ -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,6 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#signed-integers
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function isInt(value: string): boolean;
@@ -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,6 @@
1
+ /**
2
+ * Non-negative integer greater than zero
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function isNonZeroUint(value: string): boolean;
@@ -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,7 @@
1
+ /**
2
+ *
3
+ * @param value
4
+ * @param units
5
+ * @param numberType
6
+ */
7
+ export declare function isQuantity(value: string, units: string[], numberType?: 'int' | 'uint' | 'float'): boolean;
@@ -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,8 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#non-negative-integers
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function isUint(value: string, opions?: {
7
+ gt?: number;
8
+ }): boolean;
@@ -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,8 @@
1
+ /**
2
+ * It is in the range between `from` and `to`.
3
+ *
4
+ * @param value
5
+ * @param from
6
+ * @param to
7
+ */
8
+ export declare function range(value: string, from: number, to: number): boolean;
@@ -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;