You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
vikingowl 77c3b90d02 added prettier and eslint for dev 3 months ago
lib added prettier and eslint for dev 3 months ago
.eslintrc.json added prettier and eslint for dev 3 months ago
.gitignore initial commit 6 months ago
.prettierrc.toml added prettier and eslint for dev 3 months ago
LICENCE added licence + fixed dependencies 6 months ago
README.md added README.md 6 months ago
index.html added index.html for testing 6 months ago
package-lock.json added prettier and eslint for dev 3 months ago
package.json added prettier and eslint for dev 3 months ago
tsconfig.json added prettier and eslint for dev 3 months ago

README.md

vikingowls-ts-snippets

A collection of often used typescript snippets


Utils

sleep()

The sleep function takes the time to sleep in milliseconds.

import { sleep } from "vikingowls-ts-snippets";

sleep(2000).then(any => {
  // do-something
})

Dom Utils

addGlobalEventListener()

The addGlobalEventListener function takes a type (string), a selector (string), a callback function, options (boolean or AddEventListenerOptions), and a parent (DOM or querySelector, etc)

import {addGlobalEventListener, qs} from "./domUtils";

addGlobalEventListener(
    "click",
    ".btn",
    () => console.log("Run once"),
    {once: true}
)

addGlobalEventListener(
    "click",
    ".btn",
    () => console.log("Only works inside the wrapper class"),
    {},
    qs(".wrapper")
)

qs()

The qs function takes a selector (string) and a parent (DOM etc)

import {qs} from "./domUtils";

// instead of document.querySelector(".btn")
qs(".btn")

qsa()

The qsa function takes a selector (string) and a parent (DOM etc) and returns an Array of Elements

import {qsa} from "./domUtils";

// instead of document.querySelectorAll(".btn")
qsa(".btn")

Formatters

formatCurrency()

The formatCurrency function takes a number and a currency (as shown). The output defaults to the locale set in the browser but can be set manually.

import { formatCurrency } from "vikingowls-ts-snippets";

formatCurrency(9_123_456.789123, "EUR", "de-de")
// prints in germany "9.123.456,79 €"
formatCurrency(9_123_456.789123, "USD")
// prints in usa "$9,123,456.79"

formatNumber()

The formatNumber function takes a number. The output defaults to the locale set in the browser but can be set manually.

import { formatNumber } from "vikingowls-ts-snippets";

formatNumber(9_123_456.789123, "de-de")
// prints in germany "9.123.456,789"
formatNumber(9_123_456.789123)
// prints in usa "9,123,456.789"

formatCompactNumber()

The formatCompactNumber function takes a number. The output defaults to the locale set in the browser but can be set manually.

import { formatCompactNumber } from "vikingowls-ts-snippets";

formatCompactNumber(9_123_456.789123, "de-de")
// prints in germany "9,1 Mio."
formatCompactNumber(9_123_456.789123)
// prints in usa "9.1M"

formatRelativeDate()

The formatRelativeDate function takes a Date (end Date). You also can set a default 'start Date'. The output defaults to the locale set in the browser but can be set manually.

import { formatRelativeDate } from "vikingowls-ts-snippets";

formatRelativeDate(new Date().getSeconds() - 20000, new Date().getSeconds(), "de-de")
// prints in germany "vor 20 Sekunden"
formatRelativeDate(new Date().getSeconds() - 20000, new Date().getSeconds())
// prints in usa "20 seconds ago"