0.12.0 β€’ Published 7 years ago

tili v0.12.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

🌴ili

Build Status codecov npm.io code style: prettier

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


_.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

ParamTypeDescription
...funcsfunctionThe functions to compose.

_.curry(fn, ...args) β‡’ function

Curry a function.

Kind: static method of tili
Category: Function
Since: v0.1.0

ParamType
fnfunction
...argsfunction

_.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

ParamTypeDescription
lengthNumberThe arity for the returned function.
fnfunctionThe 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

ParamTypeDefaultDescription
waitNumberAmount of milliseconds
funcfunction
immediateBooleanfalse

_.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

ParamTypeDescription
funcfunctionDeferred 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

ParamTypeDescription
waitnumberThe number of milliseconds to delay invocation.
funcfunctionThe 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

ParamType
fnfunction

_.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

ParamTypeDescription
fnfunctionThe 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

ParamTypeDescription
...funcsfunctionThe 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

ParamTypeDescription
fnfunctionThe 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

ParamTypeDefaultDescription
waitNumber
fnfunction
optionsObject
options.leadingBooleantrueTrigger a leading function call.
options.trailingBooleantrueTrigger 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

ParamTypeDescription
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

ParamTypeDescription
depthNumberThe flatten depth level.
listArrayThe 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

ParamType
search*
arrArray | 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

ParamTypeDescription
xsArrayThe values to be removed from list2.
listArrayThe 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

ParamTypeDescription
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

ParamType
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

ParamTypeDefaultDescription
numbernumberThe number to round.
precisionnumber0The 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) Arrays and Objects, Numbers, Strings, Booleans and Dates. Functions 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

ParamTypeDescription
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

ParamTypeDescription
targetObjectThe destination object.
...sourcesObjectThe 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

ParamType
pathsString | Array
objObject

_.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

ParamTypeDescription
propStringThe name of the property to check for.
objObjectThe 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

ParamTypeDescription
pathArrayThe path to use.
objObjectThe 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

ParamTypeDescription
objObjectThe 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

ParamTypeDescription
targetObjectThe destination object.
...sourcesObjectThe 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

ParamTypeDescription
namesArrayan array of String property names to omit from the new object
objObjectThe 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

ParamTypeDescription
pathsArrayThe path to use.
objObjectThe 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

ParamTypeDescription
namesArrayan array of String property names to copy onto a new object
objObjectThe 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

ParamTypeDescription
objObjectThe 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

ParamTypeDescription
minNumberThe lower limit of the clamp (inclusive)
maxNumberThe upper limit of the clamp (inclusive)
valueNumberValue 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

ParamTypeDefaultDescription
stringstring"''"The string to escape.

Example

escape('fred, barney, & pebbles');
// => 'fred, barney, &amp; pebbles'

_.unescape(string) β‡’ string

The inverse of escapethis method converts the HTML entities &amp;, &lt;, &gt;, &quot; and &#39; 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

ParamTypeDefaultDescription
stringstring"''"The string to unescape.

Example

unescape('fred, barney, &amp; 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

ParamTypeDescription
CtorObjectA 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

ParamTypeDescription
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

ParamTypeDescription
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

ParamTypeDefaultDescription
prefixstring"''"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.

0.12.0

7 years ago

0.11.0

7 years ago

0.10.0

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.0.1

7 years ago