Skip to content
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

Experiment with Temporal API #105

Draft
wants to merge 50 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a520b91
Use Temporal API polyfill for development purpose
meduzen Oct 13, 2023
45460b7
Start `duration` using the Temporal API
meduzen Oct 14, 2023
ddc9c0f
Add `weekNumber` using the Temporal API
meduzen Oct 14, 2023
835282b
Add `daysBetween` using the Temporal API
meduzen Oct 14, 2023
874200a
Transfer decimals parameters in Temporal duration
meduzen Oct 17, 2023
4dbe584
Improve transfer of decimals in `Temporal.Duration`
meduzen Oct 17, 2023
8fb0b57
Move Temporal API polyfill to peer dependencies
meduzen Oct 17, 2023
56a0b61
Add `size-limit` section for `Temporal` implementation
meduzen Oct 17, 2023
9e25e7c
Merge branch 'main' into experiment-temporal-api
meduzen Nov 19, 2023
f89f1a8
Run Temporal duration tests
meduzen Oct 19, 2023
a771a0e
Add `datetime` using the Temporal API
meduzen Nov 25, 2023
4f7e1ba
Add `utc` using the Temporal API
meduzen Nov 25, 2023
e769ec1
Add `tzOffset` using the Temporal API
meduzen Nov 25, 2023
377ca56
Add `datetimeTz` using the Temporal API
meduzen Nov 25, 2023
b8d8f95
Unmark “todo” on config tests
meduzen Nov 25, 2023
bf80426
Add `DateTime` class using the Temporal API
meduzen Nov 25, 2023
3dcf3ff
Fix path in Temporal duration
meduzen Nov 25, 2023
8e26497
Merge main into experiment-temporal-api
meduzen Mar 1, 2024
4babba1
Update `size-limit` with Temporal exports
meduzen Mar 4, 2024
6ee2141
Merge main into experiment-temporal-api
meduzen Apr 4, 2024
7901e9d
Merge branch 'main' into experiment-temporal-api
meduzen Apr 12, 2024
8b63f3e
Merge main into experiment-temporal-api
meduzen Apr 18, 2024
82ab5c5
Duplicate test from non-Temporal implementation
meduzen Apr 19, 2024
78c218a
Add missing date utils in Temporal implementation
meduzen Apr 19, 2024
e8e094e
Update day index util for `Temporal`
meduzen Apr 19, 2024
9f13bbf
Fix `datetime` with `'week'` precision
meduzen Apr 19, 2024
e954705
Lint Temporal implementation
meduzen Apr 19, 2024
cf2f59c
Remove unused `Temporal` imports
meduzen Apr 19, 2024
8d6475c
Add `temporal-polyfill` to enable testing against multiple polyfills
meduzen Apr 19, 2024
783729c
Add commented imports to `temporal-polyfill`
meduzen Apr 19, 2024
8032721
Remove unused code
meduzen Apr 19, 2024
219389c
Add missing file extension in JS imports
meduzen Apr 19, 2024
f396e66
Add some notes in Temporal util
meduzen Apr 19, 2024
e2fc6e2
Merge main into experiment-temporal-api
meduzen Apr 19, 2024
2985e03
Destructure `Temporal` classes where it shorten the code
meduzen Apr 19, 2024
ca6e4b7
Update JSDoc in Temporal implementation
meduzen Apr 19, 2024
f1c6a0e
Update `Temporal` timezone test to be on par with `main`
meduzen Apr 20, 2024
1a1f648
Fix timezone offset overflow ignored when it should end being `'Z'`
meduzen Apr 20, 2024
7adff2e
Merge main into experiment-temporal-api
meduzen Apr 20, 2024
42e20d4
Merge branch 'main' into experiment-temporal-api
meduzen Apr 21, 2024
6831766
Rename internal timezone boundaries variables
meduzen Apr 21, 2024
1dc8611
Update `Temporal` to be on par with last `main` changes
meduzen Apr 21, 2024
c3b05ca
Comment unwanted code
meduzen Apr 21, 2024
ecaf51f
Merge main into experiment-temporal-api
meduzen Apr 24, 2024
fb2262c
Draft comparison chart
meduzen Apr 26, 2024
2393d31
Move Temporal implementation having parity to another folder
meduzen Apr 27, 2024
5225558
Start experimenting in a new folder
meduzen Apr 27, 2024
4ad9d3d
Fix `size-limit` config
meduzen Apr 27, 2024
bab5999
Fix test file
meduzen Apr 27, 2024
43dde81
Merge main into experiment-temporal-api
meduzen Aug 10, 2024
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
64 changes: 64 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,127 @@
"path": "src/index.js",
"import": "{ datetime }"
},
{
"name": "`datetime` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ datetime }"
},
{
"name": "`utc`",
"path": "src/index.js",
"import": "{ utc }"
},
{
"name": "`utc` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ utc }"
},
{
"name": "`datetimeTz`",
"path": "src/index.js",
"import": "{ datetimeTz }"
},
{
"name": "`datetimeTz` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ datetimeTz }"
},
{
"name": "`duration`",
"path": "src/index.js",
"import": "{ duration }"
},
{
"name": "`duration` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ duration }"
},
{
"name": "`tzOffset`",
"path": "src/index.js",
"import": "{ tzOffset }"
},
{
"name": "`tzOffset` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ tzOffset }"
},
{
"name": "`setTimeSeparator`",
"path": "src/index.js",
"import": "{ setTimeSeparator }"
},
{
"name": "`setTimeSeparator` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ setTimeSeparator }"
},
{
"name": "`setTzConfig`",
"path": "src/index.js",
"import": "{ setTzConfig }"
},
{
"name": "`setTzConfig` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ setTzConfig }"
},
{
"name": "`setTzInRealWorldRange`",
"path": "src/index.js",
"import": "{ setTzInRealWorldRange }"
},
{
"name": "`setTzInRealWorldRange` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ setTzInRealWorldRange }"
},
{
"name": "`setTzSeparator`",
"path": "src/index.js",
"import": "{ setTzSeparator }"
},
{
"name": "`setTzSeparator` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ setTzSeparator }"
},
{
"name": "`daysBetween`",
"path": "src/index.js",
"import": "{ daysBetween }"
},
{
"name": "`daysBetween` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ daysBetween }"
},
{
"name": "`weekNumber`",
"path": "src/index.js",
"import": "{ weekNumber }"
},
{
"name": "`weekNumber` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ weekNumber }"
},
{
"name": "`DateTime`",
"path": "src/index.js",
"import": "{ DateTime }"
},
{
"name": "`DateTime` (Temporal)",
"path": "src/temporal-same-features/index.js",
"import": "{ DateTime }"
},
{
"name": "**All modules** (`import *`)",
"path": "src/index.js"
},
{
"name": "**All modules** (`import *`, Temporal)",
"path": "src/temporal-same-features/index.js"
}
]
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@
"has": "npm:@nolyfill/has@latest",
"hasown": "npm:@nolyfill/hasown@latest"
}
},
"peerDependencies": {
"@js-temporal/polyfill": "^0.4.4",
"temporal-polyfill": "^0.2.4"
}
}
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export function datetime(date = new Date(), precision = 'day') {
* - `datetime(someDate, 'somePrecision utc')`
* - `utc(someDate, 'somePrecision')`
*
*
* @param {Date} [date=new Date()] - default to now
* @param {'datetime'|'datetime second'|'datetime ms'|'time'|'second'|'ms'} [precision=datetime]
* @returns {string}
Expand Down
32 changes: 32 additions & 0 deletions src/temporal-experimenting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
> [!INFO]
>
> Work in progress document. Very drafty.

This folder will contain the implementation of `datetime-attribute` where functions will accept [`Temporal`](https://tc39.es/proposal-temporal/docs) objects while keeping current parameters.

It’s possible that some functions will not require to accept a `Temporal` object because what they accomplish is maybe already available as a `Temporal.{className}` feature.

It’s also possible that new functions are created to facilitate the usage of `Temporal` objects, and the extended scope they cover in comparison with `Date`.

## Parity between `Date` and `Temporal` implementations

**Drafty.**

function | in `Date` | in `Temporal` | note
---|---|---|---
`tzOffset` | yes | yes | @todo accept a `Temporal.TimeZone` (or any temporal) and output its “real-world” timezone.
`datetime` | yes | yes | @todo evaluate
`utc` | yes | yes | @todo evaluate
`datetimeTz` | yes | yes | @todo evaluate
`DateTime` | yes | yes | @todo evaluate
`duration` | yes | yes | @todo evaluate
`daysBetween` | yes | yes | @todo evaluate
`weekNumber` | yes | yes | @todo evaluate
(config feature…)| yes | yes | @todo evaluate

## `tzOffset` notes

```js
const tz = new Temporal.TimeZone('-23:59')
console.log(tz.id) // -23:59
```
5 changes: 5 additions & 0 deletions src/temporal-same-features/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# datetime-attribute implemented with the `Temporal` API

This folder contains the implementation of `datetime-attribute` using the [`Temporal` API](https://tc39.es/proposal-temporal/docs).

It has feature parity with the initial `datetime-attribute`.
38 changes: 38 additions & 0 deletions src/temporal-same-features/config/datetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @typedef {Object} DateTimeOptions
* @property {('T'|' ')} separator The separator between date and time
* (`'2024-04-21T10:34'`) can be a
* 'T' (default) or a space.
*/

/** @type DateTimeOptions */
export const config = {
separator: 'T',
}

/**
* Set datetime related configuration
*
* @param {DateTimeOptions} options
* @returns {DateTimeOptions}
*/
export const setConfig = options => {

/**
* The separator can only be 'T' or ' '.
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#local-dates-and-times
*/
if (options.separator != 'T' && options.separator != ' ') {
throw new Error(`Invalid timezone separator. Use 'T' or ' '.`)
}

return Object.assign(config, options)
}

/**
* Set separator between date and time
*
* @param {DateTimeOptions['separator']} [separator='T']
* @returns {DateTimeOptions}
*/
export const setTimeSeparator = (separator = 'T') => setConfig({ separator })
63 changes: 63 additions & 0 deletions src/temporal-same-features/config/datetime.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, expect, test } from 'vitest'

import { datetime, setTimeSeparator } from '../index.js'

import { setConfig } from './datetime.js'

const togoIndependanceDay = new Date(1960, 3, 27)
const date = togoIndependanceDay // alias for the sake of brevity

describe('setTimeSeparator', () => {

test('is a function', () => {
expect(setTimeSeparator).toBeInstanceOf(Function)
})

test('throws with invalid separator', () => {
expect(() => setTimeSeparator(42)).toThrow(Error)
})

test('no time separator', () => {
setTimeSeparator()
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00')
})

test('T as time separator', () => {
setTimeSeparator('T')
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00')
})

test('space as time separator', () => {
setTimeSeparator(' ')
expect(datetime(date, 'datetime')).toBe('1960-04-27 00:00')
})
})

describe('setConfig', () => {

test('is a function', () => {
expect(setConfig).toBeInstanceOf(Function)
})

test('throws without parameter', () => {
expect(() => setConfig()).toThrow(TypeError)
})

test('throws without proper parameter', () => {
expect(() => setConfig({})).toThrow(Error)
})

test('throws with invalid separator', () => {
expect(() => setConfig({ separator: 42 })).toThrow(Error)
})

test('T as time separator using datetime', () => {
setConfig({ separator: 'T' })
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00')
})

test('space as time separator using datetime', () => {
setConfig({ separator: ' ' })
expect(datetime(date, 'datetime')).toBe('1960-04-27 00:00')
})
})
6 changes: 6 additions & 0 deletions src/temporal-same-features/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { setTimeSeparator } from './datetime.js'
export {
setTzConfig,
setTzInRealWorldRange,
setTzSeparator,
} from './tz.js'
Loading
Loading