Package Exports
- jazzy-utility
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jazzy-utility) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Jazzy Utility
A small utility library for use with... Well anything really.
Table of contents
- Installation
- Workflow
- Stash
- forEachCallbacks
- doAll
- deleteArrayElement
- randomInt
- randomEl
- randomEls
- extractRandomEls
- Report Bug
Installation
Installing
npm install 'jazzy-utility'Importing
Cjs
const jazzy-utility = require('jazzy-utility');
const Stash = jazzy-utility.Stash;Es Module
import {Stash} from 'jazzy-utility';
jazzy-utility.Workflow
class Workflow()
A class that allows developers to build and dynamically update workflows. This enables developers to build dynamic flows and add steps at runtime making versatile and easily extendable code.
A work flow is made up of tasks which run sequentially; A task object contains an action which is a function, and optionally you can id for searching and an options object:
{
action: (data, control) => {
control.next(data);
},
options: {
skipError: true, // task will be skipped if an error is thrown
unblock: true, // will run the task at the end of the event queue good for spreading load when running cpu heavy workflows
},
id: 'some id'
}You can just pass the action function instead of the task object if you do not requires ids to search or additional options.
Methods:
run(data) => void
add(Object action) => int insertedIndex
insertAfter(function findFunction, Object action) => int insertedIndex
insertBefore(function findFunction, Object action) => int insertedIndex
findAndDelete(function findFunction) => int deletedIndex
Usage:
const myTask = (taskID) => (arr, control) => {
arr.push(taskID);
control.next(arr);
};
const myWorkflow = new Workflow([
{ action: myTask('b'), id: 'b' },
{ action: myTask('c'), id: 'c' }
]);
myWorkflow.add({
action: myTask('e'), id: 'e'
});
myWorkflow.insertBefore((el) => el.id === 'b', {
action: myTask('a'), id: 'a'
});
myWorkflow.insertAfter((el) => el.id === 'c', {
action: myTask('d'), id: 'd'
});
myWorkflow.add(myTask('f'));
myWorkflow.run([], (arr) => {
console.log(arr) // output: ['a', 'b', 'c', 'd', 'e', 'f']
});
jazzy-utility.Stash
A class that you can place data in and it returns an id as an integer. The data can then be retrieved with the integer. Particularly useful when interacting with external systems where a reference is required to relate a response to a query.
class Stash()
Methods:
put(any value) => int id
see(int id) => any value
take(int id) => any value
replace(int id, any value) => void
size() => int size
isEmpty() => boolean result
clear() => void
iterate(function forEachFunction(any item)) => void
Usage:
const myStash = new Stash();
const myId = myStash.put('My Message');
console.log(myStash.see(myId)); // output 'My Message'
console.log(myStash.size()); // output 1
myStash.take(myId);
console.log(myStash.isEmpty()); // output true
jazzy-utility.forEachCallbacks
function forEachCallbacks(array array, function forEachFunction, function thenFunction) => void
Usage:
const actions = ['SaveLogs', 'CheckErrors', 'cleanUpData'];
forEachCallbacks(actions, (action, index, next) => {
performAction(action, (result) => {
if (result) next();
else console.log('Failed at action ' + action + ' and aborted'); // Will not continue if next is not called
});
}, () => {
console.log('Job Complete');
});
jazzy-utility.doAll
function doAll(array array, function forEachFunction, function thenFunction) => void
Usage:
const displayDelayedMessage = (delay, message, cb) => {
setTimeout(() => {
console.log(message);
cb();
}, delay)
}
const messages = [
{delay: '200', text: 'Message 1'},
{delay: '150', text: 'Message 2'},
{delay: '100', text: 'Message 3'}
];
doAll(messages, (message, index, done) => {
displayDelayedMessage(message.delay, message.text, () => {
done();
});
}, () => {
console.log('Job Complete');
});
jazzy-utility.deleteArrayElement
function deleteArrayElement(array array, any value) => boolean result
Usage:
const myArr = ['y', 'e', 'l', 'l', 'o'];
deleteArrayElement(myArr, 'l');
console.log(myArr); // output: ['y', 'e', 'l', 'o']
jazzy-utility.randomInt
function randomInt(int min, int max) => int result
Usage:
console.log(randomInt(0, 5)); // outputs an integer between 0 and 5 inclusive.
jazzy-utility.randomEl
function randomEl(array array) => any result
Usage:
const myArr = ['y', 'e', 'l', 'l', 'o'];
console.log(randomEl(myArr)); // outputs a random element from the array.
jazzy-utility.randomEls
function randomEls(number multiplier, array array) => array result
Usage:
const myArr = [0, 1, 2, 3, 4];
console.log(randomEls(0.4, myArr)); // outputs an array with two random elements.
console.log(myArr.length) // Outputs 5 as original array is not affected.
jazzy-utility.extractRandomEls
function extractRandomEls((number multiplier, array array) => array result
Usage:
const myArr = [0, 1, 2, 3, 4];
console.log(randomEls(0.4, myArr)); // outputs an array with two random elements.
console.log(myArr.length) // Outputs 3 as 2 elements have been extracted
Issues
If you encounter any issues please report them on the Library's Github.