Compare commits
2 Commits
2cf41b2cd0
...
23957950df
Author | SHA1 | Date | |
---|---|---|---|
23957950df | |||
110e83afdf |
546
.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
vendored
Normal file
546
.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
vendored
Normal file
File diff suppressed because one or more lines are too long
785
.yarn/releases/yarn-3.2.0.cjs
vendored
Executable file
785
.yarn/releases/yarn-3.2.0.cjs
vendored
Executable file
File diff suppressed because one or more lines are too long
7
.yarnrc.yml
Normal file
7
.yarnrc.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
nodeLinker: node-modules
|
||||
|
||||
plugins:
|
||||
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
|
||||
spec: "@yarnpkg/plugin-interactive-tools"
|
||||
|
||||
yarnPath: .yarn/releases/yarn-3.2.0.cjs
|
36
package-lock.json
generated
36
package-lock.json
generated
@@ -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=="
|
||||
}
|
||||
}
|
||||
}
|
34
package.json
34
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": "npm build && npm publish",
|
||||
"publish:beta": "npm build && npm publish --tag beta",
|
||||
"publish:dryrun": "npm build && npm 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",
|
||||
"devDependencies": {},
|
||||
"types": "./dist/index.d.ts",
|
||||
"packageManager": "yarn@3.2.0",
|
||||
"dependencies": {
|
||||
"typescript": "^4.6.3"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"/dist"
|
||||
]
|
||||
}
|
||||
|
24
src/domUtils.ts
Normal file
24
src/domUtils.ts
Normal 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
55
src/formatters.ts
Normal 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
|
||||
}
|
||||
}
|
@@ -1 +1,3 @@
|
||||
export * from "./utils"
|
||||
export * from "./domUtils"
|
||||
export * from "./formatters"
|
||||
|
@@ -1,3 +1,3 @@
|
||||
export function sleep (time: number) {
|
||||
export function sleep (time: number): Promise<unknown> {
|
||||
return new Promise((resolve => setTimeout(resolve, time)))
|
||||
}
|
||||
|
34
yarn.lock
Normal file
34
yarn.lock
Normal file
@@ -0,0 +1,34 @@
|
||||
# This file is generated by running "yarn install" inside your project.
|
||||
# Manual changes might be lost - proceed with caution!
|
||||
|
||||
__metadata:
|
||||
version: 6
|
||||
cacheKey: 8
|
||||
|
||||
"snippets-ts@workspace:.":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "snippets-ts@workspace:."
|
||||
dependencies:
|
||||
typescript: ^4.6.3
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"typescript@npm:^4.6.3":
|
||||
version: 4.6.3
|
||||
resolution: "typescript@npm:4.6.3"
|
||||
bin:
|
||||
tsc: bin/tsc
|
||||
tsserver: bin/tsserver
|
||||
checksum: 255bb26c8cb846ca689dd1c3a56587af4f69055907aa2c154796ea28ee0dea871535b1c78f85a6212c77f2657843a269c3a742d09d81495b97b914bf7920415b
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"typescript@patch:typescript@^4.6.3#~builtin<compat/typescript>":
|
||||
version: 4.6.3
|
||||
resolution: "typescript@patch:typescript@npm%3A4.6.3#~builtin<compat/typescript>::version=4.6.3&hash=bda367"
|
||||
bin:
|
||||
tsc: bin/tsc
|
||||
tsserver: bin/tsserver
|
||||
checksum: 6bf45caf847062420592e711bc9c28bf5f9a9a7fa8245343b81493e4ededae33f1774009d1234d911422d1646a2c839f44e1a23ecb111b40a60ac2ea4c1482a8
|
||||
languageName: node
|
||||
linkType: hard
|
Reference in New Issue
Block a user