Skip to content

Commit 90d85e2

Browse files
authored
fix: invalid date should fallback (#24)
1 parent 996c51e commit 90d85e2

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/FastColor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ type ParseNumber = (num: number, txt: string, index: number) => number;
1313
* When `base` is provided, the percentage value will be divided by `base`.
1414
*/
1515
function splitColorStr(str: string, parseNum: ParseNumber): number[] {
16-
const match = str.match(/\d*\.?\d+%?/g);
17-
const numList = match ? match.map((item) => parseFloat(item)) : [];
16+
const match: string[] = str.match(/\d*\.?\d+%?/g) || [];
17+
const numList = match.map((item) => parseFloat(item));
1818

1919
for (let i = 0; i < 3; i += 1) {
20-
numList[i] = parseNum(numList[i], match[i], i);
20+
numList[i] = parseNum(numList[i] || 0, match[i] || '', i);
2121
}
2222

2323
// For alpha. 50% should be 0.5

tests/css-rgb-syntax.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,13 @@ describe('css rgb() syntax', () => {
141141
});
142142
});
143143
});
144+
145+
it('invalid rgb', () => {
146+
expect(new FastColor('rgb').toRgb()).toEqual({
147+
r: 0,
148+
g: 0,
149+
b: 0,
150+
a: 1,
151+
});
152+
});
144153
});

0 commit comments

Comments
 (0)