Filters array to unique elements.
import {unique} from "array";
const array = ["a", "a", "b", "b", "c", "c", "d"];
const uniqueArray = unique(array, (a, b) => a.localeCompare(b));
validationController.addRenderer(new Bootstrap5ValidationFormRenderer());
import {createChecksum} from 'objects';
const myObject = {
"hello": "world"
}
const checksum = createChecksum(myObject);
Implements a workaround for copying text in framed applications.
import {Clipboard} from 'clipboard';
const clipboard = new Clipboard()
clipboard.writeText("hello world").then(() => console.log("Copied"));
Number handling and formatting.
import {bytesMap, getFactor, kiloMap} from "utils/numbers";
const posts = 1300;
const kilo = getFactor(posts, kiloMap);
const bytes = getFactor(posts, bytesMap);
console.log(`Your wrote ${(posts / kilo.factor).toFixed(1)} ${kilo.unit} posts with a size of ${(posts / bytes.factor).toFixed(0)} ${bytes.unit} `);
// You wrote 1.3k posts with a size of 1 KiB
import {round} from "utils/numbers";
const value = 0.016
const round = round(2)
// round == 0.02
import {calcDecimalPlace} from "utils/numbers";
const value = 0.000123
const precision = calcDecimalPlace(value, 3)
// precision == 6
Combined example
import {round, calcDecimalPlace} from "utils/numbers";
function financialRound(value: number) {
return round(value, calcDecimalPlace(value, 3));
}
Provides some time functions.
import {addTimedelta, DateTimeComponents, subtractTimedelta} from "utils/time";
const now = new Date();
const timedelta: DateTimeComponents = {
days: 10
}
const newDate = addTimedelta(now, timedelta);
const oldDate = subtractTimedelta(newDate, timedelta);
Normalizes the time of a given date to full divisors of the components.
import {normalizeTime, TimeComponents} from "./time";
const now = new Date();
// now: 13:44:33
const timedelta: TimeComponents = {
hours: 4,
minutes: 5,
seconds: 0
}
const newDate = normalizeTime(now, timedelta);
// newDate: 12:40:00
import {setComponents, TimeComponents} from "./time";
const now = new Date();
// now: 13:44:33
const timedelta: TimeComponents = {
minutes: 5,
seconds: 0
}
const newDate = setComponents(now, timedelta);
// newDate: 13:05:00
import {toMilliseconds} from "./time";
window.setTimeout(() => console.log("Hello"), toMilliseconds({seconds: 10}));
import {sleep} from "./time";
await sleep({seconds: 10});
Generic Timer implementation
ViewModel
import {autoinject} from "aurelia-dependency-injection";
import {Timer} from "utils/timer";
import {IntlDateFormatValueConverter} from "value-converters/intl-date-format-value-converter";
@autoinject
export class ViewModel {
private readonly _timer: Timer
construct(dateFormatter: IntlDateFormatValueConverter) {
dateFormatter.setOptions("hour", {
minute: "numeric",
second: "numeric"
})
this._timer = new Timer({
duration: {minutes: 5},
onComplete: timer => {
// do something
}
})
}
attached() {
this._timer.start()
}
detached() {
this._timer.dispose()
}
}
Template
${_timer.timeLeft|dateFormat:"hour"}
// 04:59
Implements the behaviour of a trash bin.
This utiliy is @deprecated
because it's too specific and hard to reuse generally.
TypeScript
import {TrashBin} from 'trash-bin';
const itemList = [
{
name: "Something",
deleted: false,
deletable: false,
}
];
const trashBin = TrashBin.create<any>();
trashBin.source(itemList);
trashBin.onTrash(item => {
// Prevent deletion
if (item.deletable === false) {
return false;
} else {
item.deleted = true;
}
});
trashBin.onRestore(item=>{
item.deleted = false;
});
const item = itemList[0];
trashBin.trash(item);
console.log(trashBin.sourceItems.length); // 0
console.log(trashBin.trashItems.length); // 1
trashBin.restore(item);
console.log(trashBin.sourceItems.length); // 1
console.log(trashBin.trashItems.length); // 0
trashBin.empty();
import {recursiveObjectSort} from 'objects';
const myObject = {
"katze": "kuh",
"hello": "world",
}
const sorted = recursiveObjectSort(myObject);
Allows setting objects to an implementation of a Storage interface
import {ObjectStorage} from "object-storage";
const objectStorage = new ObjectStorage();
objectStorage.setStorage(localStorage);
objectStorage.setItem("my-object", {});
const object = objectStorage.getItem("my-object");