@vnejs/helpers.format-timestamp 0.2.1

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.
@@ -0,0 +1,5 @@
1
+ export type FormatTimestampPayload = {
2
+ ts?: number | string | Date;
3
+ format?: string;
4
+ };
5
+ export declare const formatTimestamp: ({ ts, format }?: FormatTimestampPayload) => string;
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ const pad = (value) => String(value).padStart(2, "0");
2
+ export const formatTimestamp = ({ ts, format = "" } = {}) => {
3
+ const date = ts === undefined ? new Date() : new Date(ts);
4
+ const tokens = {
5
+ YYYY: String(date.getFullYear()),
6
+ MM: pad(date.getMonth() + 1),
7
+ DD: pad(date.getDate()),
8
+ HH: pad(date.getHours()),
9
+ mm: pad(date.getMinutes()),
10
+ ss: pad(date.getSeconds()),
11
+ };
12
+ return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (token) => tokens[token] ?? token);
13
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@vnejs/helpers.format-timestamp",
3
+ "version": "0.2.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "src",
10
+ "tsconfig.json"
11
+ ],
12
+ "scripts": {
13
+ "test": "npx @vnejs/monorepo test",
14
+ "build": "npx @vnejs/monorepo package",
15
+ "publish:major:plugin": "npm run publish:major",
16
+ "publish:minor:plugin": "npm run publish:minor",
17
+ "publish:patch:plugin": "npm run publish:patch",
18
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
19
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
20
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
21
+ },
22
+ "author": "",
23
+ "license": "ISC",
24
+ "devDependencies": {
25
+ "@vnejs/configs.ts-common": "~0.2.0",
26
+ "@vnejs/configs.vitest": "~0.2.0"
27
+ }
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ export type FormatTimestampPayload = {
2
+ ts?: number | string | Date;
3
+ format?: string;
4
+ };
5
+
6
+ const pad = (value: number) => String(value).padStart(2, "0");
7
+
8
+ export const formatTimestamp = ({ ts, format = "" }: FormatTimestampPayload = {}) => {
9
+ const date = ts === undefined ? new Date() : new Date(ts);
10
+ const tokens: Record<string, string> = {
11
+ YYYY: String(date.getFullYear()),
12
+ MM: pad(date.getMonth() + 1),
13
+ DD: pad(date.getDate()),
14
+ HH: pad(date.getHours()),
15
+ mm: pad(date.getMinutes()),
16
+ ss: pad(date.getSeconds()),
17
+ };
18
+
19
+ return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (token) => tokens[token] ?? token);
20
+ };
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { formatTimestamp } from "../index.js";
4
+
5
+ describe("formatTimestamp", () => {
6
+ it("formats year", () => {
7
+ expect(formatTimestamp({ ts: 0, format: "YYYY" })).toBe("1970");
8
+ });
9
+
10
+ it("formats date", () => {
11
+ expect(formatTimestamp({ ts: 0, format: "DD.MM.YYYY" })).toBe(
12
+ formatTimestamp({ ts: 0, format: "DD" }) + "." + formatTimestamp({ ts: 0, format: "MM" }) + ".1970",
13
+ );
14
+ });
15
+
16
+ it("formats time", () => {
17
+ const formatted = formatTimestamp({ ts: 0, format: "HH:mm:ss" });
18
+
19
+ expect(formatted).toMatch(/^\d{2}:\d{2}:\d{2}$/);
20
+ });
21
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": [
8
+ "src/**/*.ts"
9
+ ],
10
+ "exclude": [
11
+ "dist",
12
+ "node_modules",
13
+ "src/tests"
14
+ ]
15
+ }