Skip to content

Commit

Permalink
Fix boolean arg types parsing and encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Feb 15, 2023
1 parent 14850ec commit 7b12226
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('parseArgsParam', () => {
expect(args).toStrictEqual({ key: undefined });
});

it('parses true', () => {
const args = parseArgsParam('key:!true');
expect(args).toStrictEqual({ key: true });
});

it('parses false', () => {
const args = parseArgsParam('key:!false');
expect(args).toStrictEqual({ key: false });
});

it('parses hex color values', () => {
const args = parseArgsParam('key:!hex(ff4785)');
expect(args).toStrictEqual({ key: '#ff4785' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const QS_OPTIONS = {
if (type === 'value' && str.startsWith('!')) {
if (str === '!undefined') return undefined;
if (str === '!null') return null;
if (str === '!true') return true;
if (str === '!false') return false;
if (str.startsWith('!date(') && str.endsWith(')')) return new Date(str.slice(6, -1));
if (str.startsWith('!hex(') && str.endsWith(')')) return `#${str.slice(5, -1)}`;

Expand Down
2 changes: 1 addition & 1 deletion code/lib/router/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('buildArgsParam', () => {

it('builds booleans', () => {
const param = buildArgsParam({}, { yes: true, no: false });
expect(param).toEqual('yes:true;no:false');
expect(param).toEqual('yes:!true;no:!false');
});

it('builds arrays', () => {
Expand Down
5 changes: 5 additions & 0 deletions code/lib/router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ const encodeSpecialValues = (value: unknown): any => {
if (COLOR_REGEXP.test(value)) return `!${value.replace(/[\s%]/g, '')}`;
return value;
}

if (typeof value === 'boolean') {
return `!${value}`;
}

if (Array.isArray(value)) return value.map(encodeSpecialValues);
if (isPlainObject(value)) {
return Object.entries(value as Record<string, any>).reduce(
Expand Down

0 comments on commit 7b12226

Please sign in to comment.