π΄ Small javascript utilities.
MIT License
Small javascript utilities.
$ npm install tili
or from a CDN:
<script src="//unpkg.com/tili@latest/dist/tili.min.js"></script>
import * as _ from 'tili';
or
import { get, compose, merge } from 'tili';
Kind: global namespace
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.
Note: The result of compose is not automatically curried.
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 |
---|---|---|
...fns | function | The functions to compose. |
Curry a function.
Kind: static method of tili Category: Function Since: v0.1.0
Param | Type |
---|---|
fn | function |
...args | 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
Memoize a function.
Kind: static method of tili Category: Function Since: v0.1.0
Param | Type |
---|---|
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
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.
Note: The result of pipe is not automatically curried.
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 |
---|---|---|
...fns | function | The functions to compose. |
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'
Returns a new array 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 array. Category: List Sig: [a] -> [b] Since: v0.12.0
Param | Type | Description |
---|---|---|
depth | Number | The flatten depth level. |
arr | Array | The array to consider. |
Example
flat(10, [1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
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.13.0
Param | Type | Description |
---|---|---|
list | Array | The array to consider. |
Example
flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
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 |
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]
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 |
---|---|---|
def | * | The default value. |
value | * | The passed value. |
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
Computes number
rounded to precision
.
Kind: static method of tili Returns: number - Returns the rounded number. Category: Math Since: 0.4.0
Param | Type | Description |
---|---|---|
precision | number | The precision to round to. |
number | number | The number to round. |
Example
round(0, 4.006);
// => 4
round(2, 4.006);
// => 4.01
round(-2, 4060);
// => 4100
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
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 |
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 = has('name');
hasName({ name: 'alice' }); //=> true
hasName({ name: 'bob' }); //=> true
hasName({}); //=> false
const point = { x: 0, y: 0 };
const pointHas = has(__, point);
pointHas('x'); //=> true
pointHas('y'); //=> true
pointHas('z'); //=> false
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
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']
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}
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
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}
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]
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
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'
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'
If the provided value
is an array returns a copy of it otherwise returns an array containing the original value
.
Kind: static constant of tili Returns: Array - Returns the cast array. Category: Type 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]
var array = [1, 2, 3];
console.log(_.castArray(array) === array);
// => false
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 |
value | * | 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
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
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"
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 | Description |
---|---|---|
prefix | string | The value to prefix the ID with. |
Example
uniqueId('contact_');
// => 'contact_104'
uniqueId();
// => '105'
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.