tili v0.12.0
π΄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:
<script src="//unpkg.com/tili@latest/dist/tili.min.js"></script>
Usage
import * as _ from 'tili';
or
import { get, compose, merge } from 'tili';
API
tili : object
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
- Lang
- .castArray(value) β Array
- List
- .flat([depth], list) β Array
- .includes(search, arr) β Boolean
- .without(xs, list) β Array
- Logic
- .defaultTo(d, v) β *
- .isEmpty(val) β Boolean
- Math
- .round(number, [precision]) β number
- 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
- Relation
- .clamp(min, max, value) β Number
- String
- .escape([string]) β string
- .unescape([string]) β string
- Type
- .is(Ctor, val) β Boolean
- .isPlainObject(obj) β boolean
- .type(val) β String
- Util
- .uniqueId([prefix]) β string
- Function
_.compose(...funcs) β function
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. |
_.curry(fn, ...args) β function
Curry a function.
Kind: static method of tili
Category: Function
Since: v0.1.0
Param | Type |
---|---|
fn | function |
...args | function |
_.curryN(length, fn) β function
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 = (...args) => sum(args);
const curriedAddFourNumbers = curryN(4, sumArgs);
const f = curriedAddFourNumbers(1, 2);
const g = f(3);
g(4); //=> 10
_.debounce(wait, func, immediate) β function
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 |
_.delay(wait, func, ...args) β number
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
delay(text => console.log(text), 1000, 'later');
// => 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 |
_.once(fn) β function
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 = once(x => x + 1);
addOneOnce(10); //=> 11
addOneOnce(addOneOnce(50)); //=> 11
_.pipe(...funcs) β function
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 = x => console.log('x is ' + x);
tap(sayX, 100); //=> 100
// logs 'x is 100'
_.throttle(wait, fn, options) β function
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. |
_.castArray(value) β Array
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
_.castArray(1);
// => [1]
_.castArray({ a: 1 });
// => [{ 'a': 1 }]
_.castArray('abc');
// => ['abc']
_.castArray(null);
// => [null]
_.castArray(undefined);
// => [undefined]
_.castArray();
// => []
var array = [1, 2, 3];
console.log(_.castArray(array) === array);
// => true
_.flat(depth, list) β Array
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
flat([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
_.includes(search, arr) β Boolean
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 |
_.without(xs, list) β Array
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
without([1, 2], [1, 2, 1, 3, 4]); //=> [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. |
_.isEmpty(val) β Boolean
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
isEmpty([1, 2, 3]); //=> false
isEmpty([]); //=> true
isEmpty(''); //=> true
isEmpty(null); //=> true
isEmpty({}); //=> true
isEmpty({ length: 0 }); //=> false
_.round(number, precision) β number
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
round(4.006);
// => 4
round(4.006, 2);
// => 4.01
round(4060, -2);
// => 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 = clone(objects);
objects === objectsClone; //=> false
objects[0] === objectsClone[0]; //=> false
_.defaultsDeep(target, ...sources) β Object
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
defaultsDeep({ a: { b: 2 } }, { a: { b: 1, c: 3 } });
// => { '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 |
_.has(prop, obj) β Boolean
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 = curry(has)('name');
hasName({ name: 'alice' }); //=> true
hasName({ name: 'bob' }); //=> true
hasName({}); //=> false
const point = { x: 0, y: 0 };
const pointHas = curry(has)(__, point);
pointHas('x'); //=> true
pointHas('y'); //=> true
pointHas('z'); //=> false
_.hasPath(path, obj) β Boolean
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
hasPath(['a', 'b'], { a: { b: 2 } }); // => true
hasPath(['a', 'b'], { a: { b: undefined } }); // => true
hasPath(['a', 'b'], { a: { c: 2 } }); // => false
hasPath(['a', 'b'], {}); // => false
_.keys(obj) β Array
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
keys({ a: 1, b: 2, c: 3 }); //=> ['a', 'b', 'c']
_.merge(target, ...sources) β Object
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 }]
};
merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
_.omit(names, obj) β Object
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
omit(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }); //=> {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
path(['a', 'b'], { a: { b: 2 } }); //=> 2
path(['a', 'b'], { c: { b: 2 } }); //=> undefined
_.pick(names, obj) β Object
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
pick(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }); //=> {a: 1, d: 4}
pick(['a', 'e', 'f'], { a: 1, b: 2, c: 3, d: 4 }); //=> {a: 1}
_.values(obj) β Array
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
values({ a: 1, b: 2, c: 3 }); //=> [1, 2, 3]
_.clamp(min, max, value) β Number
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
clamp(1, 10, -5); // => 1
clamp(1, 10, 15); // => 10
clamp(1, 10, 4); // => 4
_.escape(string) β 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
escape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
_.unescape(string) β 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
unescape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
_.is(Ctor, val) β Boolean
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
is(Object, {}); //=> true
is(Number, 1); //=> true
is(Object, 1); //=> false
is(String, 's'); //=> true
is(String, new String('')); //=> true
is(Object, new String('')); //=> true
is(Object, 's'); //=> false
is(Number, {}); //=> false
_.isPlainObject(obj) β boolean
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
function Foo() {
this.a = 1;
}
isPlainObject(new Foo());
// => false
isPlainObject([1, 2, 3]);
// => false
isPlainObject({ x: 0, y: 0 });
// => true
isPlainObject(Object.create(null));
// => true
_.type(val) β String
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
type({}); //=> "Object"
type(1); //=> "Number"
type(false); //=> "Boolean"
type('s'); //=> "String"
type(null); //=> "Null"
type([]); //=> "Array"
type(/[A-z]/); //=> "RegExp"
type(() => {}); //=> "Function"
type(undefined); //=> "Undefined"
_.uniqueId(prefix) β string
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
uniqueId('contact_');
// => 'contact_104'
uniqueId();
// => '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.