Shamelessly named after the JavaScript package, Lodash.
Also, the initial commit was shamelessly copy-pasted from mason.nvim (sorry, it was just so well done).
-
I wanted to package these functions into a plugin because I kept needing the same functionality in several of my projects for NeoVim.
-
Documentation. Lua packages tend to suck at this. I wanted to make it better.
Using lazy.nvim
-- init.lua
{
'darksinge/neodash.nvim',
}
If you'd like to contribute, please open a new issue with your idea and let's chat!
Things that would be extremely welcome:
- Example snippets!
- ChatGPT generated the majority of the documentation under Usage, so it's possible some of the documentation is incorrect.
- Also, ChatGPT generated the majority of the documentation, so some of the method descriptions read like garbage.
If you find issues in the documentation or are willing to add examples, your PR would be welcome!
-
_.table_pack(...) -> table
Packs the given arguments into a table.
Example:
local packed = _.table_pack('a', 'b', 'c') print(packed[1]) -- a print(packed[2]) -- b print(packed[3]) -- c print(packed.n) -- 3
-
---@generic T : string ---@param values T[] ---@return table<T, T> _.enum(values: string[]) -> table<string, string>
Creates an enum from the given list of string values. The returned table uses each string as both a key and a value.
Example:
_.enum({ 'foo', 'bar', 'baz' }) -- => { foo = 'foo', bar = 'bar', baz = 'baz' }
-
---@generic T ---@param list T[] ---@return table<T, boolean> _.set_of(list: any[]) -> table<any, boolean>
Creates a set from the given list. The returned table uses each value from the list as a key, and the value for each key is
true
.Example:
_.enum({ 'foo', 'bar', 'baz' }) -- => { foo = true, bar = true, baz = true}
-
---@generic T : fun(...) ---@param fn T ---@param arity integer ---@return T _.curryN(fn: T, arity: integer) -> T
This function
curryN
takes a functionfn
and an integerarity
as arguments. It returns a curried version of the input functionfn
, which waits until it has receivedarity
number of arguments before being invoked.Example:
local function sumThreeNumbers (a, b, c) return a + b + c end local curriedSum = _.curryN(sumThreeNumbers, 3) print(curriedSum(1)(2)(3)) -- Output: 6
-
_.compose(...) -> T
This function
compose
takes one or more functions as arguments and returns a function. When the returned function is called, it runs the original input functions from right to left, passing the result of each function into the next. -
---@generic T ---@param fn fun(...): T ---@return fun(...): T _.partial(fn: fun(...): T, ...) -> fun(...): T
This function
partial
takes a functionfn
and any number of arguments. It returns a new function that, when called, will invokefn
with the initial arguments provided topartial
, followed by any additional arguments provided when the new function is invoked. -
---@generic T ---@param value T ---@return T _.identity(value: T) -> T
This function
identity
takes an argumentvalue
and returns the samevalue
as is. -
---@generic T ---@return fun(): T _.always(a: T) -> fun(): T
This function
always
takes an argumenta
and returns a new function. When the new function is invoked, it will always returna
. -
---@return true _.T = _.always(true)
A function that always returns
true
. -
---@return false _.F = _.always(false)
A function that always returns
false
. -
---@generic T : fun(...) ---@param fn T ---@param cache_key_generator (fun(...): any)? ---@return T _.memoize(fn: T, cache_key_generator: fun(...): any) -> T
This function
memoize
takes a functionfn
and an optional functioncache_key_generator
as arguments. It returns a new function that caches the result offn
for each unique set of arguments. Thecache_key_generator
is used to generate keys for the cache, defaulting to_.identity
if no generator is provided. -
---@generic T ---@param fn fun(): T ---@return fun(): T _.lazy(fn: fun(): T) -> fun(): T
This function
lazy
takes a functionfn
as argument. It returns a new function that, when called, will callfn
and cache its result. Subsequent calls to the new function will return the cached result. -
---@generic U ---@param fn fun(value: U): unknown ---@param value U ---@return U _.tap(fn: fun(value: U): T, value: U) -> U
The function
_.tap()
takes two arguments: a functionfn
and a value. It appliesfn
to the value and then returns the value regardless of the result of the function application. -
---@generic T, U ---@param value T ---@param fn fun(value: T): U ---@return U _.apply_to(value: T, fn: fun(value: T): U) -> U
This function
apply_to
takes avalue
and a functionfn
as arguments. It returns the result of applyingfn
tovalue
. -
---@generic T, R, V ---@param fn fun (args...: V[]): R ---@param args V[] ---@return R _.apply(fn: fun (args...: V[]): R, args: V[]) -> R
This function
apply
takes a functionfn
and an arrayargs
as arguments. It returns the result of applyingfn
to the elements ofargs
. -
---@generic T, V ---@param fn fun(...): T ---@param fns (fun(value: V))[] ---@param val V ---@return T _.converge(fn: fun(...): T, fns: (fun(value: V))[], val: V) -> T
This function
converge
takes a functionfn
, a list of functionsfns
, and a valueval
. It applies each function infns
toval
, collects the results into an array, and appliesfn
to that array.
-
_.reverse(list: T[]) -> T[]
Reverses the given list and returns the result.
-
_.list_not_nil(...) -> T[]
Returns a list of arguments where nil values are omitted.
-
_.find_first(predicate: fun(item: T): boolean, list: T[]) -> T | nil
Returns the first item in the list that satisfies the predicate function. If no such item is found, returns nil.
-
_.any(predicate: fun(item: T): boolean, list: T[]) -> boolean
Checks if any item in the list satisfies the predicate function. Returns true if at least one item does, else false.
-
_.all(predicate: fun(item: T): boolean, list: T[]) -> boolean
Checks if all items in the list satisfy the predicate function. Returns true only if all items satisfy the predicate.
-
_.filter(filter_fn: (fun(item: T): boolean), items: T[]) -> T[]
Filters the items in the list based on the filter function and returns the resulting list.
-
_.map(map_fn: (fun(item: T): U), items: T[]) -> U[]
Applies the map function to all items in the list and returns the resulting list.
-
_.flatten(value: any[]) -> any[]
Flattens a nested list into a single level list.
-
---@generic T, U ---@type fun(map_fn: (fun(item: T): U), items: T[]): U[] _.flat_map = _.curryN(function(map_fn, list)
Example:
function duplicate(n) return { n, n } end _.flat_map(duplicate, { 1, 2 }) -- => { 1, 1, 2, 2 }
-
_.filter_map(map_fn: fun(item: T): Optional, list: T[]) -> any[]
Applies the map function to all items in the list and filters out items that are not present.
-
_.each(fn: fun(item: T, index: integer), list: T[])
Invokes the function on each item in the list. The function is called with two arguments: the item and its index.
-
_.list_copy(list: T[]) -> T[]
Returns a copy of the given list.
-
_.concat(a: any, b: any) -> any
Concatenates two lists or strings.
-
_.append(value: T, list: T[]) -> T[]
Appends a value to the end of a list and returns the new list.
-
_.prepend(value: T, list: T[]) -> T[]
Prepends a value to the start of a list and returns the new list.
-
_.zip_table(keys: T[], values: U[]) -> table<T, U>
Returns a new table where keys from one list are associated with values from another list.
-
_.nth(offset: number, value: T[]|string) -> T|string|nil
Returns the item at the given offset. For a negative offset, items are counted from the end.
-
_.head(value: T[]|string) -> T|string|nil
Returns the first item of a list or string.
-
_.last(list: T[]) -> T?
Returns the last item from a list.
-
_.length(value: string|any[]) -> integer
Returns the length of a list or string.
-
_.sort_by(comp: fun(item: T): any, list: T[]) -> T[]
Sorts a list based on the comparison function and returns the sorted list.
-
_.join(sep: string, list: any[]) -> string
Joins a list into a string with the given separator.
-
_.uniq_by(id: fun(item: T): any, list: T[]) -> T[]
Returns a list with unique items based on the id function.
-
_.partition(predicate: fun(item: T): boolean, list: T[]) -> T[][]
Partitions a list into two lists, where the first contains items that satisfy the predicate and the second contains items that don't.
-
_.take(n: integer, list: T[]) -> T[]
Returns the first n items from a list.
-
_.drop(n: integer, list: T[]) -> T[]
Removes the first n items from a list and returns the remaining items.
-
_.drop_last(n: integer, list: T[]) -> T[]
Removes the last n items from a list and returns the remaining items.
-
_.reduce(fn: fun(acc: U, item: T): U, acc: U, list: T[]) -> U
Reduces a list to a single value by iteratively applying the function to an accumulator and each item in the list.
-
_.split_every(n: integer, list: T[]) -> T[][]
Splits a list into sub-lists each containing n items, except for the last one which may contain less.
-
_.index_by(index: fun(item: T): U, list: T[]) -> table<U, T>
Returns a table where each item in the list is indexed by the result of the index function.
-
---@generic T : any ---@param list T[] ---@return T[] _.shift(list: T[]) -> T[]
Removes the first element from an array.
Example:
local shifted = _.shift({ 1, 2, 3 }) print(vim.inspect(shifted)) -- { 2, 3 }
-
---@generic T : any ---@param list T[] ---@param ... T ---@return T[] _.unshift(list: T[], ...<T>) -> T[]
Adds the specified elements to the beginning of a list.
Example:
local list = { 1, 2, 3 } local unshifted = _.unshift(list, 4, 5) print(vim.inspect(unshifted)) -- { 4, 5, 1, 2, 3 }
-
---@generic T ---@param predicates (fun(item: T): boolean)[] ---@param item T ---@return boolean _.all_pass(predicates: (fun(item: T): boolean)[], item: T) -> boolean
Takes an array of predicate functions and an item. It returns true if all predicate functions pass for the item (i.e., return true), otherwise returns false.
-
---@generic T ---@param predicates (fun(item: T): boolean)[] ---@param item T ---@return boolean _.any_pass(predicates: (fun(item: T): boolean)[], item: T) -> boolean
Takes an array of predicate functions and an item. Returns true if any of the predicate functions pass for the item (i.e., return true), otherwise returns false.
-
---@generic T, U ---@param predicate fun(item: T): boolean ---@param on_true fun(item: T): U ---@param on_false fun(item: T): U ---@param value T ---@return U _.if_else(predicate: fun(item: T): boolean, on_true: fun(item: T): U, on_false: fun(item: T): U, value: T) -> U
Takes a predicate function, two transformation functions, and a value. If the predicate returns true for the value, it applies the first transformation function (on_true) on the value, otherwise applies the second transformation function (on_false).
-
---@param value boolean ---@return boolean _.is_not(value: boolean) -> boolean
Takes a boolean value and returns its negation.
-
---@generic T ---@param predicate fun(value: T): boolean ---@param value T ---@return boolean _.complement(predicate: fun(value: T): boolean, value: T) -> boolean
Takes a predicate function and a value, returns the negation of the predicate result for that value.
-
---@generic T, U ---@param predicate_transformer_pairs {[1]: (fun(value: T): boolean), [2]: (fun(value: T): U)}[] ---@param value T ---@return U? _.cond(predicate_transformer_pairs: {[1]: (fun(value: T): boolean), [2]: (fun(value: T): U)}[], value: T) -> U?
Takes an array of predicate-transformer pairs and a value. For each pair, it checks if the value passes the predicate (the first element of the pair), if so, it returns the result of applying the transformer function (the second element of the pair) on the value. If none of the predicates pass, it returns nil.
-
---@generic T ---@param default_val T ---@param val T? ---@return T _.default_to(default_val: T, val: T?) -> T
Takes a default value and an optional value. If the optional value is not nil, it returns the optional value, otherwise it returns the default value.
-
_.equals(expected: any, value: any) -> boolean
Checks if the provided value is equal to the expected value. Returns true if they are equal, false otherwise.
-
_.not_equals(expected: any, value: any) -> boolean
Checks if the provided value is not equal to the expected value. Returns true if they are not equal, false otherwise.
-
_.prop_eq(property: any, value: any, tbl: table) -> boolean
Checks if the value of a given property in the provided table is equal to the provided value. Returns true if they are equal, false otherwise.
-
_.prop_satisfies(predicate: fun(value: any): boolean, property: any, tbl: table) -> boolean
Applies a predicate function to the value of a given property in the provided table. Returns the result of the predicate.
-
_.path_satisfies(predicate: fun(value: any): boolean, path: any[], tbl: table) -> boolean
Applies a predicate function to the value at a given path in the provided table. Returns the result of the predicate.
-
_.min(a: number, b: number) -> number
Subtracts the first number from the second one. Returns the result.
-
_.add(a: number, b: number) -> number
Adds two numbers together. Returns the result.
-
---@param pattern string ---@param str string _.matches(pattern: string, str: string) -> boolean
Returns true if the string matches the provided pattern.
-
_.match(pattern: string, str: string) -> table
Returns a table with the matches of the provided pattern in the string.
-
---@param template string ---@param str string _.format(template: string, str: string) -> string
Formats the string according to the provided template.
-
---@param sep string ---@param str string _.split(sep: string, str: string) -> table
Splits the string into a table by the provided separator.
-
---@param pattern string ---@param repl string|function|table ---@param str string _.gsub(pattern: string, repl: string|function|table, str: string) -> string
Returns a string where the matches for the pattern have been replaced by the repl.
-
_.trim(str: string) -> string
Returns the string without leading/trailing whitespace.
-
---https://github.com/nvim-lua/nvim-package-specification/blob/93475e47545b579fd20b6c5ce13c4163e7956046/lua/packspec/schema.lua#L8-L37 ---@param str string ---@return string _.dedent(str: string) -> string
Removes common leading indentation from a multiline string.
-
---@param prefix string ---@str string _.starts_with(prefix: string, str: string) -> boolean
Returns true if the string starts with the provided prefix.
-
---@param str string _.to_upper(str: string) -> string
Returns the string converted to uppercase.
-
---@param str string _.to_lower(str: string) -> string
Returns the string converted to lowercase.
-
---@param pattern string ---@param str string _.trim_start_matches(pattern: string, str: string) -> string
Returns the string with leading characters that match the pattern removed.
-
---@param pattern string ---@param str string _.trim_end_matches(pattern: string, str: string) -> string
Returns the string with trailing characters that match the pattern removed.
-
_.strip_prefix(prefix_pattern: string, str: string) -> string
Returns the string with the prefix that matches the prefix_pattern removed.
-
_.strip_suffix(suffix_pattern: string, str: string) -> string
Returns the string with the suffix that matches the suffix_pattern removed.
-
---@param number number _.negate(number: number) -> number
Negates the given number.
-
_.gt(number: number, value: number) -> boolean
Returns true if the value is greater than the number.
-
_.gte(number: number, value: number) -> boolean
Returns true if the value is greater than or equal to the number.
-
_.lt(number: number, value: number) -> boolean
Returns true if the value is less than the number.
-
_.lte(number: number, value: number) -> boolean
Returns true if the value is less than or equal to the number.
-
_.inc(increment: number, value: number) -> number
Increases the value by the increment.
-
_.dec(decrement: number, value: number) -> number
Decreases the value by the decrement.
-
---@generic T, U ---@param index T ---@param tbl table<T, U> ---@return U? _.prop(index: T, tbl: table<T, U>) -> U?
Returns the value at the specified index in the table.
-
---@param path any[] ---@param tbl table _.path(path: any[], tbl: table) -> any
Returns the value at the specified path in the table.
-
---@generic T, U ---@param keys T[] ---@param tbl table<T, U> ---@return table<T, U> _.pick(keys: T[], tbl: table<T, U>) -> table<T, U>
Returns a new table with only the keys specified in the keys array.
-
_.keys(tbl: table) -> table
Returns an array of the table's keys.
-
_.size(tbl: table) -> number
Returns the number of keys in the table.
-
---@generic K, V ---@param tbl table<K, V> ---@return { [1]: K, [2]: V }[] _.to_pairs(tbl: table<K, V>) -> { [1]: K, [2]: V }[]
Returns an array of key-value pairs from the table.
-
---@generic K, V ---@param pairs { [1]: K, [2]: V }[] ---@return table<K, V> _.from_pairs(pairs: { [1]: K, [2]: V }[]) -> table<K, V>
Returns a new table created from the array of key-value pairs.
-
---@generic K, V ---@param tbl table<K, V> ---@return table<V, K> _.invert(tbl: table<K, V>) -> table<V, K>
Returns a new table where keys become values and values become keys.
-
---@generic K, V ---@param transforms table<K, fun (value: V): V> ---@param tbl table<K, V> ---@return table<K, V> _.evolve(transforms: table<K, fun (value: V): V>, tbl: table<K, V>) -> table<K, V>
Returns a new table where each value is transformed by its corresponding function in the transforms table.
-
---@generic T : table ---@param left T ---@param right T ---@return T _.merge_left(left: T, right: T) -> T
Returns a new table that is the result of merging the two input tables, with values from the left table overriding those from the right.
-
---@generic K, V ---@param key K ---@param value V ---@param tbl table<K, V> ---@return table<K, V> _.assoc(key: K, value: V, tbl: table<K, V>) -> table<K, V>
Returns a new table with the key set to the specified value.
-
---@generic K, V ---@param key K ---@param tbl table<K, V> ---@return table<K, V> _.dissoc(key: K, tbl: table<K, V>) -> table<K, V>
Returns a new table with the specified key removed.
-
_.is_nil(value: any) -> boolean
Checks if the value is nil.
-
---@param typ type ---@param value any _.is(typ: type, value: any) -> boolean
Checks if the type of the value matches the provided type.