Function
Static Public Summary | ||
public |
A function that always returns |
|
public |
A function that always returns |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Returns a function that always returns the given value. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
applyOrReturn(args: Array, test: *): * Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
Checks if |
|
public |
assign(sources: *): * Create a new object/array with the own properties of the first entry merged with the own properties of the others objects. |
|
public |
this function was deprecated. use object/propSet instead
Autocurry for 3 arguments |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
camelCaseName(str: String): String Converts string to camel case. |
|
public |
capitalize(str: String): String Returns the capitalized version of a string. |
|
public |
clone(x: *): * Creates a deep copy of the value which may contain (nested) |
|
public |
comparator(pred: Function): Function Makes a comparator function out of a function that reports whether the first element is less than the second. |
|
public |
complement(fn: Function): Function Takes a function |
|
public |
Performs right-to-left function composition. |
|
public |
Performs right-to-left composition of Promise-returning functions. |
|
public |
Autocurry for 2 arguments |
|
public |
Returns a function, |
|
public |
createCache(obj: Function): * Creates a cache instance base on passed object. |
|
public |
Returns a curried equivalent of the provided function. |
|
public |
Returns a curried equivalent of the provided function, with the specified arity. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
defaultTo(dflt: a, x: b): * Autocurry for 2 arguments |
|
public |
difference(a: Array, b: Array): Array Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Converts the characters "&", "<", ">", '"', and "'" in |
|
public |
escapeRegExp(str: string): string Escapes the |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
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. |
|
public |
Returns a new function much like the supplied one, except that the first two arguments' order is reversed. |
|
public |
Creates a new object from a list key-value pairs. |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Returns the first element of the given array. |
|
public |
identity(x: *): * A function that does nothing but return the parameter supplied to it. |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Returns all but the last element of the given list or string. |
|
public |
intersection(a: Array, b: Array): Array Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
isPlainObject(test: *): boolean Returns whether a value is a plain object (an object that is created using an object literal, Object.create(null) or similar). |
|
public |
Returns true if |
|
public |
Autocurry for 2 arguments |
|
public |
kebabCaseName(str: String): String Converts string to kebab case. |
|
public |
Autocurry for 2 arguments |
|
public |
Returns a list containing the names of all the enumerable own properties of the supplied object. |
|
public |
Returns the last element of the given array. |
|
public |
Returns the number of elements in the array by returning |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Negates its argument. |
|
public |
Checks if |
|
public |
noop() This method doing nothing and returns |
|
public |
A function that returns the |
|
public |
nothing(): Undefined A function that always returns |
|
public |
Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
Accepts a function |
|
public |
Memoize function with multiply arguments of any type. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Checks if |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Creates an array of numbers (positive and/or negative) progressing from
|
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
reduceWhile(pred: Function, fn: Function, acc: *, arr: Array): * Autocurry for 4 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
rejectWith(func: Function, payload: *[]): Promise Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
resolveWith(func: Function, payload: *[]): Promise Autocurry for 2 arguments |
|
public |
Returns a new list or string with the elements or characters in reverse order. |
|
public |
shallowEqual(test1: *, test2: *): Boolean Returns |
|
public |
Creates an array of shuffled values |
|
public |
Gets the size of |
|
public |
Autocurry for 3 arguments |
|
public |
snakeCaseName(str: String): String Returns the snake case version of a string. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
stableSortBy(fn: Function, arr: Array): Array Autocurry for 2 arguments |
|
public |
startsWith(prefix: string, str: string): Boolean Autocurry for 2 arguments |
|
public |
strictEqual(test1: *, test2: *): Boolean Returns |
|
public |
Checks if |
|
public |
Adds together all the elements of a list. |
|
public |
Returns all but the first element of the given array. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
throttleEnd(wait: number, fn: Function): * Autocurry for 2 arguments |
|
public |
Converts val to array. |
|
public |
Returns the lower case version of a string. |
|
public |
Converts an object into an array of key, value arrays. |
|
public |
Converts |
|
public |
Returns the upper case version of a string. |
|
public |
Removes leading and trailing whitespace from |
|
public |
Removes leading whitespace from |
|
public |
Autocurry for 2 arguments |
|
public |
Gives a single-word string description of the (native) type of a value, returning such answers as 'Object', 'Number', 'Array', or 'Null'. |
|
public |
Checks if |
|
public |
The inverse of |
|
public |
Returns unique items in array |
|
public |
Autocurry for 2 arguments |
|
public |
Generates a unique ID. |
|
public |
Autocurry for 3 arguments |
|
public |
updatePropertyValue(propertyName: String, propertyValue: Any, obj: Object): Object this function was deprecated. use propSet instead
Autocurry for 2 arguments |
|
public |
upperFirst(str: String): String Converts the first character of string to upper case and returns the new string. |
|
public |
Returns a list of all the enumerable own properties of the supplied object. |
|
public |
Autocurry for 3 arguments |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
|
public |
this function was deprecated. use array/difference instead
Autocurry for 2 arguments |
|
public |
Splits string into an array of its words. |
|
public |
Autocurry for 2 arguments |
|
public |
Autocurry for 3 arguments |
Static Private Summary | ||
private |
_path(paths: *[], obj: {}): * Autocurry for 2 arguments |
Static Public
public F(): Boolean source
import F from '@tinkoff/utils/function/F'
A function that always returns false
. Any passed in parameters are ignored.
Example:
F(); //=> false
public T(): Boolean source
import T from '@tinkoff/utils/function/T'
A function that always returns true
. Any passed in parameters are ignored.
Example:
T(); //=> true
public adjust(fn: Function, idx: Number, list: Array): Array source
import adjust from '@tinkoff/utils/array/adjust'
Autocurry for 3 arguments
Applies a function to the value at the given index of an array, returning a
new copy of the array with the element at the given index replaced with the
result of the function application.
Return:
Array | A copy of the supplied array with
the element at index |
Example:
adjust(add(10), 1, [1, 2, 3]); //=> [1, 12, 3]
adjust(add(10))(1)([1, 2, 3]); //=> [1, 12, 3]
public all(fn: Function, arr: Array): Boolean source
import all from '@tinkoff/utils/array/all'
Autocurry for 2 arguments
Returns true
if all the elements of the array match the predicate,
false
otherwise.
Example:
var lessThan2 = x => x < 2;
var lessThan3 = x => x < 3;
all(lessThan2)([1, 2]); //=> false
all(lessThan3)([1, 2]); //=> true
public all(fn: Function, obj: Object): Boolean source
import all from '@tinkoff/utils/object/all'
Autocurry for 2 arguments
Tests whether every [key, value] pair in the object passes the test implemented by the provided function.
Return:
Boolean | Returns |
Example:
var isBiggerThanZero = x => x > 0;
all(isBiggerThanZero, {}); //=> true
all(isBiggerThanZero, { a: 1 }); //=> true
all(isBiggerThanZero, { a: 1, b: 1, c: 1 }); //=> true
all(isBiggerThanZero, { a: 0, b: 1, c: 0 }); //=> false
all(isBiggerThanZero, { a: 1, b: 0, c: 1 }); //=> false
all(isBiggerThanZero)({ a: 0, b: 0, c: 0 }); //=> false
all(isBiggerThanZero)({ a: 1, b: 0, c: 1 }); //=> false
public allPass(fns: Array, args: ...*): Function source
import allPass from '@tinkoff/utils/function/allPass'
Autocurry for 2 arguments
Takes a list of predicates and returns a predicate that returns true for a
given list of arguments if every one of the provided predicates is satisfied
by those arguments.
Params:
Name | Type | Attribute | Description |
fns | Array | predicates |
|
args | ...* | passed arguments to predicates |
Example:
var isQueen = propEq('rank', 'Q');
var isSpade = propEq('suit', '♠︎');
var isQueenOfSpades = allPass([isQueen, isSpade]);
isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true
public always(x: *): Function source
import always from '@tinkoff/utils/function/always'
Returns a function that always returns the given value. Note that for non-primitives the value returned is a reference to the original value.
Params:
Name | Type | Attribute | Description |
x | * | The value to wrap in a function |
Example:
var t = always('Tee');
t(); //=> 'Tee'
public any(fn: Function, arr: Array): Boolean source
import any from '@tinkoff/utils/array/any'
Autocurry for 2 arguments
Returns true
if at least one of elements of the list match the predicate,
false
otherwise.
Example:
var lessThan0 = x => x < 0;
var lessThan2 = x => x < 2;
any(lessThan0)([1, 2]); //=> false
any(lessThan2)([1, 2]); //=> true
public any(fn: Function, obj: Object): Boolean source
import any from '@tinkoff/utils/object/any'
Autocurry for 2 arguments
Tests whether at least one [key, value] pair in the object passes the test implemented by the provided function.
Return:
Boolean | Returns |
Example:
var isBiggerThanZero = x => x > 0;
any(isBiggerThanZero, {}); //=> false
any(isBiggerThanZero, { a: 0 }); //=> false
any(isBiggerThanZero, { a: 0, b: 1, c: 0 }); //=> true
any(isBiggerThanZero, { a: 1, b: 0, c: -1 }); //=> true
any(isBiggerThanZero)({ a: 0, b: 0, c: 0 }); //=> false
any(isBiggerThanZero)({ a: 0, b: 1, c: 0 }); //=> true
public anyPass(fns: Array, args: ...*): Function source
import anyPass from '@tinkoff/utils/function/anyPass'
Autocurry for 2 arguments
Takes a list of predicates and returns a predicate that returns true for a
given list of arguments if at least one of the provided predicates is satisfied
by those arguments.
Params:
Name | Type | Attribute | Description |
fns | Array | predicates |
|
args | ...* | passed arguments to predicates |
Example:
var isClub = propEq('rank', '♣');
var isSpade = propEq('suit', '♠︎');
var isBlackCard = anyPass([isClub, isSpade]);
isBlackCard({rank: '10', suit: '♣'}); //=> true
isBlackCard({rank: 'Q', suit: '♠'}); //=> true
isBlackCard({rank: 'Q', suit: '♦'}); //=> false
public append(el: *, list: Array): Array source
import append from '@tinkoff/utils/array/append'
Autocurry for 2 arguments
Returns a new list containing the contents of the given list, followed by
the given element.
Params:
Name | Type | Attribute | Description |
el | * | The element to add to the end of the new list. |
|
list | Array | The list of elements to add a new item to. list. |
Example:
append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
append('tests', []); //=> ['tests']
append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
public applyOrReturn(args: Array, test: *): * source
import applyOrReturn from '@tinkoff/utils/function/applyOrReturn'
Autocurry for 2 arguments
If test
is function it calls with applied first argument, otherwise just returns test
Params:
Name | Type | Attribute | Description |
args | Array | Arguments passed to |
|
test | * | if |
Return:
* |
Example:
applyOrReturn([1,2,3], (...args) => args) // => [1,2,3]
applyOrReturn([1,2,3], 'test') // => 'test'
public array(test: *): boolean source
import array from '@tinkoff/utils/is/array'
Checks if test
is array.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isArray([]); // => true
isArray(null); // => false
public arrayLike(test: *): boolean source
import arrayLike from '@tinkoff/utils/is/arrayLike'
Checks if test
is arrayLike (has length
and index properties).
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isArrayLike([]); // => true
isArrayLike({ 0:'a', 1: 'b', length: 2 }); // => true
isArrayLike(null); // => false
public assign(sources: *): * source
import assign from '@tinkoff/utils/assign'
Create a new object/array with the own properties of the first entry merged with the own properties of the others objects. If a key exists in several objects, the value from the last object will be used.
Note if first argument is array then result of function is array, otherwise it is an object.
Params:
Name | Type | Attribute | Description |
sources | * |
Return:
* |
Example:
assign({a:1}, {b:2}) // => {a:1, b:2}
assign([1,2,3], [4,5]) // => [4,5,3]
public assoc(prop: String, val: *, obj: Object): Object source
import assoc from '@tinkoff/utils/object/assoc'
Autocurry for 3 arguments
Makes a shallow clone of an object, setting or overriding the specified
property with the given value. All non-primitive properties are
copied by reference.
Example:
assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
public boolean(test: *): boolean source
import boolean from '@tinkoff/utils/is/boolean'
Checks if test
is boolean.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isBool(false); // => true
isBool(new Boolean()); // => true
isBool(null); // => false
public both(f: Function, g: Function): Function source
import both from '@tinkoff/utils/function/both'
Autocurry for 2 arguments
A function wrapping calls to the two functions in an &&
operation,
returning the result of the first function if it is false-y and the result
of the second function otherwise. Note that this is short-circuited,
meaning that the second function will not be invoked if the first returns a
false-y value.
Example:
var gt10 = x => x > 10;
var even = x => x % 2 === 0;
var f = both(gt10, even);
f(100); //=> true
f(101); //=> false
public camelCaseName(str: String): String source
import camelCaseName from '@tinkoff/utils/string/camelCaseName'
Converts string to camel case.
This function convert only latin chars. The preferred purpose is to generate file or class names in your code generation tools.
Params:
Name | Type | Attribute | Description |
str | String | The string to convert. |
Example:
camelCase('Foo Bar'); //=> 'fooBar'
camelCase('foo-bar'); //=> 'fooBar'
camelCase('foo_bar'); //=> 'fooBar'
camelCase('FOO_BAR'); //=> 'fooBar'
camelCase('fooBar'); //=> 'fooBar'
camelCase('Foo Bär'); //=> 'fooBR' only latin chars are supported
public capitalize(str: String): String source
import capitalize from '@tinkoff/utils/string/capitalize'
Returns the capitalized version of a string.
Params:
Name | Type | Attribute | Description |
str | String | The string to capitalize. |
Example:
capitalize('xyz'); //=> 'Xyz'
public clone(x: *): * source
import clone from '@tinkoff/utils/clone'
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 not
copied, but assigned by their reference.
Params:
Name | Type | Attribute | Description |
x | * | The value to clone |
Return:
* | A copy of a value. |
Example:
var objects = [{}, {}, {}];
var objectsClone = clone(objects);
objects[0] === objectsClone[0]; //=> false
public comparator(pred: Function): Function source
import comparator from '@tinkoff/utils/function/comparator'
Makes a comparator function out of a function that reports whether the first element is less than the second.
Params:
Name | Type | Attribute | Description |
pred | Function | A predicate function of arity two which will return |
Example:
var byAge = comparator((a, b) => a.age < b.age);
var people = [
// ...
];
var peopleByIncreasingAge = sort(byAge, people);
public complement(fn: Function): Function source
import complement from '@tinkoff/utils/function/complement'
Takes a function f
and returns a function g
such that if called with the same arguments
when f
returns a "truthy" value, g
returns false
and when f
returns a "falsy" value g
returns true
.
Params:
Name | Type | Attribute | Description |
fn | Function |
Example:
var isNotNil = complement(isNil);
isNil(null); //=> true
isNotNil(null); //=> false
isNil(7); //=> false
isNotNil(7); //=> true
public compose(fns: ...Function): Function source
import compose from '@tinkoff/utils/function/compose'
Performs right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary.
Note: The result of compose is not automatically curried.
Params:
Name | Type | Attribute | Description |
fns | ...Function |
Example:
var f = compose(x => x + 1, x => -x, Math.pow);
f(3, 4); // -(3^4) + 1
public composeP(chain: ...Function): Function source
import composeP from '@tinkoff/utils/function/composeP'
Performs right-to-left composition of Promise-returning functions. The rightmost function may have any arity; the remaining functions must be unary.
Note: ALL of the chained functions must return a Promise.
Note: The result of compose is not automatically curried.
Params:
Name | Type | Attribute | Description |
chain | ...Function |
Example:
var res = x => Promise.resolve(x);
var f = composeP(x => res(-x), (x, y) => res(Math.pow(x, y)));
f(3, 4).then(console.log); // -(3^4)
public concat(a: Array | String, b: Array | String): Array | String source
import concat from '@tinkoff/utils/array/concat'
Autocurry for 2 arguments
Returns the result of concatenating the given arrays or strings.
Return:
Array | String | A list consisting of the elements of |
Example:
concat('ABC', 'DEF'); // 'ABCDEF'
concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
concat([], []); //=> []
public cond(pairs: F): Function source
import cond from '@tinkoff/utils/function/cond'
Returns a function, fn
, which encapsulates if/else-if/else logic.
cond
takes a list of [predicate, transform] pairs. All of the arguments
to fn
are applied to each of the predicates in turn until one returns a
"truthy" value, at which point fn
returns the result of applying its
arguments to the corresponding transformer. If none of the predicates
matches, fn
returns undefined.
Params:
Name | Type | Attribute | Description |
pairs | F |
Example:
var fn = cond([
[isEqual(0), always('water freezes at 0°C')],
[isEqual(100), always('water boils at 100°C')],
[T, temp => 'nothing special happens at ' + temp + '°C']
]);
fn(0); //=> 'water freezes at 0°C'
fn(50); //=> 'nothing special happens at 50°C'
fn(100); //=> 'water boils at 100°C'
public createCache(obj: Function): * source
import createCache from '@tinkoff/utils/function/memoize/createCache'
Creates a cache instance base on passed object. Return instance has 3 methods: get, set, has
*
Params:
Name | Type | Attribute | Description |
obj | Function | object that will be used as cache |
Return:
* |
Example:
const cache = createCache();
cache.set('a', 5);
cache.has('a'); // true
cache.get('a'); // 5
public curry(fn: Function): Function source
import curry from '@tinkoff/utils/function/curry'
Returns a curried equivalent of the provided function. The arguments of curried function
needn't be provided one at a time. If f
is a ternary function and g
is curry(f)
, the
following are equivalent:
g(1)(2)(3)
g(1)(2, 3)
g(1, 2)(3)
g(1, 2, 3)
Params:
Name | Type | Attribute | Description |
fn | Function | The function to curry. |
Example:
var addFourNumbers = (a, b, c, d) => a + b + c + d;
var curriedAddFourNumbers = curry(addFourNumbers);
var f = curriedAddFourNumbers(1, 2);
var g = f(3);
g(4); //=> 10
public curryN(arity: Number, fn: Function): Function source
import curryN from '@tinkoff/utils/function/curryN'
Returns a curried equivalent of the provided function, with the specified
arity. 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)
Example:
var sumArgs = (...args) => sum(args);
var curriedAddFourNumbers = curryN(4, sumArgs);
var f = curriedAddFourNumbers(1, 2);
var g = f(3);
g(4); //=> 10
public debounce(wait: number, fn: Function): Function source
import debounce from '@tinkoff/utils/function/debounce'
Autocurry for 2 arguments
Creates a debounced function that delays invoking func
until after wait
milliseconds have elapsed since the last time the debounced function was
invoked. Delayed function invocation might be cancelled by calling cancel method.
public deepSizeLimit(): Function source
import deepSizeLimit from '@tinkoff/utils/function/memoize/deepSizeLimit'
Autocurry for 2 arguments
Memoize function with multiply arguments of any type, but it
clears cache every time it reaches the limit. Use it when you need
deep equality for cache lookup and afraid of memory leak.
Params:
Name | Type | Attribute | Description |
sizeLimit. | Number | Cache size limit |
Example:
const addFlag = obj => ({...obj, flag: true });
const memoize = memoizeDeepSizeLimit(2, addFlag));
memoize({test: 2}); // from addFlag call
memoize({test: 2}); // from cache
memoize({test: 3}); //from addFlag call
memoize({test: 4}); // from addFlag call
memoize({test: 2}); // from addFlag call (memory was cleared)
public defaultTo(dflt: a, x: b): * source
import defaultTo from '@tinkoff/utils/defaultTo'
Autocurry for 2 arguments
Returns the second argument if it is not null
, undefined
or NaN
otherwise the first argument is returned.
Params:
Name | Type | Attribute | Description |
dflt | a | The default value. |
|
x | b | The value to return if it is not null or undefined |
Return:
* | The the second value or the default value |
Example:
var defaultTo42 = defaultTo(42);
defaultTo42(null); //=> 42
defaultTo42(undefined); //=> 42
defaultTo42('Example'); //=> 'Example'
defaultTo42(parseInt('string')); //=> 42
public difference(a: Array, b: Array): Array source
import difference from '@tinkoff/utils/array/difference'
Autocurry for 2 arguments
Returns the array of all elements in the first array not
contained in the second array.
Example:
difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
public drop(n: Number, xs: *): * source
import drop from '@tinkoff/utils/array/drop'
Autocurry for 2 arguments
Returns all but the first n
elements of the given list, string.
Params:
Name | Type | Attribute | Description |
n | Number | ||
xs | * |
Return:
* | A copy of list without the first |
Example:
drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
drop(3, ['foo', 'bar', 'baz']); //=> []
drop(4, ['foo', 'bar', 'baz']); //=> []
public dropLast(n: Number, xs: Array): Array source
import dropLast from '@tinkoff/utils/array/dropLast'
Autocurry for 2 arguments
Returns a list containing all but the last n
elements of the given list
.
Example:
dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
dropLast(3, ['foo', 'bar', 'baz']); //=> []
dropLast(4, ['foo', 'bar', 'baz']); //=> []
dropLast(3, 'ramda'); //=> 'ra'
public dropWhile(fn: Function, arr: Array): Array source
import dropWhile from '@tinkoff/utils/array/dropWhile'
Autocurry for 2 arguments
Returns a new list excluding the leading elements of a given list which
satisfy the supplied predicate function. It passes each value to the supplied
predicate function, skipping elements while the predicate function returns
true
.
Example:
var lteTwo = x => x <= 2;
dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
public each(fn: Function, arr: Array) source
import each from '@tinkoff/utils/array/each'
Autocurry for 2 arguments
Iterate over an input list
, calling a provided function fn
for each
element in the list.
Example:
var printXPlusFive = x => console.log(x + 5);
each(printXPlusFive, [1, 2, 3]);
// logs 6
// logs 7
// logs 8
public each(fn: Function, obj: Object): Object source
import each from '@tinkoff/utils/object/each'
Autocurry for 2 arguments
Iterate over an input object
, calling a provided function fn
for each
key and value in the object.
Example:
var printKeyConcatValue = (value, key) => console.log(key + ':' + value);
each(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
// logs x:1
// logs y:2
public either(f: Function, g: Function): Function source
import either from '@tinkoff/utils/function/either'
Autocurry for 2 arguments
A function wrapping calls to the two functions in an ||
operation,
returning the result of the first function if it is truth-y and the result
of the second function otherwise. Note that this is short-circuited,
meaning that the second function will not be invoked if the first returns a
truth-y value.
Example:
var gt10 = x => x > 10;
var even = x => x % 2 === 0;
var f = either(gt10, even);
f(101); //=> true
f(8); //=> true
f(3); //=> false
public element(test: *): boolean source
import element from '@tinkoff/utils/is/element'
Checks if value
is likely a DOM element.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isElement(document.body); // => true
isElement('<body>'); // => false
public empty(test: *): boolean source
import empty from '@tinkoff/utils/is/empty'
Checks if test
is empty.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isEmpty([]); // => true
isEmpty(null); // => true
isEmpty({}); // => true
isEmpty(''); // => true
isEmpty('test'); // => false
isEmpty({ a: 5 }); // => false
isEmpty([1]); // => false
public endsWith(postfix: string, str: string): Boolean source
import endsWith from '@tinkoff/utils/string/endsWith'
Autocurry for 2 arguments
Checks if a string ends with the provided postfix
Example:
endsWith('c', 'abc') //=> true
endsWith('b', 'abc') //=> false
public eqProps(prop: String, obj1: Object, obj2: Object): Boolean source
import eqProps from '@tinkoff/utils/object/eqProps'
Autocurry for 3 arguments
Reports whether two objects have the same value, in equal
terms, for the specified property. Useful as a curried predicate.
Example:
var o1 = { a: 1, b: 2, c: 3, d: 4 };
var o2 = { a: 10, b: 20, c: 3, d: 40 };
eqProps('a', o1, o2); //=> false
eqProps('c', o1, o2); //=> true
public escape(str: string): string source
import escape from '@tinkoff/utils/string/escape'
Converts the characters "&", "<", ">", '"', and "'" in string
to their
corresponding HTML entities.
Params:
Name | Type | Attribute | Description |
str | string | The string to escape. |
Example:
escape('fred, barney, & pebbles'); // => 'fred, barney, & pebbles'
public escapeRegExp(str: string): string source
import escapeRegExp from '@tinkoff/utils/string/escapeRegExp'
Escapes the RegExp
special characters "^", "$", "\", ".", "*", "+",
"?", "(", ")", "[", "]", "{", "}", and "|" in string
.
Params:
Name | Type | Attribute | Description |
str | string | The string to escape. |
Example:
escapeRegExp('[test](utils)'); // => '\[test\]\(utils\)'
public filter(fn: Function, arr: Array): Array source
import filter from '@tinkoff/utils/array/filter'
Autocurry for 2 arguments
Takes a predicate and a "arr", and returns a new array of the
same type containing the members of the given arr which satisfy the
given predicate.
Example:
var isEven = n => n % 2 === 0;
filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
public filter(fn: Function, obj: Object): Object source
import filter from '@tinkoff/utils/object/filter'
Autocurry for 2 arguments
Takes a predicate and a object, and returns a new object
containing only members which satisfy the
given predicate.
Example:
var isEven = n => n % 2 === 0;
filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
public filterMap(filterFn: Function, mapFn: Function, arr: Array): Array source
import filterMap from '@tinkoff/utils/array/filterMap'
Autocurry for 3 arguments
This is shortcut for filter + map, but applied in one iteration.
Example:
var isEven = n => n % 2 === 0;
var double = x => x * 2;
filterMap(isEven, double, [1, 2, 3, 4]); //=> [4, 8]
// this is equivalent to
// map(double, filter(isEven, [1, 2, 3, 4]));
public find(fn: Function, arr: Array): Object source
import find from '@tinkoff/utils/array/find'
Autocurry for 2 arguments
Returns the first element of the list which matches the predicate, or
undefined
if no element matches.
Example:
var xs = [{a: 1}, {a: 2}, {a: 3}];
find(x => x.a === 2)(xs); //=> {a: 2}
find(x => x.a === 4)(xs); //=> undefined
public findIndex(fn: Function, arr: Array): Number source
import findIndex from '@tinkoff/utils/array/findIndex'
Autocurry for 2 arguments
Returns the index of the first element of the list which matches the
predicate, or -1
if no element matches.
Example:
var xs = [{a: 1}, {a: 2}, {a: 3}];
findIndex(x => x.a === 2)(xs); //=> 1
findIndex(x => x.a === 4)(xs); //=> -1
public findKey(fn: Function, obj: Object): String | undefined source
import findKey from '@tinkoff/utils/object/findKey'
Autocurry for 2 arguments
Returns first key in obj
satisfied to fn
, or undefined if there is no such keys
Example:
findKey(identity, {a: 0, b: 3}) // => 'b'
findKey(x => x > 3, { a: 2, b: 1 }) // => undefined
findKey(x => x > 3, { a: 4, b: 5 }) // => 'a'
public findLast(fn: Function, list: Array): Object source
import findLast from '@tinkoff/utils/array/findLast'
Autocurry for 2 arguments
Returns the last element of the list which matches the predicate, or
undefined
if no element matches.
Example:
const xs = [{a: 1, b: 0}, {a:1, b: 1}];
findLast(propEq('a', 1))(xs); //=> {a: 1, b: 1}
findLast(propEq('a', 4))(xs); //=> undefined
public finite(test: *): boolean source
import finite from '@tinkoff/utils/is/finite'
Checks if test
is finite.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isFinite(5); // => true
isFinite(null); // => false
public flatten(arr: Array): Array source
import flatten from '@tinkoff/utils/array/flatten'
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.
Params:
Name | Type | Attribute | Description |
arr | 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]
public flip(fn: Function): * source
import flip from '@tinkoff/utils/function/flip'
Returns a new function much like the supplied one, except that the first two arguments' order is reversed.
Params:
Name | Type | Attribute | Description |
fn | Function | The function to invoke with its first two parameters reversed. |
Return:
* | The result of invoking |
Example:
const neg = (a, b) => a - b
flip(neg)(3, 5); //=> 2
public fromPairs(arr: Array): Object source
import fromPairs from '@tinkoff/utils/object/fromPairs'
Creates a new object from a list key-value pairs. If a key appears in multiple pairs, the rightmost pair is included in the object.
Params:
Name | Type | Attribute | Description |
arr | Array | An array of two-element arrays that will be the keys and values of the output object. |
Example:
fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
public function(test: *): boolean source
import function from '@tinkoff/utils/is/function'
Checks if test
is function.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isFunction(() => {}); // => true
isFunction(null); // => false
public groupBy(fns: Array<Function>, list: Array): Array source
import groupBy from '@tinkoff/utils/array/groupBy'
Autocurry for 2 arguments
Creates an array of arrays generated from the results of running
each element of list thru each fn
. The corresponding
value of each index is an array of elements satisfies to function with that index, and
the last array is elements with no satisfied function
Example:
groupBy([isPositive, isZero, isNegative], [0, -3, 2, 'str', 4, -1]);// => [[2,4], [0], [-3, -1], ['str']]
public groupBy(fn: Function, obj: Object): Object source
import groupBy from '@tinkoff/utils/object/groupBy'
Autocurry for 2 arguments
Creates an object composed of keys generated from the results of running
each element of object thru fn
. The corresponding
value of each key is an array of elements responsible for generating the key.
Example:
groupBy(x => x, { a: 1, b: 1, c: 3});// => { '1': [1, 1], '3': [3] }
public has(prop: String, obj: Object): Boolean source
import has from '@tinkoff/utils/object/has'
Autocurry for 2 arguments
Returns whether or not an object has an own property with the specified name
Example:
var hasName = has('name');
hasName({name: 'alice'}); //=> true
hasName({name: 'bob'}); //=> true
hasName({}); //=> false
public head(arr: Array): * source
import head from '@tinkoff/utils/array/head'
Returns the first element of the given array.
Params:
Name | Type | Attribute | Description |
arr | Array |
Return:
* |
Example:
head(['fi', 'fo', 'fum']); //=> 'fi'
head([]); //=> undefined
public identity(x: *): * source
import identity from '@tinkoff/utils/function/identity'
A function that does nothing but return the parameter supplied to it. Good as a default or placeholder function.
Params:
Name | Type | Attribute | Description |
x | * | The value to return. |
Return:
* | The input value, |
Example:
identity(1); //=> 1
const obj = {};
identity(obj) === obj; //=> true
public ifElse(condition: Function, onTrue: Function, onFalse: Function): Function source
import ifElse from '@tinkoff/utils/function/ifElse'
Autocurry for 3 arguments
Creates a function that will process either the onTrue
or the onFalse
function depending upon the result of the condition
predicate.
Return:
Function | A new unary function that will process either the |
Example:
const prop = ifElse(
x => x.a,
x => x.b,
x => x.c
);
prop({ a: false, c: 2 }); //=> 2
prop({ a: true, b: 1, c: 2 }); //=> 1
public includes(value: *, arr: Array | String): Boolean source
import includes from '@tinkoff/utils/array/includes'
Autocurry for 2 arguments
Dispatches call to arr.indexOf, returns true if arr is array and value in the array or
if arr is string and value is substring of arr
Params:
Name | Type | Attribute | Description |
value | * | The item to compare against. |
|
arr | Array | String | The array to consider. |
Example:
includes(3, [1, 2, 3]); //=> true
includes(4, [1, 2, 3]); //=> false
includes('test', 'aaatest'); //=> true
public indexBy(fn: Function, arr: Array): Object source
import indexBy from '@tinkoff/utils/array/indexBy'
Autocurry for 2 arguments
Given a function that generates a key, turns a list of objects into an
object indexing the objects by the given key. Note that if multiple
objects generate the same value for the indexing key only the last value
will be included in the generated object.
Example:
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
indexBy(x => x.id, list);
//=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
public indexOf(target: *, xs: Array): Number source
import indexOf from '@tinkoff/utils/array/indexOf'
Autocurry for 2 arguments
Returns the position of the first occurrence of an item in an array, or -1
if the item is not included in the array.
Params:
Name | Type | Attribute | Description |
target | * | The item to find. |
|
xs | Array | The array to search in. |
Example:
indexOf(3, [1,2,3,4]); //=> 2
indexOf(10, [1,2,3,4]); //=> -1
public init(arr: Array): * source
import init from '@tinkoff/utils/array/init'
Returns all but the last element of the given list or string.
Params:
Name | Type | Attribute | Description |
arr | Array |
Return:
* |
Example:
init([1, 2, 3]); //=> [1, 2]
init([1, 2]); //=> [1]
init([1]); //=> []
init([]); //=> []
public intersection(a: Array, b: Array): Array source
import intersection from '@tinkoff/utils/array/intersection'
Autocurry for 2 arguments
Combines two array into a set (i.e. no duplicates) composed of those
elements common to both arrays.
Example:
intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
public isEqual(test1: *, test2: *): Boolean source
Autocurry for 2 arguments
Returns true
if its arguments are equivalent, false
otherwise.
Params:
Name | Type | Attribute | Description |
test1 | * | ||
test2 | * |
Example:
isEqual(1, 1); //=> true
isEqual(1, '1'); //=> false
isEqual([1, 2, 3], [1, 2, 3]); //=> true
isEqual({ a: { b: 1 }}, { a: { b: 1 }}); //=> true
public isPlainObject(test: *): boolean source
import isPlainObject from '@tinkoff/utils/is/plainObject'
Returns whether a value is a plain object (an object that is created using an object literal, Object.create(null) or similar). Rejects anything with a custom prototype or a non-object ECMAScript type. Also rejects React elements and components
Note: if the host environment does not support Symbol, any object with a $$typeof property is considered a React element
Params:
Name | Type | Attribute | Description |
test | * | The value to check |
Example:
isPlainObject({ a: 'test' }); // => true
isPlainObject(moment()); // => false
isPlainObject(<span></span>); // => false
public isTrue(val: *): boolean source
import isTrue from '@tinkoff/utils/isTrue'
Returns true if val
is true or equal to string 'true'
Params:
Name | Type | Attribute | Description |
val | * |
Example:
isTrue(true) // => true
isTrue('true') // => true
isTrue([true]) // => false
isTrue('false') // => false
public join(separator: String, arr: Array): String source
import join from '@tinkoff/utils/array/join'
Autocurry for 2 arguments
Returns a string made by inserting the separator
between each element and
concatenating all the elements into a single string.
Example:
var spacer = join(' ');
spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
join('|', [1, 2, 3]); //=> '1|2|3'
public kebabCaseName(str: String): String source
import kebabCaseName from '@tinkoff/utils/string/kebabCaseName'
Converts string to kebab case.
This function convert only latin chars. The preferred purpose is to generate file or class names in your code generation tools.
Params:
Name | Type | Attribute | Description |
str | String | The string to convert. |
Example:
kebabCase('Foo Bar'); //=> 'foo-bar'
kebabCase('fooBar'); //=> 'foo-bar'
kebabCase('foo_bar'); //=> 'foo-bar'
kebabCase('FOO_BAR'); //=> 'foo-bar'
kebabCase('Foo Bär'); //=> 'foo-b-r' — only latin chars are supported
public keyBy(fn: Function, obj: Object): Object source
import keyBy from '@tinkoff/utils/object/keyBy'
Autocurry for 2 arguments
Creates an object composed of keys generated from the results of running
each element of object thru fn
. The corresponding value of
each key is the last element responsible for generating the key
Example:
keyBy(x => x < 2, { a: 0, b: 1, c: 3});// => { false: 1, true: 3 }
public keys(obj: Object): Array source
import keys from '@tinkoff/utils/object/keys'
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.
Params:
Name | Type | Attribute | Description |
obj | Object | The object to extract properties from |
Example:
keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
public last(arr: Array): * source
import last from '@tinkoff/utils/array/last'
Returns the last element of the given array.
Params:
Name | Type | Attribute | Description |
arr | Array |
Return:
* |
Example:
last(['fi', 'fo', 'fum']); //=> 'fum'
last([]); //=> undefined
public length(arr: Array): Number source
import length from '@tinkoff/utils/array/length'
Returns the number of elements in the array by returning arr.length
.
Params:
Name | Type | Attribute | Description |
arr | Array | The array to inspect. |
Example:
length([]); //=> 0
length([1, 2, 3]); //=> 3
public map(fn: Function, arr: Array): Array source
import map from '@tinkoff/utils/array/map'
Autocurry for 2 arguments
Creates an array of values by running each element in list through fn. The fn is invoked with three arguments:
(value, index, arr).
Example:
var double = x => x * 2;
map(double, [1, 2, 3]); //=> [2, 4, 6]
public map(fn: Function, obj: Object): Object source
import map from '@tinkoff/utils/object/map'
Autocurry for 2 arguments
Applies fn
to each of the obj
values, and returns new object.
Example:
var double = x => x * 2;
map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
public maxBy(fn: Function, arr: Array): * source
import maxBy from '@tinkoff/utils/array/maxBy'
Autocurry for 2 arguments
Return max value in array, depending on result of calling fn
Return:
* | Returns the maximum value. |
Example:
var objects = [{ 'n': 1 }, { 'n': 2 }];
maxBy(o => o.n, objects); // => { 'n': 2 }
public merge(sources: ...Object): Object source
import merge from '@tinkoff/utils/object/merge'
Autocurry for 2 arguments
Create a new object with the own properties of the first object merged with
the own properties of the others objects. If a key exists in several objects,
the value from the last object will be used.
Params:
Name | Type | Attribute | Description |
sources | ...Object |
Example:
merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 }); //=> { 'name': 'fred', 'age': 40 }
public mergeDeep(sources: ...Object): Object source
Autocurry for 2 arguments
Create a new object with the own properties of the first object deeply merged with
the own properties of the other objects. If a key exists in several objects, then all values associated with this key
are merged recursively unless at least one of them is not a plain object. Merging is done from left to right.
Params:
Name | Type | Attribute | Description |
sources | ...Object |
Example:
mergeDeep({ 'name': 'fred', 'info': { 'age': 10, 'sex': 'm' } }, { 'info': { 'age': 40 }); //=> { 'name': 'fred', 'info': { 'age': 40, 'sex': 'm' } }
public mergeWith(fn: Function, sources: ...Object): Object source
import mergeWith from '@tinkoff/utils/object/mergeWith'
Autocurry for 3 arguments
Create a new object with the own properties of the first object merged with
the own properties of the others objects. If a key exists in several objects
provided fn
will be called and should return resolve value
Example:
mergeWith((x, y) => x + y, { 'name': 'fred', 'age': 10 }, { 'age': 40 }); //=> { 'name': 'fred', 'age': 50 }
public negate(n: Number): Number source
import negate from '@tinkoff/utils/negate'
Negates its argument.
Params:
Name | Type | Attribute | Description |
n | Number |
Example:
negate(42); //=> -42
public nil(test: *): boolean source
import nil from '@tinkoff/utils/is/nil'
Checks if test
is nil (null or undefined).
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isNil(undefined); // => true
isNil(null); // => true
isNil(''); // => false
isNil(5); // => false
public noop() source
import noop from '@tinkoff/utils/function/noop'
This method doing nothing and returns undefined
.
public not(val: *): Boolean source
import not from '@tinkoff/utils/not'
A function that returns the !
of its argument. It will return true
when
passed false-y value, and false
when passed a truth-y one.
Params:
Name | Type | Attribute | Description |
val | * | any value |
Example:
not(true); //=> false
not(false); //=> true
not(0); //=> true
not(1); //=> false
public nothing(): Undefined source
import nothing from '@tinkoff/utils/function/nothing'
A function that always returns undefined
. Any passed in parameters are ignored.
Return:
Undefined |
Example:
nothing(); //=> undefined
public nth(index: Number, arr: *): * source
import nth from '@tinkoff/utils/array/nth'
Autocurry for 2 arguments
Returns the nth element of the given array. If n is negative the
element at index length + n is returned.
Params:
Name | Type | Attribute | Description |
index | Number | ||
arr | * |
Return:
* |
Example:
var list = ['foo', 'bar', 'baz', 'quux'];
nth(1, list); //=> 'bar'
nth(2, list); //=> 'baz'
nth(-1, list); //=> 'quux'
nth(-99, list); //=> undefined
public number(test: *): boolean source
import number from '@tinkoff/utils/is/number'
Checks if test
is number.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isNumber(5); // => true
isNumber(null); // => false
public objOf(key: String, value: *): Object source
import objOf from '@tinkoff/utils/object/objOf'
Autocurry for 2 arguments
Creates an object containing a single key:value pair.
Params:
Name | Type | Attribute | Description |
key | String | ||
value | * |
Example:
objOf('key', 5) // => { key: 5 }
public object(test: *): boolean source
import object from '@tinkoff/utils/is/object'
Checks if test
is object.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isObject({ a: 'test' }); // => true
isObject(null); // => false
public omit(props: Array, obj: Object): Object source
import omit from '@tinkoff/utils/object/omit'
Autocurry for 2 arguments
Returns a partial copy of an object omitting the keys specified.
Example:
omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
public once(fn: Function): Function source
import once from '@tinkoff/utils/function/once'
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.
Params:
Name | Type | Attribute | Description |
fn | Function | The function to wrap in a call-only-once wrapper. |
Example:
var addOneOnce = once(x => x + 1);
addOneOnce(10); //=> 11
addOneOnce(addOneOnce(50)); //=> 11
public one(fn: Function, isEqual: Function): Function source
import one from '@tinkoff/utils/function/memoize/one'
Memoize function with multiply arguments of any type. Use it when you need to cache lookup only for last result (like reselect).
Example:
const addFlag = obj => ({...obj, flag: true });
const memoize = memoizeOnce(addFlag));
memoize(1); // from addFlag call
memoize(1); // from cache
memoize(1,2) // from addFlag call,
memoize(1) // from addFlag call, cache was flashed on previous step
public optional(fn: Function): Function source
import optional from '@tinkoff/utils/function/optional'
Autocurry for 2 arguments
Wraps a one-parameter function,
new function returns true if passed parameter is undefined, otherwise returns result of calling fn
Params:
Name | Type | Attribute | Description |
fn | Function | function to wrap |
Example:
const optIsString = optional(isString);
optIsString() // => true
optIsString({}) // => false
optIsString('str') // => true
public partition(fn: Function, arr: Array): Array source
import partition from '@tinkoff/utils/array/partition'
Autocurry for 2 arguments
Takes a predicate and a array and returns the
pair of arrays of the same type of elements which do and do not
satisfy, the predicate, respectively.
Return:
Array | An array, containing first the subset of elements that satisfy the predicate, and second the subset of elements that do not satisfy. |
Example:
partition(includes('s'), ['sss', 'ttt', 'foo', 'bars']);
// => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
public pathApply(paths: [String], fn: Function, obj: Object): * source
import pathApply from '@tinkoff/utils/object/pathApply'
Autocurry for 3 arguments
Returns the result of fn
with given value at path
.
Return:
* | result of calling fn with nested property. |
Example:
pathApply(['a', 'b'], x => 'is ' + x, {a: {b: 2}}); //=> is 2
pathApply(['a', 'b'], x => x > 0, {a: {b: 2}}); //=> true
public pathEq(paths: Array, value: *, obj: Object): Boolean source
import pathEq from '@tinkoff/utils/object/pathEq'
Autocurry for 3 arguments
Determines whether a nested path on an object has a specific value
Example:
var user1 = { address: { zipCode: 90210 } };
var user2 = { address: { zipCode: 55555 } };
var user3 = { name: 'Bob' };
var users = [ user1, user2, user3 ];
var isFamous = pathEq(['address', 'zipCode'], 90210);
filter(isFamous, users); //=> [ user1 ]
public pathOr(paths: [String], value: *, obj: Object): * source
import pathOr from '@tinkoff/utils/object/pathOr'
Autocurry for 3 arguments
If the given, non-null object has a value at the given path, returns the
value at that path. Otherwise returns the provided default value.
Params:
Name | Type | Attribute | Description |
paths | [String] | The path to use. |
|
value | * | The default value. |
|
obj | Object | The object to retrieve the nested property from. |
Return:
* | The data at |
Example:
pathOr(['a', 'b'],'N/A', {a: {b: 2}}); //=> 2
pathOr(['a', 'b'], 'N/A', {c: {b: 2}}); //=> "N/A"
public pathSet(paths: [String], value: *, obj: Object): Object source
import pathSet from '@tinkoff/utils/object/pathSet'
Autocurry for 3 arguments
Returns the result of "setting" the portion of the given data structure
focused by the given paths to the given value.
Note: If property in the object is equal to value by reference then function just returns object without changes
Params:
Name | Type | Attribute | Description |
paths | [String] | ||
value | * | ||
obj | Object |
Example:
pathSet(['a', 'b'], 3, {}) // => { a: { b: 3 } }
public pathSetBy(paths: [String], fn: Function, obj: Object): Object source
import pathSetBy from '@tinkoff/utils/object/pathSetBy'
Autocurry for 3 arguments
Returns the result of "setting" the portion of the given data structure
focused by the given paths to the result of fn
call.
Note: If property in the object is equal to value by reference then function just returns object without changes
Example:
pathSetBy(['a', 'b'], x => x+1, {a: { b:1 }}) // => { a: { b: 2 } }
public pick(props: [String], obj: Object): Object source
import pick from '@tinkoff/utils/object/pick'
Autocurry for 2 arguments
Returns a partial copy of an object containing only the keys specified. If
the key does not exist, the property is ignored.
Params:
Name | Type | Attribute | Description |
props | [String] | 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}
public pickBy(fn: Function, obj: Object): Object source
import pickBy from '@tinkoff/utils/object/pickBy'
Autocurry for 2 arguments
Returns a partial copy of an object containing only the keys with fn
predicate returns true
Example:
pickBy(x => x > 2, {a: 1, b: 2, c: 3, d: 4}); //=> {c: 3, d: 4}
public pluck(key: String, arr: Array): Array source
import pluck from '@tinkoff/utils/array/pluck'
Autocurry for 2 arguments
Returns a new list by plucking the same named property off all objects in the list supplied.
Example:
pluck('a')([{a: 1}, {a: 2}]); //=> [1, 2]
pluck(0)([[1, 2], [3, 4]]); //=> [1, 3]
public promise(test: *): boolean source
import promise from '@tinkoff/utils/is/promise'
Checks if test
is Promise.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isPromise({}) // => false
isPromise(Promise.resolve()) // => true
public prop(prop: String, obj: Object): * source
import prop from '@tinkoff/utils/object/prop'
Autocurry for 2 arguments
Returns a function that when supplied an object returns the indicated
property of that object, if it exists.
Return:
* | The value at |
Example:
prop('x', {x: 100}); //=> 100
prop('x', {}); //=> undefined
public propApply(propName: String, fn: Function, obj: Object): * source
import propApply from '@tinkoff/utils/object/propApply'
Autocurry for 3 arguments
Returns the result of fn
with value of property in obj
.
Return:
* | result of calling fn with property. |
Example:
propApply('a', x => 'is ' + x, {a: 2}); //=> is 2
propApply('b', x => x > 0, {b: 2}); //=> true
public propEq(propName: String, value: *, obj: *): Boolean source
import propEq from '@tinkoff/utils/object/propEq'
Autocurry for 3 arguments
Returns true
if the specified object property is equal to the given value; false
otherwise.
Params:
Name | Type | Attribute | Description |
propName | String | ||
value | * | ||
obj | * |
Example:
var abby = {name: 'Abby', age: 7, hair: 'blond'};
var fred = {name: 'Fred', age: 12, hair: 'brown'};
var rusty = {name: 'Rusty', age: 10, hair: 'brown'};
var alois = {name: 'Alois', age: 15, disposition: 'surly'};
var kids = [abby, fred, rusty, alois];
var hasBrownHair = propEq('hair', 'brown');
filter(hasBrownHair, kids); //=> [fred, rusty]
public propOr(propName: String, value: *, obj: Object): * source
import propOr from '@tinkoff/utils/object/propOr'
Autocurry for 3 arguments
If the given, non-null object has an own property with the specified name,
returns the value of that property. Otherwise returns the provided default
value.
Return:
* | The value of given property of the supplied object or the default value. |
Example:
var alice = {
name: 'ALICE',
age: 101
};
propOr('name', 'Bob')(alice); //=> 'ALICE'
propOr('favoriteLibrary', 'react')(alice); //=> 'react'
public propSet(prop: String, val: *, obj: Object): Object source
import propSet from '@tinkoff/utils/object/propSet'
Autocurry for 3 arguments
Makes a shallow clone of an object, setting or overriding the specified
property with the given value. All non-primitive properties are
copied by reference.
Note: If property in the object is equal to value by reference then function just returns object without changes
Example:
propSet('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
public propSetBy(prop: String, fn: Function, obj: Object): Object source
import propSetBy from '@tinkoff/utils/object/propSetBy'
Autocurry for 3 arguments
Makes a shallow clone of an object, setting or overriding the specified
property with the result of fn
call.
Note: If property in the object is equal to value by reference then function just returns object without changes
Example:
propSetBy('a', x => x+1, {a: 1}); //=> {a: 2}
public range(a: number, b: number, step: number): Array source
import range from '@tinkoff/utils/array/range'
Creates an array of numbers (positive and/or negative) progressing from
start
up to, but not including, end
. A step of -1
is used if a negative
start
is specified without an end
or step
. If end
is not specified,
it's set to start
with start
then set to 0
.
Note: JavaScript follows the IEEE-754 standard for resolving floating-point values which can produce unexpected results.
Example:
range(4); // => [0, 1, 2, 3]
range(-4); // => [0, -1, -2, -3]
range(1, 5); // => [1, 2, 3, 4]
range(0, 20, 5); // => [0, 5, 10, 15]
range(0, -4, -1); // => [0, -1, -2, -3]
range(0); // => []
public reduce(fn: Function, acc: *, arr: Array): * source
import reduce from '@tinkoff/utils/array/reduce'
Autocurry for 3 arguments
Returns a single item by iterating through the list, successively calling
the iterator function and passing it an accumulator value and the current
value from the array, and then passing the result to the next call.
Return:
* | The final, accumulated value. |
Example:
var numbers = [1, 2, 3];
var plus = (a, b) => a + b;
reduce(plus, 10, numbers); //=> 16
public reduce(fn: Function, acc: *, obj: Object): * source
import reduce from '@tinkoff/utils/object/reduce'
Autocurry for 3 arguments
Returns a single item by iterating through the obj, successively calling
the iterator function and passing it an accumulator value, current
value and current key from the obj, and then passing the result to the next call.
Return:
* | The final, accumulated value. |
Example:
var obj = { a: 1, b: 2, c: 3};
var plus = (a, b) => a + b;
reduce(plus, 10, obj); //=> 16
public reduceWhile(pred: Function, fn: Function, acc: *, arr: Array): * source
import reduceWhile from '@tinkoff/utils/array/reduceWhile'
Autocurry for 4 arguments
Returns a single item by iterating through the list, successively calling
the iterator function. reduceWhile also takes a predicate that is evaluated
before each step. If the predicate returns false, it "short-circuits"
the iteration and returns the current value of the accumulator.
Note: if arr
is undefined or null, acc
will be returned by reference immediately
Params:
Name | Type | Attribute | Description |
pred | Function | The predicate function. If it returns a truthy value, reduce continues. Receives the accumulator and the current element |
|
fn | Function | The iterator function. Receives four values, the accumulator, the current element from the array, its index, and the original array. |
|
acc | * | The accumulator value. |
|
arr | Array | The list to iterate over. |
Return:
* | The final, accumulated value. |
Example:
reduceWhile(acc => acc.length < 3, (acc, x) => acc + x, '1', ['2', '3', '4', '5']) // '123'
public reject(fn: Function, arr: Array): Array source
import reject from '@tinkoff/utils/array/reject'
Autocurry for 2 arguments
Takes a predicate and a "arr", and returns a new array of the
same type containing the members of the given arr which do not satisfy the
given predicate.
Example:
var isEven = n => n % 2 === 0;
reject(isEven, [1, 2, 3, 4]); //=> [1, 3]
public rejectWith(func: Function, payload: *[]): Promise source
import rejectWith from '@tinkoff/utils/promise/rejectWith'
Autocurry for 2 arguments
Returns a promise that rejects with a value returned
by the supplied function when passed the supplied payload
Params:
Name | Type | Attribute | Description |
func | Function | ||
payload | *[] |
Example:
rejectWith(toLowerCase, 'Error').catch( err => console.error(err) );// => 'error'
public remove(start: Number, count: Number, list: Array): * source
import remove from '@tinkoff/utils/array/remove'
Autocurry for 3 arguments
Removes the sub-list of list
starting at index start
and containing
count
elements. Note that this is not destructive: it returns a copy of
the list with the changes.
<small>No lists have been harmed in the application of this function.</small>
Return:
* |
Example:
remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
public repeat(n: Number, value: *): Array source
import repeat from '@tinkoff/utils/array/repeat'
Autocurry for 2 arguments
Returns a fixed list of size n
containing a specified identical value.
Params:
Name | Type | Attribute | Description |
n | Number | The desired size of the output list. |
|
value | * | The value to repeat. |
Example:
repeat(5, 'hi'); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
var obj = {};
var repeatedObjs = repeat(5, obj); //=> [{}, {}, {}, {}, {}]
repeatedObjs[0] === repeatedObjs[1]; //=> true
public repeat(n: Number, value: String): String source
import repeat from '@tinkoff/utils/string/repeat'
Autocurry for 2 arguments
Returns a string containing a repeated identical value.
Example:
repeat(5, 'h'); //=> 'hhhhh'
public replace(pattern: RegExp | String, replacement: String, str: String): String source
import replace from '@tinkoff/utils/string/replace'
Autocurry for 3 arguments
Replace a substring or regex match in a string with a replacement.
Example:
replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
// Use the "g" (global) flag to replace all occurrences:
replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
public resolveWith(func: Function, payload: *[]): Promise source
import resolveWith from '@tinkoff/utils/promise/resolveWith'
Autocurry for 2 arguments
Returns a promise that resolves with a value returned
by the supplied function when passed the supplied payload
Params:
Name | Type | Attribute | Description |
func | Function | ||
payload | *[] |
Example:
rejectWith(toLowerCase, 'OK').then( res => console.info(res) );// => 'ok'
public reverse(list: Array | String): Array | String source
import reverse from '@tinkoff/utils/array/reverse'
Returns a new list or string with the elements or characters in reverse order.
Example:
reverse([1, 2, 3]); //=> [3, 2, 1]
reverse([1, 2]); //=> [2, 1]
reverse([1]); //=> [1]
reverse([]); //=> []
reverse('abc'); //=> 'cba'
reverse('ab'); //=> 'ba'
reverse('a'); //=> 'a'
reverse(''); //=> ''
public shallowEqual(test1: *, test2: *): Boolean source
import shallowEqual from '@tinkoff/utils/is/shallowEqual'
Returns true
if its arguments are equivalent is shallow equal terms, false
otherwise.
Params:
Name | Type | Attribute | Description |
test1 | * | ||
test2 | * |
Example:
isShallowEqual(1, 1); //=> true
isShallowEqual(1, '1'); //=> false
isShallowEqual([1, 2, 3], [1, 2, 3]); //=> true
isShallowEqual({ a: { b: 1 }}, { a: { b: 1 }}); //=> false
public shuffle(arr: Array): Array source
import shuffle from '@tinkoff/utils/array/shuffle'
Creates an array of shuffled values
Params:
Name | Type | Attribute | Description |
arr | Array | The collection to shuffle. |
Example:
shuffle([1, 2, 3, 4]); // => [4, 1, 3, 2]
public size(obj: Object): number source
import size from '@tinkoff/utils/object/size'
Gets the size of obj
by returning the number of own enumerable properties.
Params:
Name | Type | Attribute | Description |
obj | Object | The collection to inspect. |
Example:
size({ 'a': 1, 'b': 2 }); // => 2
public slice(fromIndex: Number, toIndex: Number, list: Array | String): Array | String source
import slice from '@tinkoff/utils/array/slice'
Autocurry for 3 arguments
Returns the elements of the given list or string (or object with a slice
method) from fromIndex
(inclusive) to toIndex
(exclusive).
Return:
Array | String |
Example:
slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
public snakeCaseName(str: String): String source
import snakeCaseName from '@tinkoff/utils/string/snakeCaseName'
Returns the snake case version of a string.
This function convert only latin chars. The preferred purpose is to generate file or class names in your code generation tools.
Params:
Name | Type | Attribute | Description |
str | String | The string to switch to snake case. |
Example:
snakeCaseName('Foo Bar'); //=> 'foo_bar'
snakeCaseName('fooBar'); //=> 'foo_bar'
snakeCaseName('foo-bar'); //=> 'foo_bar'
snakeCaseName('FOO-BAR'); //=> 'foo_bar'
snakeCaseName('Foo Bär'); //=> 'foo_b_r' only latin chars are supported
public sort(comparator: Function, arr: Array): Array source
import sort from '@tinkoff/utils/array/sort'
Autocurry for 2 arguments
Returns a copy of the array, sorted according to the comparator function,
which should accept two values at a time and return a negative number if the
first value is smaller, a positive number if it's larger, and zero if they
are equal. Please note that this is a copy of the list. It does not
modify the original.
Example:
var diff = function(a, b) { return a - b; };
sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
public sortBy(fn: Function, arr: Array): Array source
import sortBy from '@tinkoff/utils/array/sortBy'
Autocurry for 2 arguments
Sorts the array according to the supplied function.
Example:
var sortByFirstItem = sortBy(x => x[0]);
var pairs = [[-1, 1], [-2, 2], [-3, 3]];
sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
public split(delim: String | RegExp, str: String): Array source
import split from '@tinkoff/utils/string/split'
Autocurry for 2 arguments
Splits a string into an array of strings based on the given
separator.
Example:
split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
public splitEvery(length: Number, arr: Array | String): Array[] | String[] source
import splitEvery from '@tinkoff/utils/array/splitEvery'
Autocurry for 2 arguments
Splits a collection into slices of the specified length
Example:
splitEvery(2, [1, 2, 3, 4, 5]); // => [[1, 2], [3, 4], [5]]
public stableSortBy(fn: Function, arr: Array): Array source
import stableSortBy from '@tinkoff/utils/array/stableSortBy'
Autocurry for 2 arguments
Sorts the array according to the supplied function and keeping the order of elements.
Example:
const people = [
{ name: 'Ann', age: 20 },
{ name: 'Gary', age: 20 },
{ name: 'John', age: 17 },
{ name: 'Sam', age: 21 },
{ name: 'Bob', age: 17 }
];
stableSortBy(path(['age']), people) => [
{ name: 'John', age: 17 },
{ name: 'Bob', age: 17 },
{ name: 'Ann', age: 20 },
{ name: 'Gary', age: 20 },
{ name: 'Sam', age: 21 }
]
public startsWith(prefix: string, str: string): Boolean source
import startsWith from '@tinkoff/utils/string/startsWith'
Autocurry for 2 arguments
Checks if a string starts with the provided prefix
Example:
startsWith('a', 'abc') //=> true
startsWith('b', 'abc') //=> false
public strictEqual(test1: *, test2: *): Boolean source
import strictEqual from '@tinkoff/utils/is/strictEqual'
Returns true
if its arguments are equivalent is same value zero terms, false
otherwise.
Params:
Name | Type | Attribute | Description |
test1 | * | ||
test2 | * |
Example:
isStrictEqual(1, 1); //=> true
isStrictEqual(1, '1'); //=> false
isStrictEqual({}, {}); //=> false
isStrictEqual(NaN, NaN); //=> true
isStrictEqual(-0, 0); //=> true
public string(test: *): boolean source
import string from '@tinkoff/utils/is/string'
Checks if test
is string.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isString('test'); // => true
isString(new String('test')); // => true
isString(null); // => false
public sum(arr: Array): Number source
import sum from '@tinkoff/utils/array/sum'
Adds together all the elements of a list.
Params:
Name | Type | Attribute | Description |
arr | Array | An array of numbers |
Example:
sum([2,4,6,8,100,1]); //=> 121
public tail(arr: Array): * source
import tail from '@tinkoff/utils/array/tail'
Returns all but the first element of the given array.
Params:
Name | Type | Attribute | Description |
arr | Array |
Return:
* |
Example:
tail(['fi', 'fo', 'fum']); //=> ['fo', 'fum']
tail([]); //=> []
public take(n: Number, arr: Array | String): * source
import take from '@tinkoff/utils/array/take'
Autocurry for 2 arguments
Returns the first n
elements of the given array or string
Return:
* |
Example:
take(1, ['foo', 'bar', 'baz']); //=> ['foo']
take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
take(3, 'ramda'); //=> 'ram'
public takeRightWhile(fn: Function, arr: Array | String): Array | String source
import takeRightWhile from '@tinkoff/utils/array/takeRightWhile'
Autocurry for 2 arguments
Returns a new array|string containing the last n
elements of a given array|string, passing
each value to the supplied predicate function, and terminating when the
predicate function returns false
. Excludes the element that caused the
predicate function to fail.
Example:
var isNotOne = x => x !== 1;
takeRightWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
public tap(fn: Function, x: *): * source
import tap from '@tinkoff/utils/function/tap'
Autocurry for 2 arguments
Runs the given function with the supplied argument, then returns the argument.
Params:
Name | Type | Attribute | Description |
fn | Function | The function to call with |
|
x | * |
Return:
* |
|
Example:
var sayX = x => console.log('x is ' + x);
tap(sayX, 100); //=> 100
// logs 'x is 100'
public tap(fn: Function, x: *): * source
import tap from '@tinkoff/utils/promise/tap'
Autocurry for 2 arguments
Runs the given promise with the supplied argument. Returns the argument, when promise is resolved.
Params:
Name | Type | Attribute | Description |
fn | Function | The function to call with |
|
x | * |
Return:
* |
|
Example:
var sayX = x => console.log('x is ' + x);
Promise.resolve(5).then(promiseTap(sayX));
// logs 'x is 5'
public template(replacements: Object, str: String): String source
import template from '@tinkoff/utils/string/template'
Autocurry for 2 arguments
Replaces string keywords with provided values
Example:
template({ fruit: 'banana' }, 'Petya has a %{fruit}') // => Petya has a banana
public test(pattern: RegExp, str: String): Boolean source
import test from '@tinkoff/utils/string/test'
Autocurry for 2 arguments
Determines whether a given string matches a given regular expression.
public throttle(wait: number, fn: Function): * source
import throttle from '@tinkoff/utils/function/throttle'
Autocurry for 2 arguments
Creates a throttled function that only invokes fn
at most once per
every wait
milliseconds. fn
is called in start of wait
delay
Return:
* |
public throttleEnd(wait: number, fn: Function): * source
import throttleEnd from '@tinkoff/utils/function/throttleEnd'
Autocurry for 2 arguments
Creates a throttled function that only invokes fn
at most once per
every wait
milliseconds. fn
is called in the end of wait
delay
Return:
* |
public toArray(val: *): Array source
import toArray from '@tinkoff/utils/array/toArray'
Converts val to array. If val is array return it.
Params:
Name | Type | Attribute | Description |
val | * |
Example:
toArray([1,2,3]) // => [1,2,3]
toArray(5) // => [5]
toArray('test') // => ['test']
public toLower(str: String): String source
import toLower from '@tinkoff/utils/string/toLower'
Returns the lower case version of a string.
Params:
Name | Type | Attribute | Description |
str | String | The string to lower case. |
Example:
toLower('XYZ'); //=> 'xyz'
public toPairs(obj: Object): Array source
import toPairs from '@tinkoff/utils/object/toPairs'
Converts an object into an array of key, value arrays. Only the object's own properties are used. Note that the order of the output array is not guaranteed to be consistent across different JS platforms.
Params:
Name | Type | Attribute | Description |
obj | Object | The object to extract from |
Example:
toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
public toString(val: *): string source
import toString from '@tinkoff/utils/string/toString'
Converts value
to a string. An empty string is returned for null
and undefined
values.
Params:
Name | Type | Attribute | Description |
val | * | The value to convert. |
Example:
toString(null); // => ''
toString('test'); // => 'test'
toString([1, 2, 3]); => '1,2,3'
public toUpper(str: String): String source
import toUpper from '@tinkoff/utils/string/toUpper'
Returns the upper case version of a string.
Params:
Name | Type | Attribute | Description |
str | String | The string to upper case. |
Example:
toLower('xyz'); //=> 'XYZ'
public trim(str: String): String source
import trim from '@tinkoff/utils/string/trim'
Removes leading and trailing whitespace from str
.
Params:
Name | Type | Attribute | Description |
str | String | The string to trim. |
Example:
trim(' abc '); // => 'abc'
public trimLeft(str: String): String source
import trimLeft from '@tinkoff/utils/string/trimLeft'
Removes leading whitespace from str
.
Params:
Name | Type | Attribute | Description |
str | String | The string to trim. |
Example:
trimLeft(' abc '); // => 'abc '
public tryCatch(tryer: Function, catcher: Function): Function source
import tryCatch from '@tinkoff/utils/function/tryCatch'
Autocurry for 2 arguments
Takes two functions, tryer and catcher.
Returns a function that, when invoked with zero or more parameters, calls the tryer with those parameters, and if tryer throws, calls the catcher with the error as first argument and original arguments as rest. If that still throws, then ¯_(ツ)_/¯
Example:
tryCatch(x => x.length, () => 0)([ 1, 2, 3 ]) // 3
tryCatch(x => x.length, () => 0)( undefined ) // 0
tryCatch(
someDataTransform,
(err, ...rest) => {
logAsyncToServer('someDataTransform failed', err, 'with arguments', rest);
return DEFAULT_VALUE;
}
)( someIncompleteData ) // DEFAULT_VALUE (error is logged somewhere)
public type(x: *): String source
import type from '@tinkoff/utils/type'
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'.
Params:
Name | Type | Attribute | Description |
x | * | 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"
public undefined(test: *): boolean source
import undefined from '@tinkoff/utils/is/undefined'
Checks if test
is undefined
.
Params:
Name | Type | Attribute | Description |
test | * | The value to check. |
Example:
isUndefined(void 0); // => true
isUndefined(null);// => false
public unescape(str: string): string source
import unescape from '@tinkoff/utils/string/unescape'
The inverse of escape
; this method converts the HTML entities
&
, <
, >
, "
, and '
in string
to
their corresponding characters.
Params:
Name | Type | Attribute | Description |
str | string | The string to unescape. |
Example:
unescape('fred, barney, & pebbles'); // => 'fred, barney, & pebbles'
public uniq(arr: Array): Array source
import uniq from '@tinkoff/utils/array/uniq'
Returns unique items in array
Params:
Name | Type | Attribute | Description |
arr | Array |
Example:
unique([1,2,1,2,3,4,1,2]) // => [1, 2, 3, 4]
public uniqBy(fn: Function, arr: Array): Array source
import uniqBy from '@tinkoff/utils/array/uniqBy'
Autocurry for 2 arguments
Returns unique items in array. Uniqueness is defined by fn
.
Example:
uniqBy(x => x % 2, [1, 2, 2, 3, 4, 5, 5, 5]) // [1, 2]
public uniqueId(): number source
import uniqueId from '@tinkoff/utils/uniqueId'
Generates a unique ID.
public update(idx: Number, x: *, list: Array): Array source
import update from '@tinkoff/utils/array/update'
Autocurry for 3 arguments
Returns a new copy of the array with the element at the provided index
replaced with the given value.
Example:
update(1, 11, [0, 1, 2]); //=> [0, 11, 2]
update(1)(11)([0, 1, 2]); //=> [0, 11, 2]
public updatePropertyValue(propertyName: String, propertyValue: Any, obj: Object): Object source
import updatePropertyValue from '@tinkoff/utils/function/updatePropertyValue'
Autocurry for 2 arguments
A function to change value by property name in object.
Example:
var fn = () => {};
var updateName = updatePropertyValue('name', 'newFn');
fn.name //=> 'fn'
var newFn = updateName(fn);
newFn.name //=> 'newFn'
fn.name //=> 'newFn'
public upperFirst(str: String): String source
import upperFirst from '@tinkoff/utils/string/upperFirst'
Converts the first character of string to upper case and returns the new string.
Params:
Name | Type | Attribute | Description |
str | String | The string to convert. |
Example:
upperFirst('foo'); //=> 'Foo'
upperFirst('über'); //=> 'Über'
upperFirst('FOO'); //=> 'FOO'
public values(obj: Object): Array source
import values from '@tinkoff/utils/object/values'
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.
Params:
Name | Type | Attribute | Description |
obj | Object | The object to extract values from |
Example:
values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
public when(pred: Function, onTrue: Function, x: *): * source
import when from '@tinkoff/utils/function/when'
Autocurry for 3 arguments
Tests the final argument by passing it to the given predicate function. If
the predicate is satisfied, the function will return the result of calling
the onTrue
function with the same argument. If the predicate is not
satisfied, the argument is returned as is.
Return:
* | Either |
Example:
// truncate :: String -> String
var truncate = R.when(
x => x.length > 10,
x => x.slice(0, 10) + '...'
);
truncate('12345'); //=> '12345'
truncate('0123456789ABC'); //=> '0123456789…'
public where(spec: Object, obj: Object): Boolean source
import where from '@tinkoff/utils/object/where'
Autocurry for 2 arguments
Takes a spec object and a test object; returns true if the test satisfies
the spec. Each of the spec's own properties must be a predicate function.
Each predicate is applied to the value of the corresponding property of the
test object. where
returns true if all the predicates return true, false
otherwise.
Example:
// pred :: Object -> Boolean
var pred = where({
a: isEqual('foo'),
b: complement(isEqual('bar')),
x: a => a > 10,
y: a => a < 20
});
pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
public with(createCache: Function, hasher: Function, function: Function): Function source
import with from '@tinkoff/utils/function/memoize/with'
Autocurry for 3 argumentsmemoizeWith
is a memoize function fabric. It has simple classic memoization signature.
Use it to build your own memoization function.
It takes three params: 1) function that will be used to create cache object. Cache must have three methods - get, has, set. So it's compatible with Map, WeakMap, LRU (https://www.npmjs.com/package/lru-cache) instances. 2) function that will be used to create hash key for cache lookup. It is invoked with arguments for result function. 3) function, that will be memoized. *
Example:
const createCache = () => new Map();
const hasher = (...args) => JSON.stringify(args);
const memoize = memoizeWith(createCache, hasher);
const add = (a,b) => a + b;
const memoizeAdd = memoize(add);
const sum = add(1,3); // from original add call
const sum = add(1,4); // from original add call
const sum = add(1,3); // from cache
public without(xs: Array, list: Array): Array source
import without from '@tinkoff/utils/array/without'
Autocurry for 2 arguments
Returns a new list without values in the first argument.
Example:
without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
public words(str: String): String[] source
import words from '@tinkoff/utils/string/words'
Splits string into an array of its words.
This function supports only latin chars.
Params:
Name | Type | Attribute | Description |
str | String | The string to split. |
Example:
words('fooBarZoo'); //=> ['foo', 'Bar', 'Zoo']
words('foo_bar_zoo'); //=> ['foo', 'bar', 'zoo']
words('foo-bar-zoo'); //=> ['foo', 'bar', 'zoo']
words('foo bar zoo'); //=> ['foo', 'bar', 'zoo']
words('fooBär'); //=> ['foo', 'B', 'r'] only latin chars are supported
public zip(a: Array, b: Array): Array source
import zip from '@tinkoff/utils/array/zip'
Autocurry for 2 arguments
Creates a new list out of the two supplied by pairing up equally-positioned
items from both lists. The returned list is truncated to the length of the
shorter of the two input lists.
Example:
zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
public zipWith(fn: Function, a: Array, b: Array): Array source
import zipWith from '@tinkoff/utils/array/zipWith'
Autocurry for 3 arguments
Creates a new list out of the two supplied by applying the function to each
equally-positioned pair in the lists. The returned list is truncated to the
length of the shorter of the two input lists.
Example:
var f = (x, y) => {
// ...
};
zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
//=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
Static Private
private _path(paths: *[], obj: {}): * source
Autocurry for 2 arguments
null
Params:
Name | Type | Attribute | Description |
paths | *[] |
|
|
obj | {} |
|
Return:
* |