-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhance HSLA color parsing and validation
This commit improves HSLA color parsing by returning a detailed object including hue, saturation, lightness, alpha, and their respective units. It also enhances the HSLA validation to support different units for hue and allows alpha as both a percentage and a floating number. The test suite has been expanded accordingly.
- Loading branch information
1 parent
b0339c6
commit 7648724
Showing
5 changed files
with
224 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,145 @@ | ||
import { parseHsla } from "@/parser/parseHsla"; | ||
|
||
describe('parseHsla', () => { | ||
test('parses valid HSLA colors correctly', () => { | ||
expect(parseHsla('hsla(120, 100%, 50%, 0.5)')).toEqual([120, 100, 50, 0.5]); | ||
expect(parseHsla('hsla(240, 50%, 50%, 1)')).toEqual([240, 50, 50, 1]); | ||
expect(parseHsla('hsla(360, 100%, 100%, 0)')).toEqual([360, 100, 100, 0]); | ||
expect(parseHsla('hsla(0, 0%, 0%, 0.25)')).toEqual([0, 0, 0, 0.25]); | ||
}); | ||
|
||
test('parses HSLA colors with different units for hue correctly', () => { | ||
expect(parseHsla('hsla(180deg, 100%, 50%, 0.5)')).toEqual([180, 100, 50, 0.5]); | ||
expect(parseHsla('hsla(3.14rad, 100%, 50%, 0.5)')).toEqual([180, 100, 50, 0.5]); | ||
expect(parseHsla('hsla(200grad, 100%, 50%, 0.5)')).toEqual([180, 100, 50, 0.5]); | ||
expect(parseHsla('hsla(0.5turn, 100%, 50%, 0.5)')).toEqual([180, 100, 50, 0.5]); | ||
}); | ||
|
||
test('parses HSLA colors with different alpha values correctly', () => { | ||
expect(parseHsla('hsla(120, 100%, 50%, 0.75)')).toEqual([120, 100, 50, 0.75]); | ||
expect(parseHsla('hsla(240, 50%, 50%, 50%)')).toEqual([240, 50, 50, 0.5]); | ||
expect(parseHsla('hsla(360, 100%, 100%, none)')).toEqual([360, 100, 100, 1]); | ||
expect(parseHsla('hsla(0, 0%, 0%, 0%)')).toEqual([0, 0, 0, 0]); | ||
}); | ||
|
||
test('throws error for invalid HSLA colors', () => { | ||
expect(() => parseHsla('hsla(120, 100, 50, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 100%, 50)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 100, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120deg, 100%, 50, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 100%, 50%, 1.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(-10, 100%, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
test('parses HSLA color with degrees', () => { | ||
const result = parseHsla('hsla(180deg, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: 'deg', | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with radians', () => { | ||
const result = parseHsla('hsla(3.14rad, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 3.14, | ||
hUnit: 'rad', | ||
hDeg: 179.91, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with gradians', () => { | ||
const result = parseHsla('hsla(200grad, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 200, | ||
hUnit: 'grad', | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with turns', () => { | ||
const result = parseHsla('hsla(0.5turn, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 0.5, | ||
hUnit: 'turn', | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color without unit (default to degrees)', () => { | ||
const result = parseHsla('hsla(180, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: undefined, | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with alpha percentage', () => { | ||
const result = parseHsla('hsla(180, 100%, 50%, 50%)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: undefined, | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: '%' | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with mixed case units', () => { | ||
const result = parseHsla('hsla(180DeG, 100%, 50%, 0.5)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: 'DeG', | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0.5, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('throws error for invalid HSLA color', () => { | ||
expect(() => parseHsla('hsla(370, 100%, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, -10%, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 110%, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 100%, -10%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(120, 100%, 110%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(180, 110%, 50%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(180, 100%, 150%, 0.5)')).toThrow('Invalid HSLA color format'); | ||
expect(() => parseHsla('hsla(180, 100%, 50%, 1.5)')).toThrow('Invalid HSLA color format'); | ||
}); | ||
|
||
test('parses HSLA color with integer alpha', () => { | ||
const result = parseHsla('hsla(180, 100%, 50%, 1)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: undefined, | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 1, | ||
aUnit: undefined | ||
}); | ||
}); | ||
|
||
test('parses HSLA color with alpha as 0%', () => { | ||
const result = parseHsla('hsla(180, 100%, 50%, 0%)'); | ||
expect(result).toEqual({ | ||
h: 180, | ||
hUnit: undefined, | ||
hDeg: 180, | ||
s: 100, | ||
sUnit: '%', | ||
l: 50, | ||
lUnit: '%', | ||
a: 0, | ||
aUnit: '%' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
import { isValidHsla } from '@/validations/isValidHsla'; | ||
|
||
describe('isValidHsla', () => { | ||
test('validates HSLA colors correctly', () => { | ||
expect(isValidHsla('hsla(120, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(240, 50%, 50%, 1)')).toBe(true); | ||
expect(isValidHsla('hsla(360, 100%, 100%, 0)')).toBe(true); | ||
expect(isValidHsla('hsla(0, 0%, 0%, 0.25)')).toBe(true); | ||
}); | ||
|
||
test('validates HSLA colors with different units for hue correctly', () => { | ||
test('valid HSLA colors', () => { | ||
expect(isValidHsla('hsla(180deg, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(3.14rad, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(200grad, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(0.5turn, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 50%)')).toBe(true); | ||
expect(isValidHsla('hsla(180deg, 100%, 50%, 100%)')).toBe(true); | ||
expect(isValidHsla('hsla(180deg, 100%, 50%, 0%)')).toBe(true); | ||
}); | ||
|
||
test('invalid HSLA colors', () => { | ||
expect(isValidHsla('hsla(370deg, 100%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(180, 110%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(180, 100%, 150%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 1.5)')).toBe(false); | ||
expect(isValidHsla('hsla(180, 100%, 50%, -0.5)')).toBe(false); | ||
}); | ||
|
||
test('valid HSLA colors with mixed case units', () => { | ||
expect(isValidHsla('hsla(180DeG, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(3.14Rad, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(200GraD, 100%, 50%, 0.5)')).toBe(true); | ||
expect(isValidHsla('hsla(0.5TuRn, 100%, 50%, 0.5)')).toBe(true); | ||
}); | ||
|
||
test('validates HSLA colors with different alpha values correctly', () => { | ||
expect(isValidHsla('hsla(120, 100%, 50%, 0.75)')).toBe(true); | ||
expect(isValidHsla('hsla(240, 50%, 50%, 50%)')).toBe(true); | ||
expect(isValidHsla('hsla(360, 100%, 100%, none)')).toBe(true); | ||
expect(isValidHsla('hsla(0, 0%, 0%, 0%)')).toBe(true); | ||
test('valid HSLA colors with integer alpha', () => { | ||
expect(isValidHsla('hsla(180, 100%, 50%, 1)')).toBe(true); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 0)')).toBe(true); | ||
}); | ||
|
||
test('invalidates incorrect HSLA colors', () => { | ||
expect(isValidHsla('hsla(120, 100, 50, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 100%, 50)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 100, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120deg, 100%, 50, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 100%, 50%, 1.5)')).toBe(false); | ||
expect(isValidHsla('hsla(-10, 100%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(370, 100%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, -10%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 110%, 50%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 100%, -10%, 0.5)')).toBe(false); | ||
expect(isValidHsla('hsla(120, 100%, 110%, 0.5)')).toBe(false); | ||
test('valid HSLA colors with alpha as percentage', () => { | ||
expect(isValidHsla('hsla(180, 100%, 50%, 50%)')).toBe(true); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 100%)')).toBe(true); | ||
expect(isValidHsla('hsla(180, 100%, 50%, 0%)')).toBe(true); | ||
}); | ||
}); |