Library with helper methods.
npm install pure-utilities
Returns the average of the array elements.
average(100, 200)
<= 150
Returns a string created by joining array elements with comma and optional spaces.
comma([1, 2, 3], 2)
<= "1, 2, 3"
Returns an array with truthy values.
compact([0, 1, false, null, 2, "", 3])
<= [1,2,3]
compact([0, 1, null, 2, undefined, 3], false)
<= [0,1,2,3]
Returns an array containing elements that do not occur in other arrays.
difference([1, 2, 3, 4, 5, 100], [100, 2, 3, 10, 31])
<= [1,4,5]
Removes elements of the array.
drop([1, 2, 3], 2)
<= [3]
Returns an array containing repeated elements.
duplicates([1, 1, 2, 3, 5, 5])
<= [1,5]
Returns the eigth element of the array.
eigth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 8
Returns the fifth element of the array.
fifth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 5
Returns the first element of the array.
first([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 1
Returns the fourth element of the array.
fourth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 4
Returns the first element of the array.
head([1, 2, 3, 4, 5, 6])
<= 1
Returns a new unique identifier based on passed in identifiers.
identifier(["a"])
<= "b"
Returns an array containing elements common to all arrays that have been passed to the function.
intersection([1, 2, 3, 4, 5, 100], [100, 2, 3, 10, 31])
<= [2,3,100]
Returns the last element of the array.
last([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 10
Returns the median of the array elements.
median([6, 4, 2, 4, 4])
<= 4
Returns the ninth element of the array.
ninth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 9
Returns the element located at the passed position. When position is negative it searches from the end of the array.
nth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)
<= 3
Returns an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for. The predicate has one argument.
partition([{ user: "barney", active: false }, { user: "fred", active: true }, { user: "pebbles", active: false }], item => item.active)
<= [[{"user":"fred","active":true}],[{"user":"barney","active":false},{"user":"pebbles","active":false}]]
Returns a list of property values from an array of objects.
pluck([{ foo: "bar"}, { name: "baz"}], "name")
<= ["baz"]
Returns an array of numbers within the range.
range(1, 5)
<= [1,2,3,4,5]
Rotates the elements on the array. When second parameter is negative, shifting starts from the end of the array.
rotate([1, 2, 3], 2)
<= [3,1,2]
Returns random element of the array.
sample([[1, 2, 3]])
<= 1
Returns the second element of the array.
second([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 2
Returns the seventh element of the array.
seventh([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 7
Returns the sixth element of the array.
sixth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 6
Extracts a fragment of the array.
slice([1, 2, 3, 4,5, 6], 3, 5)
<= [4,5]
Returns the sum of the array elements.
sum([3, 10, 2])
<= 15
Returns an array of elements that are not part of intersection.
symdifference([1, 3, 4, 6, 7, 9], [3, 5, 6, 7, 8, 9])
<= [1,4,5,8]
Takes elements from the array based on the second parameter, starting from the beginning of the array.
take([1, 2, 3, 4, 5, 6], 2)
<= [1,2]
Returns the tenth element of the array.
tenth([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 10
Returns the third element of the array.
third([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
<= 3
Returns an array of unique values.
union([2], [1, 2])
<= [2,1]
Returns an array with unique elements.
unique([1, 1, 2, 10, 2, 33])
<= [1,2,10,33]
Adds arguments at the end of the collecion.
append("foo", "bar")
<= "foobar"
append([1, 2, 3], 4, 5, 6)
<= [1,2,3,4,5,6]
Flattens n-dimensional array or n-depth object to a single depth array or object.
flatten([1, [2], [ [3, [ 4, [5] ] ] ] ])
<= [1,2,3,4,5]
flatten({"titles":{"index":"Buxus - Plants, seedlings, producer"}})
<= {"titles.index":"Buxus - Plants, seedlings, producer"}
Returns the count of occurences of the string in the collection or string.
occurences(["foo", "foo", "bar"], "foo")
<= 2
occurences("foo foo bar", "foo")
<= 2
Adds arguments at the beginning of the collection.
prepend("foo", "bar")
<= "barfoo"
prepend([1, 2, 3], 4, 5, 6)
<= [4,5,6,1,2,3]
Reverses the elements of the collection.
reverse([1, 2, 3, 4])
<= [4,3,2,1]
reverse("foo")
<= "oof"
Returns the size of the collection.
size(1, 2, 3, 4, 5)
<= 5
size({ a: 1, b: 2 })
<= 2
Converts properties of the object, which contain in the name dots(.), to multi-depth object.
unflatten({"errors.404.title":"Page not found"})
<= {"errors":{"404":{"title":"Page not found"}}}
Returns the day of the month (from 1 to 31) of the passed date.
day(new Date(2018, 4, 28)
<= 28
Formats the date. First argument can be a Date object or a date string. By default date is parsed to the YYYY-MM-DD format.
format(new Date("2013/06/13"))
<= "13-06-2013"
format(new Date(2018, 4, 23), "MM")
<= "05"
format(new Date(2018, 4, 23), "DD/MM/YYYY")
<= "23/05/2018"
format(new Date("2013/06/09"))
<= "09-06-2013"
Returns the hours of the day (from 0 to 23) of the passed date.
hours(new Date(2018, 4, 28, 10))
<= 10
Returns a string in simplified extended ISO format.
isostring("1460303444338")
<= "2016-04-10T15:50:44.338Z"
Returns the minutes of the hour (from 0 to 59) of the passed date.
minutes(new Date(2018, 4, 28, 10, 11))
<= 11
Returns the month (from 0 to 11) of the passed date.
month(new Date(2018, 4, 28))
<= 4
Returns a string, that contains: the name of weekday, day of the month (from 1 to 31), name of the month, and year (four digits for dates between year 1000 and 9999). Localization can be "en-En" or "pl-Pl", in other case function throws error. By default localization equals "en-En".
prettydate(new Date(2043, 9, 25))
<= "Sunday, 25th of October 2043"
prettydate(new Date(2018, 2, 21)
<= "Wednesday, 21st of March 2018"
prettydate(new Date(2018, 2, 21)
<= "środa, 21 marca 2018"
Returns the seconds of the minute (from 0 to 59) of the passed date.
seconds(new Date(2018, 4, 28, 10, 11, 12))
<= 12
Returns a formatted date, based on the passed pattern.
timestamp(new Date(2018, 4, 28), "YYYY")
<= "2018"
timestamp(new Date(2018, 4, 28), "YYYY/MM")
<= "2018/05"
timestamp(new Date(2018, 4, 28), "YYYY/MM/DD")
<= "2018/05/28"
Returns the day of the week (from 0 to 6) of the specified date.
weekday(new Date(2017, 1, 11))
<= 6
Returns the year of the passed date.
year(new Date (2018, 4, 2))
<= 2018
Converts a value to JSON string. When the passed value is not a string, returns the value.
prettify({"foo":"bar"})
<= "{\n \"foo\": \"bar\"\n}"
Returns the absolute value of the number.
abs(-1)
<= 1
Returns the arccosine of the number, in radians.
acos(0.5)
<= 1.0471975511965979
Returns the hyperbolic arccosine of the number, in radians.
acosh(2)
<= 1.3169578969248166
Adds two parameters.
add(10, 5)
<= 15
Returns the arcsine of the number, in radians.
asin(1)
<= 1.5707963267948966
Returns the hyperbolic arcsine of the number.
asinh(1)
<= 0.881373587019543
Returns the hyperbolic arctangent of the number.
atan(1)
<= 0.7853981633974483
Returns the arctangent of the quotient of its arguments.
atan2(5, 5)
<= 0.7853981633974483
Returns the hyperbolic arctangent of the number.
atanh(0.5)
<= 0.5493061443340548
Returns the cubic root of the number.
cbrt(1)
<= 1
Returns the number, rounded upwards to the nearest integer.
ceil(100.2)
<= 101
Returns the highest possible value from range min to max of the number.
clamp(25, 20, 30)
<= 25
clamp(30, 20, 30)
<= 30
Returns the cosine of the number (number is in radians).
cos(1)
<= 0.5403023058681398
Returns the hyperbolic cosine of the number.
cosh(0)
<= 1
Returns the third power of the number.
cube(-4)
<= -64
Decrements the number.
decrement(1)
<= 0
Converts radians to degrees and rounds received result by default precision equals 2.
degrees(5)
<= 286.48
Calculates the distance between points a and b in space.
distance([1,2,3], [1,2,4])
<= 1
Divides two parameters.
divide(10, 5)
<= 2
Returns the value of E^number, where E is Euler's number.
exp(0)
<= 1
Returns factorial of the number.
factorial(5)
<= 120
Converts inches to feets. By default precision equals 2. When decimal flag is set to false, returns result with prim (′) symbol.
feet(1)
<= 0.08
Formats the number using fixed-point notation. By default digit equals 0.
fixed(25.32)
<= "25"
Parses the number to the float.
float("4.25")
<= 4.25
Returns the number, rounded downwards to the nearest integer.
floor(300.91)
<= 300
Converts feets to inches. By default precision equals 2. When decimal flag is set to false, returns result with bis (″) symbol.
inches(1)
<= 12
Increments the number.
increment(1)
<= 2
Parses the number to the integer.
int("4.25")
<= 4
Computes centroids of k clusters, which are the average values of those points.
kmeans(3, [[1,2],[2,3],[2,5],[1,6],[4,6],[3,5],[2,4],[4,3],[5,2],[6,9],[4,4],[3,3],[8,6],[7,5],[9,6],[9,7],[8,8],[7,9],[11,3],[11,2],[9,9],[7,8],[6,8],[12,2],[14,3],[15,1],[15,4],[14,2],[13,1],[16,4]])
<= {"centroids":[[11.75,2],[14.8,2.8],[5.095238095238095,5.619047619047619]],"iteration":2,"complete":true}
Returns the natural logarithm (base E) of the number.
log(2, 8)
<= 0.6931471805599453
Returns the number with the highest value.
max([2,5,1])
<= 5
Computes the mean of the values in array.
mean([1,2,3,4])
<= 2.5
Returns the number with the lowest value.
min([2,5,1])
<= 1
Divides two parameters and returns the remainder.
modulo(7, 2)
<= 1
Formats the number to currency format options is an object that accepts the following parameters: - digits: Number of digits after comma. By default equals 2. - separator: Separator separating the decimal part. By default equals ','. - symbol: The symbol of currency. By default equals 'zł'. - ending: Boolean flag, that specifies position of currency symbol. By default set to true. - space: Boolean flag, that specifies if space should exist before currency symbol. By default set to true. - hyphen: The symbol between groups of integer part. By default set to empty space. - size: The number of integers, that should be grouped. By default set to 3.
monetize(100)
<= "100,00 zł"
monetize(100.5, {"symbol":"€","separator":"."})
<= "100.50 €"
Multiplies two parametrs.
multiply(10, 5)
<= 50
Converts the number to percentage format.
percentage(0.25)
<= "25%"
Returns the value of first parameter to the power of the second parameter.
pow(2, 5)
<= 32
Converts degrees to radians and rounds received result by default precision equals 2.
radians(12)
<= 0.21
Returns a floating-point, pseudo-random number in the range 0–1.
random()
<= 0.75800265935387
Rounds the number, to the nearest integer.
round(20.49)
<= 20
Returns the sine of the number.
sin(2)
<= 0.9092974268256817
Returns the hyperbolic sine of the number.
sinh(1)
<= 1.1752011936438014
Returns the square root of the number.
sqrt(4)
<= 2
Returns square of the number.
square(5)
<= 25
Subtracts two parameters.
subtract(10, 5)
<= 5
Returns the tangent of the number.
tan(1)
<= 1.5574077246549023
Returns the hyperbolic tangent of the number.
tanh(-1)
<= -0.7615941559557649
Returns the integer part of the number.
trunc(42.84)
<= 42
Converts object keys to camelCase.
camelcase({"created_at":"2000-01-01"})
<= {"createdAt":"2000-01-01"}
Creates shallow copy of the passed object (objects are clone as reference).
clone({"a":0,"b":{"c":0}})
<= {"a":0,"b":{"c":0}}
Creates deep copy of the passed object (objects are clone as value).
deepclone({"a":0,"b":{"c":0}})
<= {"a":0,"b":{"c":0}}
Returns the value of any object property, which the name has been passed in the string.
dig({"foo":{"bar":"baz"}}, "foo.bar")
<= "baz"
Returns an array of a given object's property names.
keys({"foo":"bar","baz":[1,2,3]})
<= ["foo","baz"]
Merges properties of sources into the destination object.
merge({"foo":{"a":1,"b":1,"c":1},"baz":{"b":2,"c":2}})
<= {"foo":{"a":1,"b":1,"c":1},"baz":{"b":2,"c":2}}
merge({"foo":{"a":1,"b":1,"c":1},"baz":{"b":2,"c":2},"bar":{"c":3}})
<= {"foo":{"a":1,"b":1,"c":1},"baz":{"b":2,"c":2},"bar":{"c":3}}
Overrides the value of any object property, which the name has been passed in the string with the value.
pat({"foo":"bar"}, "foo", "baz")
<= {"foo":"baz"}
Sorts keys of an object.
recsort({"b":2,"a":1})
<= {"a":1,"b":2}
Changes names of the object properties to responds values from the keys object.
rename({"created_at":"2000-01-01"}, {"created_at":"createdAt"})
<= {"createdAt":"2000-01-01"}
Returns an array of a given object's own enumerable property values.
values({"foo":"bar","baz":[1,2,3]})
<= ["bar",[1,2,3]]
Converts string with specific unit to bytes.
bytes("400KB")
<= 409600
Converts string to camelcase notation. Second parameter sets the first letter to lowercase or uppercase.
camelize("foo_bar")
<= "fooBar"
camelize("FooBar")
<= "fooBar"
Converts first letter of the string to uppercase.
capitalize("foo")
<= "Foo"
capitalize("foo bar and baz")
<= "Foo bar and baz"
Converts temperature in Fahrenheit and Kelvin notation to Celsius degree. When passed string is a number, appends °C to the string.
celsius("25")
<= "25°C"
celsius("0°F")
<= "-18°C"
celsius("0K")
<= "-273°C"
Removes from the end of the string passed pattern.
chomp("foo bar", "ar")
<= "foo b"
Returns string with last character removed. If the string ends with line ending characters then both will be removed.
chop("foo bar baz")
<= "foo bar ba"
Creates a class name based on the string.
classify("cars")
<= "Car"
Converts string to constant notation.
constantize("foo")
<= "FOO"
Truncates string at full words. Adds ... if the string is longer than the second parameter.
crop("foo bar baz ban bar", 10)
<= "foo bar..."
Replaces _ with -.
dasherize("foo_bar__baz")
<= "foo-bar--baz"
Returns string with dot at the end.
dot("foo bar")
<= "foo bar."
Converts temperature in Celsius and Kelvin notation to Fahrenheit degree. When passed string is a number, appends °F to the string.
fahrenheit("50")
<= "50°F"
fahrenheit("30°C")
<= "86°F"
Removes HTML tags from a string. Warning: user input might include malicious content, htmlstrip is not a silver bullet against xss.
htmlstrip("<div>foo</div>")
<= "foo"
htmlstrip("Hello <b><i>world!</i></b>")
<= "Hello world!"
Replaces _ with singlespaces. When capitalize is truthy, converts first char of the string to uppercase.
humanize("foo bar")
<= "Foo bar"
humanize("foo_bar")
<= "Foo bar"
Replaces spaces with hyphens, splits camelcase text, remove non-word chars and converts string to lowercase.
hyphenate("%# lorem ipsum ? $ dolor")
<= "lorem-ipsum-dolor"
hyphenate("loremIpsum")
<= "lorem-ipsum"
Returns index of searched pattern.
index("hello", "e")
<= 1
index("hello", "lo")
<= 3
Returns initials separated by the separator.
initials("Foo Bar")
<= "FB"
initials("Foo Barin-Bar", ".")
<= "F.B.B"
Converts temperature in Celsius and Fahrenheit notation to Kelvin scale. When passed string is a number, appends K to the string.
kelvin("100")
<= "100K"
kelvin("26°C")
<= "299K"
Lowercases the string.
lowercase("Foo BAR and baZ")
<= "foo bar and baz"
Converts first letter of the string to lowercase.
lowerfirst("Foo bar baz")
<= "foo bar baz"
Removes white space characters or other specified in parameter characters from the beginning of the string.
ltrim(" foo")
<= "foo"
Returns string with added pad from left side or right side.
pad("hello world", 5)
<= " hello world"
pad("Hello world", 5, false)
<= "Hello world "
pad("hello world", "** ")
<= "** hello world"
Returns plural form of the string.
pluralize("car")
<= "cars"
pluralize("dress")
<= "dresses"
pluralize("wife")
<= "wives"
Puts the string inside quotations marks.
quote("foo bar baz")
<= "\"foo bar baz\""
quote("foo bar baz", "pl")
<= "„foo bar baz”"
Repeats the string n times.
repeat("foo", 2)
<= "foofoo"
Replaces searched pattern by the last parameter.
replace("foo bar baz ban", "baz", "qux")
<= "foo bar qux ban"
Removes white space characters or other specified in parameter characters from the end of the string.
rtrim("foo ")
<= "foo"
Replaces in the string multiple spaces to single spaces.
singlespace("foo bar baz")
<= "foo bar baz"
Returns singular form of the string.
singularize("cars")
<= "car"
singularize("watches")
<= "watch"
singularize("shelves", "f")
<= "shelf"
Converts string to lower case, remove non-word chars and replace spaces with the separator.
slugify("LoremIpsum dolor special chars")
<= "loremipsum-dolor-special-chars"
Splits string into an array by separating the string into substrings.
split("foo,bar,bazz", ",")
<= ["foo","bar","bazz"]
Replaces multiple occurances of the same characters to the one character.
squeeze("yellow moon")
<= "yelow mon"
Removes the passed pattern from the string.
strip(" foo ")
<= "foo"
strip("foo bar baz ban", ["o","r","a"])
<= "f b bz bn"
Adds three dots at the end of the string, when the string is longer than the passed length.
summarize("foo bar baz ban", 10)
<= "foo bar baz ban..."
Replaces in string lowercased letters to uppercase and uppercased letters to lowercase.
swapcase("foo")
<= "FOO"
swapcase("fOo Bar BAZ")
<= "FoO bAR baz"
Truncates the tail of a given string.
tail("Once upon a time in a world far far away", 15)
<= "...far far away"
Converts to 'uppercase' first letter of each word in string.
titleize("foo bar")
<= "Foo Bar"
Removes whitespace from the start and the end of the string.
trim(" foo ")
<= "foo"
Truncates a given string if it longer than the passed length and replaces last chars of new string with the passed ending.
truncate("Once upon a time in a world far far away")
<= "Once upon a time in a world..."
truncate("Once upon a time in a world far far away", 15)
<= "Once upon a ..."
Returns an unique identifier.
uid(10)
<= "dp0VOfYq4f"
Returns new string with words separated by _. All letters are lowercased.
underscore("fooBar")
<= "foo_bar"
Converts the HTML entities to their corresponding characters.
unescape("&")
<= "&"
Removes "" or „” from the string.
unquote("„foo bar baz”")
<= "foo bar baz"
unquote("\"foo bar baz\"")
<= "foo bar baz"
Removes characters from the start and end of the string based on the passed values.
unwrap("\"foo bar baz ban\"", "\"")
<= "foo bar baz ban"
unwrap("„foo bar baz”", "„", "”")
<= "foo bar baz"
Converts string to uppercase.
uppercase("foo")
<= "FOO"
Removes whitespaces from the string.
whitespacestrip(" f o o")
<= "foo"
Wraps string between the first and last parameters.
wrap("foo bar baz ban", "„", "”")
<= "„foo bar baz ban”"