jazzy-utility

    1.3.1 • Public • Published

    Jazzy Utility

    Build Status Coverage Status Dependencies Version

    A small utility library for use with... Well anything really.

    Table of contents

    1. Installation
    2. Stash
    3. doAllAsync
    4. doAllCallbacks
    5. drill
    6. deleteArrayElement
    7. randomInt
    8. randomArrayElement
    9. randomArrayElements
    10. extractRandomArrayElements
    11. Workflow
    12. 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';
    

    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) => text id
    see(text id) => any value
    take(text id) => any value
    replace(text 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
    

    doAllAsync

    function doAllCallbacks(array array, function forEachFunction, function thenFunction) => void

    Usage:

    const urls = ['www.someurl.com/path', 'www.someurl.com/path', 'www.someurl.com/path'];
    const data = new Array(urls.length);
    
    await doAllAsync(urls, async (url, index) => {
      response = await fetch(url);
      if(!response.ok) throw new Error('Request Failed');
      const data[index] = response.text();
    });
    
    console.log(data)
    

    doAllCallbacks

    function doAllCallbacks(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');
    });
    

    drill

    function drill(array path, object object) => any result

    Usage:

    const path = ['some', 'path', 'into', 'some', 'object'];
    const obj = { some: { path: { into: { some: { object: 'success' } } } } };
    console.log(drill(path, obj)); // output: success
    

    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']
    

    randomInt

    function randomInt(int min, int max) => int result

    Usage:

    console.log(randomInt(0, 5)); // outputs an integer between 0 and 5 inclusive.
    

    randomArrayElement

    function randomArrayElement(array array) => any result

    Usage:

    const myArr = ['y', 'e', 'l', 'l', 'o'];
    console.log(randomArrayElement(myArr)); // outputs a random element from the input array.
    

    randomArrayElements

    function randomArrayElements(number multiplier, array array) => array result

    Usage:

    const myArr = [0, 1, 2, 3, 4];
    console.log(randomArrayElements(0.4, myArr)); // outputs an array with two random elements.
    console.log(myArr.length) // Outputs 5 as original array is not affected.
    

    extractRandomArrayElements

    function extractRandomArrayElements((number multiplier, array array) => array result

    Usage:

    const myArr = [0, 1, 2, 3, 4];
    console.log(extractRandomArrayElements(0.4, myArr)); // outputs an array with two random elements.
    console.log(myArr.length) // Outputs 3 as 2 elements have been extracted
    

    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']
    });
    

    Issues

    If you encounter any issues please report them on the Library's Github.

    Install

    npm i jazzy-utility

    DownloadsWeekly Downloads

    3

    Version

    1.3.1

    License

    MIT

    Unpacked Size

    75.3 kB

    Total Files

    40

    Last publish

    Collaborators

    • jazzbrown1