From d7d461632140598bfd9fcdfceb5d0fa2fe5a6882 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Fri, 8 Aug 2025 18:11:57 +0200 Subject: [PATCH 1/2] chore: add readme and adapt license --- LICENSE | 1 + Readme.md | 67 +++++++++++++++++++++++++++++++++++---- test/dateformat/Readme.md | 3 ++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 test/dateformat/Readme.md diff --git a/LICENSE b/LICENSE index 1c37c1d..4bb274e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ The MIT License (MIT) +Copyright (c) 2007-2009 Steven Levithan Copyright (c) 2025 the Pino team listed at https://github.com/pinojs/pino#the-team Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Readme.md b/Readme.md index d9e8c2e..61f256a 100644 --- a/Readme.md +++ b/Readme.md @@ -1,14 +1,69 @@ -# @pino/dateformat +# @pinojs/dateformat -[![NPM Package Version](https://img.shields.io/npm/v/@pino/dateformat)](https://www.npmjs.com/package/@pino/dateformat) -[![Build Status](https://img.shields.io/github/actions/workflow/status/pinojs/dateformat/ci.yml?branch=master)](https://github.com/pinojs/dateformat/actions?query=workflow%3ACI)) +[![NPM Package Version](https://img.shields.io/npm/v/@pino/dateformat)](https://www.npmjs.com/package/@pinojs/dateformat) +[![Build Status](https://img.shields.io/github/actions/workflow/status/pinojs/dateformat/ci.yml?branch=main)](https://github.com/pinojs/dateformat/actions?query=workflow%3ACI) [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) +This packages is a reimplementation of the original [dateformat](https://github.com/felixge/node-dateformat). It focuses on performance and simplicity, while maintaining compatibility with the original API. -### Mask options +This package provides a `DateFormat` class that can be used to format dates in various ways. It supports a wide range of formatting options, including custom masks and named formats. While this package provides a `dateformat` function for convenience, it is recommended to use the `DateFormat` class directly for better performance and flexibility. +While the `dateformat` function parses the mask-string on every call, the `DateFormat` class allows you to create a reusable instance with a pre-parsed mask. This can significantly improve performance when formatting dates multiple times with the same mask. -| Mask | Description | +## Usage + +### `dateformat(mask, date, utc, gmt)` + +The `dateformat` function is a convenience function that allows you to format a date using a mask string. It accepts the following parameters: +- `mask` (string): The format mask to use for formatting the date. +- `date` (Date | number): The date to format. If a number is provided, it is treated as a timestamp. +- `utc` (boolean, optional): If true, the date is formatted in UTC. Defaults to false. +- `gmt` (boolean, optional): If true, the date is formatted in GMT. Defaults to false. + +#### Example: + +```javascript +import { dateformat } from '@pinojs/dateformat'; +const formattedDate = dateformat('yyyy-mm-dd HH:MM:ss', new Date(0)); +console.log(formattedDate); // Outputs: "1970-01-01 00:00:00" +``` +### `DateFormat(mask, mode)` + +The `DateFormat` class allows you to create a reusable date formatter instance with a pre-parsed mask. + +It accepts the following parameters: +- `mask` (string|function): The format mask to use for formatting the date. +- `mode` (string, optional): The mode to use for formatting. Can be either 'UTC' or 'GMT'. Defaults to 'GMT'. + +If the `mask` is a string, it is tokenized and compiled into a function for efficient date formatting. If the `mask` is a function, it is used directly for formatting dates. + +The `mask` function should accept a `Date` object and return a formatted string. The `this` context of the function will be set to the `DateFormat` instance, allowing you to access instance methods. + +#### Example: + +With a string as `mask`: + +```javascript +import { DateFormat } from '@pinojs/dateformat'; +const dateFormatter = new DateFormat('yyyy-mm-dd HH:MM:ss'); +const formattedDate = dateFormatter.format(new Date(0)); +console.log(formattedDate); // Outputs: "1970-01-01 00:00:00" +``` + +With a function as `mask`: + +```javascript +import { DateFormat } from '@pinojs/dateformat'; +const dateFormatter = new DateFormat((date) => { + return `${this.yyyy(date)}-${this.mm(date)}-${this.dd(date)} ${this.HH(date)}:${this.MM(date)}:${this.ss(date)}`; +}); +const formattedDate = dateFormatter.format(new Date(0)); +console.log(formattedDate); // Outputs: "1970-01-01 00:00:00" +``` + +### Tokens + +| Token | Description | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `d` | Day of the month as digits; no leading zero for single-digit days. | | `dd` | Day of the month as digits; leading zero for single-digit days. | @@ -42,7 +97,7 @@ | `TT` | Uppercase, two-character time marker string: AM or PM. | | `W` | ISO 8601 week number of the year, e.g. 4, 42 | | `WW` | ISO 8601 week number of the year, leading zero for single-digit, e.g. 04, 42 | -| `Z` | US timezone abbreviation, e.g. EST or MDT. For non-US timezones, the GMT/UTC offset is returned, e.g. GMT-0500 | +| `Z` | The GMT-Offset is, e.g. GMT-0500, or if it is GMT-0000 it will be return UTC | | `'...'`, `"..."` | Literal character sequence. Surrounding quotes are removed. | | `UTC:` | Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The "UTC:" prefix is removed. | diff --git a/test/dateformat/Readme.md b/test/dateformat/Readme.md new file mode 100644 index 0000000..feb816a --- /dev/null +++ b/test/dateformat/Readme.md @@ -0,0 +1,3 @@ +# dateformat Test Suite + +These tests were adapted from the original [dateformat](https://github.com/felixge/node-dateformat) and ensure that the dateformat function behaves identically to the original library. From 15fa97ac8f1a01d8fdd7d31dfc1bcf57241aa897 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Fri, 8 Aug 2025 19:10:08 +0200 Subject: [PATCH 2/2] remove dependenyreview --- .github/workflows/ci.yml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aab652d..60eaee3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,20 +16,6 @@ concurrency: cancel-in-progress: true jobs: - dependency-review: - name: Dependency Review - if: github.event_name == 'pull_request' - runs-on: ubuntu-latest - permissions: - contents: read - steps: - - name: Check out repo - uses: actions/checkout@v4 - with: - persist-credentials: false - - - name: Dependency review - uses: actions/dependency-review-action@v4 test: name: Test