@markuplint/types 1.0.0-dev.20211213.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +117 -0
- package/extends.json +12 -0
- package/gen/specific-schema.json +98 -0
- package/gen/types.ts +66 -0
- package/lib/check-multi-types.d.ts +2 -0
- package/lib/check-multi-types.js +29 -0
- package/lib/check.d.ts +8 -0
- package/lib/check.js +49 -0
- package/lib/check.spec.d.ts +1 -0
- package/lib/check.spec.js +57 -0
- package/lib/css-syntax.d.ts +2 -0
- package/lib/css-syntax.js +141 -0
- package/lib/css-syntax.spec.d.ts +1 -0
- package/lib/css-syntax.spec.js +157 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.js +6 -0
- package/lib/defs.d.ts +3 -0
- package/lib/defs.js +752 -0
- package/lib/enum.d.ts +3 -0
- package/lib/enum.js +26 -0
- package/lib/get-candicate.d.ts +3 -0
- package/lib/get-candicate.js +26 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/keyword-type.d.ts +3 -0
- package/lib/keyword-type.js +46 -0
- package/lib/list.d.ts +3 -0
- package/lib/list.js +29 -0
- package/lib/list.spec.d.ts +1 -0
- package/lib/list.spec.js +129 -0
- package/lib/match-result.d.ts +9 -0
- package/lib/match-result.js +33 -0
- package/lib/number.d.ts +3 -0
- package/lib/number.js +41 -0
- package/lib/primitive/index.d.ts +7 -0
- package/lib/primitive/index.js +17 -0
- package/lib/primitive/index.spec.d.ts +1 -0
- package/lib/primitive/index.spec.js +62 -0
- package/lib/primitive/is-float.d.ts +6 -0
- package/lib/primitive/is-float.js +12 -0
- package/lib/primitive/is-int.d.ts +6 -0
- package/lib/primitive/is-int.js +12 -0
- package/lib/primitive/is-non-zero-uint.d.ts +6 -0
- package/lib/primitive/is-non-zero-uint.js +12 -0
- package/lib/primitive/is-quantity.d.ts +7 -0
- package/lib/primitive/is-quantity.js +41 -0
- package/lib/primitive/is-uint.d.ts +8 -0
- package/lib/primitive/is-uint.js +19 -0
- package/lib/primitive/range.d.ts +8 -0
- package/lib/primitive/range.js +18 -0
- package/lib/primitive/split-unit.d.ts +9 -0
- package/lib/primitive/split-unit.js +24 -0
- package/lib/rfc/is-bcp-47.d.ts +5 -0
- package/lib/rfc/is-bcp-47.js +14 -0
- package/lib/rfc/is-bcp-47.spec.d.ts +1 -0
- package/lib/rfc/is-bcp-47.spec.js +19 -0
- package/lib/token/index.d.ts +2 -0
- package/lib/token/index.js +7 -0
- package/lib/token/token-collection.d.ts +62 -0
- package/lib/token/token-collection.js +383 -0
- package/lib/token/token-collection.spec.d.ts +1 -0
- package/lib/token/token-collection.spec.js +144 -0
- package/lib/token/token.d.ts +55 -0
- package/lib/token/token.js +128 -0
- package/lib/types.d.ts +78 -0
- package/lib/types.js +2 -0
- package/lib/types.schema.d.ts +54 -0
- package/lib/types.schema.js +8 -0
- package/lib/w3c/check-serialized-permissions-policy.d.ts +15 -0
- package/lib/w3c/check-serialized-permissions-policy.js +251 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.d.ts +1 -0
- package/lib/w3c/check-serialized-permissions-policy.spec.js +26 -0
- package/lib/whatwg/check-autocomplete.d.ts +5 -0
- package/lib/whatwg/check-autocomplete.js +282 -0
- package/lib/whatwg/check-autocomplete.spec.d.ts +1 -0
- package/lib/whatwg/check-autocomplete.spec.js +370 -0
- package/lib/whatwg/check-datetime/date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/date-string.js +31 -0
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +3 -0
- package/lib/whatwg/check-datetime/datetime-tokens.js +509 -0
- package/lib/whatwg/check-datetime/duration-string.d.ts +6 -0
- package/lib/whatwg/check-datetime/duration-string.js +322 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +66 -0
- package/lib/whatwg/check-datetime/index.d.ts +5 -0
- package/lib/whatwg/check-datetime/index.js +35 -0
- package/lib/whatwg/check-datetime/index.spec.d.ts +1 -0
- package/lib/whatwg/check-datetime/index.spec.js +318 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +9 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +116 -0
- package/lib/whatwg/check-datetime/month-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/month-string.js +20 -0
- package/lib/whatwg/check-datetime/time-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/time-string.js +35 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +7 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +75 -0
- package/lib/whatwg/check-datetime/week-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/week-string.js +29 -0
- package/lib/whatwg/check-datetime/year-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/year-string.js +20 -0
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +5 -0
- package/lib/whatwg/check-datetime/yearless-date-string.js +27 -0
- package/lib/whatwg/check-mime-type.d.ts +11 -0
- package/lib/whatwg/check-mime-type.js +42 -0
- package/lib/whatwg/check-mime-type.spec.d.ts +1 -0
- package/lib/whatwg/check-mime-type.spec.js +58 -0
- package/lib/whatwg/is-abs-url.d.ts +15 -0
- package/lib/whatwg/is-abs-url.js +34 -0
- package/lib/whatwg/is-abs-url.spec.d.ts +1 -0
- package/lib/whatwg/is-abs-url.spec.js +8 -0
- package/lib/whatwg/is-browser-context-name.d.ts +11 -0
- package/lib/whatwg/is-browser-context-name.js +18 -0
- package/lib/whatwg/is-custom-element-name.d.ts +22 -0
- package/lib/whatwg/is-custom-element-name.js +77 -0
- package/lib/whatwg/is-itemprop-name.d.ts +12 -0
- package/lib/whatwg/is-itemprop-name.js +19 -0
- package/package.json +33 -0
- package/src/check-multi-types.ts +35 -0
- package/src/check.spec.ts +62 -0
- package/src/check.ts +48 -0
- package/src/css-syntax.spec.ts +167 -0
- package/src/css-syntax.ts +177 -0
- package/src/debug.ts +3 -0
- package/src/defs.ts +810 -0
- package/src/enum.ts +25 -0
- package/src/get-candicate.ts +24 -0
- package/src/index.ts +3 -0
- package/src/keyword-type.ts +52 -0
- package/src/list.spec.ts +133 -0
- package/src/list.ts +34 -0
- package/src/match-result.ts +49 -0
- package/src/number.ts +46 -0
- package/src/primitive/index.spec.ts +65 -0
- package/src/primitive/index.ts +7 -0
- package/src/primitive/is-float.ts +8 -0
- package/src/primitive/is-int.ts +8 -0
- package/src/primitive/is-non-zero-uint.ts +8 -0
- package/src/primitive/is-quantity.ts +38 -0
- package/src/primitive/is-uint.ts +15 -0
- package/src/primitive/range.ts +14 -0
- package/src/primitive/split-unit.ts +20 -0
- package/src/rfc/is-bcp-47.spec.ts +23 -0
- package/src/rfc/is-bcp-47.ts +13 -0
- package/src/token/index.ts +2 -0
- package/src/token/token-collection.spec.ts +147 -0
- package/src/token/token-collection.ts +444 -0
- package/src/token/token.ts +144 -0
- package/src/types.schema.ts +1059 -0
- package/src/types.ts +118 -0
- package/src/w3c/check-serialized-permissions-policy.spec.ts +27 -0
- package/src/w3c/check-serialized-permissions-policy.ts +287 -0
- package/src/whatwg/check-autocomplete.spec.ts +378 -0
- package/src/whatwg/check-autocomplete.ts +324 -0
- package/src/whatwg/check-datetime/date-string.ts +44 -0
- package/src/whatwg/check-datetime/datetime-tokens.ts +600 -0
- package/src/whatwg/check-datetime/duration-string.ts +399 -0
- package/src/whatwg/check-datetime/global-date-and-time-string.ts +96 -0
- package/src/whatwg/check-datetime/index.spec.ts +333 -0
- package/src/whatwg/check-datetime/index.ts +36 -0
- package/src/whatwg/check-datetime/local-date-and-time-string.ts +163 -0
- package/src/whatwg/check-datetime/month-string.ts +31 -0
- package/src/whatwg/check-datetime/time-string.ts +49 -0
- package/src/whatwg/check-datetime/time-zone-offset-string.ts +93 -0
- package/src/whatwg/check-datetime/week-string.ts +41 -0
- package/src/whatwg/check-datetime/year-string.ts +26 -0
- package/src/whatwg/check-datetime/yearless-date-string.ts +38 -0
- package/src/whatwg/check-mime-type.spec.ts +59 -0
- package/src/whatwg/check-mime-type.ts +46 -0
- package/src/whatwg/is-abs-url.spec.ts +8 -0
- package/src/whatwg/is-abs-url.ts +31 -0
- package/src/whatwg/is-browser-context-name.ts +16 -0
- package/src/whatwg/is-custom-element-name.ts +76 -0
- package/src/whatwg/is-itemprop-name.ts +17 -0
- package/tsconfig.test.json +3 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types.schema.json +1092 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { UnmatchedResult, UnmatchedResultOptions, UnmatchedResultReason } from '../types';
|
|
2
|
+
|
|
3
|
+
export class Token {
|
|
4
|
+
static getType(value: string, separators?: string[]) {
|
|
5
|
+
if (Token.whitespace.includes(value[0])) {
|
|
6
|
+
return Token.WhiteSpace;
|
|
7
|
+
}
|
|
8
|
+
if (separators?.includes(value[0])) {
|
|
9
|
+
switch (value[0]) {
|
|
10
|
+
case ',':
|
|
11
|
+
return Token.Comma;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return Token.Ident;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static getLine(value: string, offset: number) {
|
|
18
|
+
return value.slice(0, offset).split(/\n/g).length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static getCol(value: string, offset: number) {
|
|
22
|
+
const lines = value.slice(0, offset).split(/\n/g);
|
|
23
|
+
return lines[lines.length - 1].length + 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static shiftLocation(token: Token, offset: number) {
|
|
27
|
+
const shifted = token.offset + offset;
|
|
28
|
+
return {
|
|
29
|
+
offset: shifted,
|
|
30
|
+
line: Token.getLine(token.#originalValue, shifted),
|
|
31
|
+
column: Token.getCol(token.#originalValue, shifted),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* ASCII whitespace is
|
|
37
|
+
* - U+0009 TAB
|
|
38
|
+
* - U+000A LF
|
|
39
|
+
* - U+000C FF
|
|
40
|
+
* - U+000D CR
|
|
41
|
+
* - U+0020 SPACE.
|
|
42
|
+
*
|
|
43
|
+
* @see https://infra.spec.whatwg.org/#ascii-whitespace
|
|
44
|
+
*/
|
|
45
|
+
static readonly whitespace: ReadonlyArray<string> = ['\u0009', '\u000A', '\u000C', '\u000D', '\u0020'];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
|
|
49
|
+
*/
|
|
50
|
+
static readonly Ident = 1;
|
|
51
|
+
static readonly WhiteSpace = 13;
|
|
52
|
+
static readonly Comma = 18;
|
|
53
|
+
|
|
54
|
+
readonly type: number;
|
|
55
|
+
readonly value: string;
|
|
56
|
+
readonly offset: number;
|
|
57
|
+
#originalValue: string;
|
|
58
|
+
|
|
59
|
+
constructor(value: string, offset: number, originalValue: string, separators?: string[]) {
|
|
60
|
+
this.type = Token.getType(value, separators);
|
|
61
|
+
this.value = value;
|
|
62
|
+
this.offset = offset;
|
|
63
|
+
this.#originalValue = originalValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get length() {
|
|
67
|
+
return this.value.length;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get origin() {
|
|
71
|
+
return this.#originalValue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
unmatched(
|
|
75
|
+
options?: UnmatchedResultOptions & {
|
|
76
|
+
ref?: string;
|
|
77
|
+
reason?: UnmatchedResultReason;
|
|
78
|
+
},
|
|
79
|
+
): UnmatchedResult {
|
|
80
|
+
return {
|
|
81
|
+
...options,
|
|
82
|
+
matched: false,
|
|
83
|
+
ref: options?.ref || null,
|
|
84
|
+
raw: this.value,
|
|
85
|
+
offset: this.offset,
|
|
86
|
+
length: this.value.length,
|
|
87
|
+
line: Token.getLine(this.#originalValue, this.offset),
|
|
88
|
+
column: Token.getCol(this.#originalValue, this.offset),
|
|
89
|
+
reason: options?.reason ?? 'syntax-error',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
*
|
|
95
|
+
* @param value The token value or the token type or its list
|
|
96
|
+
*/
|
|
97
|
+
match(value: string | RegExp | number | (string | RegExp | number)[], caseInsensitive?: boolean): boolean {
|
|
98
|
+
if (Array.isArray(value)) {
|
|
99
|
+
return value.some(v => this.match(v));
|
|
100
|
+
}
|
|
101
|
+
if (typeof value === 'string') {
|
|
102
|
+
const a = caseInsensitive ? this.value.toLowerCase() : this.value;
|
|
103
|
+
const b = caseInsensitive ? value.toLowerCase() : value;
|
|
104
|
+
return a === b;
|
|
105
|
+
}
|
|
106
|
+
if (value instanceof RegExp) {
|
|
107
|
+
const pattern = new RegExp(value, caseInsensitive ? 'i' : '');
|
|
108
|
+
return pattern.test(this.value);
|
|
109
|
+
}
|
|
110
|
+
return this.type === value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* @param value The token value or the token type or its list
|
|
116
|
+
*/
|
|
117
|
+
includes(value: string | RegExp | number | (string | RegExp | number)[], caseInsensitive?: boolean): boolean {
|
|
118
|
+
if (Array.isArray(value)) {
|
|
119
|
+
return value.some(v => this.includes(v));
|
|
120
|
+
}
|
|
121
|
+
if (typeof value === 'string') {
|
|
122
|
+
const a = caseInsensitive ? this.value.toLowerCase() : this.value;
|
|
123
|
+
const b = caseInsensitive ? value.toLowerCase() : value;
|
|
124
|
+
return a.indexOf(b) !== -1;
|
|
125
|
+
}
|
|
126
|
+
if (value instanceof RegExp) {
|
|
127
|
+
const pattern = new RegExp(value, caseInsensitive ? 'i' : '');
|
|
128
|
+
return pattern.test(this.value);
|
|
129
|
+
}
|
|
130
|
+
return this.type === value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
toNumber() {
|
|
134
|
+
return parseFloat(this.value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
toJSON() {
|
|
138
|
+
return {
|
|
139
|
+
type: this.type,
|
|
140
|
+
value: this.value,
|
|
141
|
+
offset: this.offset,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|