@ax-design/typed-om 0.1.0 → 0.2.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/README.md +7 -2
- package/dist/reader.js +6 -0
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +19 -10
- package/package.json +1 -1
- package/src/reader.ts +3 -0
- package/src/schema.ts +20 -10
package/README.md
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
<h1 align="center">
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ax-design/typed-om/master/docs/logo-r.png">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/ax-design/typed-om/master/docs/logo.png" alt="Ax Typed OM">
|
|
5
|
+
</picture>
|
|
6
|
+
</h1>
|
|
2
7
|
|
|
3
8
|
<p align="center">
|
|
4
|
-
Typed reads and
|
|
9
|
+
Typed reads and monitors CSS custom properties for Axiom Design System.
|
|
5
10
|
</p>
|
|
6
11
|
|
|
7
12
|
<p align="center">
|
package/dist/reader.js
CHANGED
|
@@ -41,10 +41,16 @@ export class StyleReader {
|
|
|
41
41
|
function parseFast(spec, text) {
|
|
42
42
|
switch (spec.syntax.type) {
|
|
43
43
|
case 'length':
|
|
44
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0)
|
|
45
|
+
return text;
|
|
44
46
|
return parseLength(text);
|
|
45
47
|
case 'length-percentage':
|
|
48
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0)
|
|
49
|
+
return text;
|
|
46
50
|
return parseLengthPercentage(text);
|
|
47
51
|
case 'number':
|
|
52
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0)
|
|
53
|
+
return text;
|
|
48
54
|
return parseNumber(text);
|
|
49
55
|
case 'color':
|
|
50
56
|
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0)
|
package/dist/schema.d.ts
CHANGED
package/dist/schema.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
const RESERVED = ['initial', 'inherit', 'unset', 'revert', 'revert-layer', 'default'];
|
|
2
|
+
function checkKeywords(name, keywords) {
|
|
3
|
+
for (const value of keywords) {
|
|
4
|
+
if (RESERVED.indexOf(value) >= 0)
|
|
5
|
+
throw new Error(`${name}: keyword "${value}" cannot be observed`);
|
|
6
|
+
if (!/^-?[_a-zA-Z][_a-zA-Z0-9-]*$/.test(value))
|
|
7
|
+
throw new Error(`${name}: "${value}" is not an identifier`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
2
10
|
export function validateSchema(schema) {
|
|
3
11
|
const seen = [];
|
|
4
12
|
for (const spec of schema) {
|
|
@@ -8,24 +16,25 @@ export function validateSchema(schema) {
|
|
|
8
16
|
seen.push(spec.name);
|
|
9
17
|
if (spec.name.indexOf('--') !== 0)
|
|
10
18
|
throw new Error(`${spec.name} is not a custom property name`);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
const syntax = spec.syntax;
|
|
20
|
+
if (syntax.type === 'keyword') {
|
|
21
|
+
checkKeywords(spec.name, syntax.values);
|
|
22
|
+
}
|
|
23
|
+
else if (syntax.type !== 'color' && syntax.keywords) {
|
|
24
|
+
// Numeric types may hold a keyword instead of a value ("<length> | auto");
|
|
25
|
+
// validate them the same way as the keyword type. Color keywords stay unvalidated.
|
|
26
|
+
checkKeywords(spec.name, syntax.keywords);
|
|
18
27
|
}
|
|
19
28
|
}
|
|
20
29
|
}
|
|
21
30
|
export function cssSyntax(syntax) {
|
|
22
31
|
switch (syntax.type) {
|
|
23
32
|
case 'length':
|
|
24
|
-
return '<length>';
|
|
33
|
+
return syntax.keywords && syntax.keywords.length ? `<length> | ${syntax.keywords.join(' | ')}` : '<length>';
|
|
25
34
|
case 'length-percentage':
|
|
26
|
-
return '<length-percentage>';
|
|
35
|
+
return syntax.keywords && syntax.keywords.length ? `<length-percentage> | ${syntax.keywords.join(' | ')}` : '<length-percentage>';
|
|
27
36
|
case 'number':
|
|
28
|
-
return '<number>';
|
|
37
|
+
return syntax.keywords && syntax.keywords.length ? `<number> | ${syntax.keywords.join(' | ')}` : '<number>';
|
|
29
38
|
case 'color':
|
|
30
39
|
return syntax.keywords && syntax.keywords.length ? `<color> | ${syntax.keywords.join(' | ')}` : '<color>';
|
|
31
40
|
case 'keyword':
|
package/package.json
CHANGED
package/src/reader.ts
CHANGED
|
@@ -54,10 +54,13 @@ export class StyleReader {
|
|
|
54
54
|
function parseFast(spec: PropertySpec, text: string): PropertyValue {
|
|
55
55
|
switch (spec.syntax.type) {
|
|
56
56
|
case 'length':
|
|
57
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0) return text;
|
|
57
58
|
return parseLength(text);
|
|
58
59
|
case 'length-percentage':
|
|
60
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0) return text;
|
|
59
61
|
return parseLengthPercentage(text);
|
|
60
62
|
case 'number':
|
|
63
|
+
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0) return text;
|
|
61
64
|
return parseNumber(text);
|
|
62
65
|
case 'color':
|
|
63
66
|
if (spec.syntax.keywords && spec.syntax.keywords.indexOf(text) >= 0) return text;
|
package/src/schema.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Description of one custom property the caller wants to read and observe.
|
|
2
2
|
export type PropertySyntax =
|
|
3
|
-
| { type: 'length' }
|
|
4
|
-
| { type: 'length-percentage' }
|
|
5
|
-
| { type: 'number' }
|
|
3
|
+
| { type: 'length'; keywords?: string[] }
|
|
4
|
+
| { type: 'length-percentage'; keywords?: string[] }
|
|
5
|
+
| { type: 'number'; keywords?: string[] }
|
|
6
6
|
// `keywords`: extra identifiers the property may hold instead of a color (returned as strings).
|
|
7
7
|
| { type: 'color'; keywords?: string[] }
|
|
8
8
|
// Allowed values. CSS-wide keywords are rejected. `none` is allowed for existing APIs, but
|
|
@@ -37,6 +37,13 @@ export type PropertyValue = LengthPercentage | number | string | Rgba | null;
|
|
|
37
37
|
|
|
38
38
|
const RESERVED = ['initial', 'inherit', 'unset', 'revert', 'revert-layer', 'default'];
|
|
39
39
|
|
|
40
|
+
function checkKeywords(name: string, keywords: string[]): void {
|
|
41
|
+
for (const value of keywords) {
|
|
42
|
+
if (RESERVED.indexOf(value) >= 0) throw new Error(`${name}: keyword "${value}" cannot be observed`);
|
|
43
|
+
if (!/^-?[_a-zA-Z][_a-zA-Z0-9-]*$/.test(value)) throw new Error(`${name}: "${value}" is not an identifier`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
40
47
|
export function validateSchema(schema: PropertySpec[]): void {
|
|
41
48
|
const seen: string[] = [];
|
|
42
49
|
for (const spec of schema) {
|
|
@@ -45,10 +52,13 @@ export function validateSchema(schema: PropertySpec[]): void {
|
|
|
45
52
|
}
|
|
46
53
|
seen.push(spec.name);
|
|
47
54
|
if (spec.name.indexOf('--') !== 0) throw new Error(`${spec.name} is not a custom property name`);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
const syntax = spec.syntax;
|
|
56
|
+
if (syntax.type === 'keyword') {
|
|
57
|
+
checkKeywords(spec.name, syntax.values);
|
|
58
|
+
} else if (syntax.type !== 'color' && syntax.keywords) {
|
|
59
|
+
// Numeric types may hold a keyword instead of a value ("<length> | auto");
|
|
60
|
+
// validate them the same way as the keyword type. Color keywords stay unvalidated.
|
|
61
|
+
checkKeywords(spec.name, syntax.keywords);
|
|
52
62
|
}
|
|
53
63
|
}
|
|
54
64
|
}
|
|
@@ -56,11 +66,11 @@ export function validateSchema(schema: PropertySpec[]): void {
|
|
|
56
66
|
export function cssSyntax(syntax: PropertySyntax): string {
|
|
57
67
|
switch (syntax.type) {
|
|
58
68
|
case 'length':
|
|
59
|
-
return '<length>';
|
|
69
|
+
return syntax.keywords && syntax.keywords.length ? `<length> | ${syntax.keywords.join(' | ')}` : '<length>';
|
|
60
70
|
case 'length-percentage':
|
|
61
|
-
return '<length-percentage>';
|
|
71
|
+
return syntax.keywords && syntax.keywords.length ? `<length-percentage> | ${syntax.keywords.join(' | ')}` : '<length-percentage>';
|
|
62
72
|
case 'number':
|
|
63
|
-
return '<number>';
|
|
73
|
+
return syntax.keywords && syntax.keywords.length ? `<number> | ${syntax.keywords.join(' | ')}` : '<number>';
|
|
64
74
|
case 'color':
|
|
65
75
|
return syntax.keywords && syntax.keywords.length ? `<color> | ${syntax.keywords.join(' | ')}` : '<color>';
|
|
66
76
|
case 'keyword':
|