diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 5a293fd..0000000 --- a/package-lock.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "snippets-ts", - "version": "0.0.1", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "snippets-ts", - "version": "0.0.1", - "license": "MIT", - "dependencies": { - "typescript": "^4.6.3" - }, - "devDependencies": {} - }, - "node_modules/typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==", - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=4.2.0" - } - } - }, - "dependencies": { - "typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==" - } - } -} diff --git a/package.json b/package.json index aa8bbf6..a13ac2c 100644 --- a/package.json +++ b/package.json @@ -1,35 +1,35 @@ { "name": "snippets-ts", "version": "0.0.1", - "description": "Private typescript snippets lib", - "main": "dist/index.js", - "types": "dist/index.d.ts", - "files": [ - "/dist" - ], + "description": "Private typescript snippets library", + "main": "./dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "tsc", - "publish": "yarn run build && yarn publish", - "publish:beta": "yarn run build && yarn publish --tag beta", - "publish:dryrun": "yarn run build && yarn publish --dry-run" + "publish": "yarn run build && npm publish", + "publish-beta": "yarn run build && npm publish --tag beta" }, "repository": { "type": "git", - "url": "git+https://git.harting.dev/vikingowl/snippets-ts.git" + "url": "git://git.harting.dev/vikingowl/snippets-ts.git" }, - "keywords": [ - "typescript", - "library", - "snippets" - ], "bugs": { "url": "https://git.harting.dev/vikingowl/snippets-ts/issues" }, + "keywords": [ + "typescript", + "snippets", + "beta", + "library" + ], "author": "Christian Nachtigall ", "license": "MIT", + "types": "./dist/index.d.ts", + "packageManager": "yarn@3.2.0", "dependencies": { "typescript": "^4.6.3" }, - "packageManager": "yarn@3.2.0" + "files": [ + "/dist" + ] } diff --git a/src/domUtils.ts b/src/domUtils.ts new file mode 100644 index 0000000..b27e2d8 --- /dev/null +++ b/src/domUtils.ts @@ -0,0 +1,24 @@ +export function addGlobalEventListener( + type: string, + selector: string, + callback: (arg0: Event) => void, + options: boolean | AddEventListenerOptions, + parent = document +): void { + parent.addEventListener( + type, + e => { + // @ts-ignore + if (e.target.matches(selector)) callback(e) + }, + options + ) +} + +export function qs(selector: string, parent = document): Element { + return parent.querySelector(selector) +} + +export function qsa(selector: string, parent = document): Element[] { + return [...parent.querySelectorAll(selector)] +} diff --git a/src/formatters.ts b/src/formatters.ts new file mode 100644 index 0000000..dbd3067 --- /dev/null +++ b/src/formatters.ts @@ -0,0 +1,55 @@ +const CURRENCY_FORMATTER = new Intl.NumberFormat(undefined, { + currency: "EUR", + style: "currency", +}) +export function formatCurrency (number: number): string { + return CURRENCY_FORMATTER.format(number) +} + +const NUMBER_FORMATTER = new Intl.NumberFormat(undefined) +export function formatNumber (number: number): string { + return NUMBER_FORMATTER.format(number) +} + +const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat(undefined, { + notation: "compact" +}) +export function formatCompactNumber (number: number): string { + return COMPACT_NUMBER_FORMATTER.format(number) +} + +type DivisionsT = { + amount: number + name: + "year" | "years" | + "quarter" | "quarters" | + "month" | "months" | + "week" | "weeks" | + "day" | "days" | + "hour" | "hours" | + "minute" | "minutes" | + "second" | "seconds" +} +const DIVISTIONS: DivisionsT[] = [ + { amount: 60, name: "seconds" }, + { amount: 60, name: "minutes" }, + { amount: 24, name: "hours" }, + { amount: 7, name: "days" }, + { amount: 4.34524, name: "weeks" }, + { amount: 12, name: "months" }, + { amount: Number.POSITIVE_INFINITY, name: "years" }, +] +const RELATIVE_DATE_FORMATTER = new Intl.RelativeTimeFormat(undefined, { + numeric: "auto" +}) +export function formatRelativeDate(toDate: Date, fromDate: Date = new Date()) { + let duration = (toDate.getSeconds() - fromDate.getSeconds()) / 1000 + + for (let i = 0; i <= DIVISTIONS.length; i++) { + const division = DIVISTIONS[i] + if (Math.abs(duration) < division.amount) { + return RELATIVE_DATE_FORMATTER.format(Math.round(duration), division.name) + } + duration /= division.amount + } +} diff --git a/src/index.ts b/src/index.ts index 3eeaeaa..f7c2baa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,3 @@ export * from "./utils" +export * from "./domUtils" +export * from "./formatters" diff --git a/src/utils.ts b/src/utils.ts index 6148e00..357ff83 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,3 @@ -export function sleep (time: number) { +export function sleep (time: number): Promise { return new Promise((resolve => setTimeout(resolve, time))) }