@source4society/scepter-utility-lib

5.0.0 • Public • Published

SCEPTER-utility-lib

scepter-logo

js-standard-style

Build Status

codecov

A library of useful javascript utility functions

Usage

Add this library to your project via npm or yarn with the command:

npm install -S @source4society/scepter-utility-lib

or yarn add @source4society/scepter-utility-lib

Then in your code, you can import the utility class:

import SCEPTERUtils from '@source4society/scepter-utility-lib';

or for commonJS

const SCEPTERUtils = require('@source4society/scepter-utility-lib')

Functions

isEmpty / isNotEmpty

use this to determine if a given variable is "empty". "Emptiness" is defined as an undefined value, null, NaN, {}, [], or ''. A value such as boolean false is not counted as empty, nor is a function or a class (instantiated or definition). isNotEmpty is simply the not of isEmpty

getRandomInt

Specify a min (inclusive) and max (exclusive) to get back a random number based on the platform (or injected) Math.random() function between min/max

valueOrDefault

If the value is empty, return default, otherwise return the value. Very useful for avoiding ternary's for better line coverage when setting default values

ssValueOrDefault

Same as above, but the second argument is a function or lambda to be executed if the first argument is empty. SS stands for "short-circuit".

ifTrueElseDefault

If the first argument evaluates to true, return the second argument, otherwise return the third. Very useful for avoiding branching logic but beware - you cannot short circuit the arguments like you can with a normal ternary

ssIfTrueElseDefault

Like above but value1 and value2 should be defined as functions or lambdas to provide short circuiting.

emptyAt / notEmptyAt

Like isEmpty and isNotEmpty but will traverse an objects properties to see if the property chain exists/has an empty value. The first argument is the object, the second argument is an array of subproperties to search in the order of nesting.

ucFirst

Useful utility to transform a word to title case (first letter becomes uppercase). Pass in a string and get back a string

validateApiResult

SCEPTER API results always return with a status: true property when successful, and status: false when something goes wrong. This will attempt to throw the error in the response if status is anything other than true. It will also parse from a string if the given result is in string form.

keyGenerator

Generates a random character string of length length, useful for creating API keys or jwt encoding keys.

getInOrDefault

This is useful for getting values from nester properties within objects, and returning a default value if the property does not exist or is empty. The first argument is the object to search, the second is an array of properties in nesting order, the third is the default value to return if the property does not exist or is empty

immutableToJS

Convert an immutable object to its JS form if the object is not empty, otherwise return undefined. Simply takes the immutableJS object as an argument

standardCallback

Use this to split the results of a standard callback into two separate function calls. The first argument is the error object, the second is the success (data) object. If the error object is not empty, then the error callback is called with the error data, otherwise the success callback is called with the success data

eitherOf

Use this function to disjoin (||) two values

conjunctionOf

Use this function to conjoin (&&) two values

ssEitherOf

Use this function to disjoin (||) two values, but the second argument should be a function that will be executed if the first is not true or undefined

ssConjunctionOf

Use this function to conjoin (&&) two values, but the second argument should be a function that will be executed if the first is not true or undefined

trueIfEqual

Function will return the result of value1 === value2

nullOp

Returns null value, useful if you want to return null in a short circuited function or if you want to render a null React component

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.0.00latest

Version History

VersionDownloads (Last 7 Days)Published
5.0.00
4.1.00
4.0.10
4.0.00
3.0.00
2.3.00
2.2.00
2.1.00
2.0.00
1.2.01
1.1.00
1.0.00
0.0.260
0.0.250
0.0.240
0.0.230
0.0.220
0.0.210
0.0.200
0.0.191
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.20
0.0.10

Package Sidebar

Install

npm i @source4society/scepter-utility-lib

Weekly Downloads

2

Version

5.0.0

License

AGPL-3.0

Unpacked Size

315 kB

Total Files

21

Last publish

Collaborators

  • danfredriksen