Module lodash
lodash for lua
Info:
- License: MIT
- Author: Daniel Moghimi
Array
- _.chunk line 23
-
Creates an array of elements split into groups the length of size.
If collection can’t be split evenly, the final chunk will be the remaining elements.Parameters:
- array The array to process.
- size The length of each chunk. (default 1)
Returns:
-
the new array containing chunks.
Usage:
local t = _.chunk({'x', 'y', 'z', 1, 2, 3, 4, true , false}, 4) _.print(t) --> {{"x", "y", "z", 1}, {2, 3, 4, true}, {false}}
- _.compact line 50
-
Creates an array with all falsey values removed. The values false,
nil are falsey.
Parameters:
- array The array to compact
Returns:
-
Returns the new array of filtered values
Usage:
local t = _.compact({'x', 'y', nil, 1, 2, 3, false, true , false}) _.print(t) --> {"x", "y", 1, 2, 3, true}
- _.difference line 70
-
Creates an array of unique array values not included in the other
provided arrays.
Parameters:
- array The array to inspect.
- ... The arrays of values to exclude.
Returns:
-
Returns the new array of filtered values.
Usage:
_.print(_.difference({3, 1, 2, 9, 5, 9}, {4, 5}, {9, 1})) --> {3, 2}
- _.drop line 96
-
Creates a slice of array with n elements dropped from the beginning.
Parameters:
- array The array to query.
- n The number of elements to drop. (default 1)
Returns:
-
Returns the slice of array.
Usage:
_.print(_.drop({1, 2, 3, 4, 5, 6}, 2)) --> {3, 4, 5, 6}
- _.dropRight line 121
-
Creates a slice of array with n elements dropped from the end.
Parameters:
- array The array to query.
- n The number of elements to drop. (default 1)
Returns:
-
Returns the slice of array.
Usage:
_.print(_.dropRight({1, 2, 3, 4, 5, 6}, 2)) --> {1, 2, 3, 4}
- _.dropRightWhile line 159
-
Creates a slice of array excluding elements dropped from the end.
Elements are dropped until predicate returns falsey.Parameters:
- array The array to query.
- predicate The function to invoked per iteratin. (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Return the slice of array.
Usage:
_.print(_.dropRightWhile({1, 5, 2, 3, 4, 5, 4, 4}, function(n) return n > 3 end)) --> {1, 5, 2, 3}
- _.dropWhile line 175
-
Creates a slice of array excluding elements dropped from the beginning.
Elements are dropped until predicate returns falsey.Parameters:
- array The array to query.
- predicate The function invoked per iteration. (default _.idenitity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Return the slice of array.
Usage:
_.print(_.dropWhile({1, 2, 2, 3, 4, 5, 4, 4, 2}, function(n) return n < 3 end)) --> {3, 4, 5, 4, 4, 2}
- _.fill line 195
-
Fills elements of array with value from start up to, including, end.
Parameters:
- array The array to fill.
- value The value to fill array with.
- start The start position. (default 1)
- stop The end position. (default #array)
Returns:
-
Returns array.
Usage:
local array = {1, 2, 3, 4} _.fill(array, 'x', 2, 3) _.print(array) --> {1, "x", "x", 4}
- _.findIndex line 228
-
This method is like _.find except that it returns the index of the
first element predicate returns truthy for instead of the element itself.
Parameters:
- array The array to search.
- predicate The function invoked per iteration. (default _.idenitity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the index of the found element, else -1.
Usage:
_.print(_.findIndex({{a = 1}, {a = 2}, {a = 3}, {a = 2}, {a = 3}}, function(v) return v.a == 3 end)) --> 3
- _.findLastIndex line 244
-
This method is like _.findIndex except that it iterates over
elements of collection from right to left.
Parameters:
- array The array to search.
- predicate The function invoked per iteration. (default _.idenitity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the index of the found element, else -1.
Usage:
_.print(_.findLastIndex({{a = 1}, {a = 2}, {a = 3}, {a = 2}, {a = 3}}, function(v) return v.a == 3 end)) --> 5
- _.first line 256
-
Gets the first element of array.
Parameters:
- array The array to query.
Returns:
-
Returns the first element of array.
Usage:
_.print(_.first({'w', 'x', 'y', 'z'})) --> w
- _.flatten line 272
-
Flattens a nested array.
If isDeep is true the array is recursively flattened, otherwise it’s only flattened a single level.Parameters:
- array The array to flatten.
- isDeep Specify a deep flatten
Returns:
-
Returns the new flattened array.
Usage:
_.print(_.flatten({1, 2, {3, 4, {5, 6}}})) --> {1, 2, 3, 4, {5, 6}} _.print(_.flatten({1, 2, {3, 4, {5, 6}}}, true)) --> {1, 2, 3, 4, 5, 6}
- _.flattenDeep line 299
-
Recursively flattens a nested array.
Parameters:
- array The array to flatten.
Returns:
-
Returns the new flattened array.
Usage:
_.print(_.flattenDeep({1, 2, {3, 4, {5, 6}}})) --> {1, 2, 3, 4, 5, 6}
- _.indexOf line 312
-
Gets the index at which the first occurrence of value is found in array.
Parameters:
- array The array to search.
- value The value to search for.
- fromIndex The index to search from. (default 1)
Returns:
-
Returns the index of the matched value, else -1.
Usage:
_.print(_.indexOf({2, 3, 'x', 4}, 'x')) --> 3
- _.initial line 326
-
Gets all but the last element of array.
Parameters:
- array The array to query.
Returns:
-
Returns the slice of array.
Usage:
_.print(_.initial({1, 2, 3, 'a'})) --> {1, 2, 3}
- _.intersection line 339
-
Creates an array of unique values that are included in all of the
provided arrays.
Parameters:
- The arrays to inspect.
Returns:
-
Returns the new array of shared values.
Usage:
_.print(_.intersection({1, 2}, {4, 2}, {2, 1})) --> {2}
- _.last line 367
-
Gets the last element of array.
Parameters:
- array The array to query.
Returns:
-
Returns the last element of array.
Usage:
_.print(_.last({'w', 'x', 'y', 'z'})) --> z
- _.lastIndexOf line 381
-
This method is like _.indexOf except that it iterates
over elements of array from right to left.
Parameters:
- array The array to search.
- value The value to search for.
- fromIndex The index to search from. (default #array)
Returns:
-
Returns the index of the matched value, else -1.
Usage:
_.print(_.lastIndexOf({2, 3, 'x', 4, 'x', 5}, 'x')) --> 5
- _.pull line 397
-
Removes all provided values from array.
Parameters:
- array The array to modify.
- ... The values to remove.
Returns:
-
Returns array
Usage:
local array = {1, 2, 3, 4, 5, 4, 1, 2, 3} _.pull(array, 2, 3) _.print(array) --> {1, 4, 5, 4, 1}
- _.pullAt line 430
-
Removes elements from array corresponding to the given indexes and
returns an array of the removed elements. Indexes may be specified
as an array of indexes or as individual arguments.
Parameters:
- array The array to modify.
- ... The indexes of elements to remove
Returns:
-
Returns the new array of removed elements.
Usage:
local array = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'} local t = _.pullAt(array, 4, 9, 8) _.print(array) --> {"a", "b", "c", "e", "f", "g", "j"} _.print(t) --> {"d", "h", "i"}
- _.remove line 457
-
Removes all elements from array that predicate returns truthy for
and returns an array of the removed elements.
Parameters:
- array The array to modify.
- predicate The function invoked per iteration.
Returns:
-
Returns the new array of removed elements
Usage:
local array = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 1, 2, 3, 5, 4} local t = _.remove(array, function(value) return value > 4 end) _.print(array) --> {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4} _.print(t) --> {5, 6, 7, 5, 6, 5}
- _.rest line 479
-
Gets all but the first element of array.
Parameters:
- array The array to query.
Returns:
-
Returns the slice ofa array.
Usage:
_.print(_.rest({1, 2, 3, 'a'})) --> {2, 3, 'a'}
- _.reverse line 492
-
Reverses the array so the first element becomes the last, the second
element becomes the second to last, and so on.
Parameters:
- array The array to mutate.
Returns:
-
Returns the new reversed array.
Usage:
_.print(_.reverse({1, 2, 3, 'a', 'b'})) --> {'b', 'a', 3, 2, 1}
- _.slice line 510
-
Creates a slice of array from start up to, including, end.
Parameters:
- array The array to slice.
- start The start position. (default 1)
- stop The end position (default #array)
Returns:
-
Returns the slice of array.
Usage:
_.print(_.slice({1, 2, 3, 4, 5, 6}, 2, 3)) --> {2, 3}
- _.take line 528
-
Creates a slice of array with n elements taken from the beginning.
Parameters:
- array The array to query.
- n The number of elements to take. (default 1)
Returns:
-
Returns the slice of array.
Usage:
_.print(_.take({1, 2, 3, 4, 5}, 3)) --> {1, 2, 3}
- _.takeRight line 542
-
Creates a slice of array with n elements taken from the end.
Parameters:
- array The array to query.
- n The number of elements to take. (default 1)
Returns:
-
Returns the slice of array.
Usage:
_.print(_.takeRight({1, 2, 3, 4, 5}, 3)) --> {3, 4, 5}
- _.takeRightWhile line 577
-
Creates a slice of array with elements taken from the end. Elements
are taken until predicate returns falsey. The predicate is bound to
selfArg and invoked with three arguments: (value, index, array).
Parameters:
- array The array to query.
- predicate The function invoked per iteration.
- selfArg The self binding of predicate.
Usage:
_.print(_.takeRightWhile({1, 2, 3, 4, 5, 6, 7, 8}, function(n) return n > 4 end)) --> {5, 6, 7, 8}
- _.takeWhile line 615
-
Creates a slice of array with elements taken from the beginning. Elements
are taken until predicate returns falsey. The predicate is bound to
selfArg and invoked with three arguments: (value, index, array).
Parameters:
- array The array to query.
- predicate The function invoked per iteration.
- selfArg The self binding of predicate.
Usage:
_.print(_.takeWhile({1, 2, 3, 4, 5, 6, 7, 8}, function(n) return n < 5 end)) --> {1, 2, 3, 4}
- _.union line 588
-
Creates an array of unique values, from all of the
provided arrays.
Parameters:
- ... The arrays to inspect
Usage:
_.print(_.union({1, 2}, {4, 2}, {2, 1})) --> {1, 2, 4}
- _.uniq line 636
-
Creates a duplicate-free version of an array in which only the first
occurence of each element is kept. If an iteratee function is provided
it’s invoked for each element in the array to generate the criterion
by which uniqueness is computed. The iteratee is bound to thisArg and
invoked with three arguments: (value, index, array).
Parameters:
- array The array to inspect.
- iteratee The function invoked per iteration.
- selfArg The self binding of predicate.
Returns:
-
Returns the new duplicate-value-free array.
Usage:
_.print(_.uniq({1, 3, 2, 2})) --> {1, 3, 2} _.print(_.uniq({{x=1}, {x=2}, {x=2}, {x=3}, {x=1}}, function(n) return n.x end)) --> {{["x"]=1}, {["x"]=2}, {["x"]=3}}
- _.unzip line 689
-
This method is like _.zip except that it accepts an array of grouped
elements and creates an array regrouping the elements to their pre-zip
configuration.
Parameters:
- array The array of grouped elements to process.
Returns:
-
Returns the new array of regrouped elements.
Usage:
local t = _.zip({'a', 'b', 'c'}, {1, 2, 3}, {10, 20, 30}) _.print(t) --> {{"a", 1, 10}, {"b", 2, 20}, {"c", 3, 30}} _.print(_.unzip(t)) --> {{"a", "b", "c"}, {1, 2, 3}, {10, 20, 30}}
- _.without line 701
-
Creates an array excluding all provided values
Parameters:
- array The array to filter.
- ... The values to exclude.
Returns:
-
Returns the new array of filtered values.
Usage:
_.print(_.without({1,1, 2, 3, 2, 3, 5, 5, 1, 2}, 5, 1)) --> {2, 3, 2, 3, 2}
- _.zip line 722
-
Creates an array of grouped elements, the first of which contains
the first elements of the given arrays, the second of which contains
the second elements of the given arrays, and so on.
Parameters:
- ... The arrays to process
Returns:
-
Returns the new array of grouped elements.
Usage:
local t = _.zip({'a', 'b', 'c'}, {1, 2, 3}, {10, 20, 30}) _.print(t) --> {{"a", 1, 10}, {"b", 2, 20}, {"c", 3, 30}}
- _.zipObject line 661
-
The inverse of _.pairs; this method returns an object composed from
arrays of property names and values. Provide either a single two dimensional
array, e.g. [[key1, value1], [key2, value2]] or two arrays, one of
property names and one of corresponding values.
Parameters:
- ... The properties/values
Returns:
-
Returns the new object.
Usage:
_.print(_.zipObject({{'fred', 30}, {'alex', 20}})) --> {["alex"]=20, ["fred"]=30} _.print(_.zipObject({'fred', 'alex'}, {30, 20})) --> {["alex"]=20, ["fred"]=30}
Collection
- _.at line 747
-
Creates an array of elements corresponding to the given keys,
or indexes, of collection. Keys may be specified as individual
arguments or as arrays of keys.
Parameters:
- collection The collection to iterate over.
- ... The property names or indexes of elements to pick, specified individually or in arrays.
Returns:
-
Return the new array of picked elements.
Usage:
_.print(_.at({'1', '2', '3', '4', a='a', b='b'}, {1, 2}, 'b')) --> {"1", "2", "b"}
- _.countBy line 777
-
Creates an object composed of keys generated from the results of
running each element of collection through iteratee. The corresponding
value of each key is the number of times the key was returned by
iteratee. The iteratee is bound to selfArg and invoked with three arguments:
(value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- iteratee The function invoked per iteration. (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the composed aggregate object.
Usage:
_.print(_.countBy({4.3, 6.1, 6.4}, function(n) return math.floor(n) end)) --> {[4]=1, [6]=2}
- _.every line 864
-
Checks if predicate returns truthy for all elements of collection.
The predicate is bound to selfArg and invoked with three arguments: (value, index|key, collection).Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Usage:
_.print(_.every({1, 2, 3, 4, '5', 6}, _.isNumber)) --> false _.print(_.every({1, 2, 3, 4, 5, 6}, _.isNumber)) --> true
- _.filter line 900
-
Iterates over elements of collection, returning an array of all
elements predicate returns truthy for. The predicate is bound to
selfArg and invoked with three arguments: (value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Usage:
_.print(_.filter({1, 2, 3, 4, '5', 6, '7'}, _.isNumber)) --> {1, 2, 3, 4, 6}
- _.find line 1235
-
Iterates over elements of collection, returning the first element
predicate returns truthy for. The predicate is bound to selfArg and
invoked with three arguments: (value, index|key, collection).
Parameters:
- collection The collection to search. (table|string)
- predicate The function invoked per iteration
- selfArg The self binding of predicate.
Usage:
_.print(_.find({{a = 1}, {a = 2}, {a = 3}, {a = 2}, {a = 3}}, function(v) return v.a == 3 end)) --> {[a]=3}
- _.findLast line 1255
-
This method is like _.find except that it iterates over elements of
collection from right to left.
Parameters:
- collection The collection to search. (table|string)
- predicate The function invoked per iteration
- selfArg The self binding of predicate.
Usage:
_.findLast({{a = 1}, {a = 2}, {a = 3, x = 1}, {a = 2}, {a = 3, x = 2}}, function(v) return v.a == 3 end)) --> {[a]=3, [x]=2}
- _.forEach line 914
-
Iterates over elements of collection invoking iteratee for each element.
The iteratee is bound to selfArg and invoked with three arguments:
(value, index|key, collection). Iteratee functions may exit iteration
early by explicitly returning false.
Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns collection.
- _.forEachRight line 929
-
This method is like _.forEach except that it iterates
over elements of collection from right to left.
Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns collection.
- _.groupBy line 807
-
Creates an object composed of keys generated from the results of
running each element of collection through iteratee. The corresponding
value of each key is an array of the elements responsible for generating
the key. The iteratee is bound to selfArg and invoked with three arguments:
(value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- iteratee The function invoked per iteration. (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the composed aggregate object.
Usage:
_.print(_.groupBy({4.3, 6.1, 6.4}, function(n) return math.floor(n) end)) --> {[4]={4.3}, [6]={6.1, 6.4}}
- _.includes line 944
-
Checks if target is in collection.
Parameters:
- collection The collection to search
- target The value to search for.
Usage:
print(_.includes({1, 2, 'x', 3, ['5']=4, x=3, 5}, 'x')) --> true print(_.includes({1, 2, 'x', 3, ['5']=4, x=3, 5}, 'z')) --> false
- _.indexBy line 841
-
Creates an object composed of keys generated from the results of
running each element of collection through iteratee. The corresponding
value of each key is the last element responsible for generating the key.
The iteratee function is bound to selfArg and invoked with three arguments:
(value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- iteratee The function invoked per iteration. (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the composed aggregate object.
Usage:
local keyData = { {dir='l', a=1}, {dir='r', a=2} } _.print('40.indexBy :', _.indexBy(keyData, function(n) return n.dir end)) --> {["l"]={[a]=1, [dir]="l"}, ["r"]={[a]=2, [dir]="r"}}
- _.invoke line 962
-
Invokes method of each element in collection, returning an array of the
results of each invoked method. Any additional arguments are provided
to each invoked method. func bound to, each element in collection.
Parameters:
- collection The collection to iterate over.
- method The method to invoke per iteration.
- ... The arguments to invoke the method with.
Returns:
-
Returns the array of results.
Usage:
_.print(_.invoke({'1.first', '2.second', '3.third'}, string.sub, 1, 1)) --> {"1", "2", "3"}
- _.map line 982
-
Creates an array of values by running each element in collection through
iteratee. The iteratee is bound to selfArg and invoked with three
arguments: (value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- iteratee The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Usage:
_.print(_.map({1, 2, 3, 4, 5, 6, 7, 8, 9}, function(n) return n * n end)) --> {1, 4, 9, 16, 25, 36, 49, 64, 81}
- _.partition line 1005
-
Creates an array of elements split into two groups, the first of
which contains elements predicate returns truthy for, while the second
of which contains elements predicate returns falsey for. The predicate
is bound to selfArg and invoked with three arguments:
(value, index|key, collection).
Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the array of grouped elements.
Usage:
_.print(_.partition({1, 2, 3, 4, 5, 6, 7}, function (n) return n > 3 end)) --> {{4, 5, 6, 7}, {1, 2, 3}}
- _.pluck line 1030
-
Gets the property value of path from all elements in collection.
Parameters:
- collection The collection to iterate over.
- path The path of the property to pluck.
Usage:
local users = { { user = 'barney', age = 36, child = {age = 5}}, { user = 'fred', age = 40, child = {age = 6} } } _.print(_.pluck(users, {'user'})) --> {"barney", "fred"} _.print(_.pluck(users, {'child', 'age'})) --> {5, 6}
- _.reduce line 1061
-
Reduces collection to a value which is the accumulated result of
running each element in collection through iteratee, where each
successive invocation is supplied the return value of the previous.
If accumulator is not provided the first element of collection is used as the initial value. The iteratee is bound to selfArg and invoked with four arguments: (accumulator, value, index|key, collection).Parameters:
- collection The collection to iterate over.
- iteratee The function invoked per iteration. (default _.identity)
- accumulator
The initial value.
(default
) - selfArg The self binding of predicate. (optional)
Returns:
-
Returns the accumulated value.
Usage:
_.print(_.reduce({1, 2, 3}, function(total, n) return n + total; end)) --> 6 _.print(_.reduce({a = 1, b = 2}, function(result, n, key) result[key] = n * 3 return result; end, {})) --> {["a"]=3, ["b"]=6}
- _.reduceRight line 1087
-
This method is like _.reduce except that it iterates over elements
of collection from right to left.
Parameters:
- collection The collection to iterate over.
- iteratee The function invoked per iteration. (default _.identity)
- accumulator
The initial value.
(default
) - selfArg The self binding of predicate. (optional)
Returns:
-
Returns the accumulated value.
Usage:
local array = {0, 1, 2, 3, 4, 5}; _.print(_.reduceRight(array, function(str, other) return str .. other end, '')) --> 543210
- _.reject line 1108
-
The opposite of _.filter; this method returns the elements of
collection that predicate does not return truthy for.
Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Usage:
_.print(_.reject({1, 2, 3, 4, '5', 6, '7'}, _.isNumber)) --> {"5", "7"}
- _.sample line 1122
-
Gets a random element or n random elements from a collection.
Parameters:
- collection The collection to sample.
- n The number of elements to sample. (default 1)
Returns:
-
Returns the random sample(s).
Usage:
_.print(_.sample({1, 2, 3, a=4, b='x', 5, 23, 24}, 4)) --> {5, "x", 1, 23} _.print(_.sample({1, 2, 3, a=4, b='x', 5, 23, 24})) --> 4
- _.size line 1145
-
Gets the size of collection by returning its length for array-like
values or the number of own enumerable properties for objects.
Parameters:
- collection The collection to inspect.
Returns:
-
Returns the size of collection.
Usage:
_.print(_.size({'abc', 'def'})) --> 2 _.print(_.size('abcdefg')) --> 7 _.print(_.size({a=1, b=2,c=3})) --> 3
- _.some line 1167
-
Checks if predicate returns truthy for any element of collection.
The function returns as soon as it finds a passing value and does not iterate over the entire collection. The predicate is bound to selfArg and invoked with three arguments: (value, index|key, collection).Parameters:
- collection The collection to iterate over. (table|string)
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns true if any element passes the predicate check, else false.
Usage:
_.print(_.some({1, 2, 3, 4, 5, 6}, _.isString)) --> false _.print(_.some({1, 2, 3, 4, '5', 6}, _.isString)) --> true
- _.sortBy line 1201
-
Creates an array of elements, sorted in ascending order by the
results of running each element in a collection through iteratee.
The iteratee is bound to selfArg and invoked with three arguments: (value, index|key, collection).Parameters:
- collection The collection to iterate over.
- predicate The function invoked per iteration (default _.identity)
- selfArg The self binding of predicate. (optional)
Returns:
-
Returns the new sorted array.
Usage:
local t = {1, 2, 3} _.print(_.sortBy(t, function (a) return math.sin(a) end)) --> {1, 3, 2} local users = { { user='fred' }, { user='alex' }, { user='zoee' }, { user='john' }, } _.print(_.sortBy(users, function (a) return a.user end)) --> {{["user"]="alex"}, {["user"]="fred"}, {["user"]="john"}, {["user"]="zoee"}}
Function
- _.after line 1279
-
This method creates a function that invokes func once it’s called n
or more times.
Parameters:
- n The number of calls before func invoked.
- func The function to restrict.
Returns:
-
Returns the new restricted function.
Usage:
local printAfter3 = _.after(3, print) for i = 1, 5 do printAfter3('done', i) end --> done 4 --> done 5
- _.ary line 1299
-
Creates a function that accepts up to n arguments ignoring any
additional arguments.
Parameters:
- func The function to cap arguments for.
- n the arity cap.
Returns:
-
Returns the new function
Usage:
local printOnly3 =_.ary(print, 3) printOnly3(1, 2, 3, 'x', 'y', 6) --> 1 2 3
- _.before line 1326
-
Creates a function that invokes func while it’s called less than n times.
Subsequent calls to the created function return the result of the
last func invocation.
Parameters:
- n The number of calls at which func is no longer invoked.
- func The function to restrict.
Returns:
-
Returns the new restriced function.
Usage:
local printBefore3 = _.before(3, print) for i = 1, 10 do printBefore3(i, 'ok') end --> 1 ok --> 2 ok --> 3 ok
- _.modArgs line 1361
-
Creates a function that runs each argument through a corresponding
transform function.
Parameters:
- func The function to wrap
- ... The functions to transform arguments, specified as individual functions or arrays of functions.
Returns:
-
Returns the new function.
Usage:
local increment = function(...) return _.args(_.map(_.table(...), function(n) return n + 1 end)) end local pow = function(...) return _.args(_.map(_.table(...), function(n) return n * n end)) end local modded = _.modArgs(function(...) print(...) end, {increment, increment}, pow) modded(0, 1, 2) --> 4 9 16
- _.negate line 1403
-
Creates a function that negates the result of the predicate func.
The func predicate is invoked with arguments of the created function.Parameters:
- func The preadicate to negate.
Returns:
-
Returns the new function
Usage:
local isEven = function (n) return n % 2 == 0 end local isOdd = _.negate(isEven) _.print(_.filter({1, 2, 3, 4, 5, 6}, isEven)) --> {2, 4, 6} _.print(_.filter({1, 2, 3, 4, 5, 6}, isOdd)) --> {1, 3, 5}
- _.once line 1428
-
Creates a function that is restricted to invoking func once. Repeat
calls to the function return the value of the first call. The func
is invoked with arguments of the created function.
Parameters:
- func The function to restrict.
Returns:
-
Returns the new function.
Usage:
local createApp = function(version) print('App created with version '..version) return version end local initialize = _.once(createApp) initialize(1.1) initialize(1.1) initialize(1.1) --> App created with version 1.1 --> 1.1 --> 1.1 --> 1.1
- _.rearg line 1458
-
Creates a function that invokes func with arguments arranged according
to the specified indexes where the argument value at the first index
is provided as the first argument, the argument value at the second
index is provided as the second argument, and so on.
Parameters:
- func The function to rearrange arguments for.
- ... The arranged argument indexes, specified as individual indexes or arrays of indexes.
Returns:
-
Returns the new function.
Usage:
local rearged = _.rearg(function(a, b, c) return {a, b, c}; end, 2, 1, 3) _.print(rearged('a', 'b', 'c')) --> {"b", "a", "c"} _.print(rearged('b', 'a', 'c')) --> {"a", "b", "c"}
Lang
- _.args line 1496
-
Cast value to arguments
Parameters:
- value value to cast
Returns:
-
Returns arguments
Usage:
print(_.args({1, 2, 3})) --> 1 2 3
- _.bool line 1758
-
Cast anything to boolean. If any function detected, call and cast its
result. Return false for 0, nil, table and empty string.
Parameters:
- value value to cast
- ... The parameters to pass to any detected function
Returns:
-
casted value
Usage:
print(_.bool({1, 2, 3})) --> false print(_.bool("123")) --> true print(_.bool(0)) --> false print(_.bool(function(a) return a end, "555")) --> true
- _.func line 1720
-
Cast parameters to a function that return passed parameters.
Parameters:
- value value to cast
- ... The parameters to pass to any detected function
Returns:
-
casted value
Usage:
local f = _.func(1, 2, 3) _.print(f()) --> 1 2 3
- _.gt line 1511
-
Checks if value is greater than other.
Parameters:
- value The value to compare.
- other The other value to compare.
Usage:
_.print(_.gt(1, 3)) --> false _.print(_.gt(4, 3)) --> true
- _.gte line 1530
-
Checks if value is greater than other.
Parameters:
- value The value to compare.
- other The other value to compare.
Usage:
_.print(_.gte(1, 3)) --> false _.print(_.gte(3, 3)) --> true
- _.isBoolean line 1554
-
Checks if value is classified as a boolean primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isBoolean(false)) --> true _.print(_.isBoolean('x')) --> false
- _.isEmpty line 1573
-
Checks if value is empty. A value is considered empty unless it’s an
arguments table, array, string with a length greater than 0 or an
object with own enumerable properties.
Parameters:
- value The value to inspect.
Returns:
-
Returns true if value is empty, else false.
Usage:
_.print(_.isEmpty(true)) --> true _.print(_.isEmpty(1)) --> true _.print(_.isEmpty({1, 2, 3})) --> false _.print(_.isEmpty({a= 1})) --> false
- _.isFunction line 1596
-
Checks if value is classified as a function primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isFunction(function() end)) --> true _.print(_.isFunction(1)) --> false
- _.isNil line 1610
-
Checks if value is classified as a nil primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isNil(variable) --> true variable = 1 _.print(_.isNil(variable)) --> false
- _.isNumber line 1623
-
Checks if value is classified as a number primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isNumber(1)) --> true _.print(_.isNumber('1')) --> false
- _.isString line 1636
-
Checks if value is classified as a string primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isString('1')) --> true _.print(_.isString(1)) --> false
- _.isTable line 1649
-
Checks if value is classified as a table primitive.
Parameters:
- value the value to check
Returns:
-
Returns true if value is correctly classified, else false.
Usage:
_.print(_.isTable({'1'})) --> true _.print(_.isString(1)) --> false
- _.lt line 1666
-
Checks if value is less than other.
Parameters:
- value The value to compare.
- other The other value to compare.
Usage:
_.print(_.lt(1, 3)) --> true _.print(_.lt(3, 3)) --> false
- _.lte line 1684
-
Checks if value is less than or euqal to other.
Parameters:
- value The value to compare.
- other The other value to compare.
Usage:
_.print(_.lt(4, 3)) --> false _.print(_.lt(3, 3)) --> true
- _.num line 1787
-
Cast anything to number. If any function detected, call and cast its
result. Return 0 for nil and table.
Parameters:
- value value to cast
- ... The parameters to pass to any detected function
Returns:
-
casted value
Usage:
print(_.num({1, 2, 3})) --> 0 print(_.num("123")) --> 123 print(_.num(true)) --> 1 print(_.num(function(a) return a end, "555")) --> 555
- _.str line 1822
-
Cast anything to string. If any function detected, call and cast its
result.
Parameters:
- value value to cast
- ... The parameters to pass to any detected function
Returns:
-
casted value
Usage:
print(_.str({1, 2, 3, 4, {k=2, {'x', 'y'}}})) --> {1, 2, 3, 4, {{"x", "y"}, ["k"]=2}} print(_.str({1, 2, 3, 4, function(a) return a end}, 5)) --> {1, 2, 3, 4, 5}
- _.table line 1739
-
Cast parameters to table using table.pack
Parameters:
- value value to cast
- ... The parameters to pass to any detected function
Returns:
-
casted value
Usage:
print(_.table(1, 2, 3)) --> {1, 2, 3} print(_.table("123")) --> {"123"} print(_.table(0)) --> {0}
Number
- _.inRange line 1864
-
Checks if n is between start and up to but not including, end.
If end is not specified it’s set to start with start then set to 0.Parameters:
- n The number to check.
- start The start of the range.
- stop The end of the range.
Returns:
-
Returns true if n is in the range, else false.
Usage:
print(_.inRange(-3, -4, 8)) --> true
- _.random line 1886
-
Produces a random number between min and max (inclusive).
If only one argument is provided a number between 0 and the given number is returned. If floating is true, a floating-point number is returned instead of an integer.Parameters:
- min the minimum possible value. (default 0)
- max the maximum possible value. (default 1)
- floating Specify returning a floating-point number. (default false)
Returns:
-
Returns the random number.
Usage:
_.print(_.random()) --> 1 _.print(_.random(5)) --> 3 _.print(_.random(5, 10, true)) --> 8.8120200577248
Object
- _.findKey line 1963
-
This method is like _.find except that it returns the key of the
first element predicate returns truthy for instead of the element itself.
Parameters:
- object The collection to search. (table|string)
- predicate The function invoked per iteration
- selfArg The self binding of predicate.
Returns:
-
Returns the key of the matched element, else nil
Usage:
_.print(_.findKey({a={a = 1}, b={a = 2}, c={a = 3, x = 1}}, function(v) return v.a == 3 end)) --> c
- _.findLastKey line 1984
-
This method is like _.find except that it returns the key of the
first element predicate returns truthy for instead of the element itself.
Parameters:
- object The object to search. (table|string)
- predicate The function invoked per iteration
- selfArg The self binding of predicate.
Returns:
-
Returns the key of the matched element, else nil
Usage:
_.print(_.findLastKey({a={a=3}, b={a = 2}, c={a=3, x = 1}}, function(v) return v.a == 3 end)) --> c
- _.functions line 2000
-
Creates an array of function property names from all enumerable
properties, own and inherited, of object.
Parameters:
- object The object to inspect.
Returns:
-
Returns the new array of property names.
Usage:
_.print(_.functions(table)) --> {"concat", "insert", "maxn", "pack", "remove", "sort", "unpack"}
- _.get line 1911
-
Gets the property value at path of object. If the resolved value
is nil the defaultValue is used in its place.
Parameters:
- object The object to query.
- path The path of the property to get.
- defaultValue The value returned if the resolved value is nil. (default nil)
Returns:
-
Returns the resolved value.
Usage:
local object = {a={b={c={d=5}}}} _.print(_.get(object, {'a', 'b', 'c', 'd'})) --> 5
- _.has line 1935
-
Checks if path is a direct property.
Parameters:
- object The object to query
- path The path to check (Array)
Usage:
local object = {a={b={c={d}}}} print(_.has(object, {'a', 'b', 'c'})) --> true
- _.invert line 2022
-
Creates an object composed of the inverted keys and values of object.
If object contains duplicate values, subsequent values overwrite property assignments of previous values unless multiValue is true.Parameters:
- object The object to invert.
- multiValue Allow multiple values per key.
Returns:
-
Returns the new inverted object.
Usage:
_.print(_.invert({a='1', b='2', c='3', d='3'})) --> {[2]="b", [3]="d", [1]="a"} _.print(_.invert({a='1', b='2', c='3', d='3'}, true)) --> {[2]="b", [3]={"c", "d"}, [1]="a"}
- _.keys line 2058
-
Creates an array of the own enumerable property names of object.
Parameters:
- object The object to query. (table|string)
Returns:
-
Returns the array of property names.
Usage:
_.print(_.keys("test")) --> {1, 2, 3, 4} _.print(_.keys({a=1, b=2, c=3})) --> {"c", "b", "a"}
- _.pairs line 2077
-
Creates a two dimensional array of the key-value pairs for object.
Parameters:
- object The object to query
Returns:
-
Returns the new array of key-value pairs.
Usage:
_.print(_.pairs({1, 2, 'x', a='b'})) --> {{1, 1}, {2, 2}, {3, "x"}, {"a", "b"}}
- _.result line 2097
-
This method is like _.get except that if the resolved value is a
function it’s invoked with additional parameters and its result is returned.
Parameters:
- object The object to query.
- path The path of the property to get.
- defaultValue The value returned if the resolved value is nil. (default nil)
- ... Additional parameters to pass to function
Returns:
-
Returns the resolved value.
Usage:
local object = {a=5, b={c=function(a) print(a) end}} _.result(object, {'b', 'c'}, nil, 5) --> 5
- _.values line 2116
-
Creates an array of the own enumerable property values of object.
Parameters:
- object The object to query. (table|string)
Returns:
-
Returns the array of property values.
Usage:
_.print(_.values("test")) --> {"t", "e", "s", "t"} _.print(_.values({a=1, b=2, c=3})) --> {1, 3, 2}
Utility
- _.constant line 2140
-
Creates a function that returns value.
Parameters:
- value Any value.
Returns:
-
Returns the new function.
Usage:
local object = {x=5} local getter = _.constant(object) _.print(getter() == object); --> true
- _.identity line 2152
-
This method returns the first argument provided to it.
Parameters:
- value Any value.
Returns:
-
Returns value.
Usage:
local object = {x=5} _.print(_.identity(object) == object); --> true
- _.noop line 2209
-
A no-operation function that returns nil regardless of the arguments
it receives.
Parameters:
- ... Any arguments
Returns:
-
Returns nil
Usage:
local object = {x=5} _.print(_.noop(object) == nil); --> true
- _.print line 2219
-
Print more readable representation of arguments using _.str
Parameters:
- ... values to print
Returns:
-
Return human readable string of the value
Usage:
_.print({1, 2, 3, 4, {k=2, {'x', 'y'}}}) --> {1, 2, 3, 4, {{"x", "y"}, [k]=2}}
- _.range line 2238
-
Creates an array of numbers (positive and/or negative) progressing
from start up to, including, end.
If end is not specified it’s set to start with start then set to 1.Parameters:
- start The start of the range. (default 1)
- stop Then end of the range.
- step The value to increment or decrement by (default 1)
Returns:
-
Returns the new array of numbers
Usage:
_.print(_.range(5, 20, 3)) --> {5, 8, 11, 14, 17, 20}