0.0.2 • Published 3 years ago

@krayfaus/essentials v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Krayfaus' Typescript Essentials

This package is a collection of utilities I find useful when coding in TypeScript.

FunctionDescription
String.prototype.formatEquivalent* alternative to String literals.
String.formatEquivalent* alternative to String literals.
String.valueAvoid (string | undefined) types.
resolveResolves a promise (try...catch) and returns data, error.
waitSleep worker thread for n milliseconds.

* Slightly faster according to a performance test.

👨‍🏫 Usage

wait() and String.format

import '@krayfaus/essentials/extend/string';
import { wait } from '@krayfaus/essentials';

async function withEssentials() {
    const delay = await wait(5000); // Sleep for 5 seconds.
    console.info('Waited for {0}ms.'.format(delay));
}

async function withoutEssentials() {
    const delay = setTimeout(function () {
        /* do something after the timeout */
    }, 5000); // Sleep for 5 seconds.
    console.info(`Waited for ${delay}ms.`);
}

resolve() and String.value()

import axios from 'axios';
import { resolve } from '@krayfaus/essentials';

async function withEssentials() {
    const [response, error] = await resolve(axios.get('/user?ID=12345'));
    if (error) { return handleError(error); }
    const data = String.value(response.data);
    console.log(data);
}

async function withoutEssentials() {
    try {
        const response = await axios.get('/user?ID=12345');
        const data = response.data?.toString() || '';
        console.log(data);
    } catch (error) {
        /* error handling */
    }
}

⚠️ Disclaimer

The code provided here is opinionated, you may like to write code differently and that's okay.

Feel free to take stuff you find useful from here.

Good luck on your projects!

📝 License

This repository is available under the MIT license.