Giter Club home page Giter Club logo

jstrivia's Introduction

jstrivia

delayInSeconds(seconds)

await delayInSeconds(1); // wait 1 second

isBlank(value)

isBlank(); // true
isBlank(null); // true
isBlank(""); // true
isBlank(1 / 0); // true

isFunction(value)

isFunction("Tony"); // false
isFunction(function () {}); // true

isNumber(value)

isNumber(); // false
isNumber(null); // false
isNumber(""); // false
isNumber("1"); // false
isNumber(Infinity); // false
isNumber(NaN); // false
isNumber(1); // true
isNumber(1.1); // true

isPresent(value)

Exact opposite of isBlank.

length(value)

length(null); // undefined
length("James"); // 5
length([1, 2, 3]); // 3

merge(...objects)

merge([1, 2, 3], [4, 5, [6, 7]]); // [1, 2, 3, 4, 5, 6, 7]
merge([1, 2], null, undefined); // [1, 2]

parentFolder(path)

parentFolder("/home/tony"); // "/home"
parentFolder("C:\\Users\\tony"); // "C:\\Users"

parseFloat(value, defaultValue)

parseFloat(); // null
parseFloat(null); // null
parseFloat(null, 2); // 2
parseFloat(""); // null
parseFloat("1.1"); // 1.1
parseFloat("a", 2); // 2

parseInteger(value, defaultValue)

parseInteger(); // null
parseInteger(null); // null
parseInteger(null, 2); // 2
parseInteger(""); // null
parseInteger("1"); // 1
parseInteger("1.1"); // 1
parseInteger("a", 2); // 2

presence(value, defaultValue)

presence(0, 1); // 0
presence(null, 1); // 1

retry(fn, options)

// given a risky function
const riskyFunction = () => { ... };

// we can produce a new function which automatically
// retries the risky function on failure

// retry 5 times with 1, 2, 4, 8, 16 seconds delay
const riskyWithRetry = retry(riskyFunction);

// retry 3 times with 1 second delay
// (the leading 0 is required, and can be used for initial delay)
const riskyWithRetry = retry(
  riskyFunction, {
    backoffs: [0, 1, 1, 1]
  }
);

// retry only on a specific error
const riskyWithRetry = retry(
  riskyFunction, {
    canRetry: (error, attempt) => error instanceof LimitExceededException
  }
);

squish(text, options)

squish("  hello  world  "); // "hello world"
squish("  hello \n world  "); // "hello\nworld"
squish("  hello \n world  ", { onOneLine: true }); // "hello world"

validateEmail(email)

validateEmail("test@example"); // ["invalid email"]
validateEmail("[email protected]"); // null

validateLength(value, min, max)

validateLength("test", 2, 4); // null
validateLength("test", 2, 3); // ["must be at most 3 characters"]

validatePresence(value)

validatePresence(); // ["must be present"]
validatePresence(null); // ["must be present"]
validatePresence("Jones"); // null

validateRegex(value, regex)

validateRegex("test", /^[a-z]+$/); // null
validateRegex("test123", /^[a-z]+$/); // ["is invalid"]

jstrivia's People

Contributors

dimakura avatar

Stargazers

 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.