added more functions

This commit is contained in:
2022-03-31 05:33:59 +02:00
parent 110e83afdf
commit 23957950df
6 changed files with 98 additions and 53 deletions

36
package-lock.json generated
View File

@@ -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=="
}
}
}

View File

@@ -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 <cw.nachtigall@gmail.com>",
"license": "MIT",
"types": "./dist/index.d.ts",
"packageManager": "yarn@3.2.0",
"dependencies": {
"typescript": "^4.6.3"
},
"packageManager": "yarn@3.2.0"
"files": [
"/dist"
]
}

24
src/domUtils.ts Normal file
View File

@@ -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)]
}

55
src/formatters.ts Normal file
View File

@@ -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
}
}

View File

@@ -1 +1,3 @@
export * from "./utils"
export * from "./domUtils"
export * from "./formatters"

View File

@@ -1,3 +1,3 @@
export function sleep (time: number) {
export function sleep (time: number): Promise<unknown> {
return new Promise((resolve => setTimeout(resolve, time)))
}