lya
v0.5.0
Published
A JS utility belt tailored towards functional programming
Downloads
1
Readme
lya
Don't mind me -- yet another (functional) lodash implementation
Getting started
Add with your favorite (of these two) package managers.
$ yarn add -E lya
or
$ npm i -E -S lya
Import 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')
Guiding Principles
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
.
API
Functions
assign(fromCollection, toCollection) ⇒ Object
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 }
clone(collection) ⇒ Object | Array
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 != res
Example
var arr = [1, 2, 3]
var res = clone(arr) // => res = [1, 2, 3], obj != res
concat(value, array) ⇒ Array | string
Append 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']
endsWith(searchString, string) ⇒ boolean
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') // => true
filter(iteratee, array) ⇒ Array
Filters 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]
first(array) ⇒ *
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]) // => 1
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.
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) // => 2
Example
flow(x => x + 1)(1) // => 2
Example
flow(x => x + 1, x => x + 1, 1) // => 3
get(path, collection) ⇒ *
Gets 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 } }) // => 42
Example
get(['a', 'b'], { a: { b: 42 } }) // => 42
Example
get('a.1', { a: [1, 2] }) // => 2
identity(value) ⇒ *
Takes a value and returns the same value.
Returns: * - Returns value
Since: 0.1.0
| Param | Type | | --- | --- | | value | * |
Example
identity(2) // => 2
Example
identity(() => 5) // => () => 5
includes(value, collection) ⇒ boolean
Check 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]) // => true
Example
includes(1, { a: 1, b: 2 }) // => true
indexOf(value, array) ⇒ integer
Search 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]) // => 2
Example
indexOf('c', 'abc') // => 2
Example
indexOf('d', 'abc') // => -1
isEmpty(value) ⇒ boolean
Checks 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({}) // => true
Example
isEmpty([]) // => true
Example
isEmpty(123) // => true
Example
isEmpty('') // => true
Example
isEmpty('foo') // => false
isObject(value) ⇒ boolean
Determines 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({}) // => true
Example
isObject(new Date()) // => true
Example
isObject([1, 2, 3]) // => false
join(separator, array) ⇒ string
Joins 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'
keys(collection) ⇒ Array
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']
last(array) ⇒ *
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]) // => 3
map(iteratee, array) ⇒ Array
Map 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]
mapValues(iteratee, object) ⇒ Object
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 }
match(regexp, string) ⇒ Array
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']
negate(predicate) ⇒ function
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() // => false
notEmpty(value) ⇒ boolean
Checks 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({}) // => false
Example
notEmpty[1, 2, 3] // => true
Example
notEmpty('foo') // => true
nth(index, array) ⇒ *
Get 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]) // => 7
Example
nth(5, [6, 7, 8]) // => undefined
Example
nth(-1, [6, 7, 8]) // => 8
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.
Since: 0.1.0
| Param | Type | | --- | --- | | iteratee | function | | accumulator | * | | array | Array |
Example
reduce(
(acc, current) => acc + current, // sum function
0,
[1, 2, 3]
) // => 6
reject(iteratee, array) ⇒ Array
Rejects 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(regexp, replacement, string) ⇒ string
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, value, object) ⇒ Object
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(start, end, array) ⇒ Array
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]
sortBy(iteratee, array) ⇒ Array
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(delimeter, string) ⇒ Array
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']
startsWith(searchString, string) ⇒ boolean
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') // => true
substring(startIndex, endIndex, string) ⇒ string
Exracts 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'
toLowerCase(string) ⇒ string
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'
toUpperCase(string) ⇒ string
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) ⇒ string
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'
trimLeft(string) ⇒ string
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 '
trimRight(string) ⇒ string
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, predicate, object) ⇒ Object
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 } }
values(collection) ⇒ Array
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