@markuplint/types 3.0.0-canary.5 → 3.0.0-dev.176

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 (110) hide show
  1. package/README.md +1 -1
  2. package/lib/check-multi-types.d.ts +4 -1
  3. package/lib/check-multi-types.js +5 -4
  4. package/lib/check.d.ts +6 -5
  5. package/lib/check.spec.js +64 -58
  6. package/lib/css-syntax.js +49 -47
  7. package/lib/css-syntax.spec.js +152 -149
  8. package/lib/defs.js +2 -2
  9. package/lib/enum.d.ts +2 -1
  10. package/lib/enum.js +1 -1
  11. package/lib/get-candidate.d.ts +4 -1
  12. package/lib/keyword-type.js +3 -2
  13. package/lib/list.d.ts +2 -1
  14. package/lib/list.js +2 -1
  15. package/lib/list.spec.js +118 -117
  16. package/lib/match-result.d.ts +21 -8
  17. package/lib/match-result.js +2 -1
  18. package/lib/number.d.ts +1 -1
  19. package/lib/primitive/index.spec.js +53 -52
  20. package/lib/primitive/is-quantity.d.ts +5 -1
  21. package/lib/primitive/is-uint.d.ts +6 -3
  22. package/lib/primitive/split-unit.d.ts +2 -2
  23. package/lib/primitive/split-unit.js +2 -2
  24. package/lib/rfc/is-bcp-47.spec.js +9 -8
  25. package/lib/token/token-collection.d.ts +71 -56
  26. package/lib/token/token-collection.js +30 -23
  27. package/lib/token/token-collection.spec.js +156 -134
  28. package/lib/token/token.d.ts +58 -52
  29. package/lib/token/token.js +19 -20
  30. package/lib/token/types.d.ts +3 -0
  31. package/lib/token/types.js +2 -0
  32. package/lib/types.d.ts +79 -55
  33. package/lib/types.schema.d.ts +1084 -31
  34. package/lib/w3c/check-serialized-permissions-policy.spec.js +30 -28
  35. package/lib/whatwg/check-autocomplete.js +6 -6
  36. package/lib/whatwg/check-autocomplete.spec.js +361 -360
  37. package/lib/whatwg/check-datetime/datetime-tokens.d.ts +21 -1
  38. package/lib/whatwg/check-datetime/datetime-tokens.js +9 -6
  39. package/lib/whatwg/check-datetime/duration-string.js +12 -2
  40. package/lib/whatwg/check-datetime/index.spec.js +277 -276
  41. package/lib/whatwg/check-datetime/local-date-and-time-string.js +6 -2
  42. package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +1 -1
  43. package/lib/whatwg/check-datetime/time-zone-offset-string.js +3 -2
  44. package/lib/whatwg/check-mime-type.d.ts +4 -4
  45. package/lib/whatwg/check-mime-type.js +1 -1
  46. package/lib/whatwg/check-mime-type.spec.js +53 -52
  47. package/lib/whatwg/is-abs-url.spec.js +6 -5
  48. package/lib/whatwg/is-itemprop-name.js +1 -1
  49. package/package.json +12 -10
  50. package/{src/check.spec.ts → test/check.spec.js} +7 -1
  51. package/{src/css-syntax.spec.ts → test/css-syntax.spec.js} +10 -12
  52. package/{src/list.spec.ts → test/list.spec.js} +7 -7
  53. package/{src/primitive/index.spec.ts → test/primitive/index.spec.js} +1 -1
  54. package/{src/rfc/is-bcp-47.spec.ts → test/rfc/is-bcp-47.spec.js} +1 -1
  55. package/{src/token/token-collection.spec.ts → test/token/token-collection.spec.js} +2 -3
  56. package/{src/w3c/check-serialized-permissions-policy.spec.ts → test/w3c/check-serialized-permissions-policy.spec.js} +1 -3
  57. package/{src/whatwg/check-autocomplete.spec.ts → test/whatwg/check-autocomplete.spec.js} +1 -1
  58. package/{src/whatwg/check-datetime/index.spec.ts → test/whatwg/check-datetime/index.spec.js} +18 -15
  59. package/{src/whatwg/check-mime-type.spec.ts → test/whatwg/check-mime-type.spec.js} +1 -1
  60. package/{src/whatwg/is-abs-url.spec.ts → test/whatwg/is-abs-url.spec.js} +1 -1
  61. package/types.schema.json +369 -318
  62. package/gen/specific-schema.json +0 -98
  63. package/gen/types.ts +0 -66
  64. package/src/check-multi-types.ts +0 -35
  65. package/src/check.ts +0 -48
  66. package/src/css-syntax.ts +0 -188
  67. package/src/debug.ts +0 -3
  68. package/src/defs.ts +0 -811
  69. package/src/enum.ts +0 -25
  70. package/src/get-candidate.ts +0 -24
  71. package/src/index.ts +0 -3
  72. package/src/keyword-type.ts +0 -64
  73. package/src/list.ts +0 -34
  74. package/src/match-result.ts +0 -49
  75. package/src/number.ts +0 -46
  76. package/src/primitive/index.ts +0 -7
  77. package/src/primitive/is-float.ts +0 -8
  78. package/src/primitive/is-int.ts +0 -8
  79. package/src/primitive/is-non-zero-uint.ts +0 -8
  80. package/src/primitive/is-quantity.ts +0 -38
  81. package/src/primitive/is-uint.ts +0 -15
  82. package/src/primitive/range.ts +0 -14
  83. package/src/primitive/split-unit.ts +0 -20
  84. package/src/rfc/is-bcp-47.ts +0 -13
  85. package/src/token/index.ts +0 -2
  86. package/src/token/token-collection.ts +0 -444
  87. package/src/token/token.ts +0 -144
  88. package/src/types.schema.ts +0 -1062
  89. package/src/types.ts +0 -118
  90. package/src/w3c/check-serialized-permissions-policy.ts +0 -303
  91. package/src/whatwg/check-autocomplete.ts +0 -380
  92. package/src/whatwg/check-datetime/date-string.ts +0 -44
  93. package/src/whatwg/check-datetime/datetime-tokens.ts +0 -600
  94. package/src/whatwg/check-datetime/duration-string.ts +0 -399
  95. package/src/whatwg/check-datetime/global-date-and-time-string.ts +0 -96
  96. package/src/whatwg/check-datetime/index.ts +0 -36
  97. package/src/whatwg/check-datetime/local-date-and-time-string.ts +0 -163
  98. package/src/whatwg/check-datetime/month-string.ts +0 -31
  99. package/src/whatwg/check-datetime/time-string.ts +0 -49
  100. package/src/whatwg/check-datetime/time-zone-offset-string.ts +0 -93
  101. package/src/whatwg/check-datetime/week-string.ts +0 -41
  102. package/src/whatwg/check-datetime/year-string.ts +0 -26
  103. package/src/whatwg/check-datetime/yearless-date-string.ts +0 -38
  104. package/src/whatwg/check-mime-type.ts +0 -46
  105. package/src/whatwg/is-abs-url.ts +0 -31
  106. package/src/whatwg/is-browser-context-name.ts +0 -16
  107. package/src/whatwg/is-custom-element-name.ts +0 -76
  108. package/src/whatwg/is-itemprop-name.ts +0 -17
  109. package/tsconfig.test.json +0 -3
  110. package/tsconfig.tsbuildinfo +0 -1
