Skip to content

Commit

Permalink
date type
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Jun 28, 2024
1 parent 9077a83 commit 1539d2c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/example-nextjs/src/app/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const Form = ({ className }: { className?: string }) => {
};

const tags = [
{ id: '1', value: { text: '1' } },
{ id: '2', value: { text: '2' } },
{ id: '3', value: { text: '3' } }
{ id: '1', value: { text: '1', time: new Date() } },
{ id: '2', value: { text: '2', time: new Date() } },
{ id: '3', value: { text: '3', time: new Date() } }
]
2 changes: 1 addition & 1 deletion packages/example-nextjs/src/app/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Form = {
name: string;
age: number | string;
'agree to terms': boolean;
tags: { id: string, value: { text: string }}[]
tags: { id: string, value: { text: string, time: Date }}[]
};
1 change: 1 addition & 0 deletions packages/url-state/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const SYMBOLS = {
null: '∙null',
undefined: '∙undefined',
number: '∓',
date: '⏲',
arrayL: '⦋',
arrayR: '⦌',
objectL: '❴',
Expand Down
13 changes: 13 additions & 0 deletions packages/url-state/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ describe('encoder', () => {
});
});


it('date', () => {
const d = new Date('2024-06-28T09:10:38.763Z')
expect(encode(d)).toStrictEqual('⏲2024-06-28T09:10:38.763Z');
});


it('boolean', () => {
expect(encode(true)).toStrictEqual('🗵true');
expect(encode(false)).toStrictEqual('🗵false');
Expand Down Expand Up @@ -96,6 +103,12 @@ describe('decoder', () => {
expect(decode('∙undefined')).toStrictEqual(undefined);
});

it('date', () => {
const dateString = '⏲2024-06-28T09:10:38.763Z'
const date = new Date(dateString.slice(1))
expect(decode(dateString)).toStrictEqual(date);
});

it('complex', () => {
expect(
decode(
Expand Down
3 changes: 3 additions & 0 deletions packages/url-state/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function encode(payload: unknown): string{
case 'function':
case 'symbol':
return '';
case 'date':
return SYMBOLS.date + (payload as Date).toISOString()
case 'string':
return encodeURIComponent(payload as string);
case 'number':
Expand Down Expand Up @@ -43,6 +45,7 @@ const replacer = (key: string, value: unknown) => {
const decodeStr = (str: string) => {
if (str.match(/^∓/)) return Number.parseFloat(str.replaceAll('∓', ''));
if (str.match(/^🗵/)) return str.includes('true') ? true : false;
if (str.match(/^⏲/)) return new Date(str.slice(1));
if (str === SYMBOLS.null) return null;
if (str === SYMBOLS.undefined) return undefined;
return decodeURIComponent(str);
Expand Down
5 changes: 5 additions & 0 deletions packages/url-state/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ describe('typeOf', () => {
expect(typeOf('5')).toEqual('string');
});

it('date', () => {
const d = new Date()
expect(typeOf(d)).toEqual('date');
});

it('number', () => {
expect(typeOf(5)).toEqual('number');
expect(typeOf(3.14567)).toEqual('number');
Expand Down
5 changes: 4 additions & 1 deletion packages/url-state/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type Type =
| 'string'
| 'date'
| 'boolean'
| 'number'
| 'bigint'
Expand All @@ -13,10 +14,12 @@ export type Type =
export const typeOf = (val: unknown): Type => {
const isNull = val === null;
const isArray = Array.isArray(val);
const isObject = !isNull && !isArray && typeof val === 'object';
const isDate = val instanceof Date
const isObject = !isNull && !isDate && !isArray && typeof val === 'object';

return (
(isNull && 'null') ||
(isDate && 'date') ||
(isArray && 'array') ||
(isObject && 'object') ||
typeof val
Expand Down

0 comments on commit 1539d2c

Please sign in to comment.