Skip to content

Add support for Temporal objects #98

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"types": "./types/index.d.ts",
"devDependencies": {
"@js-temporal/polyfill": "^0.5.1",
"dts-buddy": "^0.0.4",
"publint": "^0.1.7",
"typescript": "^3.1.3",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ export function unflatten(parsed, revivers) {
break;
}

case 'Temporal.Duration':
case 'Temporal.Instant':
case 'Temporal.PlainDate':
case 'Temporal.PlainTime':
case 'Temporal.PlainDateTime':
case 'Temporal.PlainMonthDay':
case 'Temporal.PlainYearMonth':
case 'Temporal.ZonedDateTime': {
const temporalName = type.slice(9);
// @ts-expect-error TS doesn't know about Temporal yet
hydrated[index] = Temporal[temporalName].from(value[1]);
break;
}

default:
throw new Error(`Unknown type ${type}`);
}
Expand Down
13 changes: 12 additions & 1 deletion src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,18 @@ export function stringify(value, reducers) {
str = `["ArrayBuffer","${base64}"]`;
break;
}


case 'Temporal.Duration':
case 'Temporal.Instant':
case 'Temporal.PlainDate':
case 'Temporal.PlainTime':
case 'Temporal.PlainDateTime':
case 'Temporal.PlainMonthDay':
case 'Temporal.PlainYearMonth':
case 'Temporal.ZonedDateTime':
str = `["${type}",${stringify_string(thing.toString())}]`;
break;

default:
if (!is_plain_object(thing)) {
throw new DevalueError(
Expand Down
20 changes: 20 additions & 0 deletions src/uneval.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export function uneval(value, replacer) {
case "ArrayBuffer":
return;

case 'Temporal.Duration':
case 'Temporal.Instant':
case 'Temporal.PlainDate':
case 'Temporal.PlainTime':
case 'Temporal.PlainDateTime':
case 'Temporal.PlainMonthDay':
case 'Temporal.PlainYearMonth':
case 'Temporal.ZonedDateTime':
return;

default:
if (!is_plain_object(thing)) {
throw new DevalueError(
Expand Down Expand Up @@ -199,6 +209,16 @@ export function uneval(value, replacer) {
return `new Uint8Array([${ui8.toString()}]).buffer`;
}

case 'Temporal.Duration':
case 'Temporal.Instant':
case 'Temporal.PlainDate':
case 'Temporal.PlainTime':
case 'Temporal.PlainDateTime':
case 'Temporal.PlainMonthDay':
case 'Temporal.PlainYearMonth':
case 'Temporal.ZonedDateTime':
return `${type}.from(${stringify_string(thing.toString())})`;

default:
const obj = `{${Object.keys(thing)
.map((key) => `${safe_key(key)}:${stringify(thing[key])}`)
Expand Down
60 changes: 60 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import * as assert from 'uvu/assert';
import * as uvu from 'uvu';
import { uneval, unflatten, parse, stringify } from '../index.js';

import { Temporal } from '@js-temporal/polyfill'
if (!globalThis.Temporal) {
globalThis.Temporal = Temporal;
}

class Custom {
constructor(value) {
this.value = value;
Expand Down Expand Up @@ -171,6 +176,61 @@ const fixtures = {
value: new Uint8Array([1, 2, 3]).buffer,
js: 'new Uint8Array([1,2,3]).buffer',
json: '[["ArrayBuffer","AQID"]]'
},
{
name: 'Temporal.Duration',
value: Temporal.Duration.from({ years: 1, months: 2, days: 3 }),
js: 'Temporal.Duration.from("P1Y2M3D")',
json: '[["Temporal.Duration","P1Y2M3D"]]'
},
{
name: 'Temporal.Instant',
value: Temporal.Instant.from("1999-09-29T05:30:00Z"),
js: 'Temporal.Instant.from("1999-09-29T05:30:00Z")',
json: '[["Temporal.Instant","1999-09-29T05:30:00Z"]]'
},
{
name: 'Temporal.PlainDate',
value: Temporal.PlainDate.from({ year: 1999, month: 9, day: 29 }),
js: 'Temporal.PlainDate.from("1999-09-29")',
json: '[["Temporal.PlainDate","1999-09-29"]]'
},
{
name: 'Temporal.PlainTime',
value: Temporal.PlainTime.from({ hour: 12, minute: 34, second: 56 }),
js: 'Temporal.PlainTime.from("12:34:56")',
json: '[["Temporal.PlainTime","12:34:56"]]'
},
{
name: 'Temporal.PlainDateTime',
value: Temporal.PlainDateTime.from({
year: 1999, month: 9, day: 29,
hour: 12, minute: 34, second: 56
}),
js: 'Temporal.PlainDateTime.from("1999-09-29T12:34:56")',
json: '[["Temporal.PlainDateTime","1999-09-29T12:34:56"]]'
},
{
name: 'Temporal.PlainMonthDay',
value: Temporal.PlainMonthDay.from({ month: 9, day: 29 }),
js: 'Temporal.PlainMonthDay.from("09-29")',
json: '[["Temporal.PlainMonthDay","09-29"]]'
},
{
name: 'Temporal.PlainYearMonth',
value: Temporal.PlainYearMonth.from({ year: 1999, month: 9 }),
js: 'Temporal.PlainYearMonth.from("1999-09")',
json: '[["Temporal.PlainYearMonth","1999-09"]]'
},
{
name: 'Temporal.ZonedDateTime',
value: Temporal.ZonedDateTime.from({
year: 1999, month: 9, day: 29,
hour: 12, minute: 34, second: 56,
timeZone: 'Europe/Rome'
}),
js: 'Temporal.ZonedDateTime.from("1999-09-29T12:34:56+02:00[Europe/Rome]")',
json: '[["Temporal.ZonedDateTime","1999-09-29T12:34:56+02:00[Europe/Rome]"]]'
}
],

Expand Down