Werkstatt🛠 Useful functions to encapsulate common scenarios.
As described here , is good to encapsulate conditionals to make our code more readable, reusable and avoid ambiguities. Also to avoid potential bugs due to some javascript features are error-prone:
let name = null ;
typeof name; Clearly, null is not an object. More of that 👉🏼here .
This is then, a package that encapsulates conditionals, but also other util functions.
⚠️ WARNING: This is also a proof of concept. Some of the functions' names may not make all the sense to you or also may be many breaking changes.
📦 Installnpm
yarn
browser
< script src = " https://unpkg.com/werkstatt@1.16.0/dist/index.umd.min.js" > </ script>
< script>
const { isEmail, isNull, areEqual } = werkstatt;
console.log(isEmail('asdf')); // -> false
console.log(isNull(3)); // -> false
console.log(areEqual(6, 6, 6, 6)); // -> true
</ script> ✨ Features Number
isZero
Arguments
argument
type
description
returns
value
number
will be tested if it is 0 or not
boolean
const { isZero } = require ( 'werkstatt' ) ;
isZero ( 7 ) ;
isZero ( 0 ) ;
add
Arguments
argument
type
description
returns
values
number
either an array of numbers or n args
number
const { add } = require ( 'werkstatt' ) ;
add ( 3 , 6 , 11 ) ;
const numbers = [ 1 , 2 , 3 ] ;
add ( ... numbers) ;
subtract
Arguments
argument
type
description
returns
values
number
numbers that will be subtrtacted
number
const { subtract } = require ( 'werkstatt' ) ;
subtract ( 6 , 3 ) ;
NOTE: currently it only supports two numbers as paremeters.
divide
Arguments
argument
type
description
returns
dividend
number
the dividend of the operation
number
divider
number
the divider of the operation
number
const { divide } = require ( 'werkstatt' ) ;
divide ( 100 , 2 ) ;
divide ( 10 , 5 ) ;
isNumber
Arguments
argument
type
description
returns
value
any
will be tested if it is number or not
boolean
const { isNumber } = require ( 'werkstatt' ) ;
isNumber ( 54 ) ;
isNumber ( { "hola" : "adios" } ) ;
isNumber ( [ ] ) ;
isNumber ( "" ) ;
isNumber ( 3 ) ;
isNumber ( true ) ;
NOTE: this is an implementation of is-number package.
isOdd
Arguments
argument
type
description
returns
value
number
will be tested if it is odd number or not
boolean
const { isOdd } = require ( 'werkstatt' ) ;
isOdd ( 7 ) ;
isOdd ( 4 ) ;
NOTE: this is an implementation of is-odd package.
isEven
Arguments
argument
type
description
returns
value
number
will be tested if it is even number or not
boolean
const { isEven } = require ( 'werkstatt' ) ;
isEven ( 7 ) ;
isEven ( 4 ) ;
NOTE: this is an implementation of is-even package.
isNegative
Arguments
argument
type
description
returns
value
number
will be tested if it is negative number or not
boolean
const { isNegative } = require ( 'werkstatt' ) ;
isNegative ( - 54 ) ;
isNegative ( 4 ) ;
isGreaterThan
Arguments
argument
type
description
returns
firstArgument
number
first value to be evaluated
boolean
secondArgument
number
second value to be evaluated
boolean
const { isGreaterThan } = require ( 'werkstatt' ) ;
isGreaterThan ( 100 , 50 ) ;
isGreaterThan ( 1 , 50 ) ;
Float
roundUp
Arguments
argument
type
number
float
const { roundUp } = require ( 'werkstatt' ) ;
roundUp ( 3.2 )
roundDown
Arguments
argument
type
number
float
const { roundDown } = require ( 'werkstatt' ) ;
roundDown ( 3.8 )
isFloat
Arguments
argument
type
description
returns
value
number, float
will be tested if is or not float
boolean
const { isFloat } = require ( 'werkstatt' ) ;
isFloat ( 6 ) ;
isFloat ( 6.5 ) ;
String
capitalizeFirstLetter
Arguments
argument
type
description
returns
value
string
string to capitalize first letter
string
const { capitalizeFirstLetter } = require ( 'werkstatt' ) ;
capitalizeFirstLetter ( 'hola' ) ;
capitalizeFirstLetter ( 'adios' ) ;
NOTE: this is an implementation of a Flavio's function
isEmail
Arguments
argument
type
description
returns
value
string
will be tested if it satisfies an email format
boolean
const { isEmail } = require ( 'werkstatt' ) ;
isEmail ( "a@a.c" ) ;
isEmail ( "a@a.co" ) ;
Best regex found out there .
isString
Arguments
argument
type
description
returns
value
any
whether or not the value is a string
boolean
const { isString } = require ( 'werkstatt' ) ;
isString ( "Hola" ) ;
isString ( [ 3 ] ) ;
Boolean
isTruthy
Whenever JavaScript expects a boolean value (e.g. for the condition of an if statement), any value can be used. It will be interpreted as either true or false. The following values are interpreted as false:
undefined, null
Boolean : false
Number : -0, NaN
String : ''
Speaking JavaScript by Alex Rauschmayer
That means that those values tend to to be false. So if you pass as parameter to isTruthy function any of those values, it will return false. All other values are considered true.
const { isTruthy } = require ( 'werkstatt' ) ;
isTruthy ( 3 ) ) ;
isTruthy ( { } ) ;
isTruthy ( undefined ) ;
isTruthy ( null ) ;
isTruthy ( false ) ;
isTruthy ( Number ( 'hola' ) ) ;
isTruthy ( 0 ) ;
isTruthy ( - 0 ) ;
isTruthy ( '' ) ;
isFalsy
Exactly the opposite of isTruthy.
const { isFalsy } = require ( 'werkstatt' ) ;
isFalsy ( 3 ) ) ;
isFalsy ( null ) ;
Array
orderAsc
Arguments
argument
type
description
returns
value
array
will order the list in ascending mode
array (ordened)
const { orderAsc } = require ( 'werkstatt' ) ;
orderAsc ( [ 8 , 10 , 6 ] ) ;
NOTE: this is an implementation of quicksort algorithm
lengthOf
Arguments
argument
type
description
returns
value
array, string, json
length of the passed argument
number
const { lengthOf } = require ( 'werkstatt' ) ;
lengthOf ( [ 8 , 10 , 6 ] ) ;
isLengthOf
Arguments
argument
type
description
returns
value
array, string, json
test if the first argument has the desired length (that specified in the second argument)
boolean
const { isLengthOf } = require ( 'werkstatt' ) ;
isLengthOf ( [ 8 , 10 , 6 ] , 3 ) ;
isLengthOf ( "hola" , 0 ) ;
isLengthOf ( { name : "Jorge" , lasName : "Guerra" } , 2 )
insertAt
Arguments
argument
type
description
array
any
where the element will be inserted.
index
any
at which the element will be inserted.
elementToInsert
any
element to insert in the array.
const { insertAt } = require ( 'werkstatt' ) ;
insertAt ( [ 1 , 2 , 3 ] , 1 , 4 ) ;
isArrayOfNumbers
Arguments
argument
type
description
returns
values
number
array to test
boolean
const { isArrayOfNumbers } = require ( 'werkstatt' ) ;
isArrayOfNumbers ( [ 3 , 6 , 11 , 'hola' ] ) ;
isArrayOfNumbers ( [ 1 , 2 , 3 ] ) ;
toArray
Arguments
argument
type
description
returns
arrayLikeObject/NodeList
any
object to convert
array
const { toArray } = require ( 'werkstatt' ) ;
function testToArray ( ) {
console. log ( arguments) ;
return toArray ( arguments) ;
}
testToArray ( 1 , 2 , 3 ) ;
isArray
Arguments
argument
type
description
returns
value
any
whether or not the value is an array
boolean
const { isArray } = require ( 'werkstatt' ) ;
isArray ( "Hola" ) ;
isArray ( [ 3 ] ) ;
Other
typeOf
Arguments
argument
type
description
returns
value
any
will get the type of a passed value
string
const { typeOf } = require ( 'werkstatt' ) ;
typeOf ( 6.5 ) ;
typeOf ( [ ] ) ;
typeOf ( { } ) ;
typeOf ( null ) ;
typeOf ( undefined ) ;
typeOf ( 'undefined' ) ;
typeOf ( true ) ;
typeOf ( ( ) => { } ) ;
typeOf ( 6 ) ;
areEqual
Arguments
argument
type
description
returns
n amount
any
args to compare
boolean
const { areEqual } = require ( 'werkstatt' ) ;
areEqual ( 100 , 2 ) ;
var name;
areEqual ( typeOf ( name) , 'undefined' ) ;
const numbers = [ 4 , 3 , 5 , 7 , 3 , 9 ] ;
areEqual ( ... numbers) ;
const ages = [ 9 , 9 , 9 , 9 , 9 ] ;
areEqual ( ... ages) ;
NOTE: This function supports primitive values only because objects are not compared by value but by reference.
isUndefined
Arguments
argument
type
description
returns
value
any
will be tested if is undefined or not
boolean
const { isUndefined } = require ( 'werkstatt' ) ;
isUndefined ( ) ;
isUndefined ( "a@a.co" ) ;
isDefined
Arguments
argument
type
description
returns
value
any
will be tested if is or not defined
boolean
const { isDefined } = require ( 'werkstatt' ) ;
isDefined ( 100 ) ;
var name;
isDefined ( name) ;
var age = null ;
isDefined ( age) ;
isDefined ( { } ) ;
isEmpty
Arguments
const { isEmpty } = require ( 'werkstatt' ) ;
isEmpty ( { } ) ;
isEmpty ( { "hola" : "adios" } ) ;
isEmpty ( [ ] ) ;
isEmpty ( "" ) ;
isEmpty ( 3 ) ;
isEmpty ( true ) ;
Note: isEmpty currently supports array, object and string only.
has
Arguments
argument
type
description
returns
firstArgument
number
first value to be evaluated
boolean
secondArgument
number
second value to be evaluated
boolean
const { has } = require ( 'werkstatt' ) ;
has ( [ 3 , 5 ] , 3 ) ;
has ( [ "Hola" , "adios" ] , "true" ) ;
has ( "Jorge" , "e" ) ;
isNull
Arguments
argument
type
description
returns
value
any
value to be evaluated
boolean
const { isNull } = require ( 'werkstatt' ) ;
var name = null ;
isNull ( name) ;
isNull ( "Hola" ) ;
every
Arguments
argument
type
description
returns
args
array
arguments to match to a specific type
boolean
const { every } = require ( 'werkstatt' ) ;
every ( 'adios' === 'adios' , 'hola' === 'hola' ) . is. true
every ( 'adios' , 'hola' ) . is. string
every ( 1 , 2 ) . is. number
more coming soon ✨
🙌🏽 Contribute
Fork and clone the repo
Run npm install to install dependencies
Create a branch for your PR with git checkout -b your-branch-name
To keep master branch pointing to remote repository and make
pull requests from branches on your fork. To do this, run:
git remote add upstream https://github.com/sk8guerra/werkstatt.git
git fetch upstream
git branch --set-upstream-to= upstream/master master
Make your pull request 🥳