Skip to content

wilsonshakespeare/utilzed

Repository files navigation

JS Library (utilzed)

Despite lodash and many functionalities out there, there are still some utilities didn't achive what I intend to have.

Install:

npm install utilzed

Upgrade:

# set specific version
npm install [email protected]

Usage

This library has both flow and ts declarations

For compiled library:

import utilzed from 'utilzed';

// for promise module and functions
const { waitForTrue, sleep } = utilzed.promise;

// for timeconvert module
const { TimeConverter, Format } = utilzed.timeconvert;

// If index module getting too big for the size you can import specific module
import timeconvert from 'utilzed/dist/timeconvert';

Important for pure ES6 (require)

// If uses require function you will need to use .default
// For import in typescript or flow, this is not required

const utilzed = require('utilzed').default;
const timeconvert = require('utilzed/dist/timeconvert').default;

Library Functions:

This library has jsdoc generated comment.

Promise Related:

waitForTrue

In case you might be polling for a condition before proceed:

import utilzed from 'utilzed'
// import waitForTrue from 'utilzed/dist/promise/waitForTrue'

const checkCondition = async () => (await somePollCheckFunction());

// this will waitForTrue checkCondition to be true
// checkCondition will be called every 100ms
const success = await utilzed.waitForTrue(100, checkCondition, 1000);

if (success) {
  // Meaning checkCondition function returns true before 1000 ms
  return;
}

// meaning after 1000ms the checkCondition returns false still
// handle unsuccessful poll for true

sleep

await sleep(100) will await for 100ms before proceed to next function

import utilzed from 'utilzed'

await utilzed.sleep(100);

// will proceed after sleep for 100 ms

Validation Related:

isEmptyOrZero

This will return true if object is empty, string is empty, number is 0 or array is empty

import utilzed from 'utilzed'

// if lodash isEmpty the following will return true
utilzed.isEmptyOrZero(100); // returns false
utilzed.isEmptyOrZero(0); // returns true

versionCheck

This module checks if the versioning meets the condition

import utilzed from 'utilzed'

const { OPERATORS, isVersionValid } = utilzed.versionCheck

// this will return true
isVersionValid('v2.1.1', OPERATORS.MORE_THAN_EQUAL, 'v1.1.1')

// this will return false
isVersionValid('v1.1.1', OPERATORS.LESS_THAN, 'v1.1.1')

Others

TimeConverter

Convert time value without mathematical headache

import utilzed from 'utilzed'

// Can use destructured assignment
const { TimeConverter, Format } = utilzed.timeconvert;

// returns 123000 (target in ms) from 123 (seconds)
TimeConverter.target(Format.MILISECONDS).from(Format.SECONDS, 123);

TODO: host it on github pages

This Library is Generated From create-tslibs

Can refer to create-tslibs

To run development following will be recommended:

Use VS Code as IDE:

The library template is already setup with vscode settings for auto format.

Ensure the following plugins are enabled:

Optional but Recommended: