Skip to content

Commit f868f4a

Browse files
committed
Update splitValue()
1 parent b1b23b1 commit f868f4a

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

src/js/css-gradient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ export const parseGradient = (
226226
const type = getGradientType(value);
227227
const gradValue = value.replace(REG_GRAD, '').replace(/\)$/, '');
228228
if (type && gradValue) {
229-
const [lineOrColorStop = '', ...colorStops] = splitValue(gradValue, ',');
229+
const [lineOrColorStop = '', ...colorStops] = splitValue(gradValue, {
230+
delimiter: ','
231+
});
230232
const dimension = /^(?:repeating-)?conic-gradient$/.test(type)
231233
? DIM_ANGLE_PCT
232234
: DIM_LEN_PCT;

src/js/typedef.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
export interface Options {
1818
alpha?: boolean;
1919
colorSpace?: string;
20+
comment?: boolean;
2021
currentColor?: string;
2122
customProperty?: Record<string, string | ((K: string) => string)>;
2223
d50?: boolean;

src/js/util.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,24 @@ const REG_MIX = new RegExp(SYN_MIX);
3838
/**
3939
* split value
4040
* @param value - CSS value
41-
* @param [delimiter] - comma or space
41+
* @param [opt] - options
4242
* @returns array of values, NOTE: comments are stripped
4343
*/
44-
export const splitValue = (value: string, delimiter: string = ''): string[] => {
44+
export const splitValue = (value: string, opt: Options = {}): string[] => {
4545
if (isString(value)) {
4646
value = value.trim();
4747
} else {
4848
throw new TypeError(`${value} is not a string.`);
4949
}
50+
const { comment = false, delimiter = ' ' } = opt;
5051
const cacheKey: string = createCacheKey(
5152
{
5253
namespace: NAMESPACE,
5354
name: 'splitValue',
5455
value
5556
},
5657
{
58+
comment,
5759
delimiter
5860
}
5961
);
@@ -83,6 +85,9 @@ export const splitValue = (value: string, delimiter: string = ''): string[] => {
8385
break;
8486
}
8587
case COMMENT: {
88+
if (comment && delimiter === ',') {
89+
str += value;
90+
}
8691
break;
8792
}
8893
case FUNC:

test/util.test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ describe('split value', () => {
3838
});
3939

4040
it('should get value', () => {
41-
const res = func('foo bar', ',');
41+
const res = func('foo bar', {
42+
delimiter: ','
43+
});
4244
assert.deepEqual(res, ['foo bar'], 'result');
4345
});
4446

@@ -48,7 +50,9 @@ describe('split value', () => {
4850
});
4951

5052
it('should get value', () => {
51-
const res = func('foo bar', ',');
53+
const res = func('foo bar', {
54+
delimiter: ','
55+
});
5256
assert.deepEqual(res, ['foo bar'], 'result');
5357
});
5458

@@ -58,19 +62,48 @@ describe('split value', () => {
5862
});
5963

6064
it('should get value', () => {
61-
const res = func('foo /* comment */ , bar', ',');
65+
const res = func('foo /* comment */ , bar', {
66+
delimiter: ','
67+
});
68+
assert.deepEqual(res, ['foo', 'bar'], 'result');
69+
});
70+
71+
it('should get value', () => {
72+
const res = func('foo /* comment */ bar', {
73+
comment: true
74+
});
6275
assert.deepEqual(res, ['foo', 'bar'], 'result');
6376
});
6477

6578
it('should get value', () => {
66-
const res = func(',', ',');
79+
const res = func('foo /* comment */ , bar', {
80+
comment: true,
81+
delimiter: ','
82+
});
83+
assert.deepEqual(res, ['foo /* comment */', 'bar'], 'result');
84+
});
85+
86+
it('should get value', () => {
87+
const res = func('foo /* comment */ bar, baz', {
88+
comment: true,
89+
delimiter: ','
90+
});
91+
assert.deepEqual(res, ['foo /* comment */ bar', 'baz'], 'result');
92+
});
93+
94+
it('should get value', () => {
95+
const res = func(',', {
96+
delimiter: ','
97+
});
6798
assert.deepEqual(res, ['', ''], 'result');
6899
});
69100

70101
it('should get value', () => {
71102
const res = func(
72103
'linear-gradient(red, blue), radial-gradient(yellow, green)',
73-
','
104+
{
105+
delimiter: ','
106+
}
74107
);
75108
assert.deepEqual(
76109
res,

0 commit comments

Comments
 (0)