Skip to content

Commit

Permalink
Save some bytes and pre-release 1.3.2
Browse files Browse the repository at this point in the history
Also:
* test precision in `DateTime.to`;
* update types (unchanged API);
* update documentation and changelog.
  • Loading branch information
meduzen authored Aug 5, 2023
1 parent 7cc58eb commit 6cc2a89
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

Compare with [last published version](https://github.com/meduzen/datetime-attribute/compare/1.3.1...main).

### Improved

- Shorten `daysBetween()` and `DateTime` footprint by a couple of bytes.

> **Warning**\
> If not transpiled, `DateTime` now requires support for [`class` fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#browser_compatibility) (Safari 14.0).
### Under the hood

- Replace NPM by pnpm
Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Get a [`datetime` attribute](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTimeElement/datetime) for HTML `<time>` (and [other elements](#not-only-in-time)).

It covers the [whole WHATWG specification](https://html.spec.whatwg.org/multipage/text-level-semantics.html#attr-time-datetime) in 4 functions:
- [**`datetime()`**](#expressing-moments-with-datetime) for a specific moment ([666 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{datetime}]&bundle));
- [**`datetimeTz()`**](#adding-a-timezone-offset-to-a-moment-with-datetimetz) for a specific moment in a given timezone ([936 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{datetimeTz}]&bundle));
- [**`duration()`**](#expressing-durations-with-duration) for a duration ([339 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{duration}]&bundle));
- [**`tzOffset()`**](#expressing-timezone-offsets-with-tzoffset) for a timezone offset ([340 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{tzOffset}]&bundle)).
- [**`datetime()`**](#expressing-moments-with-datetime) for a specific moment ([645 B compressed](https://bundlejs.com/?q=datetime-attribute&treeshake=[{datetime}]&bundle));
- [**`datetimeTz()`**](#adding-a-timezone-offset-to-a-moment-with-datetimetz) for a specific moment in a given timezone ([942 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{datetimeTz}]&bundle));
- [**`duration()`**](#expressing-durations-with-duration) for a duration ([307 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{duration}]&bundle));
- [**`tzOffset()`**](#expressing-timezone-offsets-with-tzoffset) for a timezone offset ([388 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{tzOffset}]&bundle)).

Additionally, a [`DateTime` class](#the-datetime-class) ([741 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{DateTime}]&bundle)) and some [other functions](#other-functions) are provided.
Additionally, a [`DateTime` class](#the-datetime-class) ([721 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{DateTime}]&bundle)) and some [other functions](#other-functions) are provided.

[![Node.js CI](https://github.com/meduzen/datetime-attribute/actions/workflows/node.js.yml/badge.svg)](https://github.com/meduzen/datetime-attribute/actions/workflows/node.js.yml)

The package is lightweight ([~ 1.39 KB compressed](https://bundlejs.com/?q=datetime-attribute&bundle) for `import *`), **tree-shakeable**, **typed** and tested.
The package is lightweight ([~ 1.42 KB compressed](https://bundlejs.com/?q=datetime-attribute&bundle) for `import *`), tree-shakeable, typed and tested.

## Table of contents

Expand Down Expand Up @@ -419,6 +419,8 @@ daysBetween(january1st, january11th) // 10
daysBetween(january19th, january11th) // -8
```

([134 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{daysBetween}]&bundle))

### `weekNumber()`

Get the week number as [defined by the WHATWG](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#weeks), following the ISO-8601 specs:
Expand All @@ -440,15 +442,18 @@ weekNumber(january1st) // 53: it’s a Friday!
weekNumber(january11th) // 2
```

([201 B](https://bundlejs.com/?q=datetime-attribute&treeshake=[{weekNumber}]&bundle))

## Changelog

See [CHANGELOG.md](https://github.com/meduzen/datetime-attribute/blob/main/CHANGELOG.md) or the [releases](https://github.com/meduzen/datetime-attribute/releases).

## Browser and tooling support

`datetime-attribute` is provided for [modern browsers usage](https://github.com/meduzen/datetime-attribute/blob/main/browserslist) with standard JavaScript syntax:
`datetime-attribute` is provided [as module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#browser_compatibility) for [modern browsers usage](https://github.com/meduzen/datetime-attribute/blob/main/browserslist) with standard JavaScript syntax:
- it is up to you to transpile it for legacy browsers;
- you can’t import it using `require('datetime-attribute')`.
- you can’t import it using `require('datetime-attribute')`;
- if you don’t transpile it, `DateTime` requires support for [`class` fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#browser_compatibility) (Safari 14.0) starting `v1.32.0`.

[Read more about ESModules](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).

Expand All @@ -472,7 +477,3 @@ See the [contributing guidelines](https://github.com/meduzen/datetime-attribute/
## License

The _datetime-attribute_ package is open-sourced software licensed under the [DWTFYWTPL](https://github.com/meduzen/datetime-attribute/blob/main/LICENSE).

## 

[![Verified on Openbase](https://badges.openbase.com/js/verified/datetime-attribute.svg?token=ngHvoLLDN27g9x7qUc+Ihp2QJNmvGWNxVkuA/5ldiKI=)](https://openbase.com/js/datetime-attribute?utm_source=embedded&amp;utm_medium=badge&amp;utm_campaign=rate-badge)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datetime-attribute",
"version": "1.3.1",
"version": "1.3.2-0",
"description": "Generate datetime attributes for the <time> HTML element, following WHATWG and ISO8601 specs.",
"type": "module",
"sideEffects": false,
Expand Down
38 changes: 19 additions & 19 deletions pnpm-lock.yaml

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

8 changes: 2 additions & 6 deletions src/datetime-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export class DateTime extends Date {
*
* @returns {number}
*/
getWeek() {
return weekNumber(this)
}
getWeek = () => weekNumber(this)

/**
* Set the week number.
Expand All @@ -34,7 +32,5 @@ export class DateTime extends Date {
* @param {import('./datetime.js').Precision} precision
* @returns {string}
*/
to(precision) {
return datetime(this, precision)
}
to = precision => datetime(this, precision)
}
12 changes: 8 additions & 4 deletions src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { MILLISECONDS_PER_DAY } from './const.js'
* @returns {number}
*/
export function daysBetween(date, furtherDate) {
date = new Date(date.getFullYear(), date.getMonth(), date.getDate())
furtherDate = new Date(furtherDate.getFullYear(), furtherDate.getMonth(), furtherDate.getDate())

const diffInDays = (furtherDate - date) / MILLISECONDS_PER_DAY
// discard time of day
[date, furtherDate] = [date, furtherDate].map(d => new Date(d.getFullYear(), d.getMonth(), d.getDate()))

return Math.ceil(diffInDays)
/**
* The commented code below could be accurate if we cared about time of day.
*/
// return Math.ceil((furtherDate - date) / MILLISECONDS_PER_DAY)

return (furtherDate - date) / MILLISECONDS_PER_DAY
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,17 @@ describe('daysBetween', () => {
})

const summer = new DateTime(2021, 5, 21)
const oneWeekAfterSummer = new DateTime(2021, 5, 28)
const twoWeeksAfterSummer = new DateTime(2021, 6, 5)

describe('DateTime class', () => {
test('extends Date', () => expect(summer).toBeInstanceOf(Date))
test('.getWeek', () => expect(summer.getWeek()).toBe(25))
test('.setWeek', () => expect(summer.setWeek(26)).toBe(oneWeekAfterSummer.getTime()))
test(".to('day')", () => expect(summer.to('day')).toBe('2021-06-28'))
test(".to('day')", () => expect(summer.to('day')).toBe('2021-06-21'))
test('.setWeek', () => expect(summer.setWeek(27)).toBe(twoWeeksAfterSummer.getTime()))
test(".to('month')", () => expect(twoWeeksAfterSummer.to('month')).toBe('2021-07'))
test('weeks is changed when day changes', () => {
summer.setDate(summer.getDate() + 14)
return expect(summer.getWeek()).toBe(28)
return expect(summer.getWeek()).toBe(29)
})
})

Expand Down
4 changes: 2 additions & 2 deletions types/datetime-class.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class DateTime extends Date {
*
* @returns {number}
*/
getWeek(): number;
getWeek: () => number;
/**
* Set the week number.
*
Expand All @@ -23,5 +23,5 @@ export class DateTime extends Date {
* @param {import('./datetime.js').Precision} precision
* @returns {string}
*/
to(precision: import('./datetime.js').Precision): string;
to: (precision: import('./datetime.js').Precision) => string;
}

0 comments on commit 6cc2a89

Please sign in to comment.