@rcooler/format 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/dist/calendar.d.ts +15 -0
- package/dist/calendar.js +52 -0
- package/dist/duration.d.ts +13 -0
- package/dist/duration.js +16 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/dist/instant.d.ts +18 -0
- package/dist/instant.js +34 -0
- package/dist/number.d.ts +19 -0
- package/dist/number.js +32 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Roman Derevianko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rcooler/format
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Durations, calendar dates, instants in an explicit time zone (default `Europe/Kyiv`), elapsed time,
|
|
4
|
+
numbers, and UAH, USD and EUR amounts. Nothing reads the machine's time zone or language: the zone,
|
|
5
|
+
the locale and "now" are arguments, so output is the same on every machine. No runtime dependencies.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @rcooler/format
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
ESM with type declarations. Part of [web-kit](https://github.com/RCooLeR/web-kit): the
|
|
12
|
+
[README](https://github.com/RCooLeR/web-kit#readme) there describes every package, and the
|
|
13
|
+
[changelog](https://github.com/RCooLeR/web-kit/blob/main/CHANGELOG.md) lists the changes.
|
|
14
|
+
|
|
15
|
+
## Licence
|
|
16
|
+
|
|
17
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The YYYY-MM-DD date `days` after (or, when negative, before) `date`. */
|
|
2
|
+
export declare function addDays(date: string, days: number): string;
|
|
3
|
+
/** Every date from `from` to `to`, both included; empty when `to` is before `from`. */
|
|
4
|
+
export declare function dateRange(from: string, to: string): string[];
|
|
5
|
+
/** Weekday of a YYYY-MM-DD date with Monday as 0. */
|
|
6
|
+
export declare function weekdayIndex(date: string): number;
|
|
7
|
+
/** "22.11" or "11/22" by locale, for chart axes where the year is obvious. */
|
|
8
|
+
export declare function formatShortDate(date: string, locale: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* A calendar date in the locale's usual form: "01.06.2026" in `uk`, "6/1/2026" in `en`. Accepts a
|
|
11
|
+
* date with a time part after it (an ISO timestamp from an API) and uses only the date. Text that
|
|
12
|
+
* is not a real calendar date, such as "2026-02-31", is returned unchanged rather than silently
|
|
13
|
+
* rolled over to another day.
|
|
14
|
+
*/
|
|
15
|
+
export declare function formatCalendarDate(date: string, locale: string): string;
|
package/dist/calendar.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Calendar dates as YYYY-MM-DD strings: a business date (a due date, a reading's day) that means
|
|
2
|
+
// the same day everywhere. They are moved around in UTC arithmetic and formatted in UTC, never
|
|
3
|
+
// through a local time zone, so no date shifts by a day on a machine east or west of the server.
|
|
4
|
+
function parts(date) {
|
|
5
|
+
const [year = 0, month = 1, day = 1] = date.split("-").map(Number);
|
|
6
|
+
return [year, month, day];
|
|
7
|
+
}
|
|
8
|
+
function utc(date) {
|
|
9
|
+
const [year, month, day] = parts(date);
|
|
10
|
+
return new Date(Date.UTC(year, month - 1, day));
|
|
11
|
+
}
|
|
12
|
+
/** The YYYY-MM-DD date `days` after (or, when negative, before) `date`. */
|
|
13
|
+
export function addDays(date, days) {
|
|
14
|
+
const [year, month, day] = parts(date);
|
|
15
|
+
return new Date(Date.UTC(year, month - 1, day + days)).toISOString().slice(0, 10);
|
|
16
|
+
}
|
|
17
|
+
/** Every date from `from` to `to`, both included; empty when `to` is before `from`. */
|
|
18
|
+
export function dateRange(from, to) {
|
|
19
|
+
const dates = [];
|
|
20
|
+
for (let date = from; date <= to; date = addDays(date, 1))
|
|
21
|
+
dates.push(date);
|
|
22
|
+
return dates;
|
|
23
|
+
}
|
|
24
|
+
/** Weekday of a YYYY-MM-DD date with Monday as 0. */
|
|
25
|
+
export function weekdayIndex(date) {
|
|
26
|
+
return (utc(date).getUTCDay() + 6) % 7;
|
|
27
|
+
}
|
|
28
|
+
/** "22.11" or "11/22" by locale, for chart axes where the year is obvious. */
|
|
29
|
+
export function formatShortDate(date, locale) {
|
|
30
|
+
return new Intl.DateTimeFormat(locale, {
|
|
31
|
+
day: "numeric",
|
|
32
|
+
month: "numeric",
|
|
33
|
+
timeZone: "UTC",
|
|
34
|
+
}).format(utc(date));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A calendar date in the locale's usual form: "01.06.2026" in `uk`, "6/1/2026" in `en`. Accepts a
|
|
38
|
+
* date with a time part after it (an ISO timestamp from an API) and uses only the date. Text that
|
|
39
|
+
* is not a real calendar date, such as "2026-02-31", is returned unchanged rather than silently
|
|
40
|
+
* rolled over to another day.
|
|
41
|
+
*/
|
|
42
|
+
export function formatCalendarDate(date, locale) {
|
|
43
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})(?:$|T|\s)/.exec(date);
|
|
44
|
+
if (!match)
|
|
45
|
+
return date;
|
|
46
|
+
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
47
|
+
const value = new Date(Date.UTC(year, month - 1, day));
|
|
48
|
+
const isReal = value.getUTCFullYear() === year &&
|
|
49
|
+
value.getUTCMonth() === month - 1 &&
|
|
50
|
+
value.getUTCDate() === day;
|
|
51
|
+
return isReal ? new Intl.DateTimeFormat(locale, { timeZone: "UTC" }).format(value) : date;
|
|
52
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const MINUTE_MS = 60000;
|
|
2
|
+
export declare const HOUR_MS: number;
|
|
3
|
+
export declare const DAY_MS: number;
|
|
4
|
+
/** The unit words of the user's language, from the app's dictionary: "h" and "min", "год" and "хв". */
|
|
5
|
+
export interface DurationUnits {
|
|
6
|
+
hours: string;
|
|
7
|
+
minutes: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* "1 h 05 min", "12 min", "0 min". Rounds down, so time that has not passed yet is never shown as
|
|
11
|
+
* passed. A negative duration counts as zero.
|
|
12
|
+
*/
|
|
13
|
+
export declare function formatDuration(ms: number, units: DurationUnits): string;
|
package/dist/duration.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Durations in milliseconds, shown as hours and minutes.
|
|
2
|
+
export const MINUTE_MS = 60_000;
|
|
3
|
+
export const HOUR_MS = 60 * MINUTE_MS;
|
|
4
|
+
export const DAY_MS = 24 * HOUR_MS;
|
|
5
|
+
/**
|
|
6
|
+
* "1 h 05 min", "12 min", "0 min". Rounds down, so time that has not passed yet is never shown as
|
|
7
|
+
* passed. A negative duration counts as zero.
|
|
8
|
+
*/
|
|
9
|
+
export function formatDuration(ms, units) {
|
|
10
|
+
const totalMinutes = Math.floor(Math.max(ms, 0) / MINUTE_MS);
|
|
11
|
+
const hours = Math.floor(totalMinutes / 60);
|
|
12
|
+
const minutes = totalMinutes % 60;
|
|
13
|
+
if (hours === 0)
|
|
14
|
+
return `${minutes} ${units.minutes}`;
|
|
15
|
+
return `${hours} ${units.hours} ${String(minutes).padStart(2, "0")} ${units.minutes}`;
|
|
16
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DAY_MS, HOUR_MS, MINUTE_MS, formatDuration } from "./duration.js";
|
|
2
|
+
export type { DurationUnits } from "./duration.js";
|
|
3
|
+
export { addDays, dateRange, formatCalendarDate, formatShortDate, weekdayIndex, } from "./calendar.js";
|
|
4
|
+
export { KYIV_TIME_ZONE, elapsedSince, formatDateTime, formatElapsed, formatTime, } from "./instant.js";
|
|
5
|
+
export type { Elapsed } from "./instant.js";
|
|
6
|
+
export { CURRENCIES, createMoneyFormatter, formatNumber } from "./number.js";
|
|
7
|
+
export type { Currency, FractionDigits, MoneyFormatter } from "./number.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// @rcooler/format: durations, dates, numbers and money, written the same way in every app.
|
|
2
|
+
//
|
|
3
|
+
// Everything is a pure function of its arguments. Nothing reads the browser's time zone: an
|
|
4
|
+
// instant is shown in an explicit zone that defaults to Kyiv, and a calendar date is a
|
|
5
|
+
// YYYY-MM-DD string that is never moved across a zone boundary.
|
|
6
|
+
export { DAY_MS, HOUR_MS, MINUTE_MS, formatDuration } from "./duration.js";
|
|
7
|
+
export { addDays, dateRange, formatCalendarDate, formatShortDate, weekdayIndex, } from "./calendar.js";
|
|
8
|
+
export { KYIV_TIME_ZONE, elapsedSince, formatDateTime, formatElapsed, formatTime, } from "./instant.js";
|
|
9
|
+
export { CURRENCIES, createMoneyFormatter, formatNumber } from "./number.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The default zone wherever an instant is shown. */
|
|
2
|
+
export declare const KYIV_TIME_ZONE = "Europe/Kyiv";
|
|
3
|
+
/** Date and time of an instant: "24 лист. 2026 р., 14:05" in `uk`, "Nov 24, 2026, 2:05 PM" in `en`. */
|
|
4
|
+
export declare function formatDateTime(iso: string, locale: string, timeZone?: string): string;
|
|
5
|
+
/** The time of day of an instant: "14:05" in `uk`, "2:05 PM" in `en`. */
|
|
6
|
+
export declare function formatTime(iso: string, locale: string, timeZone?: string): string;
|
|
7
|
+
/** A whole number of the largest unit that fits. */
|
|
8
|
+
export interface Elapsed {
|
|
9
|
+
value: number;
|
|
10
|
+
unit: "minute" | "hour" | "day";
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* How long ago `iso` was at `now` (milliseconds since the epoch), in the largest unit that fits:
|
|
14
|
+
* minutes, hours or days, rounded down. An instant in the future counts as now.
|
|
15
|
+
*/
|
|
16
|
+
export declare function elapsedSince(iso: string, now: number): Elapsed;
|
|
17
|
+
/** `elapsedSince` as a phrase in the locale's grammar: "3 hours ago", "3 години тому". */
|
|
18
|
+
export declare function formatElapsed(iso: string, now: number, locale: string): string;
|
package/dist/instant.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Instants (ISO timestamps): shown in an explicit time zone, and measured against a "now" the
|
|
2
|
+
// caller passes in.
|
|
3
|
+
import { DAY_MS, HOUR_MS, MINUTE_MS } from "./duration.js";
|
|
4
|
+
/** The default zone wherever an instant is shown. */
|
|
5
|
+
export const KYIV_TIME_ZONE = "Europe/Kyiv";
|
|
6
|
+
/** Date and time of an instant: "24 лист. 2026 р., 14:05" in `uk`, "Nov 24, 2026, 2:05 PM" in `en`. */
|
|
7
|
+
export function formatDateTime(iso, locale, timeZone = KYIV_TIME_ZONE) {
|
|
8
|
+
return new Intl.DateTimeFormat(locale, {
|
|
9
|
+
dateStyle: "medium",
|
|
10
|
+
timeStyle: "short",
|
|
11
|
+
timeZone,
|
|
12
|
+
}).format(new Date(iso));
|
|
13
|
+
}
|
|
14
|
+
/** The time of day of an instant: "14:05" in `uk`, "2:05 PM" in `en`. */
|
|
15
|
+
export function formatTime(iso, locale, timeZone = KYIV_TIME_ZONE) {
|
|
16
|
+
return new Intl.DateTimeFormat(locale, { timeStyle: "short", timeZone }).format(new Date(iso));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* How long ago `iso` was at `now` (milliseconds since the epoch), in the largest unit that fits:
|
|
20
|
+
* minutes, hours or days, rounded down. An instant in the future counts as now.
|
|
21
|
+
*/
|
|
22
|
+
export function elapsedSince(iso, now) {
|
|
23
|
+
const elapsed = Math.max(now - new Date(iso).getTime(), 0);
|
|
24
|
+
if (elapsed < HOUR_MS)
|
|
25
|
+
return { value: Math.floor(elapsed / MINUTE_MS), unit: "minute" };
|
|
26
|
+
if (elapsed < DAY_MS)
|
|
27
|
+
return { value: Math.floor(elapsed / HOUR_MS), unit: "hour" };
|
|
28
|
+
return { value: Math.floor(elapsed / DAY_MS), unit: "day" };
|
|
29
|
+
}
|
|
30
|
+
/** `elapsedSince` as a phrase in the locale's grammar: "3 hours ago", "3 години тому". */
|
|
31
|
+
export function formatElapsed(iso, now, locale) {
|
|
32
|
+
const { value, unit } = elapsedSince(iso, now);
|
|
33
|
+
return new Intl.RelativeTimeFormat(locale, { numeric: "always" }).format(-value, unit);
|
|
34
|
+
}
|
package/dist/number.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** How many digits follow the decimal separator. */
|
|
2
|
+
export interface FractionDigits {
|
|
3
|
+
minimumFractionDigits?: number;
|
|
4
|
+
maximumFractionDigits?: number;
|
|
5
|
+
}
|
|
6
|
+
/** A plain number: "1 234,5" in `uk`, "1,234.5" in `en`. Up to three decimals unless told otherwise. */
|
|
7
|
+
export declare function formatNumber(value: number, locale: string, digits?: FractionDigits): string;
|
|
8
|
+
/** The supported currencies. */
|
|
9
|
+
export declare const CURRENCIES: readonly ["UAH", "USD", "EUR"];
|
|
10
|
+
export type Currency = (typeof CURRENCIES)[number];
|
|
11
|
+
/** Formats an amount, in the formatter's own currency unless another is named. */
|
|
12
|
+
export type MoneyFormatter = (amount: number, currency?: Currency) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a formatter for money in `locale`: "1 234,50 ₴", "$1,234.50", with the narrow currency
|
|
15
|
+
* symbol and two decimals. The formatter keeps one Intl object per currency, so a table of a
|
|
16
|
+
* thousand amounts builds three at most. A missing amount is the caller's to handle: this
|
|
17
|
+
* formats numbers and never turns "unknown" into 0.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createMoneyFormatter(locale: string, defaultCurrency?: Currency): MoneyFormatter;
|
package/dist/number.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Numbers and money in the locale's notation.
|
|
2
|
+
/** A plain number: "1 234,5" in `uk`, "1,234.5" in `en`. Up to three decimals unless told otherwise. */
|
|
3
|
+
export function formatNumber(value, locale, digits = {}) {
|
|
4
|
+
return new Intl.NumberFormat(locale, digits).format(value);
|
|
5
|
+
}
|
|
6
|
+
/** The supported currencies. */
|
|
7
|
+
export const CURRENCIES = ["UAH", "USD", "EUR"];
|
|
8
|
+
/**
|
|
9
|
+
* Returns a formatter for money in `locale`: "1 234,50 ₴", "$1,234.50", with the narrow currency
|
|
10
|
+
* symbol and two decimals. The formatter keeps one Intl object per currency, so a table of a
|
|
11
|
+
* thousand amounts builds three at most. A missing amount is the caller's to handle: this
|
|
12
|
+
* formats numbers and never turns "unknown" into 0.
|
|
13
|
+
*/
|
|
14
|
+
export function createMoneyFormatter(locale, defaultCurrency = "UAH") {
|
|
15
|
+
const formatters = new Map();
|
|
16
|
+
return (amount, currency = defaultCurrency) => {
|
|
17
|
+
let formatter = formatters.get(currency);
|
|
18
|
+
if (!formatter) {
|
|
19
|
+
formatter = new Intl.NumberFormat(locale, {
|
|
20
|
+
style: "currency",
|
|
21
|
+
currency,
|
|
22
|
+
currencyDisplay: "narrowSymbol",
|
|
23
|
+
minimumFractionDigits: 2,
|
|
24
|
+
maximumFractionDigits: 2,
|
|
25
|
+
// A float that rounds to zero, such as 100.1 - 100 - 0.1, shows no minus sign.
|
|
26
|
+
signDisplay: "negative",
|
|
27
|
+
});
|
|
28
|
+
formatters.set(currency, formatter);
|
|
29
|
+
}
|
|
30
|
+
return formatter.format(amount);
|
|
31
|
+
};
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rcooler/format",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dates in Kyiv time, durations, numbers and UAH, USD and EUR money.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"@rcooler/source": "./src/index.ts",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=24"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/RCooLeR/web-kit.git",
|
|
24
|
+
"directory": "packages/format"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "node ../../node_modules/@typescript/native/bin/tsc -p tsconfig.build.json",
|
|
32
|
+
"typecheck": "node ../../node_modules/@typescript/native/bin/tsc --noEmit -p tsconfig.json"
|
|
33
|
+
}
|
|
34
|
+
}
|