Namespace: utils

utils

Utility methods used by JSData.

Details
Type Source
Object utils.js, line 1
Example
import {utils} from 'js-data'
console.log(utils.isString('foo')) // true

Members


<static> Promise

Reference to the Promise constructor used by JSData. Defaults to window.Promise or global.Promise.

Details
Type Since Source
Function 3.0.0 utils.js, line 68
Example

Make JSData use a different `Promise` constructor

import Promise from 'bluebird'
import {utils} from 'js-data'
utils.Promise = Promise

Methods


<static> addHiddenPropsToTarget(target, props)

Define hidden (non-enumerable), writable properties on target from the provided props.

Method parameters:
Name Type Description
target Object

That to which props should be added.

props Object

Properties to be added to target.

Details
Since Source
3.0.0 utils.js, line 175
Example
import {utils} from 'js-data'
function Cat () {}
utils.addHiddenPropsToTarget(Cat.prototype, {
  say () {
    console.log('meow')
  }
})
const cat = new Cat()
cat.say() // "meow"

<static> areDifferent(a, b, opts)

Return whether the two objects are deeply different.

Method parameters:
Name Type Argument Description
a Object

Base object.

b Object

Comparison object.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
equalsFn Function <optional>
utils.deepEqual

Equality function.

ignore Array <optional>
[]

Array of strings or RegExp of fields to ignore.

Return value:
Type Description
Boolean

Whether the two objects are deeply different.

Details
Since Source See
3.0.0 utils.js, line 206
Example
import {utils} from 'js-data'
utils.areDifferent({}, {}) // false
utils.areDifferent({ a: 1 }, { a: 1 }) // false
utils.areDifferent({ foo: 'bar' }, {}) // true

<static> classCallCheck(instance, ctor)

Verified that the given constructor is being invoked via new, as opposed to just being called like a normal function.

Method parameters:
Name Type Description
instance *

Instance that is being constructed.

ctor Constructor

Constructor function used to construct the instance.

Throws:

Throws an error if the constructor is being improperly invoked.

Type
Error
Details
Since Source
3.0.0 utils.js, line 234
Example
import {utils} from 'js-data'
function Cat () {
  utils.classCallCheck(this, Cat)
}
const cat = new Cat() // this is ok
Cat() // this throws an error

<static> copy(from, to, stackFrom, stackTo, blacklist, plain)

Deep copy a value.

Method parameters:
Name Type Argument Description
from *

Value to deep copy.

to * <optional>

Destination object for the copy operation.

stackFrom * <optional>

For internal use.

stackTo * <optional>

For internal use.

blacklist Array.<String> | Array.<RegExp> <optional>

List of strings or RegExp of properties to skip.

plain Boolean <optional>

