@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/src/enum.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { Result } from './types';
2
+ import type { Enum } from './types.schema';
3
+
4
+ import { matched, unmatched } from './match-result';
5
+
6
+ export function checkEnum(value: string, type: Enum, ref?: string): Result {
7
+ const disallowToSurroundBySpaces = type.disallowToSurroundBySpaces ?? true;
8
+ const caseInsensitive = type.caseInsensitive ?? true;
9
+ if (!disallowToSurroundBySpaces) {
10
+ value = value.trim();
11
+ }
12
+ let values: string[] = 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 matched();
20
+ }
21
+ return unmatched(value, 'doesnt-exist-in-enum', {
22
+ ref,
23
+ expects: type.enum.map(value => ({ type: 'const', value })),
24
+ });
25
+ }
@@ -0,0 +1,24 @@
1
+ import leven from 'leven';
2
+
3
+ type NullableString = string | null | undefined;
4
+
5
+ export function getCandicate(value: NullableString, ...candicates: (NullableString | NullableString[])[]) {
6
+ if (!value) {
7
+ return;
8
+ }
9
+ const list = candicates.flat(2).filter((s): s is string => !!s);
10
+ let candicate: string | undefined;
11
+ let maxRatio = 0;
12
+ list.forEach(word => {
13
+ const dist = leven(value.trim().toLowerCase(), word.trim().toLowerCase());
14
+ const ratio = 1 - dist / word.length;
15
+ if (0.5 <= ratio && maxRatio < ratio) {
16
+ candicate = word;
17
+ }
18
+ maxRatio = Math.max(ratio, maxRatio);
19
+ });
20
+ if (value === candicate) {
21
+ return;
22
+ }
23
+ return candicate;
24
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './whatwg/is-custom-element-name';
2
+ export * from './check';
3
+ export * from './types';
@@ -0,0 +1,52 @@
1
+ import type { Result } from './types';
2
+ import type { KeywordDefinedType, CssSyntax } from './types.schema';
3
+
4
+ import { isCSSSyntax } from './check';
5
+ import { cssSyntaxMatch } from './css-syntax';
6
+ import { log } from './debug';
7
+ import { types } from './defs';
8
+
9
+ const resultCache = new Map<string, Result>();
10
+ const CACHE_KEY_PREFIX = '@markuplint/types/checkKeywordType/cache:::';
11
+
12
+ export function checkKeywordType(value: string, type: KeywordDefinedType, cache = true) {
13
+ const key = `${CACHE_KEY_PREFIX}${value}${type}`;
14
+ if (cache) {
15
+ const cachedResult = resultCache.get(key);
16
+ if (cachedResult) {
17
+ log('Restore cache: %s / %s', value, type);
18
+ return cachedResult;
19
+ }
20
+ }
21
+ const result = _checkKeywordType(value, type);
22
+
23
+ if (cache) {
24
+ resultCache.set(key, result);
25
+ log('Cache checking: %O', {
26
+ input: value,
27
+ type,
28
+ key,
29
+ result: result,
30
+ });
31
+ }
32
+
33
+ return result;
34
+ }
35
+
36
+ function _checkKeywordType(value: string, type: KeywordDefinedType): Result {
37
+ const def = types[type];
38
+ if (!def) {
39
+ return cssSyntaxMatch(value, type as CssSyntax);
40
+ }
41
+
42
+ const matched = isCSSSyntax(def) ? cssSyntaxMatch(value, def) : def.is(value);
43
+
44
+ if (!matched.matched) {
45
+ return {
46
+ ...matched,
47
+ ref: matched.ref || def.ref,
48
+ expects: matched.expects || def.expects,
49
+ };
50
+ }
51
+ return matched;
52
+ }
@@ -0,0 +1,133 @@
1
+ import { checkList } from './list';
2
+
3
+ test('Zero space', () => {
4
+ const type = { token: 'Zero', separator: 'space' } as const;
5
+ expect(checkList('0', type).matched).toBe(true);
6
+ expect(checkList(' 0 ', type).matched).toBe(true);
7
+ expect(checkList(' 0 0', type).matched).toBe(true);
8
+ expect(checkList(' 0 0 ', type).matched).toBe(true);
9
+ expect(checkList('0 0 0 ', type).matched).toBe(true);
10
+ expect(checkList('0 0 0 0', type).matched).toBe(true);
11
+ expect(checkList('0 0 0 0', type).matched).toBe(true);
12
+ expect(checkList('1', type).matched).toBe(false);
13
+ expect(checkList(' 1 ', type).matched).toBe(false);
14
+ expect(checkList(' 1 0', type).matched).toBe(false);
15
+ expect(checkList(' 1 0 ', type).matched).toBe(false);
16
+ expect(checkList('0 1 0 ', type).matched).toBe(false);
17
+ expect(checkList('0 1 0 0', type).matched).toBe(false);
18
+ expect(checkList('0 1 0 0', type).matched).toBe(false);
19
+ });
20
+
21
+ test('Zero comma', () => {
22
+ const type = { token: 'Zero', separator: 'comma' } as const;
23
+ expect(checkList('0', type).matched).toBe(true);
24
+ expect(checkList(' 0 ', type).matched).toBe(true);
25
+ expect(checkList(' 0 0', type).matched).toBe(false);
26
+ expect(checkList(' 0,0', type).matched).toBe(true);
27
+ expect(checkList(' 0,,0 ', type).matched).toBe(false);
28
+ expect(checkList('0 0 0 ', type).matched).toBe(false);
29
+ expect(checkList(' 0,0,0,0 ', type).matched).toBe(true);
30
+ expect(checkList('0, 0, 0 , 0', type).matched).toBe(true);
31
+ expect(checkList('0,0,0,0,', type).matched).toBe(false);
32
+ expect(checkList(',0,0,0,0', type).matched).toBe(false);
33
+ });
34
+
35
+ test('Zero comma (disallowToSurroundBySpaces)', () => {
36
+ const type = { token: 'Zero', separator: 'comma', disallowToSurroundBySpaces: true } as const;
37
+ expect(checkList('0', type).matched).toBe(true);
38
+ expect(checkList(' 0 ', type).matched).toBe(false);
39
+ expect(checkList(' 0 0', type).matched).toBe(false);
40
+ expect(checkList(' 0,0', type).matched).toBe(false);
41
+ expect(checkList(' 0,,0 ', type).matched).toBe(false);
42
+ expect(checkList('0 0 0 ', type).matched).toBe(false);
43
+ expect(checkList(' 0,0,0,0 ', type).matched).toBe(false);
44
+ expect(checkList('0, 0, 0 , 0', type).matched).toBe(false);
45
+ expect(checkList('0,0,0,0', type).matched).toBe(true);
46
+ expect(checkList('0,0,0,0,', type).matched).toBe(false);
47
+ expect(checkList(',0,0,0,0', type).matched).toBe(false);
48
+ });
49
+
50
+ test('Location of the token', () => {
51
+ const type = { token: 'Accept', separator: 'comma' } as const;
52
+ expect(checkList('x/y;a=b', type)).toStrictEqual({
53
+ candicate: 'x/y',
54
+ column: 4,
55
+ expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
56
+ length: 4,
57
+ line: 1,
58
+ matched: false,
59
+ offset: 3,
60
+ raw: ';a=b',
61
+ reason: 'extra-token',
62
+ partName: 'the content of the list',
63
+ ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
64
+ });
65
+ expect(checkList('x/y,x/y;a=b', type)).toStrictEqual({
66
+ candicate: 'x/y',
67
+ column: 8,
68
+ expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
69
+ length: 4,
70
+ line: 1,
71
+ matched: false,
72
+ offset: 7,
73
+ raw: ';a=b',
74
+ reason: 'extra-token',
75
+ partName: 'the content of the list',
76
+ ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
77
+ });
78
+ expect(checkList('x/y\n,\nx/y;a=b', type)).toStrictEqual({
79
+ candicate: 'x/y',
80
+ column: 4,
81
+ expects: [{ type: 'format', value: 'MIME Type with no parameters' }],
82
+ length: 4,
83
+ line: 3,
84
+ matched: false,
85
+ offset: 9,
86
+ raw: ';a=b',
87
+ reason: 'extra-token',
88
+ partName: 'the content of the list',
89
+ ref: 'https://html.spec.whatwg.org/multipage/input.html#attr-input-accept',
90
+ });
91
+ });
92
+
93
+ test('Expected comma', () => {
94
+ const type = { token: 'Accept', separator: 'comma' } as const;
95
+ expect(checkList('x/y,', type)).toStrictEqual({
96
+ column: 4,
97
+ expects: undefined,
98
+ length: 1,
99
+ line: 1,
100
+ matched: false,
101
+ offset: 3,
102
+ raw: ',',
103
+ reason: 'extra-token',
104
+ ref: null,
105
+ });
106
+ expect(checkList('x/y,,x/y', type)).toStrictEqual({
107
+ column: 5,
108
+ expects: undefined,
109
+ length: 1,
110
+ line: 1,
111
+ matched: false,
112
+ offset: 4,
113
+ raw: ',',
114
+ reason: 'unexpected-comma',
115
+ ref: null,
116
+ });
117
+ });
118
+
119
+ test('Missing comma', () => {
120
+ const type = { token: 'Accept', separator: 'comma' } as const;
121
+ expect(checkList('a/b,x/y a/z', type)).toStrictEqual({
122
+ column: 9,
123
+ candicate: ',a/z',
124
+ expects: undefined,
125
+ length: 3,
126
+ line: 1,
127
+ matched: false,
128
+ offset: 8,
129
+ raw: 'a/z',
130
+ reason: 'missing-comma',
131
+ ref: null,
132
+ });
133
+ });
package/src/list.ts ADDED
@@ -0,0 +1,34 @@
1
+ import type { Result } from './types';
2
+ import type { List } from './types.schema';
3
+
4
+ import { check } from './check';
5
+ import { matched } from './match-result';
6
+ import { Token, TokenCollection } from './token';
7
+
8
+ export function checkList(value: string, type: List, ref?: string, cache = true): Result {
9
+ const tokens = new TokenCollection(value, type);
10
+
11
+ const matches = tokens.check({ ref });
12
+
13
+ if (!matches.matched) {
14
+ return matches;
15
+ }
16
+
17
+ const identTokens = tokens.getIdentTokens();
18
+
19
+ for (const token of identTokens) {
20
+ const res = check(token.value, type.token, ref, cache);
21
+ if (!res.matched) {
22
+ const { offset, line, column } = Token.shiftLocation(token, res.offset);
23
+ return {
24
+ ...res,
25
+ partName: res.partName || 'the content of the list',
26
+ offset,
27
+ line,
28
+ column,
29
+ };
30
+ }
31
+ }
32
+
33
+ return matched();
34
+ }
@@ -0,0 +1,49 @@
1
+ import type {
2
+ FormattedPrimitiveTypeCheck,
3
+ MatchedResult,
4
+ UnmatchedResult,
5
+ UnmatchedResultOptions,
6
+ UnmatchedResultReason,
7
+ } from './types';
8
+
9
+ export function matches(
10
+ checker: FormattedPrimitiveTypeCheck,
11
+ options?: UnmatchedResultOptions & {
12
+ ref?: string;
13
+ reason?: UnmatchedResultReason;
14
+ },
15
+ ) {
16
+ return (value: string) => {
17
+ const valid = checker(value);
18
+ if (!valid && !value) {
19
+ return unmatched(value, 'empty-token', options);
20
+ }
21
+ return valid ? matched() : unmatched(value, options?.reason, options);
22
+ };
23
+ }
24
+
25
+ export function matched(): MatchedResult {
26
+ return {
27
+ matched: true,
28
+ };
29
+ }
30
+
31
+ export function unmatched(
32
+ value: string,
33
+ reason?: UnmatchedResultReason,
34
+ options?: UnmatchedResultOptions & {
35
+ ref?: string;
36
+ },
37
+ ): UnmatchedResult {
38
+ return {
39
+ ...options,
40
+ matched: false,
41
+ ref: options?.ref || null,
42
+ raw: value,
43
+ offset: 0,
44
+ length: value.length,
45
+ line: 1,
46
+ column: 1,
47
+ reason: reason ?? 'syntax-error',
48
+ };
49
+ }
package/src/number.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { Result } from './types';
2
+ import type { Number } from './types.schema';
3
+
4
+ import { matched, unmatched } from './match-result';
5
+ import { isFloat, isInt } from './primitive';
6
+
7
+ export function checkNumber(value: string, type: Number, ref?: string): Result {
8
+ if (!value) {
9
+ return unmatched(value, 'empty-token');
10
+ }
11
+ const syntaxMatched = type.type === 'float' ? isFloat(value) : isInt(value);
12
+ if (syntaxMatched) {
13
+ const n = parseFloat(value);
14
+ if (!isFinite(n)) {
15
+ return unmatched(value, 'unexpected-token');
16
+ }
17
+
18
+ const clampable = type.clampable;
19
+ const isInt = type.type === 'integer';
20
+
21
+ if (type.gt != null && type.gt < n) {
22
+ return unmatched(value, 'out-of-range', {
23
+ candicate: clampable && isInt ? `${type.gt + 1}` : undefined,
24
+ });
25
+ }
26
+
27
+ if (type.gte != null && type.gte <= n) {
28
+ return unmatched(value, 'out-of-range', {
29
+ candicate: clampable ? `${type.gte}` : undefined,
30
+ });
31
+ }
32
+
33
+ if (type.lt != null && n < type.lt) {
34
+ return unmatched(value, 'out-of-range', {
35
+ candicate: clampable && isInt ? `${type.lt - 1}` : undefined,
36
+ });
37
+ }
38
+
39
+ if (type.lte != null && n <= type.lte) {
40
+ return unmatched(value, 'out-of-range', {
41
+ candicate: clampable ? `${type.lte}` : undefined,
42
+ });
43
+ }
44
+ }
45
+ return syntaxMatched ? matched() : unmatched(value, 'unexpected-token');
46
+ }
@@ -0,0 +1,65 @@
1
+ import { isInt, isUint, isFloat, isNonZeroUint, isQuantity } from '.';
2
+
3
+ test('int', () => {
4
+ expect(isInt('0')).toBe(true);
5
+ expect(isInt('1')).toBe(true);
6
+ expect(isInt('-0')).toBe(true);
7
+ expect(isInt('-1')).toBe(true);
8
+ expect(isInt('10')).toBe(true);
9
+ expect(isInt('100')).toBe(true);
10
+ expect(isInt('1.00')).toBe(false);
11
+ expect(isInt('.001')).toBe(false);
12
+ expect(isInt(' 1 ')).toBe(false);
13
+ expect(isInt('- 1')).toBe(false);
14
+ });
15
+
16
+ test('uint', () => {
17
+ expect(isUint('0')).toBe(true);
18
+ expect(isUint('1')).toBe(true);
19
+ expect(isUint('10')).toBe(true);
20
+ expect(isUint('100')).toBe(true);
21
+ expect(isUint('-0')).toBe(false);
22
+ expect(isUint('-1')).toBe(false);
23
+ expect(isUint('1.00')).toBe(false);
24
+ expect(isUint('.001')).toBe(false);
25
+ expect(isUint(' 1 ')).toBe(false);
26
+ expect(isUint('- 1')).toBe(false);
27
+ });
28
+
29
+ test('float', () => {
30
+ expect(isFloat('0')).toBe(true);
31
+ expect(isFloat('1')).toBe(true);
32
+ expect(isFloat('10')).toBe(true);
33
+ expect(isFloat('100')).toBe(true);
34
+ expect(isFloat('-0')).toBe(true);
35
+ expect(isFloat('-1')).toBe(true);
36
+ expect(isFloat('1.00')).toBe(true);
37
+ expect(isFloat('.001')).toBe(true);
38
+ expect(isFloat(' 1 ')).toBe(false);
39
+ expect(isFloat('- 1')).toBe(false);
40
+ });
41
+
42
+ test('nonZeroUint', () => {
43
+ expect(isNonZeroUint('0')).toBe(false);
44
+ expect(isNonZeroUint('1')).toBe(true);
45
+ expect(isNonZeroUint('10')).toBe(true);
46
+ expect(isNonZeroUint('100')).toBe(true);
47
+ expect(isNonZeroUint('-0')).toBe(false);
48
+ expect(isNonZeroUint('-1')).toBe(false);
49
+ expect(isNonZeroUint('1.00')).toBe(false);
50
+ expect(isNonZeroUint('.001')).toBe(false);
51
+ expect(isNonZeroUint(' 1 ')).toBe(false);
52
+ expect(isNonZeroUint('- 1')).toBe(false);
53
+ });
54
+
55
+ test('quantity', () => {
56
+ expect(isQuantity('0px', ['px', 'em'])).toBe(true);
57
+ expect(isQuantity('.5px', ['px', 'em'])).toBe(true);
58
+ expect(isQuantity('1.5em', ['px', 'em'])).toBe(true);
59
+ expect(isQuantity('1.5cm', ['px', 'em'])).toBe(false);
60
+ expect(isQuantity('1.5px', ['px', 'em'], 'int')).toBe(false);
61
+ expect(isQuantity('-5px', ['px', 'em'], 'int')).toBe(true);
62
+ expect(isQuantity('-5px', ['px', 'em'], 'uint')).toBe(false);
63
+ expect(isQuantity('1.12e+21px', ['px', 'em'], 'float')).toBe(true);
64
+ expect(isQuantity('1.12e+21px', ['px', 'em'], 'int')).toBe(false);
65
+ });
@@ -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,8 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#floating-point-numbers
3
+ *
4
+ * @param value
5
+ */
6
+ export function isFloat(value: string) {
7
+ return value === value.trim() && Number.isFinite(parseFloat(value));
8
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#signed-integers
3
+ *
4
+ * @param value
5
+ */
6
+ export function isInt(value: string) {
7
+ return /^-?[0-9]+$/.test(value);
8
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Non-negative integer greater than zero
3
+ *
4
+ * @param value
5
+ */
6
+ export function isNonZeroUint(value: string) {
7
+ return /^[0-9]+$/.test(value) && !/^0+$/.test(value);
8
+ }
@@ -0,0 +1,38 @@
1
+ import { isFloat } from './is-float';
2
+ import { isInt } from './is-int';
3
+ import { isUint } from './is-uint';
4
+ import { splitUnit } from './split-unit';
5
+
6
+ /**
7
+ *
8
+ * @param value
9
+ * @param units
10
+ * @param numberType
11
+ */
12
+ export function isQuantity(value: string, units: string[], numberType: 'int' | 'uint' | 'float' = 'float') {
13
+ const { num, unit } = splitUnit(value);
14
+ if (!units.includes(unit.toLowerCase())) {
15
+ return false;
16
+ }
17
+ switch (numberType) {
18
+ case 'int': {
19
+ if (!isInt(num)) {
20
+ return false;
21
+ }
22
+ break;
23
+ }
24
+ case 'uint': {
25
+ if (!isUint(num)) {
26
+ return false;
27
+ }
28
+ break;
29
+ }
30
+ case 'float': {
31
+ if (!isFloat(num)) {
32
+ return false;
33
+ }
34
+ break;
35
+ }
36
+ }
37
+ return true;
38
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#non-negative-integers
3
+ *
4
+ * @param value
5
+ */
6
+ export function isUint(value: string, opions?: { gt?: number }) {
7
+ const matched = /^[0-9]+$/.test(value);
8
+ if (matched && opions) {
9
+ const n = parseInt(value, 10);
10
+ if (opions.gt != null) {
11
+ return isFinite(n) && opions.gt < n;
12
+ }
13
+ }
14
+ return matched;
15
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * It is in the range between `from` and `to`.
3
+ *
4
+ * @param value
5
+ * @param from
6
+ * @param to
7
+ */
8
+ export function range(value: string, from: number, to: number) {
9
+ const num = parseFloat(value);
10
+ if (isNaN(num)) {
11
+ return false;
12
+ }
13
+ return from <= num && num <= to;
14
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ *
3
+ * @param value
4
+ * @returns
5
+ */
6
+ export function splitUnit(value: string) {
7
+ value = value.trim().toLowerCase();
8
+ const matched = value.match(/(^-?\.[0-9]+|^-?[0-9]+(?:\.[0-9]+(?:e[+-][0-9]+)?)?)([a-z]+$)/i);
9
+ if (!matched) {
10
+ return {
11
+ num: value,
12
+ unit: '',
13
+ };
14
+ }
15
+ const [, num, unit] = matched;
16
+ return {
17
+ num,
18
+ unit,
19
+ };
20
+ }
@@ -0,0 +1,23 @@
1
+ import { isBCP47 } from './is-bcp-47';
2
+
3
+ const is = isBCP47();
4
+
5
+ test('en-us', () => {
6
+ expect(is('en-us')).toBe(true);
7
+ });
8
+
9
+ test('ja-JP', () => {
10
+ expect(is('ja-JP')).toBe(true);
11
+ });
12
+
13
+ test('Empty', () => {
14
+ expect(is('')).toBe(false);
15
+ });
16
+
17
+ test('Invalid', () => {
18
+ expect(is(':::')).toBe(false);
19
+ });
20
+
21
+ test('Surrounded by spaces', () => {
22
+ expect(is(' en ')).toBe(false);
23
+ });
@@ -0,0 +1,13 @@
1
+ import type { FormattedPrimitiveTypeCreator } from '../types';
2
+
3
+ import { parse } from 'bcp-47';
4
+
5
+ /**
6
+ * @see https://tools.ietf.org/rfc/bcp/bcp47.html
7
+ */
8
+ export const isBCP47: FormattedPrimitiveTypeCreator = () => {
9
+ return value => {
10
+ const { language } = parse(value);
11
+ return !!language;
12
+ };
13
+ };
@@ -0,0 +1,2 @@
1
+ export { Token } from './token';
2
+ export { TokenCollection } from './token-collection';