🌴ili
Small javascript utilities.
- Tiny 🦎 in size and no dependencies.
- 100% tree-shakeable 🥥!
- Not curried 🍛 by default, but arguments set up for it.
Install
$ npm install tili
or from a CDN:
Usage
;
or
;
API
object
tili : Kind: global namespace
- tili :
object
- Function
- .compose(...funcs) ⇒
function
- .curry(fn, ...args) ⇒
function
- .curryN(length, fn) ⇒
function
- .debounce(wait, func, [immediate]) ⇒
function
- .defer(func, [...args])
- .delay(wait, func, [...args]) ⇒
number
- .memoize(fn) ⇒
*
- .once(fn) ⇒
function
- .pipe(...funcs) ⇒
function
- .tap(fn, x) ⇒
*
- .throttle(wait, fn, [options]) ⇒
function
- .compose(...funcs) ⇒
- Lang
- .castArray(value) ⇒
Array
- .castArray(value) ⇒
- List
- .flat([depth], list) ⇒
Array
- .includes(search, arr) ⇒
Boolean
- .without(xs, list) ⇒
Array
- .flat([depth], list) ⇒
- Logic
- .defaultTo(d, v) ⇒
*
- .isEmpty(val) ⇒
Boolean
- .defaultTo(d, v) ⇒
- Math
- .round(number, [precision]) ⇒
number
- .round(number, [precision]) ⇒
- Object
- .clone(value) ⇒
*
- .defaultsDeep(target, [...sources]) ⇒
Object
- .get(paths, obj) ⇒
*
- .has(prop, obj) ⇒
Boolean
- .hasPath(path, obj) ⇒
Boolean
- .keys(obj) ⇒
Array
- .merge(target, [...sources]) ⇒
Object
- .omit(names, obj) ⇒
Object
- .path(paths, obj) ⇒
*
- .pick(names, obj) ⇒
Object
- .values(obj) ⇒
Array
- .clone(value) ⇒
- Relation
- .clamp(min, max, value) ⇒
Number
- .clamp(min, max, value) ⇒
- String
- .escape([string]) ⇒
string
- .unescape([string]) ⇒
string
- .escape([string]) ⇒
- Type
- .is(Ctor, val) ⇒
Boolean
- .isPlainObject(obj) ⇒
boolean
- .type(val) ⇒
String
- .is(Ctor, val) ⇒
- Util
- .uniqueId([prefix]) ⇒
string
- .uniqueId([prefix]) ⇒
- Function
function
_.compose(...funcs) ⇒ Composes single-argument functions from right to left. The rightmost function can take multiple arguments as it provides the signature for the resulting composite function.
Kind: static method of tili
Returns: function
- - A function obtained by composing the argument functions
from right to left. For example, compose(f, g, h) is identical to doing
(...args) => f(g(h(...args))).
Category: Function
Since: v0.1.0
Param | Type | Description |
---|---|---|
...funcs | function |
The functions to compose. |
function
_.curry(fn, ...args) ⇒ Curry a function.
Kind: static method of tili
Category: Function
Since: v0.1.0
Param | Type |
---|---|
fn | function |
...args | function |
function
_.curryN(length, fn) ⇒ Returns a curried equivalent of the provided function, with the specified
arity. The curried function has two unusual capabilities. First, its
arguments needn't be provided one at a time. If g
is curryN(3, f)
, the
following are equivalent:
g(1)(2)(3)
g(1)(2, 3)
g(1, 2)(3)
g(1, 2, 3)
Secondly, the special placeholder value __
may be used to specify
"gaps", allowing partial application of any combination of arguments,
regardless of their positions. If g
is as above and _
is __
,
the following are equivalent:
g(1, 2, 3)
g(_, 2, 3)(1)
g(_, _, 3)(1)(2)
g(_, _, 3)(1, 2)
g(_, 2)(1)(3)
g(_, 2)(1, 3)
g(_, 2)(_, 3)(1)
Kind: static method of tili
Returns: function
- A new, curried function.
Category: Function
Sig: Number -> (_ -> a) -> (_ -> a)
See: curry
Since: v0.1.0
Param | Type | Description |
---|---|---|
length | Number |
The arity for the returned function. |
fn | function |
The function to curry. |
Example
const sumArgs = ; const curriedAddFourNumbers = ;const f = ;const g = ;; //=> 10
function
_.debounce(wait, func, [immediate]) ⇒ Returns a function, that, as long as it continues to be invoked, will not
be triggered. The function will be called after it stops being called for
N milliseconds. If immediate
is passed, trigger the function on the
leading edge, instead of the trailing.
Kind: static method of tili
Category: Function
Since: v0.4.0
Param | Type | Default | Description |
---|---|---|---|
wait | Number |
Amount of milliseconds | |
func | function |
||
[immediate] | Boolean |
false |
_.defer(func, [...args])
Defers invoking the func until the current call stack has cleared. Any additional arguments are provided to func when it's invoked.
Kind: static method of tili
Category: Function
See: https://github.com/jamiebuilds/tickedoff
Since: v0.4.0
Param | Type | Description |
---|---|---|
func | function |
Deferred function |
[...args] | * |
Optional arguments |
number
_.delay(wait, func, [...args]) ⇒ Invokes func
after wait
milliseconds. Any additional arguments are
provided to func
when it's invoked.
Kind: static method of tili
Returns: number
- Returns the timer id.
Category: Function
Since: 0.4.0
Param | Type | Description |
---|---|---|
wait | number |
The number of milliseconds to delay invocation. |
func | function |
The function to delay. |
[...args] | * |
The arguments to invoke func with. |
Example
;// => Logs 'later' after one second.
*
_.memoize(fn) ⇒ Memoize a function.
Kind: static method of tili
Category: Function
Since: v0.1.0
Param | Type |
---|---|
fn | function |
function
_.once(fn) ⇒ Accepts a function fn
and returns a function that guards invocation of
fn
such that fn
can only ever be called once, no matter how many times
the returned function is invoked. The first value calculated is returned in
subsequent invocations.
Kind: static method of tili
Returns: function
- The wrapped function.
Category: Function
Sig: (a... -> b) -> (a... -> b)
Since: v0.12.0
Param | Type | Description |
---|---|---|
fn | function |
The function to wrap in a call-only-once wrapper. |
Example
const addOneOnce = ;; //=> 11; //=> 11
function
_.pipe(...funcs) ⇒ Pipes single-argument functions from left to right. The leftmost function can take multiple arguments as it provides the signature for the resulting composite function.
Kind: static method of tili
Returns: function
- - A function obtained by composing the argument functions
from left to right. For example, pipe(f, g, h) is identical to doing
(...args) => h(g(f(...args))).
Category: Function
Since: v0.10.0
Param | Type | Description |
---|---|---|
...funcs | function |
The functions to compose. |
*
_.tap(fn, x) ⇒ Runs the given function with the supplied object, then returns the object.
Kind: static method of tili
Returns: *
- x
.
Category: Function
Sig: (a -> *) -> a -> a
Symb: tap(f, a) = a
Since: v0.3.0
Param | Type | Description |
---|---|---|
fn | function |
The function to call with x . The return value of fn will be thrown away. |
x | * |
Example
const sayX = console;; //=> 100// logs 'x is 100'
function
_.throttle(wait, fn, [options]) ⇒ Throttle a function.
Kind: static method of tili
Category: Function
Since: v0.2.0
Param | Type | Default | Description |
---|---|---|---|
wait | Number |
||
fn | function |
||
[options] | Object |
||
[options.leading] | Boolean |
true |
Trigger a leading function call. |
[options.trailing] | Boolean |
true |
Trigger a trailing function call. |
Array
_.castArray(value) ⇒ Casts value
as an array if it's not one.
Kind: static method of tili
Returns: Array
- Returns the cast array.
Category: Lang
Since: 0.12.0
Param | Type | Description |
---|---|---|
value | * |
The value to inspect. |
Example
_;// => [1] _;// => [{ 'a': 1 }] _;// => ['abc'] _;// => [null] _;// => [undefined] _;// => [] var array = 1 2 3;console;// => true
Array
_.flat([depth], list) ⇒ Returns a new list by pulling every item out of it (and all its sub-arrays) and putting them in a new array, depth-first.
Kind: static method of tili
Returns: Array
- The flattened list.
Category: List
Sig: [a] -> [b]
Since: v0.12.0
Param | Type | Description |
---|---|---|
[depth] | Number |
The flatten depth level. |
list | Array |
The array to consider. |
Example
;//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Boolean
_.includes(search, arr) ⇒ Check if string or array includes the searched part.
Kind: static method of tili
Category: List
Since: v0.1.0
Param | Type |
---|---|
search | * |
arr | Array | String |
Array
_.without(xs, list) ⇒ Returns a new list without values in the first argument.
Acts as a transducer if a transformer is given in list position.
Kind: static method of tili
Returns: Array
- The new array without values in list1
.
Category: List
Sig: [a] -> [a] -> [a]
Since: v0.11.0
Param | Type | Description |
---|---|---|
xs | Array |
The values to be removed from list2 . |
list | Array |
The array to remove values from. |
Example
; //=> [3, 4]
*
_.defaultTo(d, v) ⇒ Default to a value if the passed is null or undefined.
Kind: static method of tili
Category: Logic
Since: v0.1.0
Param | Type | Description |
---|---|---|
d | * |
The default value. |
v | * |
The passed value. |
Boolean
_.isEmpty(val) ⇒ Returns true
if the given value is its type's empty value; false
otherwise.
Kind: static method of tili
Category: Logic
Sig: a -> Boolean
Since: v0.4.0
Param | Type |
---|---|
val | * |
Example
; //=> false; //=> true; //=> true; //=> true; //=> true; //=> false
number
_.round(number, [precision]) ⇒ Computes number
rounded to precision
.
Kind: static method of tili
Returns: number
- Returns the rounded number.
Category: Math
Since: 0.4.0
Param | Type | Default | Description |
---|---|---|---|
number | number |
The number to round. | |
[precision] | number |
0 |
The precision to round to. |
Example
;// => 4 ;// => 4.01 ;// => 4100
*
_.clone(value) ⇒ Creates a deep copy of the value which may contain (nested) Array
s and
Object
s, Number
s, String
s, Boolean
s and Date
s. Function
s are
assigned by reference rather than copied
Dispatches to a clone
method if present.
Kind: static method of tili
Returns: *
- A deeply cloned copy of val
Category: Object
Sig: *
-> {*}
Since: v0.3.0
Param | Type | Description |
---|---|---|
value | * |
The object or array to clone |
Example
const objects = {} {} {};const objectsClone = ;objects === objectsClone; //=> falseobjects0 === objectsClone0; //=> false
Object
_.defaultsDeep(target, [...sources]) ⇒ Deeply assigns own and inherited enumerable string keyed properties of source
objects to the destination object for all destination properties that
resolve to undefined
. Source objects are applied from left to right.
Once a property is set, additional values of the same property are ignored.
Note: This method mutates object
.
Kind: static method of tili
Returns: Object
- Returns object
.
Category: Object
See: defaults
Since: 0.7.0
Param | Type | Description |
---|---|---|
target | Object |
The destination object. |
[...sources] | Object |
The source objects. |
Example
;// => { 'a': { 'b': 2, 'c': 3 } }
*
_.get(paths, obj) ⇒ Get a object value by a string dot path or array path.
Kind: static method of tili
Category: Object
Since: v0.7.0
Param | Type |
---|---|
paths | String | Array |
obj | Object |
Boolean
_.has(prop, obj) ⇒ Returns whether or not an object has an own property with the specified name
Kind: static method of tili
Returns: Boolean
- Whether the property exists.
Category: Object
Sig: s -> {s: x} -> Boolean
Since: v0.11.0
Param | Type | Description |
---|---|---|
prop | String |
The name of the property to check for. |
obj | Object |
The object to query. |
Example
const hasName = 'name';; //=> true; //=> true; //=> false const point = x: 0 y: 0 ;const pointHas = __ point;; //=> true; //=> true; //=> false
Boolean
_.hasPath(path, obj) ⇒ Returns whether or not a path exists in an object. Only the object's own properties are checked.
Kind: static method of tili
Returns: Boolean
- Whether the path exists.
Category: Object
Typedefn: Idx = String | Int
Sig: [Idx] -> {a} -> Boolean
See: has
Since: v0.11.0
Param | Type | Description |
---|---|---|
path | Array |
The path to use. |
obj | Object |
The object to check the path in. |
Example
; // => true; // => true; // => false; // => false
Array
_.keys(obj) ⇒ Returns a list containing the names of all the enumerable own properties of the supplied object. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
Kind: static method of tili
Returns: Array
- An array of the object's own properties.
Category: Object
Sig: k: v
-> [k]
See: values
Since: v0.11.0
Param | Type | Description |
---|---|---|
obj | Object |
The object to extract properties from |
Example
; //=> ['a', 'b', 'c']
Object
_.merge(target, [...sources]) ⇒ This method is like assign
except that it recursively merges own and
inherited enumerable string keyed properties of source objects into the
destination object. Source properties that resolve to undefined
are
skipped if a destination value exists. Array and plain object properties
are merged recursively. Other objects and value types are overridden by
assignment. Source objects are applied from left to right. Subsequent
sources overwrite property assignments of previous sources.
Note: This method mutates target
.
Kind: static method of tili
Returns: Object
- Returns object
.
Category: Object
Since: 0.4.0
Param | Type | Description |
---|---|---|
target | Object |
The destination object. |
[...sources] | Object |
The source objects. |
Example
const object = a: b: 2 d: 4 ; const other = a: c: 3 e: 5 ; ;// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
Object
_.omit(names, obj) ⇒ Returns a partial copy of an object omitting the keys specified.
Kind: static method of tili
Returns: Object
- A new object with properties from names
not on it.
Category: Object
Sig: [String] -> {String: _} -> {String: _}
See: pick
Since: v0.3.0
Param | Type | Description |
---|---|---|
names | Array |
an array of String property names to omit from the new object |
obj | Object |
The object to copy from |
Example
; //=> {b: 2, c: 3}
*
_.path(paths, obj) ⇒ Retrieve the value at a given path.
Kind: static method of tili
Returns: *
- The data at path
.
Category: Object
Typedefn: Idx = String | Int
Sig: [Idx] -> {a} -> a | Undefined
Since: v0.1.0
Param | Type | Description |
---|---|---|
paths | Array |
The path to use. |
obj | Object |
The object to retrieve the nested property from. |
Example
; //=> 2; //=> undefined
Object
_.pick(names, obj) ⇒ Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored.
Kind: static method of tili
Returns: Object
- A new object with only properties from names
on it.
Category: Object
Sig: [k] -> {k: v} -> {k: v}
See: omit
Since: v0.3.0
Param | Type | Description |
---|---|---|
names | Array |
an array of String property names to copy onto a new object |
obj | Object |
The object to copy from |
Example
; //=> {a: 1, d: 4}; //=> {a: 1}
Array
_.values(obj) ⇒ Returns a list of all the enumerable own properties of the supplied object. Note that the order of the output array is not guaranteed across different JS platforms.
Kind: static method of tili
Returns: Array
- An array of the values of the object's own properties.
Category: Object
Sig: k: v
-> [v]
Since: v0.6.0
Param | Type | Description |
---|---|---|
obj | Object |
The object to extract values from |
Example
; //=> [1, 2, 3]
Number
_.clamp(min, max, value) ⇒ Restricts a number to be within a range.
Also works for other ordered types such as Strings and Dates.
Kind: static method of tili
Returns: Number
- Returns minimum
when val < minimum
, maximum
when val > maximum
, returns val
otherwise
Category: Relation
Sig: Ord a => a -> a -> a -> a
Since: v0.4.0
Param | Type | Description |
---|---|---|
min | Number |
The lower limit of the clamp (inclusive) |
max | Number |
The upper limit of the clamp (inclusive) |
value | Number |
Value to be clamped |
Example
; // => 1; // => 10; // => 4
string
_.escape([string]) ⇒ Converts the characters "&", "<", ">", '"', and "'" in string
to their
corresponding HTML entities.
Note: No other characters are escaped. To escape additional characters use a third-party library like he.
Though the ">" character is escaped for symmetry, characters like ">" and "/" don't need escaping in HTML and have no special meaning unless they're part of a tag or unquoted attribute value. See Mathias Bynens's article (under "semi-related fun fact") for more details.
When working with HTML you should always quote attribute values to reduce XSS vectors.
Kind: static method of tili
Returns: string
- Returns the escaped string.
Category: String
See: escapeRegExp, unescape
Since: 0.7.0
Param | Type | Default | Description |
---|---|---|---|
[string] | string |
"''" |
The string to escape. |
Example
;// => 'fred, barney, & pebbles'
string
_.unescape([string]) ⇒ The inverse of escape
this method converts the HTML entities
&
, <
, >
, "
and '
in string
to
their corresponding characters.
Note: No other HTML entities are unescaped. To unescape additional HTML entities use a third-party library like he.
Kind: static method of tili
Returns: string
- Returns the unescaped string.
Category: String
See: escape, escapeRegExp
Since: 0.7.0
Param | Type | Default | Description |
---|---|---|---|
[string] | string |
"''" |
The string to unescape. |
Example
;// => 'fred, barney, & pebbles'
Boolean
_.is(Ctor, val) ⇒ See if an object (val
) is an instance of the supplied constructor. This
function will check up the inheritance chain, if any.
Kind: static method of tili
Category: Type
Sig: (_ -> {_}) -> a -> Boolean
Since: v0.1.0
Param | Type | Description |
---|---|---|
Ctor | Object |
A constructor |
val | * |
The value to test |
Example
; //=> true; //=> true; //=> false; //=> true; //=> true; //=> true; //=> false; //=> false
boolean
_.isPlainObject(obj) ⇒ Checks if value
is a plain object, that is, an object created by the
Object
constructor or one with a [[Prototype]]
of null
.
Kind: static method of tili
Returns: boolean
- Returns true
if value
is a plain object, else false
.
Category: Type
Since: 0.1.0
Param | Type | Description |
---|---|---|
obj | * |
The value to check. |
Example
{ thisa = 1;} ;// => false ;// => false ;// => true ;// => true
String
_.type(val) ⇒ Gives a single-word string description of the (native) type of a value, returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not attempt to distinguish user Object types any further, reporting them all as 'Object'.
Kind: static method of tili
Category: Type
Sig: (_ -> {_}) -> String
Since: v0.3.0
Param | Type | Description |
---|---|---|
val | * |
The value to test |
Example
; //=> "Object"; //=> "Number"; //=> "Boolean"; //=> "String"; //=> "Null"; //=> "Array"; //=> "RegExp"; //=> "Function"; //=> "Undefined"
string
_.uniqueId([prefix]) ⇒ Generates a unique ID. If prefix
is given, the ID is appended to it.
Kind: static method of tili
Returns: string
- Returns the unique ID.
Category: Util
Since: 0.1.0
Param | Type | Default | Description |
---|---|---|---|
[prefix] | string |
"''" |
The value to prefix the ID with. |
Example
;// => 'contact_104' ;// => '105'
Credits
Some code and most naming is borrowed from the following popular JS utility libraries but lots of code is rewritten to be as lightweight and modular as possible.