Skip to content

Commit 4f588ea

Browse files
authored
feat: export all sub-types and constants (#33)
1 parent 53c36cc commit 4f588ea

File tree

5 files changed

+72
-34
lines changed

5 files changed

+72
-34
lines changed

src/destructure-box-shadow.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { parse, type CssNode, type Value } from 'css-tree'
22
import { color_to_token } from './colors.js'
3-
import type { ColorToken, ColorValue } from './types.js'
3+
import type { ColorValue } from './types.js'
44
import { namedColors as named_colors, systemColors as system_colors, colorFunctions as color_functions } from '@projectwallace/css-analyzer'
55

6-
type CssLength = {
6+
export type CssLength = {
77
value: number
88
unit: string
99
}
1010

11-
export type DestructuredShadow = {
11+
export type ShadowValue = {
1212
color: ColorValue | undefined
1313
offsetX: CssLength | undefined
1414
offsetY: CssLength | undefined
@@ -17,7 +17,7 @@ export type DestructuredShadow = {
1717
inset: boolean | undefined
1818
}
1919

20-
function create_destructured(): DestructuredShadow {
20+
function create_destructured(): ShadowValue {
2121
return {
2222
color: undefined,
2323
offsetX: undefined,
@@ -28,10 +28,10 @@ function create_destructured(): DestructuredShadow {
2828
}
2929
}
3030

31-
export function destructure_box_shadow(value: string): null | DestructuredShadow[] {
31+
export function destructure_box_shadow(value: string): null | ShadowValue[] {
3232
let ast = parse(value, {
3333
context: 'value',
34-
positions: true
34+
positions: true,
3535
}) as Value
3636

3737
function generate(node: CssNode) {
@@ -42,7 +42,7 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
4242
}
4343

4444
let current_shadow = create_destructured()
45-
let destructured: DestructuredShadow[] = [current_shadow]
45+
let destructured: ShadowValue[] = [current_shadow]
4646

4747
if (ast.children.size < 2) {
4848
return null
@@ -59,15 +59,17 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
5959
}
6060
current_shadow.color = color_token
6161
}
62-
}
63-
else if (node.type === 'Dimension' || (node.type === 'Number' && node.value === '0')) {
64-
let length = node.type === 'Dimension' ? {
65-
value: Number(node.value),
66-
unit: node.unit
67-
} : {
68-
value: 0,
69-
unit: 'px',
70-
}
62+
} else if (node.type === 'Dimension' || (node.type === 'Number' && node.value === '0')) {
63+
let length =
64+
node.type === 'Dimension'
65+
? {
66+
value: Number(node.value),
67+
unit: node.unit,
68+
}
69+
: {
70+
value: 0,
71+
unit: 'px',
72+
}
7173

7274
if (!current_shadow.offsetX) {
7375
current_shadow.offsetX = length
@@ -78,8 +80,7 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
7880
} else if (!current_shadow.spread) {
7981
current_shadow.spread = length
8082
}
81-
}
82-
else if (node.type === 'Function') {
83+
} else if (node.type === 'Function') {
8384
if (color_functions.has(node.name)) {
8485
let color_token = color_to_token(generate(node))
8586
if (color_token === null) {
@@ -93,15 +94,13 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
9394
}
9495
current_shadow.color = color_token
9596
}
96-
}
97-
else if (node.type === 'Hash') {
97+
} else if (node.type === 'Hash') {
9898
let color_token = color_to_token(generate(node))
9999
if (color_token === null) {
100100
return
101101
}
102102
current_shadow.color = color_token
103-
}
104-
else if (node.type === 'Operator' && node.value === ',') {
103+
} else if (node.type === 'Operator' && node.value === ',') {
105104
// Start a new shadow, but only after we've made sure that the current shadow is valid
106105
complete_shadow_token(current_shadow)
107106
current_shadow = create_destructured()
@@ -116,15 +115,15 @@ export function destructure_box_shadow(value: string): null | DestructuredShadow
116115

117116
const DIMENSION_ZERO = {
118117
value: 0,
119-
unit: 'px'
118+
unit: 'px',
120119
}
121120

122121
/**
123122
* @description
124123
* According to the spec every shadow MUST have an offsetX, offsetY, blur, spread and color,
125124
* only the inset is optional and defaults to false.
126125
*/
127-
function complete_shadow_token(token: DestructuredShadow) {
126+
function complete_shadow_token(token: ShadowValue) {
128127
if (!token.offsetX) {
129128
token.offsetX = DIMENSION_ZERO
130129
}

src/destructure-font-family.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { parse, type Value, type CssNode } from 'css-tree'
22
import { unquote } from './unquote.js'
3+
import type { FontFamilyValue } from './types.js'
34

4-
export function destructure_font_family(value: string): string[] | undefined {
5+
export function destructure_font_family(value: string): FontFamilyValue | undefined {
56
if (value.toLowerCase().includes('var(')) {
67
return undefined
78
}

src/index.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { test, expect, describe } from 'vitest'
2-
import { analysis_to_tokens, css_to_tokens } from './index.js'
3-
import { EXTENSION_AUTHORED_AS, EXTENSION_CSS_PROPERTIES, EXTENSION_USAGE_COUNT } from './types.js'
2+
import { analysis_to_tokens, css_to_tokens, EXTENSION_AUTHORED_AS, EXTENSION_CSS_PROPERTIES, EXTENSION_USAGE_COUNT } from './index.js'
43
import { analyze } from '@projectwallace/css-analyzer'
54

65
describe('analysis_to_tokens', () => {

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ import {
2323
} from './types.js'
2424
import { color_to_token } from './colors.js'
2525

26+
export {
27+
EXTENSION_AUTHORED_AS,
28+
EXTENSION_USAGE_COUNT,
29+
EXTENSION_CSS_PROPERTIES,
30+
type Easing,
31+
type CubicBezierToken,
32+
type DimensionValue,
33+
type DimensionToken,
34+
type DurationToken,
35+
type DurationValue,
36+
type FontFamilyToken,
37+
type NumberToken,
38+
type UnparsedToken,
39+
type CssAnalysis,
40+
type ShadowToken,
41+
type ColorSpace,
42+
type ColorToken,
43+
type ColorValue,
44+
type ColorComponent,
45+
} from './types.js'
46+
export type { CssLength, ShadowValue } from './destructure-box-shadow.js'
47+
2648
export function css_to_tokens(css: string) {
2749
let analysis = analyze(css)
2850
return analysis_to_tokens(analysis)

src/types.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { analyze } from '@projectwallace/css-analyzer'
2-
import type { DestructuredShadow } from './destructure-box-shadow'
2+
import type { ShadowValue } from './destructure-box-shadow'
33

44
export type CssAnalysis = ReturnType<typeof analyze>
55

@@ -11,12 +11,12 @@ export type Easing = [number, number, number, number]
1111

1212
export type BaseToken = {
1313
$extensions: {
14-
[EXTENSION_AUTHORED_AS]: string,
14+
[EXTENSION_AUTHORED_AS]: string
1515
[EXTENSION_USAGE_COUNT]: number
1616
}
1717
}
1818

19-
type DurationValue = {
19+
export type DurationValue = {
2020
value: number
2121
unit: 'ms'
2222
}
@@ -31,7 +31,22 @@ export type UnparsedToken = BaseToken & {
3131
$type?: never
3232
}
3333

34-
type ColorSpace = string | 'srgb' | 'display-p3' | 'hsl' | 'hwb' | 'lab' | 'lch' | 'oklab' | 'oklch' | 'display-p3' | 'a98-rgb' | 'prophoto-rgb' | 'rec2020' | 'xyz-d65' | 'xyz-d50'
34+
export type ColorSpace =
35+
| string
36+
| 'srgb'
37+
| 'display-p3'
38+
| 'hsl'
39+
| 'hwb'
40+
| 'lab'
41+
| 'lch'
42+
| 'oklab'
43+
| 'oklch'
44+
| 'display-p3'
45+
| 'a98-rgb'
46+
| 'prophoto-rgb'
47+
| 'rec2020'
48+
| 'xyz-d65'
49+
| 'xyz-d50'
3550

3651
export type ColorComponent = number | 'none'
3752

@@ -44,7 +59,7 @@ export type ColorValue = {
4459

4560
export type ColorToken = BaseToken & {
4661
$type: 'color'
47-
$value: ColorValue,
62+
$value: ColorValue
4863
$extensions: {
4964
[EXTENSION_CSS_PROPERTIES]: Array<string>
5065
}
@@ -70,12 +85,14 @@ export type CubicBezierToken = BaseToken & {
7085
$value: Easing
7186
}
7287

88+
export type FontFamilyValue = string[]
89+
7390
export type FontFamilyToken = BaseToken & {
7491
$type: 'fontFamily'
75-
$value: string[]
92+
$value: FontFamilyValue
7693
}
7794

7895
export type ShadowToken = BaseToken & {
7996
$type: 'shadow'
80-
$value: DestructuredShadow | DestructuredShadow[]
97+
$value: ShadowValue | ShadowValue[]
8198
}

0 commit comments

Comments
 (0)