essents

Just common utils

Usage no npm install needed!

<script type="module">
  import essents from 'https://cdn.skypack.dev/essents';
</script>

README

Essents

Simple library with useful utils

Menu

Examples

Counter

Simple creation

import {Counter} from 'essents';

const counter = new Counter(3, () => {console.log('Hello')}); // Creates counter with target value 3, callback value and initial value 0

counter.increment() // currentStep: 1

counter.increment() // currentStep: 2

counter.increment() // currentStep: 3
// execute callback
// Will print "Hello"

All constructor props using

...

const counter = new Counter(3, (name: string) => console.log(`Hello ${name}`), 2, 'Bob'); // Creates counter with target value 3, callback value, initial value 2 and optional argument "Bob" which using at callback

counter.increment() // currentStep: 3
// execute callback
// Will print "Hello Bob"

Using setters

...
const counter = new Counter(3); // Creates counter with target value 3 and initial value 0

counter.onFire = (name: string, surename: string) => console.log(`Yet another hello ${name} ${surename}`); // Define callback

counter.args = ['John', 'Doe'] // Define args

console.log(counter.activated) // false

counter.increment() // currentStep: 1
counter.increment() // currentStep: 2
counter.increment() // currentStep: 3
// Execute callback
// Will print "Yet another hello John Doe"
console.log(counter.activated) // true

Cycled Counter

Simple creation

import {CycledCounter} from 'essents';

const counter = new CycledCounter(3, () => {console.log('Hello')}); // Creates counter with target value 3, callback value and initial value 0

counter.increment() // currentStep: 1
counter.increment() // currentStep: 2
counter.increment() // currentStep: 0
// execute callback
// Will print "Hello"

counter.increment() // currentStep: 1
counter.increment() // currentStep: 2
counter.increment() // currentStep: 0
// execute callback
// Will print "Hello"

All constructor props using

...

const counter = new CycledCounter(3, (name: string) => console.log(`Hello ${name}`), 2, 'Bob'); // Creates counter with target value 3, callback value, initial value 2 and optional argument "Bob" which using at callback

counter.increment() // currentStep: 0
// execute callback
// Will print "Hello Bob"

counter.increment() // currentStep: 1
counter.increment() // currentStep: 2
counter.increment() // currentStep: 0
// execute callback
// Will print "Hello Bob"

Using setters

...
const counter = new CycledCounter(3); // Creates counter with target value 3 and initial value 0

counter.onFire = (name: string, surename: string) => console.log(`Yet another hello ${name} ${surename}`); // Define callback

counter.args = ['John', 'Doe'] // Define args

counter.increment() // currentStep: 1
counter.increment() // currentStep: 2
counter.increment() // currentStep: 0
// Execute callback
// Will print "Yet another hello John Doe"

Repeater

import {Repeater} from 'essents';

const repeater = new Repeater(() => console.log('Hello'), 10); // Creates repeater with callback, 10ms interval and optional argument "Bob" which using at callback

console.log(repeater.alive) // false
console.log(repeater.period) // 10ms

repeater.start();
console.log(repeater.alive) // true

repeater.kill();
console.log(repeater.alive) // false

Timer

import {Timer} from 'essents';

const timer = new Timer((name: string) => console.log(`Hello ${name}`), 10, true, 'Bob'); // Creates timer with callback, 10ms delay, autokill flag, and optional argument "Bob"

console.log(timer.alive) // false
console.log(timer.delay) // 10ms

timer.start();
console.log(timer.alive) // true

// wait 10 ms

// will print "Hello Bob"

console.log(timer.alive) // false | Because timer automaticaly delete timeout after triggering

Random

import {Random} from 'essents';

const rd = new Random();

console.log(rd.next()) // number [0 ... 999_999]

console.log(rd.next(10)) // number [0 ... 9]

UUID Generator

import {UUIDGenerator} from 'essents';

const randomUuidGen = new UUIDGenerator();
console.log(randomUuidGen.generate()) // fully random uuid v4

const uuidGen = new UUIDGenerator(5, 'hrodvitnir');
console.log(uuidGen.generate()) // generates uuid v5 with hash of login

:warning: Generator use timestamp + Math.random + static data which you specify at constructor

Setup

To use this project, install it locally using npm:

$npm i essents

Technologies

Project is created with:

  • typescript 3.9.7
  • chai 4.2.0
  • mocha 8.1.0
  • ts-mocha 7.0.0