Skip to content

Commit

Permalink
Merge pull request #14 from frytg/dev/docs-fixes
Browse files Browse the repository at this point in the history
fix: docs and date exports
  • Loading branch information
frytg authored Dec 22, 2024
2 parents 3564c34 + 64368fc commit a92de14
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 77 deletions.
3 changes: 3 additions & 0 deletions check-required-env/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"imports": {
"@frytg/logger": "jsr:@frytg/[email protected]"
},
"publish": {
"exclude": ["*.test.ts"]
}
}
3 changes: 2 additions & 1 deletion crypto/generate-default-keys.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* @module
* Run key generation for default key sizes and print the results to the console
*
* @example
* ```bash
* deno run jsr:@frytg/crypto/generate-default-keys
* ```
*
* @module
*/

// load module
Expand Down
4 changes: 2 additions & 2 deletions crypto/generate-key.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// deno-lint-ignore-file no-console
/**
* @module
*
* This module provides a function to generate a key of the specified number of bytes and print the key in base64 and hex to the console or return the key as a Buffer.
*
* @example
Expand All @@ -10,6 +8,8 @@
*
* generateKey(32, true)
* ```
*
* @module
*/

// load packages
Expand Down
5 changes: 3 additions & 2 deletions crypto/hash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* @module
* {@linkcode hashSha256 | SHA-256} and {@linkcode hashSha512 | SHA-512} hash functions
*
* @example
* @example Hash SHA-256
* ```ts
* import { hashSha512 } from '@frytg/crypto/hash'
*
* hashSha512('hello world')
* ```
*
* @module
*/

// load packages
Expand Down
5 changes: 3 additions & 2 deletions crypto/hmac.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* @module
* {@linkcode hmacSha256 | HMAC SHA-256} and {@linkcode hmacSha512 | HMAC SHA-512} hash functions
*
* @example
* @example HMAC SHA-512
* ```ts
* import { hmacSha512 } from '@frytg/crypto/hmac'
*
* hmacSha512('hello world', '0123456789abcdef')
* ```
*
* @module
*/

// load packages
Expand Down
6 changes: 6 additions & 0 deletions dates/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Dates Changelog

## 2024-12-22 - 0.1.1

- fix: added `@deno-types` to `DateTime` import
- fix: removed `jsr:` prefix from ts examples
- fix: type errors for dates

## 2024-12-12 - 0.1.0

- feat: added `formatDuration` function to format durations in milliseconds to a human readable string.
Expand Down
16 changes: 16 additions & 0 deletions dates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@

This is a simple opinionated wrapper around the [Luxon](https://github.com/moment/luxon) library to provide pre-configured helpers.

For example, to get the current date in ISO format:

```ts
import { getISO } from '@frytg/dates';

getISO(); // returns string like 2025-01-01T00:00:00.000Z
```

You can also use the full `DateTime` object from Luxon:

```ts
import { DateTime } from '@frytg/dates';

const date = DateTime.fromMillis(1719859200000);
```

## Author

Created by [@frytg](https://github.com/frytg) / [frytg.digital](https://www.frytg.digital)
Expand Down
60 changes: 38 additions & 22 deletions dates/dates.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// load package
import { format as stdFormat } from '@std/fmt/duration'
// @deno-types="npm:@types/luxon@^3.4.2"
import { DateTime } from 'luxon'

const LOCAL_TIMEZONE = 'Europe/Amsterdam'
const DATE_HOUR_MINUTES_FORMAT = 'ccc, d. LLLL yyyy - h:mma'

/**
* Luxon DateTime
* Export DateTime from Luxon
* @returns {DateTime} DateTime
*
* @example
* ```ts
* import { DateTime } from '@frytg/dates'
*
* DateTime.fromMillis(1719859200000)
* ```
*/
export { DateTime }

Expand All @@ -17,7 +26,7 @@ export { DateTime }
*
* @example
* ```ts
* import { toLocal } from 'jsr:@frytg/dates'
* import { toLocal } from '@frytg/dates'
*
* toLocal(getNow())
* ```
Expand All @@ -31,7 +40,7 @@ export const toLocal = (date: DateTime): DateTime => date.setZone(LOCAL_TIMEZONE
*
* @example
* ```ts
* import { msToUnix } from 'jsr:@frytg/dates'
* import { msToUnix } from '@frytg/dates'
*
* msToUnix(getMs())
* ```
Expand All @@ -44,57 +53,64 @@ export const msToUnix = (ms: number): number => Number.parseInt(`${ms / 1000}`)
*
* @example
* ```ts
* import { getNow } from 'jsr:@frytg/dates'
* import { getNow } from '@frytg/dates'
*
* getNow()
* ```
*/
export const getNow = (): DateTime => DateTime.now()

/**
* Provides util to get .unix() style value
* Get unix timestamp
* @param {DateTime} [date] - date object
* @returns {number} unix timestamp
*
* @example
* ```ts
* import { getUnix } from 'jsr:@frytg/dates'
* import { getUnix } from '@frytg/dates'
*
* getUnix()
* getUnix(getNow())
* ```
*/
export const getUnix = (date?: DateTime): number => msToUnix((date || getNow()).valueOf())
export const getUnix = (date?: DateTime): number => msToUnix(getDateTime(date).valueOf())

/**
* Provides util to get ms timestamp
* Get ms (milliseconds) timestamp
* @param {DateTime} [date] - date object
* @returns {number} ms timestamp
*
* @example
* ```ts
* import { getMs } from 'jsr:@frytg/dates'
* import { getMs } from '@frytg/dates'
*
* getMs()
* getMs(getNow())
* ```
*/
export const getMs = (date?: DateTime): number => (date || getNow()).valueOf()
export const getMs = (date?: DateTime): number => getDateTime(date).valueOf()

/**
* Internal util to get DateTime object
* @param {DateTime} [date] - date object
* @returns {DateTime} DateTime object
*/
const getDateTime = (date?: DateTime): DateTime => (date instanceof DateTime ? date : getNow())

/**
* Provides util to get ISO string in UTC timezone
* Get ISO string in UTC timezone
* @param {DateTime} [date] - date object
* @returns {string} ISO string
*
* @example
* ```ts
* import { getISO } from 'jsr:@frytg/dates'
* import { getISO } from '@frytg/dates'
*
* getISO()
* getISO(getNow())
* ```
*/
export const getISO = (date?: DateTime): string => (date || getNow()).toUTC().toISO()
export const getISO = (date?: DateTime): string | null => getDateTime(date).toUTC().toISO()

/**
* Get ISO string (alias for {@link getISO})
Expand All @@ -110,7 +126,7 @@ export const getIso = getISO
*
* @example
* ```ts
* import { getMsOffset } from 'jsr:@frytg/dates'
* import { getMsOffset } from '@frytg/dates'
*
* getMsOffset(getMs())
* ```
Expand All @@ -125,13 +141,13 @@ export const getMsOffset = (ms: number): number => getMs() - ms
*
* @example
* ```ts
* import { getRelative } from 'jsr:@frytg/dates'
* import { getRelative } from '@frytg/dates'
*
* getRelative(getNow())
* getRelative(getNow(), 'nl-NL')
* ```
*/
export const getRelative = (date: DateTime, locale = 'en-US'): string => date.toRelative({ locale })
export const getRelative = (date: DateTime, locale = 'en-US'): string | null => date.toRelative({ locale })

/**
* Get year-month-day (YYYYMMDD or YYYY-MM-DD)
Expand All @@ -141,14 +157,14 @@ export const getRelative = (date: DateTime, locale = 'en-US'): string => date.to
*
* @example
* ```ts
* import { getYearMonthDay } from 'jsr:@frytg/dates'
* import { getYearMonthDay } from '@frytg/dates'
*
* getYearMonthDay(getNow()) // YYYYMMDD
* getYearMonthDay(getNow(), true) // YYYY-MM-DD
* ```
*/
export const getYearMonthDay = (date?: DateTime, withDashes = false): string | number =>
withDashes ? (date || getNow()).toFormat('yyyy-LL-dd') : Number.parseInt((date || getNow()).toFormat('yyyyLLdd'))
withDashes ? getDateTime(date).toFormat('yyyy-LL-dd') : Number.parseInt(getDateTime(date).toFormat('yyyyLLdd'))

/**
* Parse ISO string
Expand All @@ -157,7 +173,7 @@ export const getYearMonthDay = (date?: DateTime, withDashes = false): string | n
*
* @example
* ```ts
* import { parseISO } from 'jsr:@frytg/dates'
* import { parseISO } from '@frytg/dates'
*
* parseISO(getISO())
* parseISO('2024-11-23T10:00:00.000Z')
Expand All @@ -179,7 +195,7 @@ export const parseIso = parseISO
*
* @example
* ```ts
* import { getDateHourMinutes } from 'jsr:@frytg/dates'
* import { getDateHourMinutes } from '@frytg/dates'
*
* getDateHourMinutes(getNow())
* ```
Expand All @@ -194,7 +210,7 @@ export const getDateHourMinutes = (date: DateTime): string =>
*
* @example
* ```ts
* import { getFullRelativeTime } from 'jsr:@frytg/dates'
* import { getFullRelativeTime } from '@frytg/dates'
*
* getFullRelativeTime(getNow())
* ```
Expand All @@ -209,7 +225,7 @@ export const getFullRelativeTime = (date: DateTime): string => `${getDateHourMin
*
* @example
* ```ts
* import { formatDuration } from 'jsr:@frytg/dates'
* import { formatDuration } from '@frytg/dates'
*
* formatDuration(1000) // 1s
*
Expand Down
3 changes: 2 additions & 1 deletion dates/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@frytg/dates",
"version": "0.1.0",
"version": "0.1.1",
"exports": "./dates.ts",
"imports": {
"@std/fmt": "jsr:@std/fmt@^1.0.3",
"@types/luxon": "npm:@types/luxon@^3.4.2",
"luxon": "npm:luxon@^3.5.0"
},
"publish": {
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@biomejs/biome": "npm:@biomejs/biome@^1.9.4",
"@types/node": "npm:@types/node@^22.10.2",
"@cross/test": "jsr:@cross/test@^0.0.10",
"@std/assert": "jsr:@std/assert@^1.0.9",
"@std/assert": "jsr:@std/assert@^1.0.10",
"sinon": "npm:sinon@^19.0.2"
}
}
Loading

0 comments on commit a92de14

Please sign in to comment.