Giter VIP home page Giter VIP logo

lifesaver's Introduction

๐Ÿ›Ÿ Lifesaver

Ahoy! โš“๏ธ

Deterministic lifesavers for common JavaScript errors, mistakes, and undefined behavior. It also includes helper functions for missing features prevalent in other languages that usually would help avoid these issues.

These helper functions are based on experiences I've had wit' the many quirks o' JavaScript. This project be meant to create helper functions that help ye avoid these quirks that cause errors 'n unexpected behavior when you be lost at sea.

Usage

Empty value check

Check if a value is completely and totally empty. Ensures it is actually empty and not 0 or false.

let value = 0;
empty(value); // false

value = false;
empty(value); // false

value = {};
empty(value); // true

value = undefined
empty(value); // true

Error handling

const [result, error] = TRY(function);
if (error) {
    // handle error
    throw new Error();
}

// use result
console.log(result);

Sleep

Implements JavaScript version of sleep.

await sleep(5); // waits for 5 seconds.

Local Storage

Local storage helper functions that keep the type of the value intact.

set('key', {test: true});
const object = get('key'); // {test: true}
object.test; // true (not "true")

set('key', 5);
get('key'); // 5 (not "5")

set('key', true);
get('key'); // true (not "true")

set('key', "string");
get('key'); // "string"

set('key'); // clear the value of the key
get('key') // null

forget('key'); // delete key-value pair

Strings

Strip HTML tags from a string.

stripTags('<p>foo</p>'); // returns foo

Pluralize word based on an integer value. This will account for irregular plural nouns in English.

If the integer value given is not 1, then the plural version of the word will be returned. Otherwise, the word given will be returned.

status.innerText = count + ' search ' + pluralize('result', count);

Title case a string.

titleCase('change case of sentence to Title Case');

Trim prefixes and suffixes.

trimPrefix('foobar', 'foo')
trimSuffix('foobar', 'bar')

Arrays & Objects

end(value); // Returns last item without modifying the underlying array.

pluck(value, 'key'); // Returns an array of values with provided 'key' from `value`.

Random Numbers

Generate a random number with min and max.

randomInt(0, 5);
randomInt(10, 20);

Temp Elements

Sometimes you need to create elements temporarily. This function creates an element (by default a div), hides it using CSS, and mounts it to the HTML body. This element is then passed to the callback function you provide. When your function returns, the element is removed.

// Creates element, mounts to the body hidden.
// Passes it to callback.
tempElement(element => {
    element.innerText = 'Hello, lifesaver!';
});
// Element is then removed automatically.

// By default, temp element is a <div>, but you can optionally pass a tagname.
tempElement(span => {
    span.innerText = 'Hello, lifesaver!';
}, 'span');

lifesaver's People

Contributors

electrikmilk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.