jsutils2 v2.0.0
Any examples shown don't use require or any of that. The examples are shown as if they are imported using a <script> tag.
Documentation
Any
Exists
exists :: any => boolean
Checks if a value exists. (if it isn't undefined or null)
exists(5);
=> true
exists([2, 3][4]);
=> falseIdentity
identity :: any => any
Returns the passed parameter. Can be used to evaluate a boolean value like i < 3.
identity(5);
=> 5
identity(3 < 4);
=> trueprint :: (...any) => void
Prints any number of values to the console. Each value is on a separate line.
Note: Will not work properly with the Firefox dev console, and probably some other devices. This works best in some text editors such as Atom.io.
print(2, 3, 4);
=> 2
=> 3
=> 4Array
Apply
apply :: (array, function) => any
Applies a function that accepts spread arguments to an array.
apply([2, 3, 4, 5], Math.min); //Applies Math.min, which usually takes spread arguments, to the array passed in
=> 2Choice
choice :: array | string => any
Selects a random value from an array. This can work on strings as well, as they are indexable.
repeat(10, () => print(choice([1, 2, 3, 4, 5, 6])));
=> 1
=> 6
=> 4
=> 2
=> 2
=> 3
=> 5
=> 1
=> 5
=> 1Concat
concat :: (any, any) => array
Joins any 2 items into a single array. Can join 2 arrays together as well.
concat(2, 3);
=> [2, 3]
concat([5, 3], [4, 2]);
=> [5, 3, 4, 2]ConcatAll
concatAll :: array => array
Joins all items of an array into a single array. This effectively flattens the top layer of an array.
concatAll([2, 3, [4, 5], [6, 7]]);
=> [2, 3, 4, 5, 6, 7]
concatAll([3, [4, [5, 6]], [7, 8]]);
=> [3, 4, [5, 6], 7, 8]Count
count :: (array, function) => number
Counts the number of elements in an array that meet a condition.
count([2, 3, 4, 5], e => e < 4);DropWhile
dropWhile :: (array, function) => array
Drops items from an array that meet a condition until it finds one that doesn't meet a condition.
dropWhile([5, 3, 1, 6, 8, 7, 9], isOdd);
=> [6, 8, 7, 9]Flatten
flatten :: array => array
Flattens an array down to a one-dimensional array.
flatten([[[2, 3], 4, 5], [6, 7], 8]);
=> [2, 3, 4, 5, 6, 7, 8]Head
head :: array => any
Gets the first value of an array.
head([2, 3, 4]);
=> 2Insert
insert :: (array, index: number, item: any) => array
Inserts an item at a specific index of an array.
insert([2, 3, 4, 5, 6], 1, 7);
=> [2, 7, 3, 4, 5, 6]IsArray
isArray :: any => bool
Returns if a given value is an array.
isArray(5);
=> false
isArray([2, 3]);
=> trueMax
max :: array<number> => number
Returns the largest value of an array.
max([5, 2, 9, 10, 6, 3]);
=> 10Min
min :: array<number> => number
Returns the smallest value of an array.
min([5, 2, 9, 10, 6, 3]);
=> 2None
none :: (array, function) => boolean
Returns if none of the values of an array meet a condition.
none([2, 3, 4, 5], e => e < 4);
=> false
none([2, 3, 4, 5], e => e < 1);
=> trueProduct
product :: array<number> => number
Gets the product of the values of the array.
product([5, 7, 3]);
=> 105Quicksort
quicksort :: array<number> => array<number>
Sorts an array using the Quicksort algorithm.
quicksort([5, 2, 9, 8, 1, 7, 3]);
=> [1, 2, 3, 5, 7, 8, 9]Range
range :: (low: number, high: number?, step: number? = 1) => array<number>
Creates a range of numbers between 2 values, with a given step. Based on Python's built-in range function.
range(1, 11);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(2, 13, 3);
=> [2, 5, 8, 11]
range(5, -5, -1);
=> [5, 4, 3, 2, 1, 0, -1, -2, -3, -4]
range(5);
=> [0, 1, 2, 3, 4]Reject
reject :: (array, function) => array
Returns a version of an array without any items that meet a condition.
reject([2, 3, 4, 5, 7, 8, 9, 11], isEven);
=> [3, 5, 7, 9, 11]RemoveEachItem
removeEachItem :: (array, item: any) => array
Removes every instance of a value from an array.
removeEachItem([2, 3, 4, 3, 5, 6], 3);
=> [2, 4, 5, 6]RemoveIndex
removeIndex :: (array, index: number) => array
Removes a value at an index from an array.
removeIndex([2, 3, 4, 5, 6], 2);
=> [2, 3, 5, 6]RemoveItem
removeItem :: (array, item: any) => array
Removes a specific item from an array. (Only 1 instance of the item)
removeItem([2, 3, 4, 5, 6], 3);
=> [2, 4, 5, 6]ReplaceEachItem
replaceEachItem :: (array, item: any, item2: any) => array
Replaces each instance of a value in an array with a different value.
replaceEachItem([2, 3, 4, 3, 5, 6], 3, 7);
=> [2, 7, 4, 7, 5, 6]Reverse
reverse :: array => array
Reverses an array. Operates as a pure function, to avoid modifying the original array.
reverse([2, 3, 4]);
=> [4, 3, 2]Partition
partition :: (array, function) => array<array>
Splits an array into 2 arrays, where one contains items that meet a condition, and the second contains items that don't.
partition([2, 3, 4, 5, 7, 8, 9, 11], isEven);
=> [[2, 4, 8], [3, 5, 7, 9, 11]]PartitionWith
partitionWith :: (array, ...function) => array<array>
Returns an array of arrays, where each array successively contains items that meet a condition.
partitionWith([2, 3, 4, 5, 7, 8, 9, 11], isEven, e => isDivisible(e, 3));
=> [[2, 4, 8], [3, 9], [5, 7, 11]]Shuffle
shuffle :: array => array
Shuffles the values in an array randomly.
repeat(10, () => print(shuffle([1, 2, 3, 4, 5, 6])));
=> [3, 1, 2, 6, 5, 4]
=> [5, 2, 6, 4, 3, 1]
=> [5, 2, 3, 6, 4, 1]
=> [2, 4, 3, 1, 5, 6]
=> [3, 2, 1, 5, 4, 6]
=> [1, 6, 4, 3, 5, 2]
=> [2, 5, 1, 6, 3, 4]
=> [5, 6, 1, 3, 4, 2]
=> [5, 3, 1, 6, 2, 4]
=> [2, 3, 6, 1, 5, 4]Sum
sum :: array<Addable> => Addable
Adds together each item of an array. The Addable notation applies to anything that works with the + operator, which means numbers and strings.
sum([2, 3, 4, 5]);
=> 14
sum(["str", "ing"]);
=> "string"Tail
tail :: array => array
Returns everything except for the first value of an array. If the array has one item, it returns an empty array.
head([2, 3, 4]);
=> [3, 4]TakeWhile
takeWhile :: (array, function) => array
Takes items from an array while they meet a condition.
takeWhile([2, 4, 6, 8, 1, 7, 9, 8], isEven);
=> [2, 4, 6, 8]ToArray
toArray :: any => array
Converts a given value to an array. If the value is already an array, it remains as it is.
toArray(5);
=> [5]
toArray([5]);
=> [5]Functional
Compose
compose :: (...function) => any => any
Returns a function that applies any number of functions in natural order. Works very well with currying and partial application.
compose(e => e / 2, square)(3);
=> 4.5Partial
partial :: (function, ...any) => (...any) => any
Returns a function that applies any number of arguments to a function, and which accepts any number of arguments to complete the function.
partial(Math.max, 2, 3, 4)(5, 1);
=> 5ReverseArgs
reverseArgs :: function => (...any) => any
A variant of spreadArgs that operates in reverse order.
reverseArgs(concatAll)([2, 3], [4, 5], [6, 7]);
=> [6, 7, 4, 5, 2, 3]Sequence
sequence :: (...function) => any => any
Returns a function that applies any number of functions in reverse order. Works very well with currying and partial application.
sequence(e => e / 2, square)(3);
=> 2.25SpreadArgs
spreadArgs :: function => (...any) => any
Converts a function that takes an array to one that takes a spread.
spreadArgs(sum)(2, 3, 4);
=> 9Math
Accumulator Functions
:: (number, number) => number
These functions accept 2 operators, and are designed to be used with reduce and similar functions.
addsubtractmultiplydividemodpow(works likeMath.pow)bitor(bitwise operators)bitandbitxorleftshift(bitshifts)rightshift
Cbrt
cbrt :: number => number
Gets the cube root of a number. Alias for Math.cbrt.
cbrt(64);
=> 4Cosine
cosine :: number => number
Version of Math.cos which returns degrees.
cosine(60);
=> 0.5Cube
cube :: number => number
Cubes a number.
cube(5);
=> 125Factorial
number :: number => number
Gets the factorial of a number.
factorial(10);
=> 3628800Floor
floor :: (number, number? = 0) => number
Rounds a given number down to a specific number of decimal places.
floor(20.56, 1);
=> 20.5IsBetween
isBetween :: (number, number, number) => boolean
Returns if a number is between 2 other numbers.
isBetween(5, 4, 6);
=> true
isBetween(3, 4, 6);
=> falseIsDivisible
isDivisible :: (number, number) => boolean
Checks if 2 numbers are divisible by each other.
isDivisible(9, 3);
=> true
isDivisible(5, 4);
=> falseIsEven
isEven :: number => number
Checks if a number is an even number.
isEven(6);
=> true
isEven(5);
=> falseIsFibonacci
isFibonacci :: number => number
Checks if a number is a Fibonacci number.
isFibonacci(55);
=> true
isFibonacci(57);
=> falseIsOdd
isOdd :: number => number
Checks if a number is an odd number.
isOdd(5);
=> true
isOdd(6);
=> falseIsPrime
isPrime :: number => number
Checks if a number is a prime number.
isPrime(59);
=> true
isPrime(63);
=> falseIsWhole
isWhole :: number => boolean
Checks if a number is a whole number.
isWhole(7);
=> true
isWhole(6.6);
=> falseNRoot
nRoot :: (number, number) => number
Gets the nth root of a number.
nRoot(81, 3);
=> 4PI
PI :: number
An alias for Math.PI.
RandInt
randInt :: (low: number, high: number) => number
Generates a random number between 2 other numbers.
repeat(10, () => print(randInt(1, 6)));
=> 5
=> 1
=> 5
=> 1
=> 3
=> 4
=> 2
=> 2
=> 3
=> 3Random
random :: () => number
Alias for Math.random.
Round
round :: (number, number? = 0) => number
Rounds a given number to a specific number of decimal places.
round(20.53, 1);
=> 20.5Sine
sine :: number => number
Version of Math.sin which returns degrees.
sine(60);
=> 0.8660254038...Sqrt
sqrt :: number => number
Gets the square root of a number. Alias for Math.sqrt.
sqrt(16);
=> 4Square
square :: number => number
Squares a number.
square(5);
=> 25Tangent
tangent :: number => number
Version of Math.tan which returns degrees.
tangent(60);
=> 1.73205080757...Triangular
triangular :: number => number
Converts a number to a corresponding triangular number.
triangular(10);
=> 55Object
HasKey
hasKey :: (object, any) => boolean
Checks if an object has a key.
hasKey({2: 3, 3: 4}, 3);
=> true
hasKey({2: 3, 3: 4}, 1);
=> falsePluck
pluck :: (object, any) => any
Extracts a value from an object. Good for map and related functions.
pluck({1: 2, 2: 3}, 2);
=> 3String
StrShuffle
strShuffle :: string => string
Shuffles the characters in a string.
repeat(10, () => print(shuffle("string")));
=> "snirtg"
=> "gnirst"
=> "tingsr"
=> "gnrits"
=> "igtnrs"
=> "irtgns"
=> "rgtins"
=> "stgrin"
=> "nsitgr"
=> "trnsgi"StrInsert
strInsert :: (string, index: number, item: string) => string
Inserts a string at a specific index of a string.
strInsert("string", 2, "ar")
=> "starring"StrRemoveEachItem
strRemoveEachItem :: (string, item: any) => string
Removes every instance of a value from a string.
strRemoveEachItem("string is a string", "string");
=> " is a "StrRemoveIndex
strRemoveIndex :: (string, index: number) => string
Removes a value at an index from a string.
strRemoveIndex("string", 2);
=> "sting"StrRemoveItem
strRemoveItem :: (string, item: any) => string
Removes a specific item from a string. (Only 1 instance of the item)
strRemoveItem([2, 3, 4, 5, 6], 3);
=> [2, 4, 5, 6]StrReverse
strReverse :: string => string
Reverses a string without modifying the original string.
strReverse("string");
=> "gnirts"Structures
Repeat
repeat :: (number, function)
Executes a given function a certain number of times. It can accept arguments in the function.
repeat(5, () => print("Hello world!"));
=> "Hello world!"
=> "Hello world!"
=> "Hello world!"
=> "Hello world!"
=> "Hello world!"Until
until :: (cond: function, function)
Executes a function until the condition is true.
let i = 1;
until(() => i > 10, () => {
print(i);
i++;
});
=> 1
=> 2
=> 3
=> 4
=> 5
=> 6
=> 7
=> 8
=> 9
=> 10