Skip to content

Update splitValue() #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/js/css-gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ export const parseGradient = (
const type = getGradientType(value);
const gradValue = value.replace(REG_GRAD, '').replace(/\)$/, '');
if (type && gradValue) {
const [lineOrColorStop = '', ...colorStops] = splitValue(gradValue, ',');
const [lineOrColorStop = '', ...colorStops] = splitValue(gradValue, {
delimiter: ','
});
const dimension = /^(?:repeating-)?conic-gradient$/.test(type)
? DIM_ANGLE_PCT
: DIM_LEN_PCT;
Expand Down
1 change: 1 addition & 0 deletions src/js/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Options {
dimension?: Record<string, number | ((K: string) => number)>;
format?: string;
nullable?: boolean;
preserveComment?: boolean;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/js/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,25 @@ const REG_MIX = new RegExp(SYN_MIX);
/**
* split value
* @param value - CSS value
* @param [delimiter] - comma or space
* @param [opt] - options
* @returns array of values, NOTE: comments are stripped
*/
export const splitValue = (value: string, delimiter: string = ''): string[] => {
export const splitValue = (value: string, opt: Options = {}): string[] => {
if (isString(value)) {
value = value.trim();
} else {
throw new TypeError(`${value} is not a string.`);
}
const { delimiter = ' ', preserveComment = false } = opt;
const cacheKey: string = createCacheKey(
{
namespace: NAMESPACE,
name: 'splitValue',
value
},
{
delimiter
delimiter,
preserveComment
}
);
const cachedResult = getCache(cacheKey);
Expand Down Expand Up @@ -83,6 +85,9 @@ export const splitValue = (value: string, delimiter: string = ''): string[] => {
break;
}
case COMMENT: {
if (preserveComment && delimiter === ',') {
str += value;
}
break;
}
case FUNC:
Expand Down
43 changes: 38 additions & 5 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('split value', () => {
});

it('should get value', () => {
const res = func('foo bar', ',');
const res = func('foo bar', {
delimiter: ','
});
assert.deepEqual(res, ['foo bar'], 'result');
});

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

it('should get value', () => {
const res = func('foo bar', ',');
const res = func('foo bar', {
delimiter: ','
});
assert.deepEqual(res, ['foo bar'], 'result');
});

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

it('should get value', () => {
const res = func('foo /* comment */ , bar', ',');
const res = func('foo /* comment */ , bar', {
delimiter: ','
});
assert.deepEqual(res, ['foo', 'bar'], 'result');
});

it('should get value', () => {
const res = func('foo /* comment */ bar', {
preserveComment: true
});
assert.deepEqual(res, ['foo', 'bar'], 'result');
});

it('should get value', () => {
const res = func(',', ',');
const res = func('foo /* comment */ , bar', {
delimiter: ',',
preserveComment: true
});
assert.deepEqual(res, ['foo /* comment */', 'bar'], 'result');
});

it('should get value', () => {
const res = func('foo /* comment */ bar, baz', {
delimiter: ',',
preserveComment: true
});
assert.deepEqual(res, ['foo /* comment */ bar', 'baz'], 'result');
});

it('should get value', () => {
const res = func(',', {
delimiter: ','
});
assert.deepEqual(res, ['', ''], 'result');
});

it('should get value', () => {
const res = func(
'linear-gradient(red, blue), radial-gradient(yellow, green)',
','
{
delimiter: ','
}
);
assert.deepEqual(
res,
Expand Down