Utility methods used by JSData.
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
orglobal.Promise
.Exampleimport 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 providedprops
.Method parameters:Name Type Description target
Object That to which
props
should be added.props
Object Properties to be added to
target
.Exampleimport {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.
Exampleimport {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
Exampleimport {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
.Exampleimport {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.
-
<static> deepFillIn(dest, source)
-
Recursively shallow fill in own enumerable properties from
source
todest
.Method parameters:Name Type Description dest
Object The destination object.
source
Object The source object.
Exampleimport {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
todest
.Method parameters:Name Type Description dest
Object The destination object.
source
Object The source object.
Exampleimport {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.
Exampleimport {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
==
. -
<static> err(domain, target)
-
Produce a factory function for making Error objects with the provided metadata. Used throughout the various js-data components.
Return value:Type Description Function Factory function.
-
<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.
-
<static> extend(props, props)
-
Used for sublcassing. Invoke this method in the context of a superclass to to produce a subclass based on
props
andclassProps
.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.
Exampleimport {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
todest
that are onsrc
but are missing from `dest.Method parameters:Name Type Description dest
Object The destination object.
source
Object The source object.
Exampleimport {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.
-
<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.
-
<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
.Exampleimport {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.
-
<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.
Exampleimport {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).
-
<static> intersection(array1, array2)
-
Return the intersection of two arrays.
Return value:Type Description Array Array of elements common to both arrays.
-
<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.
-
<static> isBlacklisted(prop, blacklist)
-
Return whether
prop
is matched by any string or regular expression inblacklist
.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. -
<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.
-
<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.
-
<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.
-
<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.
-
<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
. -
<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.
-
<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.
-
<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.
-
<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.
-
<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.
-
<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
. -
<static> logify(target)
-
Mix in logging capabilities to the target.
Method parameters:Name Type Description target
* The target.
-
<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.
-
<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
, minuskeys
. -
<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 includingkeys
. -
<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
. -
<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
. -
<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.
-
<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.
-
<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.
-
<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.