@cgnal/utils/object

Object utils.

Source:

Methods

(static) makeEnum(values) → {Object}

Source:
Since:
  • 0.0.2

Creates an "enumerator" from a list of strings.
The result will be a frozen object having both as keys and values the strings in the list.

Example
const DIRECTION = makeEnum(["BOTTOM", "LEFT", "RIGHT", "UP"]);

// DIRECTION will be:
//     {
//         BOTTOM: "BOTTOM",
//         LEFT: "LEFT",
//         RIGHT: "RIGHT",
//         UP: "UP"
//     }
Parameters:
Name Type Description
values Array.<String>
Returns:
Type
Object

(static) ownedToMap(source) → {Map.<String, Any>}

Source:
Since:
  • 0.2.0

Builds a Map from the owned enumerable properties of an object.

Example
const person = { name: "Jane", city: "New York" };

ownedToMap(person) // =>  Map { name → "Jane", city → "New York" }
Parameters:
Name Type Description
source Object
Returns:
Type
Map.<String, Any>

(static) ownedValuesToSet(source) → {Set}

Source:
Since:
  • 0.2.0

Builds a Set from the values of the owned enumerable properties of an object.

Example
const source = { a: 1, b: 2, c: 1 };

ownedValuesToSet(source) // =>  Set [ 1, 2 ]
Parameters:
Name Type Description
source Object
Returns:
Type
Set

(static) partitionKeysWith(predicate) → {function}

Source:
Since:
  • 0.0.5

Partitions a source object into an array of two objects. The first one will have the keys of the source satisfying the given predicate, the other one the remaining keys.

Example
const person = {
    name: "John",
    surname: "Doe",
    age: 45,
    score: 23.7
};
const isNumber = v => typeof v === "number";
const partitionNumberKeys = partitionKeysWith(isNumber);

partitionNumberKeys(person) // =>
// [
//     { age: 45, score: 23.7 },
//     { name: "John", surname: "Doe" }
// ]
Parameters:
Name Type Description
predicate function

a Lamb ObjectIteratorCallback returning a Boolean

Returns:

Object => [Object, Object]

Type
function

(static) putInKey(key) → {function}

Source:
Since:
  • 0.0.1

With the given key, builds a function expecting a value to create an object with a single key / value pair.

Example
const createEntity = putInKey("id");

createEntity(1) // => { id: 1 }
createEntity(2) // => { id: 2 }
Parameters:
Name Type Description
key String
Returns:

value => {[key]: value}

Type
function

(static) renameWithTransformer(transformer) → {function}

Source:
Since:
  • 0.0.5

Creates a copy of an object with keys renamed by using the given transformer to generate the new property names.

Example
const person = {
    name: "John",
    surname: "Doe",
    age: 45,
    score: 23.7
};
const toUpperCasePerson = renameWithTransformer(s => s.toUpperCase());

toUpperCasePerson(person) // =>
// {
//     NAME: "John",
//     SURNAME: "Doe",
//     AGE: 45,
//     SCORE: 23.7
// }
Parameters:
Name Type Description
transformer function

oldKey:String => newKey:String

Returns:

Object => Object

Type
function