Don't mind me -- yet another (functional) lodash implementation
Add with your favorite (of these two) package managers.
$ yarn add -E lyaor
$ npm i -E -S lyaImport all or some of it.
// Load with import statement
import _ from 'lya'
// Load as CommonJS module
var _ = require('lya')
// Load a submodule
var map = require('lya/map')All functions are curried.
All functions take the collection/value to be altered as the last argument.
All functions love to be used with flow.
- assign(fromCollection, toCollection) ⇒
Object Assign keys from left to right.
- clone(collection) ⇒
Object|Array Get shallow clone of collection.
- concat(value, array) ⇒
Array|string Append value or array to array, or value to string
- endsWith(searchString, string) ⇒
boolean Checks if string ends with searchString
- filter(iteratee, array) ⇒
Array Filters array (keeps elements) by iteratee (function or path).
- first(array) ⇒
* Gets first element of array.
- flow(...predicates, value) ⇒
* Threads a value through a series of functions. If the last argument is not a function, it's applied as the value.
- get(path, collection) ⇒
* Gets value from (nested) path in a collection.
- getOr(defaultValue, path, collection) ⇒
* Gets value from (nested) path in a collection, falls back on default value.
- identity(value) ⇒
* Takes a value and returns the same value.
- includes(value, collection) ⇒
boolean Check whether collection includes some
value.- indexOf(value, array) ⇒
integer Search for index of value in array or string.
- isEmpty(value) ⇒
boolean Checks if a value is empty.
- isObject(value) ⇒
boolean Determines if a value is an object. An object is something with type 'object' that isn't an array or function
- join(separator, array) ⇒
string Joins elements of array together with separator between each.
- keys(collection) ⇒
Array Get keys of collection.
- last(array) ⇒
* Gets last element of array.
- map(iteratee, array) ⇒
Array Map over array calling iteratee on each value.
- mapValues(iteratee, object) ⇒
Object Map over object calling iteratee on each value.
- match(regexp, string) ⇒
Array Get result of matching
stringagainstregexp- negate(predicate) ⇒
function Negates a function.
- notEmpty(value) ⇒
boolean Checks if a value is not empty. Inverse of
isEmpty.- nth(index, array) ⇒
* Get nth element in array. If index is negative, it gets the nth last element.
- reduce(iteratee, accumulator, array) ⇒
* Reduces array into a new value. It calls the iteratee with each element in the array, providing the result as the accumulator in the following iteration.
- reject(iteratee, array) ⇒
Array Rejects elements of array by running each though iteratee.
- replace(regexp, replacement, string) ⇒
string Replace some or all matches with replacement pattern.
- set(path, value, object) ⇒
Object Set path of object to value returning the copy
- slice(start, end, array) ⇒
Array Slice array returning dense array.
- sortBy(iteratee, array) ⇒
Array Sort array using iteratee to compare elements.
- split(delimeter, string) ⇒
Array Split string into array by delimeter.
- startsWith(searchString, string) ⇒
boolean Checks if string starts with searchString
- substring(startIndex, endIndex, string) ⇒
string Exracts substring from startIndex to endIndex (not included).
- toLowerCase(string) ⇒
string Converts string to lower case.
- toUpperCase(string) ⇒
string Converts string to upper case.
- trim(string) ⇒
string Trim string by removing whitespace from left and right.
- trimLeft(string) ⇒
string Trim string by removing whitespace from left.
- trimRight(string) ⇒
string Trim string by removing whitespace from right.
- update(path, predicate, object) ⇒
Object Update path of object to result of update function on existing value returning the copy
- values(collection) ⇒
Array Get values of collection.
Assign keys from left to right.
Returns: Object - Returns copy of toCollection overwritten by fromCollection.
Since: 0.2.0
| Param | Type | Description |
|---|---|---|
| fromCollection | Object |
Source collection |
| toCollection | Object |
Target collection |
Example
assign({ a: 1 }, { a: 4, b: 2 }) // => { a: 1, b: 2 }Get shallow clone of collection.
Returns: Object | Array - Returns shallow clone of collection
Since: 0.2.0 - array clones introduced in 0.3.0
| Param | Type | Description |
|---|---|---|
| collection | Object | Array |
Collection to clone |
Example
var obj = { a: 1 }
var res = clone(obj) // => res = { a: 1 }, obj != resExample
var arr = [1, 2, 3]
var res = clone(arr) // => res = [1, 2, 3], obj != resAppend value or array to array, or value to string
Returns: Array | string - Returns concatenated array or string
See: https://mdn.io/concat
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| value | Array | * |
value or array to append to array or string |
| array | Array | string |
string or array to expand |
Example
concat(4, [1, 2, 3]) // => [1, 2, 3, 4]Example
concat([4, 5], [1, 2, 3]) // => [1, 2, 3, 4, 5]Example
concat('def', 'abc') // => 'abcdef'Example
concat('def', ['abc']) // => ['abc', 'def']Checks if string ends with searchString
Returns: boolean - Returns true if string ends with searchString, false otherwise
See: https://mdn.io/endsWith
Since: 0.5.0
| Param | Type | Description |
|---|---|---|
| searchString | string |
Substring to search for |
| string | string |
String to check |
Example
endsWith('efg', 'abcdefg') // => trueFilters array (keeps elements) by iteratee (function or path).
Returns: Array - Returns filtered copy of array
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| iteratee | string | function |
function or path to keep |
| array | Array |
array to filter |
Example
filter('a', [{ a: true }, { a: false }]) // => [{ a: true }]Example
filter(v => v > 0)([-1, 0, 1, 2]) // => [1, 2]Gets first element of array.
Returns: * - Returns first element of array
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| array | Array |
array to get first element from |
Example
first([1, 2, 3]) // => 1Threads a value through a series of functions. If the last argument is not a function, it's applied as the value.
Returns: * - Returns value run though all the functions
Since: 0.1.0
| Param | Type | Description |
|---|---|---|
| ...predicates | function |
Functions to apply (left to right) |
| value | * |
Example
flow(x => x + 1, 1) // => 2Example
flow(x => x + 1)(1) // => 2Example
flow(x => x + 1, x => x + 1, 1) // => 3Gets value from (nested) path in a collection.
Returns: * - Returns value if found, undefined otherwise
Since: 0.1.0
| Param | Type | Description |
|---|---|---|
| path | string | Array.<string> |
dot-string or string-array denoting path |
| collection | Object | Array |
collection to get value from |
Example
get('a.b', { a: { b: 42 } }) // => 42Example
get(['a', 'b'], { a: { b: 42 } }) // => 42Example
get('a.1', { a: [1, 2] }) // => 2Gets value from (nested) path in a collection, falls back on default value.
Returns: * - Returns value if found, defaultValue otherwise
Since: 0.6.0
| Param | Type | Description |
|---|---|---|
| defaultValue | * |
value to return if nothing found at path |
| path | string | Array.<string> |
dot-string or string-array denoting path |
| collection | Object | Array |
collection to get value from |
Example
getOr('foo', 'a.b', { a: { b: 42 } }) // => 42Example
getOr('foo', 'a.z', { a: { b: 42 } }) // => fooExample
getOr('foo', 'a.b', { a: { b: undefined } }) // => fooExample
getOr('foo', ['a', 'b'], { a: { b: 42 } }) // => 42Example
getOr('foo', 'a.1', { a: [1, 2] }) // => 2Takes a value and returns the same value.
Returns: * - Returns value
Since: 0.1.0
| Param | Type |
|---|---|
| value | * |
Example
identity(2) // => 2Example
identity(() => 5) // => () => 5Check whether collection includes some value.
Returns: boolean - Returns true if collection includes value
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| value | * |
value to find |
| collection | Array | Object |
array or object to search for value in |
Example
includes(1, [1, 2]) // => trueExample
includes(1, { a: 1, b: 2 }) // => trueSearch for index of value in array or string.
Returns: integer - Returns index of value in array or string, -1 if not found
See: https://mdn.io/indexOf
Since: 0.5.0
| Param | Type | Description |
|---|---|---|
| value | string | * |
value to search for |
| array | Array | string |
string or array to search in |
Example
indexOf(3, [1, 2, 3]) // => 2Example
indexOf('c', 'abc') // => 2Example
indexOf('d', 'abc') // => -1Checks if a value is empty.
Returns: boolean - Returns true if empty, false otherwise
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| value | * |
Value to check |
Example
isEmpty({}) // => trueExample
isEmpty([]) // => trueExample
isEmpty(123) // => trueExample
isEmpty('') // => trueExample
isEmpty('foo') // => falseDetermines if a value is an object. An object is something with type 'object' that isn't an array or function
Returns: boolean - Returns true if value is an object according to Lya's ad-hoc definition
Since: 0.2.0
| Param | Type |
|---|---|
| value | * |
Example
isObject({}) // => trueExample
isObject(new Date()) // => trueExample
isObject([1, 2, 3]) // => falseJoins elements of array together with separator between each.
Returns: string - String joined together with separator
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
Since: 0.3.0
| Param | Type |
|---|---|
| separator | string |
| array | Array |
Example
join('-', [1, 2, 3]) // => '1-2-3'Get keys of collection.
Returns: Array - Returns array of collection keys
Since: 0.2.0
| Param | Type | Description |
|---|---|---|
| collection | Array | Object |
object or array to get keys from |
Example
keys({ a: 123 }) // => ['a']Example
keys([1, 2, 3]) // => ['0', '1', '2']Gets last element of array.
Returns: * - Return last element of array
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| array | Array |
array to get last element from |
Example
last([1, 2, 3]) // => 3Map over array calling iteratee on each value.
Since: 0.1.0
| Param | Type |
|---|---|
| iteratee | String | function |
| array | Array |
Example
map(x => x + 1, [1, 2, 3]) // => [2, 3, 4]Example
map('x', [{ x: 1 }, { x: 2, y: 3 }]) // => [1, 2]Map over object calling iteratee on each value.
Since: 0.1.0
| Param | Type |
|---|---|
| iteratee | String | function |
| object | Object |
Example
mapValues(x => x + 1, { a: 10, b: 10 }) // => { a: 11, b: 11 }Get result of matching string against regexp
Returns: Array - Returns array of matches
See: https://mdn.io/match
Since: 0.5.0
| Param | Type | Description |
|---|---|---|
| regexp | RegExp |
regular expression |
| string | string |
Example
match(/foo/g, 'foobarfoo') // => ['foo', 'foo']Negates a function.
Returns: function - Returns function that calls the `predicate function and negates the result.
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| predicate | function |
Function to negate |
Example
var notTrue = negate(() => true)
notTrue() // => falseChecks if a value is not empty. Inverse of isEmpty.
Returns: boolean - Returns true if not empty, false if empty
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| value | * |
Value to check |
Example
notEmpty({}) // => falseExample
notEmpty[1, 2, 3] // => trueExample
notEmpty('foo') // => trueGet nth element in array. If index is negative, it gets the nth last element.
Returns: * - Value at nth index starting from 0
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| index | integer |
index in array to grab |
| array | * |
to grab value from |
Example
nth(1, [6, 7, 8]) // => 7Example
nth(5, [6, 7, 8]) // => undefinedExample
nth(-1, [6, 7, 8]) // => 8Reduces array into a new value. It calls the iteratee with each element in the array, providing the result as the accumulator in the following iteration.
Since: 0.1.0
| Param | Type |
|---|---|
| iteratee | function |
| accumulator | * |
| array | Array |
Example
reduce(
(acc, current) => acc + current, // sum function
0,
[1, 2, 3]
) // => 6Rejects elements of array by running each though iteratee.
Returns: Array - Returns copy of array with rejected elements removed
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| iteratee | string | function |
function or path to reject |
| array | Array |
array to reject |
Example
reject('a', [{ a: true }, { a: false }]) // => [{ a: false }]Example
reject(v => v > 0)([-1, 0, 1, 2]) // => [-1, 0]Replace some or all matches with replacement pattern.
Returns: string - Returns replaced string
See: https://mdn.io/replace
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| regexp | RegExp | string |
RegExp literal or string to replace |
| replacement | string | function |
replacement pattern |
| string | string |
string to run replace on |
Example
replace(/a/i, 'b', 'a-a-a') // => 'b-a-a'Example
replace(/a/gi, 'b', 'a-a-a') // => 'b-b-b'Set path of object to value returning the copy
Returns: Object - Returns copy of object with value set at path
Since: 0.2.0
| Param | Type | Description |
|---|---|---|
| path | Array | string |
path to set |
| value | * |
value to set at path |
| object | Object |
object to set value in |
Example
set('a.b', 2, { a: { b: 1 } }) // => { a: { b: 2 } }Slice array returning dense array.
Returns: Array - Returns densely sliced array
See: https://github.com/lodash/lodash/blob/master/slice.js
Since: 0.1.0
| Param | Type | Description |
|---|---|---|
| start | number |
first index |
| end | number |
last index |
| array | Array |
array to slice |
Example
slice(0, 0, [1, 2, 3]) // => []Example
slice(0)(0)([1, 2, 3]) // => []Example
slice(0, -1, [1, 2, 3]) // => [1, 2]Sort array using iteratee to compare elements.
Returns: Array - Returns array sorted by iteratee
See: https://mdn.io/sort
Since: 0.3.0
| Param | Type | Description |
|---|---|---|
| iteratee | string | function |
string or function to sort by |
| array | Array |
array to sort |
Example
sortBy(identity, ['c', 'a', 'b']) // => ['a', 'b', 'c']Example
sortBy('a', [{ a: 1 }, { a: 9 }, { a: 5 }]) // => [{ a: 1 }, { a: 5 }, { a: 9 }]Split string into array by delimeter.
Returns: Array - array of the split string
See: https://mdn.io/split
Since: 0.1.0
| Param | Type |
|---|---|
| delimeter | string |
| string | string |
Example
split('-', '1-2-3') // => ['1', '2', '3']Example
split('-')('1-2-3') // => ['1', '2', '3']Checks if string starts with searchString
Returns: boolean - Returns true if string starts with searchString, false otherwise
See: https://mdn.io/startsWith
Since: 0.5.0
| Param | Type | Description |
|---|---|---|
| searchString | string |
Substring to search for |
| string | string |
String to check |
Example
startsWith('abc', 'abcdefg') // => trueExracts substring from startIndex to endIndex (not included).
Returns: string - Returns substring of string
See: https://mdn.io/substring
Since: 0.5.0
| Param | Type |
|---|---|
| startIndex | integer |
| endIndex | integer |
| string | string |
Example
substring(0, 2, 'abcde') // => 'ab'Example
substring(0, undefined, 'abcde') // => 'abcde'Converts string to lower case.
Returns: string - Returns string converted to lower case
See: https://mdn.io/toLowerCase
Since: 0.5.0
| Param | Type |
|---|---|
| string | string |
Example
toLowerCase('aAbBcC') // => 'aabbcc'Converts string to upper case.
Returns: string - Returns string converted to upper case
See: https://mdn.io/toUpperCase
Since: 0.5.0
| Param | Type |
|---|---|
| string | string |
Example
toUpperCase('aAbBcC') // => 'aabbcc'Trim string by removing whitespace from left and right.
Returns: string - String with whitespace removed from left and right
See: https://mdn.io/trim
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| string | string |
string to trim |
Example
trim(' a ') // => 'a'Trim string by removing whitespace from left.
Returns: string - Returns string with whitespace removed from left
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| string | string |
string to trim |
Example
trim(' a ') // => 'a 'Trim string by removing whitespace from right.
Returns: string - Returns string with whitespace removed from right
Since: 0.4.0
| Param | Type | Description |
|---|---|---|
| string | string |
string to trim |
Example
trim(' a ') // => ' a'Update path of object to result of update function on existing value returning the copy
Returns: Object - Returns copy of object with predicate applied at path
Since: 0.5.0
| Param | Type | Description |
|---|---|---|
| path | Array | string |
path to update |
| predicate | function | * |
function or value to apply at path |
| object | Object |
object to update path in |
Example
update('a.b', v => v + 10, { a: { b: 1 } }) // => { a: { b: 10 } }Example
update('a.c', v => 10, { a: { b: 1 } }) // => { a: { b: 1, c: 10 } }Get values of collection.
Returns: Array - Returns array of values
Since: 0.2.0
| Param | Type | Description |
|---|---|---|
| collection | Array | Object |
object or array to get values from |
Example
values({ a: 123 }) // => [123]Example
values([1, 2, 3]) // => [1, 2, 3]© plougsgaard