Utils

APIUtils

Access it with:

const { utils } = Neft;

Source

utils.NOP

Type:
Function

No operation (an empty function).

Source

utils.is(value1, value2)

Parameters:
  • value1 — Any
  • value2 — Any
Returns:
Boolean

Returns true if the given values are exactly the same.

It's the Object.is() function polyfill (introduced in ECMAScript 6).

In opposite to the === operator, this function treats two NaNs as equal, and -0 and +0 as not equal.

console.log(utils.is('a', 'a'));
// true

console.log(utils.is(NaN, NaN));
// true, but ...
console.log(NaN === NaN);
// false

console.log(utils.is(-0, 0));
// false, but ...
console.log(-0 === 0);
// true

Source

utils.isFloat(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is a finite number.

console.log(utils.isFloat(10));
// true

console.log(utils.isFloat(0.99));
// true

console.log(utils.isFloat(NaN));
// false

console.log(utils.isFloat(Infinity));
// false

console.log(utils.isFloat('10'));
// false

Source

utils.isInteger(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is an integer.

console.log(utils.isInteger(10));
// true

console.log(utils.isInteger(-2));
// true

console.log(utils.isInteger(1.22));
// false

console.log(utils.isInteger('2'));
// false

Source

utils.isPrimitive(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is a null, string, number, boolean or an undefined.

console.log(utils.isPrimitive(null));
// true

console.log(utils.isPrimitive('abc'));
// true

console.log(utils.isPrimitive([]));
// false

Source

utils.isObject(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is an object (object, array, but not a null).

console.log(utils.isObject({}));
// true

console.log(utils.isObject([]));
// true

console.log(utils.isObject(null));
// false

console.log(utils.isObject(''));
// false

console.log(utils.isObject(function(){}));
// false

Source

utils.isPlainObject(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is an object with no prototype, or with a prototype equal the Object.prototype.

console.log(utils.isPlainObject({}))
// true

console.log(utils.isPlainObject(Object.create(null));
// true

console.log(utils.isPlainObject([]))
// false

console.log(utils.isPlainObject(function(){}))
// false

function User(){}
console.log(utils.isPlainObject(new User))
// false

console.log(utils.isPlainObject(Object.create({propertyInProto: 1})))
// false

Source

utils.isArguments(value)

Parameters:
  • value — Any
Returns:
Boolean

Returns true if the given value is an arguments object.

(function(){
  console.log(utils.isArguments(arguments))
  // true
})();

console.log(utils.isArguments({}))
// false

Source

utils.merge(source, object)

Parameters:
  • source — NotPrimitive
  • object — NotPrimitive
Returns:
NotPrimitive

Overrides the given source object properties by the given object own properties.

The source object is returned.

var config = {a: 1, b: 2};
utils.merge(config, {b: 99, d: 100});
console.log(config);
// {a: 1, b: 99, d: 100}

Source

utils.mergeAll(source, objects...)

Parameters:
  • source — NotPrimitive
  • objects... — NotPrimitive
Returns:
NotPrimitive

Like the utils.merge(), but the amount of objects to merge is unknown.

var config = {a: 1};
utils.merge(config, {b: 2}, {c: 3});
console.log(config);
// {a: 1, b: 2, c: 3}

Source

utils.mergeDeep(source, object)

Parameters:
  • source — NotPrimitive
  • object — NotPrimitive
Returns:
NotPrimitive

Overrides the given source object properties and all its objects by the given object own properties.

The source object is returned.

var user = {
  name: 'test',
  carsByName: {
    tiny: 'Ferrharhi',
    monkey: 'BMM'
  }
}

utils.mergeDeep(user, {
  name: 'Johny',
  carsByName: {
    nextCar: 'Fita'
  }
});

console.log(user);
// {name: 'Johny', carsByName: {tiny: 'Ferrharhi', monkey: 'BMM', nextCar: 'Fita'}}

Source

utils.fill(source, object)

Parameters:
  • source — NotPrimitive
  • object — NotPrimitive
Returns:
NotPrimitive

Sets the given object properties into the given source object if the property exists in the given source, but it's not defined as an own property.

The source object is returned.

function User(){
}

User.prototype.name = '';

var user = new User;
utils.fill(user, {name: 'Johny', age: 40});
console.log(user);
// {name: 'Johny'}

Source

utils.remove(object, element)

Parameters:
  • object — NotPrimitive
  • element — Any

Removes an array element or an object property from the given object.

var array = ['a', 'b', 'c'];
utils.remove(array, 'b');
console.log(array);
// ['a', 'c']

var object = {a: 1, b: 2};
utils.remove(object, 'a');
console.log(object);
// {b: 2}

Source

utils.removeFromUnorderedArray(array, element)

Parameters:
  • array — Array
  • element — Any

Removes the given element from the given array.

Elements order may be changed.

Source

utils.getPropertyDescriptor(object, property)

Parameters:
  • object — NotPrimitive
  • property — String
Returns:
Object

Returns the descriptor of the given property defined in the given object.

function User(){
  this.age = 0;
}

utils.defineProperty(User.prototype, 'isAdult', utils.CONFIGURABLE, function(){
  return this.age >= 18;
}, null);

var user = new User;
console.log(utils.getPropertyDescriptor(user, 'isAdult'));
// {enumerable: false, configurable: true, get: ..., set: undefined}

Source

utils.lookupGetter(object, property)

Parameters:
  • object — NotPrimitive
  • property — String
Returns:
Function

Returns the given property getter function defined in the given object.

var object = {loaded: 2, length: 5};
utils.defineProperty(object, 'progress', null, function(){
  return this.loaded / this.length;
}, null);
console.log(utils.lookupGetter(object, 'progress'));
// function(){ return this.loaded / this.length; }

Source

utils.lookupSetter(object, property)

Parameters:
  • object — NotPrimitive
  • property — String
Returns:
Function

Returns the given property setter function defined in the given object.

Source

utils.defineProperty(object, property, descriptors, value, setter)

Parameters:
  • object — NotPrimitive
  • property — String
  • descriptors — Integer
  • value — Anyoptional
  • setter — Functionoptional
Returns:
NotPrimitive

Defines the given property in the given object.

The descriptors argument is a bitmask accepting utils.WRITABLE, utils.ENUMERABLE and utils.CONFIGURABLE.

The value argument becomes a getter function if the given setter is not an undefined.

var object = {};

var desc = utils.ENUMERABLE | utils.WRITABLE | utils.CONFIGURABLE;
utils.defineProperty(object, 'name', desc, 'Emmy');
console.log(object.name);
// Emmy

utils.defineProperty(object, 'const', utils.ENUMERABLE, 'constantValue');
console.log(object.const);
// constantValue

utils.defineProperty(object, 'length', utils.ENUMERABLE | utils.CONFIGURABLE, function(){
  return 2;
}, null);
console.log(object.length);
// 2

Source

utils.overrideProperty(object, property, value, setter)

Parameters:
  • object — NotPrimitive
  • property — String
  • value — Anyoptional
  • setter — Functionoptional
Returns:
NotPrimitive

Source

utils.clone(param)

Parameters:
  • param — Any
Returns:
Any

Returns clone of the given array or object.

console.log(utils.clone([1, 2]))
// [1, 2]

console.log(utils.clone({a: 1}))
// {a: 1}

Source

utils.cloneDeep(param)

Parameters:
  • param — Any
Returns:
Any

Returns deep clone of the given array or object.

var obj2 = {ba: 1};
var obj = {a: 1, b: obj2};

var clonedObj = utils.cloneDeep(obj);
console.log(clonedObj);
// {a: 1, b: {ba: 1}}

console.log(clonedObj.b === obj.b)
// false

Source

utils.isEmpty(object)

Parameters:
  • object — NotPrimitive or String
Returns:
Boolean

Returns true if the given array has no elements, of the given object has no own properties.

console.log(utils.isEmpty([]));
// true

console.log(utils.isEmpty([1, 2]));
// false

console.log(utils.isEmpty({}));
// true

console.log(utils.isEmpty({a: 1}));
// false

console.log(utils.isEmpty(''));
// true

Source

utils.last(array)

Parameters:
  • array — NotPrimitive
Returns:
Any

Returns the last element of the given array, or an array-like object.

console.log(utils.last(['a', 'b']))
// b

console.log(utils.last([]))
// undefined

Source

utils.clear(object)

Parameters:
  • object — NotPrimitive
Returns:
NotPrimitive

Removes all elements from the given array, or all own properties from the given object.

var arr = ['a', 'b'];
utils.clear(arr);
console.log(arr);
// []

var obj = {age: 37};
utils.clear(obj);
console.log(obj);
// {}

Source

utils.setPrototypeOf(object, prototype)

Parameters:
  • object — NotPrimitive
  • prototype — Null or NotPrimitive
Returns:
Object

Changes the given object prototype into the given prototype.

This function on some environments returns a new object.

var obj = {a: 1};
var prototype = {b: 100};

var newObj = utils.setPrototypeOf(obj, prototype);

console.log(Object.getPrototypeOf(newObj) === prototype)
// true

console.log(newObj.a)
// 1

console.log(newObj.b)
// 100

Source

utils.has(object, value)

Parameters:
  • object — Any
  • value — Any
Returns:
Boolean

Returns true if the given array contains the given value.

console.log(utils.has(['a'], 'a'))
// true

console.log(utils.has(['a'], 'b'))
// false

Returns true if the given object has an own property names as the given value.

var object = {
  city: 'New York'
}

console.log(utils.has(object, 'New York'))
// true

Returns true if the given string contains the given value.

console.log(utils.has('abc', 'b'))
// true

console.log(utils.has('abc', 'e'))
// false

Source

utils.objectToArray(object, valueGen, target)

Parameters:
  • object — Object
  • valueGen — Functionoptional
  • target — Array= []optional
Returns:
Array

Translates the given object into an array.

Array elements are determined by the given valueGen function. The valueGen function is called with the property name, property value and the given object.

By default, the valueGen returns the object property value.

Created elements are set into the given target array (a new array by default).

var object = {
  type: 'dog',
  name: 'Bandit'
};

console.log(utils.objectToArray(object));
// ['dog', 'Bandit']

console.log(utils.objectToArray(object, function(key, val){
  return key + "_" + val;
}));
// ['type_dog', 'name_Bandit']

Source

utils.arrayToObject(array, keyGen, valueGen, target)

Parameters:
  • array — Array
  • keyGen — Functionoptional
  • valueGen — Functionoptional
  • target — Object= {}optional
Returns:
Object

Translates the given array into an object.

Object keys are determined by the given keyGen function. Object key values are determined by the given valueGen function. The keyGen and valueGen functions are called with the array element index, array element value and the array itself.

By default, the keyGen function returns the array element index. By default, the valueGen function returns the array element value.

Created proeprties are set into the given target object (a new object by default).

console.log(utils.arrayToObject(['a', 'b']))
// {0: 'a', 1: 'b'}

console.log(utils.arrayToObject(['a'], function(i, elem){
  return "value_" + elem;
}));
// {"value_a": "a"}

console.log(utils.arrayToObject(['a'], function(i, elem){
  return elem;
}, function(i, elem){
  return i;
}));
// {"a": 0}

Source

utils.capitalize(string)

Parameters:
  • string — String
Returns:
String

Capitalizes the given string.

console.log(utils.capitalize('name'))
// Name

Source

utils.addSlashes(string)

Parameters:
  • string — String
Returns:
String

Adds backslashes before each ' and " characters found in the given string.

console.log(utils.addSlashes('a"b'))
// a\"b

Source

utils.uid(length)

Parameters:
  • length — Integer= 8optional
Returns:
String

Returns pseudo-unique string with the given length.

This function doesn't quarantee uniqueness of the returned data.

console.log(utils.uid())
// "50"

Source

utils.tryFunction(function, context, arguments, onFail)

Parameters:
  • function — Function
  • context — Anyoptional
  • arguments — Arrayoptional
  • onFail — Anyoptional
Returns:
Any

Calls the given function with the given context and arguments.

If the function throws an error, the given onFail value is returned.

If the given onFail is a function, it will be called with the caught error.

function test(size){
  if (size === 0){
    throw "Wrong size!";
  }
}

console.log(utils.tryFunction(test, null, [0]))
// undefined

console.log(utils.tryFunction(test, null, [0], 'ERROR!'))
// ERROR!

console.log(utils.tryFunction(test, null, [100], 'ERROR!'))
// undefined

Source

utils.catchError(function, context, arguments)

Parameters:
  • function — Function
  • context — Anyoptional
  • arguments — Arrayoptional
Returns:
Any

Calls the given function with the given context and arguments.

Returns caught error.

function test(size){
  if (size === 0){
    throw "Wrong size!";
  }
}

console.log(utils.catchError(test, null, [0]))
// "Wrong size!"

console.log(utils.catchError(test, null, [100]))
// null

Source

utils.bindFunctionContext(function, context)

Parameters:
  • function — Function
  • context — Any
Returns:
Function

Returns a new function calling the given function with the given context and arguments in an amount lower or equal the function length.

function func(arg1){
  console.log(this, arg1);
}

var bindFunc = utils.bindFunctionContext(func, {ctx: 1});

console.log(bindFunc('a'));
// {ctx: 1} "a"

Source

utils.errorToObject(error)

Parameters:
  • error — Object
Returns:
Object

Returns a plain object with the given error name, message and other custom properties.

Standard error name and message properties are not enumerable.

var error = new ReferenceError('error message!');
console.log(utils.errorToObject(error));
// {name: 'ReferenceError', message: 'error message!'}

Source

utils.getOwnProperties(object)

Parameters:
  • object — Object
Returns:
Object

Returns an array or an object with own properties associated in the given object.

Source

utils.deprecate(function, message)

Parameters:
  • function — Function
  • message — String
Returns:
Function

Prints a warning that the given function should not be used.

Returns a new function which warns once by default.

Source

utils.isEqual(object1, object2, compareFunction, maxDeep)

Parameters:
  • object1 — Object
  • object2 — Object
  • compareFunction — Functionoptional
  • maxDeep — Integer= Infinityoptional
Returns:
Boolean

Returns true if the given objects have equal values.

The given compareFunction is used to compare two values (which at least one them is primitive). By default the compareFunction uses triple comparison (===).

utils.isEqual([1, 0], [1, 0])
// true

utils.isEqual({a: 1}, {a: 1})
// true

utils.isEqual({a: {aa: 1}}, {a: {aa: 1}})
// true

utils.isEqual([0, 1], [1, 0])
// false

utils.isEqual({a: {aa: 1}}, {a: {aa: 1, ab: 2}})
// false

Source

utils.snakeToCamel(value)

Parameters:
  • value — String
Returns:
String

Source

utils.kebabToCamel(value)

Parameters:
  • value — String
Returns:
String

Source

utils.camelToKebab(value)

Parameters:
  • value — String
Returns:
String

Source