Whether to make a plain copy (don't try to use original prototype).

Return value:
Type Description
*

Deep copy of from.

Details
Since Source
3.0.0 utils.js, line 281
Example
import {utils} from 'js-data'
const a = { foo: { bar: 'baz' } }
const b = utils.copy(a)
a === b // false
utils.areDifferent(a, b) // false

<static> deepEqual(a, b)

Check whether the two provided objects are deeply equal.

Method parameters:
Name Type Description
a Object

First object in the comparison.

b Object

Second object in the comparison.

Return value:
Type Description
Boolean

Whether the two provided objects are deeply equal.

Details
Since Source See
3.0.0 utils.js, line 1194

<static> deepFillIn(dest, source)

Recursively shallow fill in own enumerable properties from source to dest.

Method parameters:
Name Type Description
dest Object

The destination object.

source Object

The source object.

Details
Since Source See
3.0.0 utils.js, line 356
Example
import {utils} from 'js-data'
const a = { foo: { bar: 'baz' }, beep: 'boop' }
const b = { beep: 'bip' }
utils.deepFillIn(b, a)
console.log(b) // {"foo":{"bar":"baz"},"beep":"bip"}

<static> deepMixIn(dest, source)

Recursively shallow copy own enumerable properties from source to dest.

Method parameters:
Name Type Description
dest Object

The destination object.

source Object

The source object.

Details
Since Source See
3.0.0 utils.js, line 388
Example
import {utils} from 'js-data'
const a = { foo: { bar: 'baz' }, beep: 'boop' }
const b = { beep: 'bip' }
utils.deepFillIn(b, a)
console.log(b) // {"foo":{"bar":"baz"},"beep":"boop"}

<static> diffObjects(newObject, oldObject, opts)

Return a diff of the base object to the comparison object.

Method parameters:
Name Type Argument Description
newObject Object

Comparison object.

oldObject Object

Base object.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
equalsFn Function <optional>
utils.deepEqual

Equality function.

ignore Array <optional>
[]

Array of strings or RegExp of fields to ignore.

Return value:
Type Description
Object

The diff from the base object to the comparison object.

Details
Since Source See
3.0.0 utils.js, line 419
Example
import {utils} from 'js-data'
const oldObject = { foo: 'bar', a: 1234 }
const newObject = { beep: 'boop', a: 5678 }
const diff = utils.diffObjects(oldObject, newObject)
console.log(diff.added) // {"beep":"boop"}
console.log(diff.changed) // {"a":5678}
console.log(diff.removed) // {"foo":undefined}

<static> equal(a, b)

Return whether the two values are equal according to the == operator.

Method parameters:
Name Type Description
a *

First value in the comparison.

b *

Second value in the comparison.

Return value:
Type Description
Boolean

Whether the two values are equal according to ==.

Details
Since Source
3.0.0 utils.js, line 487

<static> err(domain, target)

Produce a factory function for making Error objects with the provided metadata. Used throughout the various js-data components.

Method parameters:
Name Type Description
domain String

Namespace.

target String

Target.

Return value:
Type Description
Function

Factory function.

Details
Since Source
3.0.0 utils.js, line 500

<static> eventify(target, getter, setter)

Add eventing capabilities into the target object.

Method parameters:
Name Type Argument Description
target Object

Target object.

getter Function <optional>

Custom getter for retrieving the object's event listeners.

setter Function <optional>

Custom setter for setting the object's event listeners.

Details
Since Source
3.0.0 utils.js, line 519

<static> extend(props, props)

Used for sublcassing. Invoke this method in the context of a superclass to to produce a subclass based on props and classProps.

Method parameters:
Name Type Description
props Object

Instance properties for the subclass.

Properties
Name Type Argument Description
constructor Object <optional>

Provide a custom constructor function to use as the subclass.

props Object

Static properties for the subclass.

Return value:
Type Description
Constructor

A new subclass.

Details
Since Source
3.0.0 utils.js, line 588
Example
import {utils} from 'js-data'
function Animal () {}
Animal.extend = utils.extend
const Cat = Animal.extend({
  say () {
    console.log('meow')
  }
})
const cat = new Cat()
cat instanceof Animal // true
cat instanceof Cat // true
cat.say() // "meow"

<static> fillIn(dest, source)

Shallow copy own enumerable properties from src to dest that are on src but are missing from `dest.

Method parameters:
Name Type Description
dest Object

The destination object.

source Object

The source object.

Details
Since Source See
3.0.0 utils.js, line 665
Example
import {utils} from 'js-data'
const a = { foo: 'bar', beep: 'boop' }
const b = { beep: 'bip' }
utils.fillIn(b, a)
console.log(b) // {"foo":"bar","beep":"bip"}

<static> findIndex(array, fn)

Find the last index of something according to the given checker function.

Method parameters:
Name Type Description
array Array

The array to search.

fn Function

Checker function.

Return value:
Type Description
Number

Index if found or -1 if not found.

Details
Since Source
3.0.0 utils.js, line 691

<static> forEachRelation(mapper, opts, fn, thisArg)

Recursively iterate over a Mapper's relations according to opts.with.

Method parameters:
Name Type Description
mapper Mapper

Mapper.

opts Object

Configuration options.

fn Function

Callback function.

thisArg *

Execution context for the callback function.

Details
Since Source
3.0.0 utils.js, line 714

<static> forOwn(object, fn, thisArg)

Iterate over an object's own enumerable properties.

Method parameters:
Name Type Argument Description
object Object

The object whose properties are to be enumerated.

fn Function

Iteration function.

thisArg Object <optional>

Content to which to bind fn.

Details
Since Source
3.0.0 utils.js, line 735
Example
import {utils} from 'js-data'
const a = { b: 1, c: 4 }
let sum = 0
utils.forOwn(a, function (value, key) {
  sum += value
})
console.log(sum) // 5

<static> fromJson(json)

Proxy for JSON.parse.

Method parameters:
Name Type Description
json String

JSON to parse.

Return value:
Type Description
Object

Parsed object.

Details
Since Source See
3.0.0 utils.js, line 762

<static> get(object, prop)

Retrieve the specified property from the given object. Supports retrieving nested properties.

Method parameters:
Name Type Description
object Object

Object from which to retrieve a property's value.

prop String

Property to retrieve.

Return value:
Type Description
*

Value of the specified property.

Details
Since Source See
3.0.0 utils.js, line 775
Example
import {utils} from 'js-data'
const a = { foo: { bar: 'baz' }, beep: 'boop' }
console.log(utils.get(a, 'beep')) // "boop"
console.log(utils.get(a, 'foo.bar')) // "bar"

<static> getSuper(instance, isCtor)

Return the superclass for the given instance or subclass. If an instance is provided, then finds the parent class of the instance's constructor.

Method parameters:
Name Type Argument Default Description
instance Object | Function

Instance or constructor.

isCtor Boolean <optional>
false

Whether instance is a constructor.

Return value:
Type Description
Constructor

The superclass (grandparent constructor).

Details
Since Source
3.0.0 utils.js, line 809

<static> intersection(array1, array2)

Return the intersection of two arrays.

Method parameters:
Name Type Description
array1 Array

First array.

array2 Array

Second array.

Return value:
Type Description
Array

Array of elements common to both arrays.

Details
Since Source
3.0.0 utils.js, line 827

<static> isArray(value)

Proxy for Array.isArray.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is an array.

Details
Since Source
3.0.0 utils.js, line 856

<static> isBlacklisted(prop, blacklist)

Return whether prop is matched by any string or regular expression in blacklist.

Method parameters:
Name Type Description
prop String

The name of a property to check.

blacklist Array

Array of strings and regular expressions.

Return value:
Type Description
Boolean

Whether prop was matched.

Details
Since Source
3.0.0 utils.js, line 866

<static> isBoolean(value)

Return whether the provided value is a boolean.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a boolean.

Details
Since Source
3.0.0 utils.js, line 890

<static> isDate(value)

Return whether the provided value is a date.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Date

Whether the provided value is a date.

Details
Since Source
3.0.0 utils.js, line 902

<static> isFunction(value)

Return whether the provided value is a function.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a function.

Details
Since Source
3.0.0 utils.js, line 914

<static> isInteger(value)

Return whether the provided value is an integer.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is an integer.

Details
Since Source
3.0.0 utils.js, line 926

<static> isNull(value)

Return whether the provided value is null.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is null.

Details
Since Source
3.0.0 utils.js, line 938

<static> isNumber(value)

Return whether the provided value is a number.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a number.

Details
Since Source
3.0.0 utils.js, line 950

<static> isObject(value)

Return whether the provided value is an object.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is an object.

Details
Since Source
3.0.0 utils.js, line 963

<static> isRegExp(value)

Return whether the provided value is a regular expression.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a regular expression.

Details
Since Source
3.0.0 utils.js, line 975

<static> isSorN(value)

Return whether the provided value is a string or a number.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a string or a number.

Details
Since Source
3.0.0 utils.js, line 987

<static> isString(value)

Return whether the provided value is a string.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a string.

Details
Since Source
3.0.0 utils.js, line 999

<static> isUndefined(value)

Return whether the provided value is a undefined.

Method parameters:
Name Type Description
value *

The value to test.

Return value:
Type Description
Boolean

Whether the provided value is a undefined.

Details
Since Source
3.0.0 utils.js, line 1011

<static> logify(target)

Mix in logging capabilities to the target.

Method parameters:
Name Type Description
target *

The target.

Details
Since Source
3.0.0 utils.js, line 1023

<static> noDupeAdd(array, record, fn)

Adds the given record to the provided array only if it's not already in the array.

Method parameters:
Name Type Description
array Array

The array.

record *

The value to add.

fn Function

Callback function passed to utils.findIndex.

Details
Since Source
3.0.0 utils.js, line 1053

<static> omit(props, keys)

Return a shallow copy of the provided object, minus the properties specified in keys.

Method parameters:
Name Type Description
props Object

The object to copy.

keys Array.<String>

Array of strings, representing properties to skip.

Return value:
Type Description
Object

Shallow copy of props, minus keys.

Details
Since Source
3.0.0 utils.js, line 1073

<static> pick(props, keys)

Return a shallow copy of the provided object, but only include the properties specified in keys.

Method parameters:
Name Type Description
props Object

The object to copy.

keys Array.<String>

Array of strings, representing properties to keep.

Return value:
Type Description
Object

Shallow copy of props, but only including keys.

Details
Since Source
3.0.0 utils.js, line 1093

<static> plainCopy(value)

Return a plain copy of the given value.

Method parameters:
Name Type Description
value *

The value to copy.

Return value:
Type Description
*

Plain copy of value.

Details
Since Source See
3.0.0 utils.js, line 1113

<static> reject(value)

Shortcut for utils.Promise.reject(value).

Method parameters:
Name Type Argument Description
value * <optional>

Value with which to reject the Promise.

Return value:
Type Description
Promise

Promise reject with value.

Details
Since Source See
3.0.0 utils.js, line 1126

<static> remove(array, fn)

Remove the last item found in array according to the given checker function.

Method parameters:
Name Type Description
array Array

The array to search.

fn Function

Checker function.

Details
Source
utils.js, line 1139

<static> set(object, path, value)

Set the value at the provided key or path.

Method parameters:
Name Type Argument Description
object Object

The object on which to set a property.

path String | Object

The key or path to the property. Can also pass in an object of path/value pairs, which will all be set on the target object.

value * <optional>

The value to set.

Details
Source
utils.js, line 1169

<static> toJson(value)

Proxy for JSON.stringify.

Method parameters:
Name Type Description
value *

Value to serialize to JSON.

Return value:
Type Description
String

JSON string.

Details
Since Source See
3.0.0 utils.js, line 1232

<static> unset(object, path)

Unset the value at the provided key or path.

Method parameters:
Name Type Description
object Object

The object from which to delete the property.

path String

The key or path to the property.

Details
Since Source See
3.0.0 utils.js, line 1243