Skip to content

Commit b4b9249

Browse files
authored
fix: make Length type spec-compliant; expose color_to_token(); `col… (#38)
* fix: make Length type spec-compliant; expose `color_to_token()`; `colorSpace` is no longer a string * fix: casing
1 parent 340e886 commit b4b9249

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

src/colors.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { colorKeywords as color_keywords, cssKeywords as css_keywords } from '@projectwallace/css-analyzer'
2-
import { EXTENSION_AUTHORED_AS, type ColorToken, type ColorValue, type UnparsedToken } from './types.js'
2+
import { type ColorValue, type ColorSpace as tColorSpace } from './types.js'
33
import {
44
parse,
55
ColorSpace,
@@ -25,7 +25,7 @@ import {
2525
OKLab,
2626
OKLCH,
2727
OKLrab,
28-
} from "colorjs.io/fn"
28+
} from 'colorjs.io/fn'
2929

3030
// Register color spaces for parsing and converting
3131
ColorSpace.register(sRGB) // Parses keywords and hex colors
@@ -77,13 +77,9 @@ export function color_to_token(color: string): ColorValue | null {
7777
let [component_a, component_b, component_c] = parsed_color.coords
7878

7979
return {
80-
colorSpace: parsed_color.spaceId,
81-
components: [
82-
component_a ?? 'none',
83-
component_b ?? 'none',
84-
component_c ?? 'none',
85-
],
86-
alpha: parsed_color.alpha ?? 0,
80+
colorSpace: parsed_color.spaceId as tColorSpace,
81+
components: [component_a ?? 'none', component_b ?? 'none', component_c ?? 'none'],
82+
alpha: parsed_color.alpha ?? 1,
8783
}
8884
} catch (error) {
8985
// A catch for edge cases that we don't support yet.

src/destructure-line-height.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ test('number', () => {
2727
expect.soft(destructure_line_height('1e2')).toEqual(100)
2828
})
2929

30-
test('length', () => {
30+
test('lengths with spec-compliant units', () => {
3131
expect.soft(destructure_line_height('1px')).toEqual({ value: 1, unit: 'px' })
32-
expect.soft(destructure_line_height('1em')).toEqual({ value: 1, unit: 'em' })
3332
expect.soft(destructure_line_height('1rem')).toEqual({ value: 1, unit: 'rem' })
34-
expect.soft(destructure_line_height('1cm')).toEqual({ value: 1, unit: 'cm' })
35-
3633
expect.soft(destructure_line_height('1.1px')).toEqual({ value: 1.1, unit: 'px' })
37-
expect.soft(destructure_line_height('1e2em')).toEqual({ value: 100, unit: 'em' })
34+
expect.soft(destructure_line_height('100px')).toEqual({ value: 100, unit: 'px' })
35+
expect.soft(destructure_line_height('1e2rem')).toEqual({ value: 100, unit: 'rem' })
36+
expect.soft(destructure_line_height('1REM')).toEqual({ value: 1, unit: 'rem' })
37+
expect.soft(destructure_line_height('1PX')).toEqual({ value: 1, unit: 'px' })
38+
})
39+
40+
test('lenghts with unsupported units', () => {
41+
expect.soft(destructure_line_height('1em')).toEqual(null)
42+
expect.soft(destructure_line_height('1cm')).toEqual(null)
43+
expect.soft(destructure_line_height('1e2em')).toEqual(null)
3844
})
3945

4046
test('zero', () => {

src/destructure-line-height.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { parse, type Value } from 'css-tree'
2+
import { type Length, type Unit } from './types'
23

3-
type Length = {
4-
value: number
5-
unit: string
6-
}
4+
const ALLOWED_UNITS = new Set(['px', 'rem'])
75

86
/**
97
* @description Destructure a line-height from a string
@@ -25,10 +23,14 @@ export function destructure_line_height(value: string): Length | number | null {
2523
if (value === 0) {
2624
return 0
2725
}
28-
return {
29-
value,
30-
unit: maybe_dimension.unit
26+
let unit = maybe_dimension.unit.toLowerCase()
27+
if (ALLOWED_UNITS.has(unit)) {
28+
return {
29+
value,
30+
unit: unit as Unit,
31+
}
3132
}
33+
return null
3234
}
3335
case 'Number': {
3436
return Number(maybe_dimension.value)
@@ -47,4 +49,4 @@ export function destructure_line_height(value: string): Length | number | null {
4749
}
4850

4951
return null
50-
}
52+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type ShadowToken,
2323
} from './types.js'
2424
import { color_to_token } from './colors.js'
25+
export { color_to_token } from './colors.js'
2526

2627
export {
2728
EXTENSION_AUTHORED_AS,

src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export type BaseToken = {
1616
}
1717
}
1818

19+
export type Unit = 'rem' | 'px'
20+
21+
export type Length = {
22+
value: number
23+
unit: Unit
24+
}
25+
1926
export type DurationValue = {
2027
value: number
2128
unit: 'ms'
@@ -32,7 +39,6 @@ export type UnparsedToken = BaseToken & {
3239
}
3340

3441
export type ColorSpace =
35-
| string
3642
| 'srgb'
3743
| 'display-p3'
3844
| 'hsl'
@@ -67,7 +73,7 @@ export type ColorToken = BaseToken & {
6773

6874
export type DimensionValue = {
6975
value: number
70-
unit: string
76+
unit: Unit
7177
}
7278

7379
export type DimensionToken = BaseToken & {

0 commit comments

Comments
 (0)