Home Reference Source Repository
import ObjUtil from '@xailabs/utils-obj/src/ObjUtil.js'
public class | source

ObjUtil

A class with static util methods for working with objects.

Static Method Summary

Static Public Methods
public static

diff(a: object, b: object, names: Array<String>): *

Performs a shallow comparison of two objects and returns an array containing the names of changed properties, or null if all values were strictly equal.

public static

keys(obj: object, options: object): object

Like Object.keys, but allows you to omit certain names.

public static

pick(obj: object, keys: Array): object

Returns a new object that contains only specific values of an original object.

public static

pojo(value: any): any

Deeply converts an immutable or mixed value to a plain javascript object.

public static

Register the immutable.js library.

public static

rest(obj: object, options: object): object

Returns an object containing all properties except the ones specified via {not: [...]}

public static

values(obj: object): Array<*>

Returns an array containing all values of an object, like as Object.values in ES6.

Static Public Methods

public static diff(a: object, b: object, names: Array<String>): * source

Performs a shallow comparison of two objects and returns an array containing the names of changed properties, or null if all values were strictly equal. Works with both immutable and plain JS objects.

Can be used for shouldComponentUpdate, e.g. if (Obj.diff(this.props, nextProps, ['name', 'address'])) { ... } is basically the same as if (this.props.name !== nextProps.name || this.props.address !== nextProps.address) { ... }

Params:

NameTypeAttributeDescription
a object

The first object

b object

The second object

names Array<String>

An array of property names to compare or null if nothing was changed

Return:

*

public static keys(obj: object, options: object): object source

Like Object.keys, but allows you to omit certain names.

Params:

NameTypeAttributeDescription
obj object

An object

options object
  • optional

An object with optional configuration

options.not Array
  • optional

An array of property names to exclude

Return:

object

A new object containing all keys (property names) of the object except the excluded ones

Example:

Obj.keys(this.props, {not: ['onClick']})

public static pick(obj: object, keys: Array): object source

Returns a new object that contains only specific values of an original object. If the original object was immutable, the result will be immutable too.

Params:

NameTypeAttributeDescription
obj object

An object

keys Array

An array of property names to pick from the object

Return:

object

A new object containing the specified properties

public static pojo(value: any): any source

Deeply converts an immutable or mixed value to a plain javascript object. If the given object itself was a primitive, undefined or null, it is returned as-is.

Params:

NameTypeAttributeDescription
value any

A plain or immutable value

Return:

any

A plain object or a primitive value.

public static registerImmutable(immutable: object) source

Register the immutable.js library. This is only required by the pick() method when used on immutable objects.

Params:

NameTypeAttributeDescription
immutable object

The immutable.js library export.

Example:

ObjUtil.registerImmutable(require('immutable'))

public static rest(obj: object, options: object): object source

Returns an object containing all properties except the ones specified via {not: [...]}

Params:

NameTypeAttributeDescription
obj object

An object

options object
  • optional

An object with optional configuration

options.not Array
  • optional

An array of property names to exclude

Return:

object

A new object containing all properties except the excluded ones

Example:

const clean = Obj.rest({a: 'a', b: 'b', c: 'c'}, {not: ['b', 'c']}); // {a: 'a'}

public static values(obj: object): Array<*> source

Returns an array containing all values of an object, like as Object.values in ES6.

Params:

NameTypeAttributeDescription
obj object

The object to inspect

Return:

Array<*>

The values of all object keys. Order is not safe.