0.1.16 • Published 6 months ago

jstrivia v0.1.16

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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("test@example.com"); // 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"]
0.1.10

7 months ago

0.1.11

7 months ago

0.1.12

7 months ago

0.1.13

7 months ago

0.1.14

7 months ago

0.1.15

7 months ago

0.1.16

6 months ago

0.1.8

7 months ago

0.1.7

7 months ago

0.1.9

7 months ago

0.1.4

8 months ago

0.1.6

7 months ago

0.1.5

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago