Skip to content

Commit

Permalink
increase coverage, lower bundle size (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Jan 8, 2025
1 parent dc971f9 commit 3b1b95f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
.vscode
dist
codecov
codecov
coverage
8 changes: 1 addition & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "colorjs.io/fn"

// Register color spaces for parsing and converting
ColorSpace.register(sRGB) // Can parse keywords and hex colors
ColorSpace.register(sRGB) // Parses keywords and hex colors
ColorSpace.register(P3)
ColorSpace.register(HSL)
ColorSpace.register(LCH)
Expand All @@ -33,12 +33,6 @@ function numerify(value) {
if (typeof value === 'number' && Number.isFinite(value)) {
return value
}
if (Number.isNaN(value)) {
return 0
}
if (typeof value === 'object' && value !== null && 'raw' in value) {
return parseFloat(value.raw)
}
return 0
}

Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ test('invalid colors return a default object', () => {
const colors = [
'invalid',
'hsl(0, 0, 0)',
'rgb(0 0 0 1)',
'rgb(a, b, c, 0)',
]

for (let color of colors) {
Expand All @@ -50,6 +52,14 @@ test('invalid colors return a default object', () => {
authored: color
}, `Failed convert for '${color}'`)
}

assert.equal(convert('rgb(NaN NaN NaN / 1)'), {
hue: 0,
saturation: 0,
lightness: 0,
alpha: 1,
authored: 'rgb(NaN NaN NaN / 1)'
})
})

test('Colors are sorted by Hue', () => {
Expand Down Expand Up @@ -148,6 +158,19 @@ test('Grey-ish colors are sorted by Lightness, then by Alpha', () => {
assert.equal(actual, expected)
})

test('colors with identical transparency are sorted alphabetically', () => {
const colors = [
'RGBA(255, 0, 0, 0.5)',
'rgba(255, 0, 0, 0.5)',
]
const actual = sort(colors)
const expected = [
'RGBA(255, 0, 0, 0.5)',
'rgba(255, 0, 0, 0.5)',
]
assert.equal(actual, expected)
})

test('Fully transparent colors are sorted along their opaque companions', () => {
const colors = ['rgba(255, 0, 0, 0)', 'hsla(0, 100%, 50%, 0.1)', 'red']
const actual = sort(colors)
Expand Down

0 comments on commit 3b1b95f

Please sign in to comment.