@markuplint/types 4.8.1 → 4.18.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/ARCHITECTURE.ja.md +252 -0
- package/ARCHITECTURE.md +252 -0
- package/CHANGELOG.md +14 -2
- package/README.md +37 -2
- package/SKILL.md +100 -0
- package/docs/check-pipeline.ja.md +494 -0
- package/docs/check-pipeline.md +494 -0
- package/docs/token-system.ja.md +584 -0
- package/docs/token-system.md +584 -0
- package/docs/type-system.ja.md +556 -0
- package/docs/type-system.md +556 -0
- package/docs/validators.ja.md +514 -0
- package/docs/validators.md +514 -0
- package/lib/check-base.d.ts +71 -0
- package/lib/check-base.js +71 -0
- package/lib/check-multi-types.d.ts +12 -1
- package/lib/check-multi-types.js +11 -0
- package/lib/check.d.ts +13 -0
- package/lib/check.js +13 -0
- package/lib/css-defs.d.ts +8 -0
- package/lib/css-defs.js +8 -0
- package/lib/css-overrides.d.ts +7 -0
- package/lib/css-overrides.js +7 -0
- package/lib/css-syntax.d.ts +11 -0
- package/lib/css-syntax.js +12 -1
- package/lib/css-tokenizers.d.ts +6 -0
- package/lib/css-tokenizers.js +6 -0
- package/lib/debug.d.ts +3 -0
- package/lib/debug.js +3 -0
- package/lib/defs.d.ts +8 -0
- package/lib/defs.js +8 -0
- package/lib/directive.d.ts +14 -0
- package/lib/directive.js +14 -0
- package/lib/enum.d.ts +11 -0
- package/lib/enum.js +11 -0
- package/lib/get-candidate.d.ts +11 -0
- package/lib/get-candidate.js +11 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/keyword-type.d.ts +13 -0
- package/lib/keyword-type.js +13 -0
- package/lib/list.d.ts +13 -0
- package/lib/list.js +13 -0
- package/lib/match-result.d.ts +22 -1
- package/lib/match-result.js +21 -0
- package/lib/number.d.ts +12 -0
- package/lib/number.js +12 -0
- package/lib/primitive/is-float.d.ts +4 -1
- package/lib/primitive/is-float.js +4 -1
- package/lib/primitive/is-int.d.ts +4 -1
- package/lib/primitive/is-int.js +4 -1
- package/lib/primitive/is-non-zero-uint.d.ts +3 -2
- package/lib/primitive/is-non-zero-uint.js +3 -2
- package/lib/primitive/is-quantity.d.ts +5 -3
- package/lib/primitive/is-quantity.js +5 -3
- package/lib/primitive/is-uint.d.ts +5 -1
- package/lib/primitive/is-uint.js +5 -1
- package/lib/primitive/range.d.ts +5 -4
- package/lib/primitive/range.js +5 -4
- package/lib/primitive/split-unit.d.ts +3 -2
- package/lib/primitive/split-unit.js +3 -2
- package/lib/rfc/is-bcp-47.d.ts +2 -0
- package/lib/rfc/is-bcp-47.js +2 -0
- package/lib/token/token-collection.d.ts +108 -3
- package/lib/token/token-collection.js +103 -3
- package/lib/token/token.d.ts +66 -2
- package/lib/token/token.js +66 -2
- package/lib/token/types.d.ts +9 -0
- package/lib/types.d.ts +107 -0
- package/lib/w3c/check-serialized-permissions-policy.d.ts +2 -0
- package/lib/w3c/check-serialized-permissions-policy.js +2 -0
- package/lib/whatwg/check-autocomplete.d.ts +5 -0
- package/lib/whatwg/check-autocomplete.js +6 -0
- package/lib/whatwg/check-datetime/date-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/date-string.js +2 -0
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +13 -0
- package/lib/whatwg/check-datetime/datetime-tokens.js +13 -0
- package/lib/whatwg/check-datetime/duration-string.d.ts +7 -0
- package/lib/whatwg/check-datetime/duration-string.js +7 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +2 -0
- package/lib/whatwg/check-datetime/index.d.ts +5 -0
- package/lib/whatwg/check-datetime/index.js +5 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +4 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +4 -0
- package/lib/whatwg/check-datetime/month-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/month-string.js +2 -0
- package/lib/whatwg/check-datetime/time-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/time-string.js +2 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +8 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +8 -0
- package/lib/whatwg/check-datetime/week-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/week-string.js +2 -0
- package/lib/whatwg/check-datetime/year-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/year-string.js +2 -0
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/yearless-date-string.js +2 -0
- package/lib/whatwg/check-link-type.d.ts +5 -1
- package/lib/whatwg/check-link-type.js +5 -1
- package/lib/whatwg/check-mime-type.d.ts +4 -1
- package/lib/whatwg/check-mime-type.js +5 -3
- package/lib/whatwg/is-abs-url.d.ts +2 -0
- package/lib/whatwg/is-abs-url.js +2 -0
- package/lib/whatwg/is-browser-context-name.d.ts +2 -2
- package/lib/whatwg/is-browser-context-name.js +2 -2
- package/lib/whatwg/is-custom-element-name.d.ts +1 -1
- package/lib/whatwg/is-custom-element-name.js +1 -1
- package/lib/whatwg/is-itemprop-name.d.ts +1 -0
- package/lib/whatwg/is-itemprop-name.js +1 -0
- package/lib/whatwg/is-navigable-target-name.d.ts +2 -0
- package/lib/whatwg/is-navigable-target-name.js +2 -0
- package/package.json +8 -8
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid floating-point number.
|
|
3
|
+
*
|
|
2
4
|
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#floating-point-numbers
|
|
3
5
|
*
|
|
4
|
-
* @param value
|
|
6
|
+
* @param value - The string to validate
|
|
7
|
+
* @returns Whether the value is a valid floating-point number
|
|
5
8
|
*/
|
|
6
9
|
export function isFloat(value) {
|
|
7
10
|
return value === value.trim() && Number.isFinite(Number.parseFloat(value));
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid signed integer.
|
|
3
|
+
*
|
|
2
4
|
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#signed-integers
|
|
3
5
|
*
|
|
4
|
-
* @param value
|
|
6
|
+
* @param value - The string to validate
|
|
7
|
+
* @returns Whether the value is a valid signed integer
|
|
5
8
|
*/
|
|
6
9
|
export declare function isInt(value: string): boolean;
|
package/lib/primitive/is-int.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid signed integer.
|
|
3
|
+
*
|
|
2
4
|
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#signed-integers
|
|
3
5
|
*
|
|
4
|
-
* @param value
|
|
6
|
+
* @param value - The string to validate
|
|
7
|
+
* @returns Whether the value is a valid signed integer
|
|
5
8
|
*/
|
|
6
9
|
export function isInt(value) {
|
|
7
10
|
return /^-?\d+$/.test(value);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Checks whether a string is a valid non-negative integer greater than zero.
|
|
3
3
|
*
|
|
4
|
-
* @param value
|
|
4
|
+
* @param value - The string to validate
|
|
5
|
+
* @returns Whether the value is a valid non-zero unsigned integer
|
|
5
6
|
*/
|
|
6
7
|
export declare function isNonZeroUint(value: string): boolean;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Checks whether a string is a valid non-negative integer greater than zero.
|
|
3
3
|
*
|
|
4
|
-
* @param value
|
|
4
|
+
* @param value - The string to validate
|
|
5
|
+
* @returns Whether the value is a valid non-zero unsigned integer
|
|
5
6
|
*/
|
|
6
7
|
export function isNonZeroUint(value) {
|
|
7
8
|
return /^\d+$/.test(value) && !/^0+$/.test(value);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid number with one of the allowed unit suffixes.
|
|
2
3
|
*
|
|
3
|
-
* @param value
|
|
4
|
-
* @param units
|
|
5
|
-
* @param numberType
|
|
4
|
+
* @param value - The string to validate (e.g., "10px", "1.5em")
|
|
5
|
+
* @param units - The allowed unit suffixes (e.g., `["px", "em", "rem"]`)
|
|
6
|
+
* @param numberType - The number format constraint: `"int"`, `"uint"`, or `"float"` (defaults to `"float"`)
|
|
7
|
+
* @returns Whether the value is a valid quantity with an allowed unit
|
|
6
8
|
*/
|
|
7
9
|
export declare function isQuantity(value: string, units: readonly string[], numberType?: 'int' | 'uint' | 'float'): boolean;
|
|
@@ -3,10 +3,12 @@ import { isInt } from './is-int.js';
|
|
|
3
3
|
import { isUint } from './is-uint.js';
|
|
4
4
|
import { splitUnit } from './split-unit.js';
|
|
5
5
|
/**
|
|
6
|
+
* Checks whether a string is a valid number with one of the allowed unit suffixes.
|
|
6
7
|
*
|
|
7
|
-
* @param value
|
|
8
|
-
* @param units
|
|
9
|
-
* @param numberType
|
|
8
|
+
* @param value - The string to validate (e.g., "10px", "1.5em")
|
|
9
|
+
* @param units - The allowed unit suffixes (e.g., `["px", "em", "rem"]`)
|
|
10
|
+
* @param numberType - The number format constraint: `"int"`, `"uint"`, or `"float"` (defaults to `"float"`)
|
|
11
|
+
* @returns Whether the value is a valid quantity with an allowed unit
|
|
10
12
|
*/
|
|
11
13
|
export function isQuantity(value, units, numberType = 'float') {
|
|
12
14
|
const { num, unit } = splitUnit(value);
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid non-negative integer.
|
|
3
|
+
*
|
|
2
4
|
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#non-negative-integers
|
|
3
5
|
*
|
|
4
|
-
* @param value
|
|
6
|
+
* @param value - The string to validate
|
|
7
|
+
* @param options - Optional constraints; use `gt` to require a value greater than the specified number
|
|
8
|
+
* @returns Whether the value is a valid non-negative integer within the constraints
|
|
5
9
|
*/
|
|
6
10
|
export declare function isUint(value: string, options?: {
|
|
7
11
|
readonly gt?: number;
|
package/lib/primitive/is-uint.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Checks whether a string is a valid non-negative integer.
|
|
3
|
+
*
|
|
2
4
|
* @see https://html.spec.whatwg.org/dev/common-microsyntaxes.html#non-negative-integers
|
|
3
5
|
*
|
|
4
|
-
* @param value
|
|
6
|
+
* @param value - The string to validate
|
|
7
|
+
* @param options - Optional constraints; use `gt` to require a value greater than the specified number
|
|
8
|
+
* @returns Whether the value is a valid non-negative integer within the constraints
|
|
5
9
|
*/
|
|
6
10
|
export function isUint(value, options) {
|
|
7
11
|
const matched = /^\d+$/.test(value);
|
package/lib/primitive/range.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Checks whether a numeric string value falls within an inclusive range.
|
|
3
3
|
*
|
|
4
|
-
* @param value
|
|
5
|
-
* @param from
|
|
6
|
-
* @param to
|
|
4
|
+
* @param value - The string to parse as a number and validate
|
|
5
|
+
* @param from - The minimum allowed value (inclusive)
|
|
6
|
+
* @param to - The maximum allowed value (inclusive)
|
|
7
|
+
* @returns Whether the parsed number is within the range `[from, to]`
|
|
7
8
|
*/
|
|
8
9
|
export declare function range(value: string, from: number, to: number): boolean;
|
package/lib/primitive/range.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Checks whether a numeric string value falls within an inclusive range.
|
|
3
3
|
*
|
|
4
|
-
* @param value
|
|
5
|
-
* @param from
|
|
6
|
-
* @param to
|
|
4
|
+
* @param value - The string to parse as a number and validate
|
|
5
|
+
* @param from - The minimum allowed value (inclusive)
|
|
6
|
+
* @param to - The maximum allowed value (inclusive)
|
|
7
|
+
* @returns Whether the parsed number is within the range `[from, to]`
|
|
7
8
|
*/
|
|
8
9
|
export function range(value, from, to) {
|
|
9
10
|
const num = Number.parseFloat(value);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Splits a value string into its numeric and unit parts.
|
|
2
3
|
*
|
|
3
|
-
* @param value
|
|
4
|
-
* @returns
|
|
4
|
+
* @param value - The string to split (e.g., "10px", "1.5em")
|
|
5
|
+
* @returns An object with `num` (the numeric part) and `unit` (the unit suffix, or empty string)
|
|
5
6
|
*/
|
|
6
7
|
export declare function splitUnit(value: string): {
|
|
7
8
|
num: string;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Splits a value string into its numeric and unit parts.
|
|
2
3
|
*
|
|
3
|
-
* @param value
|
|
4
|
-
* @returns
|
|
4
|
+
* @param value - The string to split (e.g., "10px", "1.5em")
|
|
5
|
+
* @returns An object with `num` (the numeric part) and `unit` (the unit suffix, or empty string)
|
|
5
6
|
*/
|
|
6
7
|
export function splitUnit(value) {
|
|
7
8
|
value = value.trim().toLowerCase();
|
package/lib/rfc/is-bcp-47.d.ts
CHANGED
package/lib/rfc/is-bcp-47.js
CHANGED
|
@@ -4,8 +4,28 @@ import { Token } from './token.js';
|
|
|
4
4
|
type TokenCollectionOptions = Partial<Omit<List, 'token'> & {
|
|
5
5
|
specificSeparator: string | string[];
|
|
6
6
|
}>;
|
|
7
|
+
/**
|
|
8
|
+
* Callback function for sequential token checking in {@link TokenCollection.eachCheck}.
|
|
9
|
+
* Receives the current head token and remaining tail tokens.
|
|
10
|
+
* Returns a result to stop iteration, or `void` to continue.
|
|
11
|
+
*/
|
|
7
12
|
export type TokenEachCheck = (head: Readonly<Token> | null, tail: TokenCollection) => Result | void;
|
|
13
|
+
/**
|
|
14
|
+
* An ordered collection of tokens parsed from a string value.
|
|
15
|
+
*
|
|
16
|
+
* Extends `Array<Token>` with parsing, validation, and query capabilities.
|
|
17
|
+
* Supports configurable separators (space, comma, or custom), uniqueness
|
|
18
|
+
* constraints, ordering checks, and case sensitivity options.
|
|
19
|
+
*/
|
|
8
20
|
export declare class TokenCollection extends Array<Token> {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a TokenCollection by matching a value against a sequence of regex patterns.
|
|
23
|
+
*
|
|
24
|
+
* @param value - The source token or string to parse
|
|
25
|
+
* @param patterns - The regex patterns to match sequentially
|
|
26
|
+
* @param typeOptions - Optional collection configuration
|
|
27
|
+
* @returns A new TokenCollection containing the matched tokens
|
|
28
|
+
*/
|
|
9
29
|
static fromPatterns(value: Readonly<Token> | string, patterns: readonly Readonly<RegExp>[], typeOptions?: Omit<TokenCollectionOptions, 'specificSeparator'> & {
|
|
10
30
|
repeat?: boolean;
|
|
11
31
|
}): TokenCollection;
|
|
@@ -19,39 +39,124 @@ export declare class TokenCollection extends Array<Token> {
|
|
|
19
39
|
readonly unique: NonNullable<List['unique']>;
|
|
20
40
|
constructor(value?: string, typeOptions?: TokenCollectionOptions);
|
|
21
41
|
constructor(value?: number);
|
|
42
|
+
/**
|
|
43
|
+
* The concatenated string value of all tokens in this collection.
|
|
44
|
+
*/
|
|
22
45
|
get value(): string;
|
|
46
|
+
/**
|
|
47
|
+
* Validates the token collection structure against its configuration.
|
|
48
|
+
*
|
|
49
|
+
* Checks for unexpected spaces, consecutive commas, empty tokens,
|
|
50
|
+
* and duplicate values based on the collection's separator, allowEmpty,
|
|
51
|
+
* and unique settings.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Optional validation settings including expected values and reference URL
|
|
54
|
+
* @returns The validation result
|
|
55
|
+
*/
|
|
23
56
|
check(options?: {
|
|
24
57
|
expects?: Expect[];
|
|
25
58
|
ref?: string;
|
|
26
59
|
cache?: boolean;
|
|
27
|
-
}):
|
|
60
|
+
}): import("../types.js").MatchedResult | UnmatchedResult;
|
|
61
|
+
/**
|
|
62
|
+
* Splits this collection into chunks of the specified size.
|
|
63
|
+
*
|
|
64
|
+
* @param split - The number of tokens per chunk
|
|
65
|
+
* @returns An array of TokenCollection chunks
|
|
66
|
+
*/
|
|
28
67
|
chunk(split: number): TokenCollection[];
|
|
68
|
+
/**
|
|
69
|
+
* Iterates over consecutive token pairs, calling the callback for each pair.
|
|
70
|
+
*
|
|
71
|
+
* @param callback - A function receiving the previous and current token; return a token to stop iteration
|
|
72
|
+
* @returns The token returned by the callback, or `null` if iteration completes
|
|
73
|
+
*/
|
|
29
74
|
compareTokens(callback: (prev: Readonly<Token>, current: Readonly<Token>) => Readonly<Token> | null | void): Readonly<Token> | null | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Splits this collection into two at the given position.
|
|
77
|
+
*
|
|
78
|
+
* @param position - The index at which to split
|
|
79
|
+
* @returns A tuple of two TokenCollections: before and after the position
|
|
80
|
+
*/
|
|
30
81
|
divide(position: number): readonly [TokenCollection, TokenCollection];
|
|
82
|
+
/**
|
|
83
|
+
* Applies a sequence of check callbacks to consecutive tokens.
|
|
84
|
+
*
|
|
85
|
+
* Each callback receives the current head token and remaining tail.
|
|
86
|
+
* Tracks pass count and cumulative offset for error reporting.
|
|
87
|
+
*
|
|
88
|
+
* @param callbacks - The check functions to apply sequentially
|
|
89
|
+
* @returns The validation result
|
|
90
|
+
*/
|
|
31
91
|
eachCheck(...callbacks: readonly TokenEachCheck[]): Result;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a new TokenCollection containing only tokens that pass the test.
|
|
94
|
+
*
|
|
95
|
+
* @param callback - The filter predicate function
|
|
96
|
+
* @returns A new TokenCollection with the filtered tokens
|
|
97
|
+
*/
|
|
32
98
|
filter(callback: Parameters<Array<Token>['filter']>[0]): TokenCollection;
|
|
99
|
+
/**
|
|
100
|
+
* Finds the first occurrence of two consecutive tokens of the same type.
|
|
101
|
+
*
|
|
102
|
+
* @param tokenType - The token type number to check for consecutive occurrences
|
|
103
|
+
* @returns The second consecutive token, or `null` if none found
|
|
104
|
+
*/
|
|
33
105
|
getConsecutiveToken(tokenType: number): Readonly<Token> | null;
|
|
106
|
+
/**
|
|
107
|
+
* Finds the first duplicated token in this collection.
|
|
108
|
+
*
|
|
109
|
+
* Comparison respects the `caseInsensitive` setting of this collection.
|
|
110
|
+
*
|
|
111
|
+
* @returns The duplicated token, or `null` if all tokens are unique
|
|
112
|
+
*/
|
|
34
113
|
getDuplicated(): Token | null;
|
|
114
|
+
/**
|
|
115
|
+
* Returns only the identifier tokens, excluding whitespace and separators.
|
|
116
|
+
*
|
|
117
|
+
* @returns A new TokenCollection containing only Ident-type tokens
|
|
118
|
+
*/
|
|
35
119
|
getIdentTokens(): TokenCollection;
|
|
36
120
|
/**
|
|
121
|
+
* Checks whether any token in this collection matches the given value.
|
|
37
122
|
*
|
|
38
|
-
* @param value The token value
|
|
123
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
124
|
+
* @returns Whether any token matches
|
|
39
125
|
*/
|
|
40
126
|
has(value: TokenValue): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Splits this collection into a head token and a tail collection.
|
|
129
|
+
*
|
|
130
|
+
* @returns An object with `head` (first token or null) and `tail` (remaining tokens)
|
|
131
|
+
*/
|
|
41
132
|
headAndTail(): {
|
|
42
133
|
head: Token | null;
|
|
43
134
|
tail: TokenCollection;
|
|
44
135
|
};
|
|
45
136
|
/**
|
|
137
|
+
* Searches for the first token that includes the given value.
|
|
46
138
|
*
|
|
47
|
-
* @param value The token value
|
|
139
|
+
* @param value - The token value, type number, regex, or array to search for
|
|
140
|
+
* @returns The first matching token, or `null` if not found
|
|
48
141
|
*/
|
|
49
142
|
search(value: TokenValue): Token | null;
|
|
143
|
+
/**
|
|
144
|
+
* Verifies that tokens alternate between the specified types in order.
|
|
145
|
+
*
|
|
146
|
+
* @param tokenNumbers - The expected repeating pattern of token types
|
|
147
|
+
* @param lastTokenNumber - Optional expected type for the last token
|
|
148
|
+
* @returns An error object if the pattern is violated, or `null` if valid
|
|
149
|
+
*/
|
|
50
150
|
takeTurns(tokenNumbers: ReadonlyArray<number>, lastTokenNumber?: number): {
|
|
51
151
|
unexpectedLastToken: boolean;
|
|
52
152
|
expectedTokenNumber: number | undefined;
|
|
53
153
|
token: Token;
|
|
54
154
|
} | null;
|
|
155
|
+
/**
|
|
156
|
+
* Converts all tokens in this collection to plain JSON-serializable objects.
|
|
157
|
+
*
|
|
158
|
+
* @returns An array of plain token objects
|
|
159
|
+
*/
|
|
55
160
|
toJSON(): {
|
|
56
161
|
type: number;
|
|
57
162
|
value: string;
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { matched, unmatched } from '../match-result.js';
|
|
2
2
|
import { Token } from './token.js';
|
|
3
|
+
/**
|
|
4
|
+
* An ordered collection of tokens parsed from a string value.
|
|
5
|
+
*
|
|
6
|
+
* Extends `Array<Token>` with parsing, validation, and query capabilities.
|
|
7
|
+
* Supports configurable separators (space, comma, or custom), uniqueness
|
|
8
|
+
* constraints, ordering checks, and case sensitivity options.
|
|
9
|
+
*/
|
|
3
10
|
export class TokenCollection extends Array {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a TokenCollection by matching a value against a sequence of regex patterns.
|
|
13
|
+
*
|
|
14
|
+
* @param value - The source token or string to parse
|
|
15
|
+
* @param patterns - The regex patterns to match sequentially
|
|
16
|
+
* @param typeOptions - Optional collection configuration
|
|
17
|
+
* @returns A new TokenCollection containing the matched tokens
|
|
18
|
+
*/
|
|
4
19
|
static fromPatterns(value, patterns,
|
|
5
20
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
6
21
|
typeOptions) {
|
|
@@ -15,7 +30,7 @@ export class TokenCollection extends Array {
|
|
|
15
30
|
cumulativeOffset += tokenValue.length;
|
|
16
31
|
return token;
|
|
17
32
|
}
|
|
18
|
-
let isBroken
|
|
33
|
+
let isBroken;
|
|
19
34
|
do {
|
|
20
35
|
isBroken = false;
|
|
21
36
|
for (const pattern of patterns) {
|
|
@@ -107,10 +122,23 @@ export class TokenCollection extends Array {
|
|
|
107
122
|
offset += v.length;
|
|
108
123
|
}
|
|
109
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* The concatenated string value of all tokens in this collection.
|
|
127
|
+
*/
|
|
110
128
|
get value() {
|
|
111
129
|
const value = this.map(t => t.value).join('');
|
|
112
130
|
return value;
|
|
113
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Validates the token collection structure against its configuration.
|
|
134
|
+
*
|
|
135
|
+
* Checks for unexpected spaces, consecutive commas, empty tokens,
|
|
136
|
+
* and duplicate values based on the collection's separator, allowEmpty,
|
|
137
|
+
* and unique settings.
|
|
138
|
+
*
|
|
139
|
+
* @param options - Optional validation settings including expected values and reference URL
|
|
140
|
+
* @returns The validation result
|
|
141
|
+
*/
|
|
114
142
|
check(options = {}) {
|
|
115
143
|
const { expects, ref } = options;
|
|
116
144
|
if (this.separator !== 'space' && this.disallowToSurroundBySpaces) {
|
|
@@ -187,6 +215,12 @@ export class TokenCollection extends Array {
|
|
|
187
215
|
}
|
|
188
216
|
return matched();
|
|
189
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Splits this collection into chunks of the specified size.
|
|
220
|
+
*
|
|
221
|
+
* @param split - The number of tokens per chunk
|
|
222
|
+
* @returns An array of TokenCollection chunks
|
|
223
|
+
*/
|
|
190
224
|
chunk(split) {
|
|
191
225
|
const chunks = [];
|
|
192
226
|
const tokens = this.slice();
|
|
@@ -197,6 +231,12 @@ export class TokenCollection extends Array {
|
|
|
197
231
|
}
|
|
198
232
|
return chunks;
|
|
199
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Iterates over consecutive token pairs, calling the callback for each pair.
|
|
236
|
+
*
|
|
237
|
+
* @param callback - A function receiving the previous and current token; return a token to stop iteration
|
|
238
|
+
* @returns The token returned by the callback, or `null` if iteration completes
|
|
239
|
+
*/
|
|
200
240
|
compareTokens(callback) {
|
|
201
241
|
const _tokens = this.slice();
|
|
202
242
|
let prev = _tokens.shift();
|
|
@@ -213,6 +253,12 @@ export class TokenCollection extends Array {
|
|
|
213
253
|
}
|
|
214
254
|
return null;
|
|
215
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Splits this collection into two at the given position.
|
|
258
|
+
*
|
|
259
|
+
* @param position - The index at which to split
|
|
260
|
+
* @returns A tuple of two TokenCollections: before and after the position
|
|
261
|
+
*/
|
|
216
262
|
divide(position) {
|
|
217
263
|
const _a = this.slice(0, position);
|
|
218
264
|
const _b = this.slice(position);
|
|
@@ -220,6 +266,15 @@ export class TokenCollection extends Array {
|
|
|
220
266
|
const b = TokenCollection._new(_b, this);
|
|
221
267
|
return [a, b];
|
|
222
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Applies a sequence of check callbacks to consecutive tokens.
|
|
271
|
+
*
|
|
272
|
+
* Each callback receives the current head token and remaining tail.
|
|
273
|
+
* Tracks pass count and cumulative offset for error reporting.
|
|
274
|
+
*
|
|
275
|
+
* @param callbacks - The check functions to apply sequentially
|
|
276
|
+
* @returns The validation result
|
|
277
|
+
*/
|
|
223
278
|
eachCheck(...callbacks) {
|
|
224
279
|
let headAndTail = this.headAndTail();
|
|
225
280
|
let head = headAndTail.head;
|
|
@@ -286,9 +341,21 @@ export class TokenCollection extends Array {
|
|
|
286
341
|
}
|
|
287
342
|
return matched();
|
|
288
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Creates a new TokenCollection containing only tokens that pass the test.
|
|
346
|
+
*
|
|
347
|
+
* @param callback - The filter predicate function
|
|
348
|
+
* @returns A new TokenCollection with the filtered tokens
|
|
349
|
+
*/
|
|
289
350
|
filter(callback) {
|
|
290
351
|
return TokenCollection._new(super.filter(callback), this);
|
|
291
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Finds the first occurrence of two consecutive tokens of the same type.
|
|
355
|
+
*
|
|
356
|
+
* @param tokenType - The token type number to check for consecutive occurrences
|
|
357
|
+
* @returns The second consecutive token, or `null` if none found
|
|
358
|
+
*/
|
|
292
359
|
getConsecutiveToken(tokenType) {
|
|
293
360
|
const resultToken = this.compareTokens((prev, current) => {
|
|
294
361
|
if (prev.type === tokenType && current.type === tokenType) {
|
|
@@ -297,6 +364,13 @@ export class TokenCollection extends Array {
|
|
|
297
364
|
});
|
|
298
365
|
return resultToken ?? null;
|
|
299
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* Finds the first duplicated token in this collection.
|
|
369
|
+
*
|
|
370
|
+
* Comparison respects the `caseInsensitive` setting of this collection.
|
|
371
|
+
*
|
|
372
|
+
* @returns The duplicated token, or `null` if all tokens are unique
|
|
373
|
+
*/
|
|
300
374
|
getDuplicated() {
|
|
301
375
|
const aList = this.slice();
|
|
302
376
|
const bList = this.slice();
|
|
@@ -318,16 +392,28 @@ export class TokenCollection extends Array {
|
|
|
318
392
|
}
|
|
319
393
|
return null;
|
|
320
394
|
}
|
|
395
|
+
/**
|
|
396
|
+
* Returns only the identifier tokens, excluding whitespace and separators.
|
|
397
|
+
*
|
|
398
|
+
* @returns A new TokenCollection containing only Ident-type tokens
|
|
399
|
+
*/
|
|
321
400
|
getIdentTokens() {
|
|
322
401
|
return this.filter(token => token.type === Token.Ident);
|
|
323
402
|
}
|
|
324
403
|
/**
|
|
404
|
+
* Checks whether any token in this collection matches the given value.
|
|
325
405
|
*
|
|
326
|
-
* @param value The token value
|
|
406
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
407
|
+
* @returns Whether any token matches
|
|
327
408
|
*/
|
|
328
409
|
has(value) {
|
|
329
410
|
return this.some(t => t.matches(value));
|
|
330
411
|
}
|
|
412
|
+
/**
|
|
413
|
+
* Splits this collection into a head token and a tail collection.
|
|
414
|
+
*
|
|
415
|
+
* @returns An object with `head` (first token or null) and `tail` (remaining tokens)
|
|
416
|
+
*/
|
|
331
417
|
headAndTail() {
|
|
332
418
|
const copy = this.slice();
|
|
333
419
|
const head = copy.shift();
|
|
@@ -338,8 +424,10 @@ export class TokenCollection extends Array {
|
|
|
338
424
|
return { head, tail };
|
|
339
425
|
}
|
|
340
426
|
/**
|
|
427
|
+
* Searches for the first token that includes the given value.
|
|
341
428
|
*
|
|
342
|
-
* @param value The token value
|
|
429
|
+
* @param value - The token value, type number, regex, or array to search for
|
|
430
|
+
* @returns The first matching token, or `null` if not found
|
|
343
431
|
*/
|
|
344
432
|
search(value) {
|
|
345
433
|
for (const token of this) {
|
|
@@ -349,6 +437,13 @@ export class TokenCollection extends Array {
|
|
|
349
437
|
}
|
|
350
438
|
return null;
|
|
351
439
|
}
|
|
440
|
+
/**
|
|
441
|
+
* Verifies that tokens alternate between the specified types in order.
|
|
442
|
+
*
|
|
443
|
+
* @param tokenNumbers - The expected repeating pattern of token types
|
|
444
|
+
* @param lastTokenNumber - Optional expected type for the last token
|
|
445
|
+
* @returns An error object if the pattern is violated, or `null` if valid
|
|
446
|
+
*/
|
|
352
447
|
takeTurns(tokenNumbers, lastTokenNumber) {
|
|
353
448
|
const tokens = this.slice();
|
|
354
449
|
for (let i = 0; i < tokens.length; i++) {
|
|
@@ -374,6 +469,11 @@ export class TokenCollection extends Array {
|
|
|
374
469
|
}
|
|
375
470
|
return null;
|
|
376
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* Converts all tokens in this collection to plain JSON-serializable objects.
|
|
474
|
+
*
|
|
475
|
+
* @returns An array of plain token objects
|
|
476
|
+
*/
|
|
377
477
|
toJSON() {
|
|
378
478
|
return this.map(t => t.toJSON());
|
|
379
479
|
}
|
package/lib/token/token.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { TokenValue } from './types.js';
|
|
2
2
|
import type { UnmatchedResult, UnmatchedResultOptions, UnmatchedResultReason } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a single token within a parsed string value.
|
|
5
|
+
*
|
|
6
|
+
* Tracks the token's value, type (whitespace, comma, or identifier),
|
|
7
|
+
* offset position within the original string, and provides methods
|
|
8
|
+
* for matching and comparison operations.
|
|
9
|
+
*/
|
|
3
10
|
export declare class Token {
|
|
4
11
|
/**
|
|
5
12
|
* @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
|
|
@@ -26,11 +33,32 @@ export declare class Token {
|
|
|
26
33
|
* @deprecated Use {@link getPosition} instead. Will be removed in v5.0.0.
|
|
27
34
|
*/
|
|
28
35
|
static getLine(value: string, offset: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Calculates the line and column position at the given offset within a string.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The source string
|
|
40
|
+
* @param offset - The character offset to calculate the position for
|
|
41
|
+
* @returns The line number (1-based) and column number (1-based)
|
|
42
|
+
*/
|
|
29
43
|
static getPosition(value: string, offset: number): {
|
|
30
44
|
line: number;
|
|
31
45
|
column: number;
|
|
32
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Determines the token type based on the first character.
|
|
49
|
+
*
|
|
50
|
+
* @param value - The token string value
|
|
51
|
+
* @param separators - Optional separator characters to detect
|
|
52
|
+
* @returns The token type number (WhiteSpace, Comma, or Ident)
|
|
53
|
+
*/
|
|
33
54
|
static getType(value: string, separators?: readonly string[]): 1 | 18 | 13;
|
|
55
|
+
/**
|
|
56
|
+
* Calculates a new position by shifting from a token's offset.
|
|
57
|
+
*
|
|
58
|
+
* @param token - The base token to shift from
|
|
59
|
+
* @param offset - The additional offset to apply
|
|
60
|
+
* @returns The shifted offset, line, and column
|
|
61
|
+
*/
|
|
34
62
|
static shiftLocation(token: Readonly<Token>, offset: number): {
|
|
35
63
|
offset: number;
|
|
36
64
|
line: number;
|
|
@@ -40,25 +68,61 @@ export declare class Token {
|
|
|
40
68
|
readonly originalValue: string;
|
|
41
69
|
readonly type: number;
|
|
42
70
|
readonly value: string;
|
|
71
|
+
/**
|
|
72
|
+
* @param value - The token string value
|
|
73
|
+
* @param offset - The offset position within the original string
|
|
74
|
+
* @param originalValue - The complete original string this token was parsed from
|
|
75
|
+
* @param separators - Optional separator characters used for type detection
|
|
76
|
+
*/
|
|
43
77
|
constructor(value: string, offset: number, originalValue: string, separators?: readonly string[]);
|
|
78
|
+
/**
|
|
79
|
+
* The character length of the token value.
|
|
80
|
+
*/
|
|
44
81
|
get length(): number;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a copy of this token.
|
|
84
|
+
*
|
|
85
|
+
* @returns A new Token instance with the same value, offset, and original value
|
|
86
|
+
*/
|
|
45
87
|
clone(): Token;
|
|
46
88
|
/**
|
|
89
|
+
* Checks whether this token's value contains the given value.
|
|
47
90
|
*
|
|
48
|
-
* @param value The token value
|
|
91
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
92
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
93
|
+
* @returns Whether this token includes the given value
|
|
49
94
|
*/
|
|
50
95
|
includes(value: TokenValue, caseInsensitive?: boolean): boolean;
|
|
51
96
|
/**
|
|
97
|
+
* Checks whether this token exactly matches the given value.
|
|
52
98
|
*
|
|
53
|
-
* @param value The token value
|
|
99
|
+
* @param value - The token value, type number, regex, or array to match against
|
|
100
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
101
|
+
* @returns Whether this token matches the given value
|
|
54
102
|
*/
|
|
55
103
|
matches(value: TokenValue, caseInsensitive?: boolean): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Converts this token to a plain JSON-serializable object.
|
|
106
|
+
*
|
|
107
|
+
* @returns An object with type, value, and offset properties
|
|
108
|
+
*/
|
|
56
109
|
toJSON(): {
|
|
57
110
|
type: number;
|
|
58
111
|
value: string;
|
|
59
112
|
offset: number;
|
|
60
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* Parses the token value as a floating-point number.
|
|
116
|
+
*
|
|
117
|
+
* @returns The parsed number, or 0 if parsing fails
|
|
118
|
+
*/
|
|
61
119
|
toNumber(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Creates an unmatched result positioned at this token's location.
|
|
122
|
+
*
|
|
123
|
+
* @param options - Optional settings including ref, reason, and expected values
|
|
124
|
+
* @returns An unmatched result with this token's position information
|
|
125
|
+
*/
|
|
62
126
|
unmatched(options?: UnmatchedResultOptions & {
|
|
63
127
|
readonly ref?: string;
|
|
64
128
|
readonly reason?: UnmatchedResultReason;
|