package/src/enum.ts DELETED
@@ -1,25 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
1
- import leven from 'leven';
2
-
3
- type NullableString = string | null | undefined;
4
-
5
- export function getCandidate(value: NullableString, ...candidates: (NullableString | NullableString[])[]) {
6
- if (!value) {
7
- return;
8
- }
9
- const list = candidates.flat(2).filter((s): s is string => !!s);
10
- let candidate: 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
- candidate = word;
17
- }
18
- maxRatio = Math.max(ratio, maxRatio);
19
- });
20
- if (value === candidate) {
21
- return;
22
- }
23
- return candidate;
24
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './whatwg/is-custom-element-name';
2
- export * from './check';
3
- export * from './types';
@@ -1,64 +0,0 @@
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
- import { matched } from './match-result';
9
-
10
- const resultCache = new Map<string, Result>();
11
- const CACHE_KEY_PREFIX = '@markuplint/types/checkKeywordType/cache:::';
12
-
13
- export function checkKeywordType(value: string, type: KeywordDefinedType, cache = true) {
14
- const key = `${CACHE_KEY_PREFIX}${value}${type}`;
15
- if (cache) {
16
- const cachedResult = resultCache.get(key);
17
- if (cachedResult) {
18
- log('Restore cache: %s / %s', value, type);
19
- return cachedResult;
20
- }
21
- }
22
- const result = _checkKeywordType(value, type);
23
-
24
- if (cache) {
25
- resultCache.set(key, result);
26
- if (log.enabled) {
27
- log('Cache checking: %O', {
28
- input: value,
29
- type,
30
- key,
31
- result: result,
32
- });
33
- }
34
- }
35
-
36
- return result;
37
- }
38
-
39
- function _checkKeywordType(value: string, type: KeywordDefinedType): Result {
40
- const def = types[type];
41
- if (!def) {
42
- log('The "%s" type is not defined in custom type identifier markuplint specified', type);
43
- try {
44
- return cssSyntaxMatch(value, type as CssSyntax);
45
- } catch (e) {
46
- if (e instanceof Error && e.message === 'MARKUPLINT_TYPE_NO_EXIST') {
47
- log("Allow all of any value because the type doesn't exist");
48
- return matched();
49
- }
50
- throw e;
51
- }
52
- }
53
-
54
- const matches = isCSSSyntax(def) ? cssSyntaxMatch(value, def) : def.is(value);
55
-
56
- if (!matches.matched) {
57
- return {
58
- ...matches,
59
- ref: matches.ref || def.ref,
60
- expects: matches.expects || def.expects,
61
- };
62
- }
63
- return matches;
64
- }
package/src/list.ts DELETED
@@ -1,34 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
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 DELETED
@@ -1,46 +0,0 @@
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 && n <= type.gt) {
22
- return unmatched(value, 'out-of-range', {
23
- candidate: clampable && isInt ? `${type.gt + 1}` : undefined,
24
- });
25
- }
26
-
27
- if (type.gte != null && n < type.gte) {
28
- return unmatched(value, 'out-of-range', {
29
- candidate: clampable ? `${type.gte}` : undefined,
30
- });
31
- }
32
-
33
- if (type.lt != null && type.lt <= n) {
34
- return unmatched(value, 'out-of-range', {
35
- candidate: clampable && isInt ? `${type.lt - 1}` : undefined,
36
- });
37
- }
38
-
39
- if (type.lte != null && type.lte < n) {
40
- return unmatched(value, 'out-of-range', {
41
- candidate: clampable ? `${type.lte}` : undefined,
42
- });
43
- }
44
- }
45
- return syntaxMatched ? matched() : unmatched(value, 'unexpected-token');
46
- }
@@ -1,7 +0,0 @@
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';
@@ -1,8 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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
- }
@@ -1,38 +0,0 @@
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
- }
@@ -1,15 +0,0 @@
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, options?: { gt?: number }) {
7
- const matched = /^[0-9]+$/.test(value);
8
- if (matched && options) {
9
- const n = parseInt(value, 10);
10
- if (options.gt != null) {
11
- return isFinite(n) && options.gt < n;
12
- }
13
- }
14
- return matched;
15
- }
@@ -1,14 +0,0 @@
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
- }
@@ -1,20 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- };
@@ -1,2 +0,0 @@
1
- export { Token } from './token';
2
- export { TokenCollection } from './token-collection';