@markuplint/types 3.0.0-dev.25 → 3.0.0-dev.290
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -61
- package/lib/check-multi-types.d.ts +2 -2
- package/lib/check-multi-types.js +9 -13
- package/lib/check.d.ts +7 -6
- package/lib/check.js +20 -30
- package/lib/css-syntax.d.ts +1 -1
- package/lib/css-syntax.js +60 -63
- package/lib/debug.js +2 -6
- package/lib/defs.d.ts +1 -1
- package/lib/defs.js +163 -91
- package/lib/enum.d.ts +4 -3
- package/lib/enum.js +7 -12
- package/lib/get-candidate.d.ts +1 -1
- package/lib/get-candidate.js +3 -8
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -6
- package/lib/keyword-type.d.ts +2 -2
- package/lib/keyword-type.js +17 -21
- package/lib/list.d.ts +4 -3
- package/lib/list.js +9 -13
- package/lib/match-result.d.ts +4 -4
- package/lib/match-result.js +6 -12
- package/lib/number.d.ts +3 -3
- package/lib/number.js +11 -15
- package/lib/primitive/index.d.ts +7 -7
- package/lib/primitive/index.js +7 -17
- package/lib/primitive/is-float.js +1 -5
- package/lib/primitive/is-int.js +1 -5
- package/lib/primitive/is-non-zero-uint.js +1 -5
- package/lib/primitive/is-quantity.d.ts +1 -1
- package/lib/primitive/is-quantity.js +9 -13
- package/lib/primitive/is-uint.d.ts +1 -1
- package/lib/primitive/is-uint.js +1 -5
- package/lib/primitive/range.js +1 -5
- package/lib/primitive/split-unit.js +3 -7
- package/lib/rfc/is-bcp-47.d.ts +1 -1
- package/lib/rfc/is-bcp-47.js +3 -7
- package/lib/token/index.d.ts +2 -2
- package/lib/token/index.js +2 -7
- package/lib/token/token-collection.d.ts +13 -13
- package/lib/token/token-collection.js +55 -50
- package/lib/token/token.d.ts +11 -10
- package/lib/token/token.js +16 -24
- package/lib/token/types.d.ts +3 -0
- package/lib/types.d.ts +47 -46
- package/lib/types.js +1 -2
- package/lib/types.schema.d.ts +2 -2
- package/lib/types.schema.js +1 -3
- package/lib/w3c/check-serialized-permissions-policy.d.ts +1 -1
- package/lib/w3c/check-serialized-permissions-policy.js +16 -20
- package/lib/whatwg/check-autocomplete.d.ts +1 -1
- package/lib/whatwg/check-autocomplete.js +28 -32
- package/lib/whatwg/check-datetime/date-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/date-string.js +9 -13
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +1 -1
- package/lib/whatwg/check-datetime/datetime-tokens.js +43 -46
- package/lib/whatwg/check-datetime/duration-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/duration-string.js +48 -44
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +17 -21
- package/lib/whatwg/check-datetime/index.d.ts +1 -1
- package/lib/whatwg/check-datetime/index.js +25 -29
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +25 -26
- package/lib/whatwg/check-datetime/month-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/month-string.js +9 -13
- package/lib/whatwg/check-datetime/time-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/time-string.js +9 -13
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +3 -3
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +16 -21
- package/lib/whatwg/check-datetime/week-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/week-string.js +9 -13
- package/lib/whatwg/check-datetime/year-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/year-string.js +9 -13
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +1 -1
- package/lib/whatwg/check-datetime/yearless-date-string.js +9 -13
- package/lib/whatwg/check-mime-type.d.ts +1 -1
- package/lib/whatwg/check-mime-type.js +12 -18
- package/lib/whatwg/is-abs-url.d.ts +1 -1
- package/lib/whatwg/is-abs-url.js +1 -5
- package/lib/whatwg/is-browser-context-name.d.ts +3 -1
- package/lib/whatwg/is-browser-context-name.js +3 -5
- package/lib/whatwg/is-custom-element-name.d.ts +1 -1
- package/lib/whatwg/is-custom-element-name.js +1 -5
- package/lib/whatwg/is-itemprop-name.d.ts +1 -1
- package/lib/whatwg/is-itemprop-name.js +2 -6
- package/lib/whatwg/is-navigable-target-name.d.ts +11 -0
- package/lib/whatwg/is-navigable-target-name.js +20 -0
- package/package.json +19 -14
- package/types.schema.json +314 -311
- package/gen/specific-schema.json +0 -98
- package/gen/types.ts +0 -66
- package/lib/check.spec.js +0 -76
- package/lib/css-syntax.spec.d.ts +0 -1
- package/lib/css-syntax.spec.js +0 -157
- package/lib/list.spec.d.ts +0 -1
- package/lib/list.spec.js +0 -129
- package/lib/primitive/index.spec.d.ts +0 -1
- package/lib/primitive/index.spec.js +0 -62
- package/lib/rfc/is-bcp-47.spec.d.ts +0 -1
- package/lib/rfc/is-bcp-47.spec.js +0 -19
- package/lib/token/token-collection.spec.d.ts +0 -1
- package/lib/token/token-collection.spec.js +0 -144
- package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +0 -1
- package/lib/w3c/check-serialized-permissions-policy.spec.js +0 -34
- package/lib/whatwg/check-autocomplete.spec.d.ts +0 -1
- package/lib/whatwg/check-autocomplete.spec.js +0 -379
- package/lib/whatwg/check-datetime/index.spec.d.ts +0 -1
- package/lib/whatwg/check-datetime/index.spec.js +0 -318
- package/lib/whatwg/check-mime-type.spec.d.ts +0 -1
- package/lib/whatwg/check-mime-type.spec.js +0 -58
- package/lib/whatwg/is-abs-url.spec.d.ts +0 -1
- package/lib/whatwg/is-abs-url.spec.js +0 -8
- package/patches/css-tree+2.3.1.patch +0 -23
- package/src/check-multi-types.ts +0 -35
- package/src/check.spec.ts +0 -83
- package/src/check.ts +0 -48
- package/src/css-syntax.spec.ts +0 -167
- package/src/css-syntax.ts +0 -188
- package/src/debug.ts +0 -3
- package/src/defs.ts +0 -811
- package/src/enum.ts +0 -25
- package/src/get-candidate.ts +0 -24
- package/src/index.ts +0 -3
- package/src/keyword-type.ts +0 -64
- package/src/list.spec.ts +0 -133
- package/src/list.ts +0 -34
- package/src/match-result.ts +0 -49
- package/src/number.ts +0 -46
- package/src/primitive/index.spec.ts +0 -65
- package/src/primitive/index.ts +0 -7
- package/src/primitive/is-float.ts +0 -8
- package/src/primitive/is-int.ts +0 -8
- package/src/primitive/is-non-zero-uint.ts +0 -8
- package/src/primitive/is-quantity.ts +0 -38
- package/src/primitive/is-uint.ts +0 -15
- package/src/primitive/range.ts +0 -14
- package/src/primitive/split-unit.ts +0 -20
- package/src/rfc/is-bcp-47.spec.ts +0 -23
- package/src/rfc/is-bcp-47.ts +0 -13
- package/src/token/index.ts +0 -2
- package/src/token/token-collection.spec.ts +0 -147
- package/src/token/token-collection.ts +0 -444
- package/src/token/token.ts +0 -144
- package/src/types.schema.ts +0 -1113
- package/src/types.ts +0 -118
- package/src/w3c/check-serialized-permissions-policy.spec.ts +0 -38
- package/src/w3c/check-serialized-permissions-policy.ts +0 -303
- package/src/whatwg/check-autocomplete.spec.ts +0 -387
- package/src/whatwg/check-autocomplete.ts +0 -380
- package/src/whatwg/check-datetime/date-string.ts +0 -44
- package/src/whatwg/check-datetime/datetime-tokens.ts +0 -600
- package/src/whatwg/check-datetime/duration-string.ts +0 -399
- package/src/whatwg/check-datetime/global-date-and-time-string.ts +0 -96
- package/src/whatwg/check-datetime/index.spec.ts +0 -333
- package/src/whatwg/check-datetime/index.ts +0 -36
- package/src/whatwg/check-datetime/local-date-and-time-string.ts +0 -163
- package/src/whatwg/check-datetime/month-string.ts +0 -31
- package/src/whatwg/check-datetime/time-string.ts +0 -49
- package/src/whatwg/check-datetime/time-zone-offset-string.ts +0 -93
- package/src/whatwg/check-datetime/week-string.ts +0 -41
- package/src/whatwg/check-datetime/year-string.ts +0 -26
- package/src/whatwg/check-datetime/yearless-date-string.ts +0 -38
- package/src/whatwg/check-mime-type.spec.ts +0 -59
- package/src/whatwg/check-mime-type.ts +0 -46
- package/src/whatwg/is-abs-url.spec.ts +0 -8
- package/src/whatwg/is-abs-url.ts +0 -31
- package/src/whatwg/is-browser-context-name.ts +0 -16
- package/src/whatwg/is-custom-element-name.ts +0 -76
- package/src/whatwg/is-itemprop-name.ts +0 -17
- package/tsconfig.test.json +0 -3
- package/tsconfig.tsbuildinfo +0 -1
- /package/lib/{check.spec.d.ts → token/types.js} +0 -0
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
|
-
}
|
package/src/get-candidate.ts
DELETED
|
@@ -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
package/src/keyword-type.ts
DELETED
|
@@ -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.spec.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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
|
-
candidate: '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
|
-
candidate: '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
|
-
candidate: '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
|
-
candidate: ',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
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
|
-
}
|
package/src/match-result.ts
DELETED
|
@@ -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,65 +0,0 @@
|
|
|
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
|
-
});
|
package/src/primitive/index.ts
DELETED
|
@@ -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';
|
package/src/primitive/is-int.ts
DELETED
|
@@ -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
|
-
}
|
package/src/primitive/is-uint.ts
DELETED
|
@@ -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
|
-
}
|
package/src/primitive/range.ts
DELETED
|
@@ -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,23 +0,0 @@
|
|
|
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
|
-
});
|
package/src/rfc/is-bcp-47.ts
DELETED
|
@@ -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
|
-
};
|
package/src/token/index.ts
DELETED