A new minor release of ABS: always be shipping! 🚢
Function bonanza!
We've been adding a plethora of functions in this release -- check'em out!
array.chunk(size)
Splits the array into chunks of the given size:
[1, 2, 3].chunk(2) # [[1, 2], [3]]
[1, 2, 3].chunk(10) # [[1,2,3]]
[1, 2, 3].chunk(1.2) # argument to chunk must be a positive integer, got '1.2'
array.intersect(other_array)
Computes the intersection between 2 arrays:
[1, 2, 3].intersect([]) # []
[1, 2, 3].intersect([3]) # [3]
[1, 2, 3].intersect([3, 1]) # [1, 3]
[1, 2, 3].intersect([1, 2, 3, 4]) # [1, 2, 3]
array.diff(other_array)
Computes the difference between 2 arrays,
returning elements that are only on the first array:
[1, 2, 3].diff([]) # [1, 2, 3]
[1, 2, 3].diff([3]) # [1, 2]
[1, 2, 3].diff([3, 1]) # [2]
[1, 2, 3].diff([1, 2, 3, 4]) # []
For symmetric difference see diff_symmetric(...)
array.diff_symmetric(other_array)
Computes the symmetric difference
between 2 arrays (elements that are only on either of the 2):
[1, 2, 3].diff([]) # [1, 2, 3]
[1, 2, 3].diff([3]) # [1, 2]
[1, 2, 3].diff([3, 1]) # [2]
[1, 2, 3].diff([1, 2, 3, 4]) # [4]
array.union(other_array)
Computes the union
between 2 arrays:
[1, 2, 3].union([1, 2, 3, 4]) # [1, 2, 3, 4]
[1, 2, 3].union([3]) # [1, 2, 3]
[].union([3, 1]) # [3, 1]
[1, 2].union([3, 4]) # [1, 2, 3, 4]
array.flatten()
Flattens an array a single level deep:
[[1, 2], 3, [4]].flatten([1, 2, 3, 4]) # [1, 2, 3, 4]
[[1, 2, 3, 4]].flatten([1, 2, 3, 4]) # [1, 2, 3, 4]
array.flatten_deep()
Flattens an array recursively until no member is an array:
[[[1, 2], [[[[3]]]], [4]]].flatten_deep() # [1, 2, 3, 4]
[[1, [2, 3], 4]].flatten_deep() # [1, 2, 3, 4]
array.max()
Finds the highest number in an array:
[].max() # NULL
[0, 5, -10, 100].max() # 100
array.min()
Finds the lowest number in an array:
[].min() # NULL
[0, 5, -10, 100].min() # -10
array.reduce(fn, accumulator)
Reduces the array to a value by iterating through its elements and applying fn
to them:
[1, 2, 3, 4].reduce(f(value, element) { return value + element }, 0) # 10
[1, 2, 3, 4].reduce(f(value, element) { return value + element }, 10) # 20
array.partition(fn)
Partitions the array by applying fn
to all of its elements
and using the result of the function invocation as the key to partition by:
f odd(n) {
return !!(n % 2)
}
[0, 1, 2, 3, 4, 5].partition(odd) # [[0, 2, 4], [1, 3, 5]]
[1, "1", {}].partition(str) # [[1, "1"], [{}]]
number.between(min, max)
Checks whether the number is between min
and max
:
10.between(0, 100) # true
10.between(10, 100) # true
10.between(11, 100) # false
number.clamp(min, max)
Clamps the number between min and max:
10.clamp(0, 100) # 10
10.clamp(0, 5) # 5
10.clamp(50, 100) # 50
1.5.clamp(2.5, 3) # 2.5
string.camel()
Converts the string to camelCase:
"a short sentence".camel() # aShortSentence
string.snake()
Converts the string to snake_case:
"a short sentence".snake() # a_short_sentence
string.kebab()
Converts the string to kebab-case:
"a short sentence".snake() # a-short-sentence
Shorthand syntax for array.find(...)
A shorthand syntax supports passing a hash and comparing
elements to the given hash:
[null, {"key": "val", "test": 123}].find({"key": "val"}) # {"key": "val", "test": 123}