Manual Reference Source Test
import {ObjectArray} from 'dot-object-array/src/index.js'
public class | source

ObjectArray

Implements array-like properties and methods to a key/value javascript object. It can be viewed as a kind of associative array in JS but it also supports dot notation keys.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public get

Returns the underlying _data object for raw reading

since 1.0.0
public set

data(data: Object)

Import data into ObjectArray.

Private Members
private

The inner data object

since 1.0.0
private

Global memorizer for automated working on a subset.

since 3.0.0
private

Global switcher for defining if ObjectArray should return undefined or throw an exception when an undefined key is requested.

since 3.0.0

Method Summary

Public Methods
public

Returns a camelized string (without uppercase leading character) Replace dashes and spaces

version 1.0.0 since 1.2.0
public

check(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey, strict: boolean): Boolean

Check the value for a given key in the ObjectArray

version 1.0.0 since 2.0.0
public

Returns the delevl down key for a given dotted key

version 1.0.0 since 1.3.0
public

clone(keepFlattened: Boolean): this

Returns a clone with same data of the current ObjectArray

version 1.0.0 since 1.3.0
public

Returns a dashed string Replace Uppercases and spaces

version 1.0.0 since 1.2.0
public

dataset(key: dottedKey, pKey: dottedKey, throwable: boolean): Object | undefined

Returns dataset for the key.

version 2.0.0 since 1.0.0
public

define(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey): this

Coalesce method to define a default value if a key is not set.

version 1.0.0 since 3.1.0
public

empty(key: dottedKey, pKey: dottedKey, throwable: boolean): this

Empty the ObjectArray data.

version 3.0.0 since 1.2.0
public

flatten(dotted: Boolean, key: dottedKey): this

Flattens the object and replace data.

version 2.0.0 since 1.3.0
public

forEach(cb: ForEachCallback, key: dottedKey, pKey: dottedKey, throwable: boolean): void

Runs a callback on each entry at the key level

version 2.0.0 since 1.0.0
public

Returns a string suitable for a form-url-encoded query string

version 2.0.0 since 1.0.0
public

getset(key: dottedKey | Object, val: number | string | array | object | dottedKey, pKey: dottedKey, throwable: boolean): GetSetObject

Shortcut for getter/setter syntax.

version 1.0.1 since 2.1.0
public

has(key: dottedKey, pKey: dottedKey): Boolean

Check if a given key exists in the ObjectArray

version 1.1.2 since 1.0.0
public

import(data: Object, pKey: dottedKey): this

Import an object as data

version 1.1.0 since 1.0.0
public

keys(key: dottedKey, pKey: dottedKey, throwable: boolean): Array

Returns keys of a given dataset in the ObjectArray If no key is provided, it will output the keys of the root data object

version 3.0.0 since 1.0.0
public

length(key: dottedKey, pKey: dottedKey, throwable: boolean): Number | undefined

Returns length of a given dataset in the ObjectArray If no key is provided, it will output the length of the root data object

version 3.0.0 since 1.0.0
public

Set the parent key global memorizer.

version 1.0.0 since 3.0.0
public

Returns e level up key for a given dotted key

version 1.0.1 since 1.0.0
public

pull(key: dottedKey, pKey: dottedKey, throwable: boolean): Object | undefined

Alias for dataset method.

version 2.0.0 since 1.4.0
public

push(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey): this

Push a new key/value pair

version 1.1.0 since 1.0.0
public

reduce(reducer: ReducerCallback, start: Mixed, key: dottedKey, pKey: dottedKey, throwable: boolean): Mixed

Reduce the ObjectArray data given a callback

version 2.0.0 since 1.0.0
public

remove(key: dottedKey, pKey: dottedKey, throwable: boolean): this

Remove key/value data

version 2.0.0 since 1.1.0
public

stringToStyles(str: String, pKey: dottedKey): this

Imports a string from a style attribute.

version 1.0.3 since 1.2.0
public

Returns a string suitable for a style attribute.

version 2.0.0 since 1.0.0
public

Overrides all throwable params settings of the object array

version 1.0.0 since 3.0.0
public

urlEncode(key: dottedKey, pKey: dottedKey, throwable: boolean): String | undefined

Returns a string suitable for a URI query string

version 2.0.0 since 1.0.0
public

values(key: dottedKey, pKey: dottedKey, throwable: boolean): Array

Returns values of a given dataset in the ObjectArray If no key is provided, it will output the values of the root data object

version 3.0.0 since 1.0.0
Private Methods
private

_key(key: dottedKey, pKey: dottedKey, throwable: boolean): dottedKey

Returns a joined dotted key from key and parent key.

version 2.0.0 since 2.0.0
private

_recurseFlatten(ret: Object, dotted: Boolean, pKey: dottedKey): Object

Private method to recurse flatten

version 1.0.0 since 1.3.0

Public Constructors

public constructor(data: Object): ObjectArray source

Params:

NameTypeAttributeDescription
data Object
  • optional
  • default: {}

Initial data to import

Return:

ObjectArray

Instance of ObjectArray

Example:

 var doa = new ObjectArray();
 var doa = new ObjectArray({
  key1: value1,
  key2: value2
 });

Test:

Public Members

public get data: Object since 1.0.0 source

Returns the underlying _data object for raw reading

Test:

See:

public set data(data: Object) source

Import data into ObjectArray.

This setter is an alias to import method.

Private Members

private _data: Object since 1.0.0 source

The inner data object

private _pKey: boolean since 3.0.0 source

Global memorizer for automated working on a subset.

It defaults to false.

private _throw: boolean since 3.0.0 source

Global switcher for defining if ObjectArray should return undefined or throw an exception when an undefined key is requested.

It is not set at ObjectArray initialization.

Public Methods

public camelize(s: String): String version 1.0.0 since 1.2.0 source

Returns a camelized string (without uppercase leading character) Replace dashes and spaces

Params:

NameTypeAttributeDescription
s String

String to camelize

Return:

String

Camelized string

Test:

public check(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey, strict: boolean): Boolean version 1.0.0 since 2.0.0 source

Check the value for a given key in the ObjectArray

Params:

NameTypeAttributeDescription
key dottedKey

Key

val Number | String | Array | Object

Value

pKey dottedKey
  • optional

Parent key

strict boolean
  • optional
  • default: true

true to perform a strict comparison (===) or false otherwise

Return:

Boolean

true if key exists and is equal to val

public childKey(key: dottedKey): String version 1.0.0 since 1.3.0 source

Returns the delevl down key for a given dotted key

Params:

NameTypeAttributeDescription
key dottedKey

Key

Return:

String

Child key

public clone(keepFlattened: Boolean): this version 1.0.0 since 1.3.0 source

Returns a clone with same data of the current ObjectArray

Params:

NameTypeAttributeDescription
keepFlattened Boolean
  • optional
  • default: true

If true, the flattened dotted keys will remains flattened otherwise, the full hierarchy will be restored

Return:

this

Chainable

Test:

public dashize(s: String): String version 1.0.0 since 1.2.0 source

Returns a dashed string Replace Uppercases and spaces

Params:

NameTypeAttributeDescription
s String
  • optional

String to dashize

Return:

String

Dashed string

Test:

public dataset(key: dottedKey, pKey: dottedKey, throwable: boolean): Object | undefined version 2.0.0 since 1.0.0 source

Returns dataset for the key. If no key is provided, the whole dataset is returned

Params:

NameTypeAttributeDescription
key dottedKey
  • optional

Key

pKey dottedKey
  • optional

Parent Key

throwable boolean
  • optional
  • default: false

Wether to throw an exception if key doesn't exist

Return:

Object | undefined

Data object

Throw:

TypeError

If key does not exist

public define(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey): this version 1.0.0 since 3.1.0 source

Coalesce method to define a default value if a key is not set.

Never use if you want to store undefined as values.

This method is obviously useless if the ObjectArray is set to throw an exception when encountering an undefined key

Params:

NameTypeAttributeDescription
key dottedKey

Key to define

val Number | String | Array | Object

Default value for the key

pKey dottedKey
  • optional

Description for pKey

Return:

this

Chainable

Test:

public empty(key: dottedKey, pKey: dottedKey, throwable: boolean): this version 3.0.0 since 1.2.0 source

Empty the ObjectArray data. It can also be used as an alias for ObjectArray~remove

Params:

NameTypeAttributeDescription
key dottedKey

Key to remove

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

this

Chainable

Throw:

TypeError

If key does not exist

Test:

public flatten(dotted: Boolean, key: dottedKey): this version 2.0.0 since 1.3.0 source

Flattens the object and replace data.

Each object in the dataset is recursively explored to extract data and bring it a top level.

With default behaviour, if two or more properties have same name under subkeys, the last explored one will replace the previous one.

If the method is called with true as first parameter, the resulting data keys will be dotted, preventing duplication. In that case, you must access new keys with dataset method or by calling the data key property with [] (e.g. doa.data['my.key']);

Flatten may be run on a subdataset by providing a key as second parameter.

Params:

NameTypeAttributeDescription
dotted Boolean
  • optional
  • default: false
key dottedKey
  • optional

Key to flatten. If not provided, the whole dataset will flattened.

Return:

this

Chainable

Throw:

TypeError

If key does not exist

Test:

public forEach(cb: ForEachCallback, key: dottedKey, pKey: dottedKey, throwable: boolean): void version 2.0.0 since 1.0.0 source

Runs a callback on each entry at the key level

Params:

NameTypeAttributeDescription
cb ForEachCallback

Callback to be run

key dottedKey
  • optional

Dotted key to limit iterations through its subset if empty, the global data object will be used

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

void

Throw:

TypeError

If key does not exist

Test:

public formUrlEncode(key: dottedKey, pKey: dottedKey, throwable: boolean): String | undefined version 2.0.0 since 1.0.0 source

Returns a string suitable for a form-url-encoded query string

Params:

NameTypeAttributeDescription
key dottedKey
  • optional

Dotted key to limit iterations through its subset if empty, the global data object will be used

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

String | undefined

style string

Throw:

TypeError

If key does not exist

public getset(key: dottedKey | Object, val: number | string | array | object | dottedKey, pKey: dottedKey, throwable: boolean): GetSetObject version 1.0.1 since 2.1.0 source

Shortcut for getter/setter syntax. It can also be used to import data.

Params:

NameTypeAttributeDescription
key dottedKey | Object
  • optional

Key / Object to import

val number | string | array | object | dottedKey
  • optional

Value to push / Parent key to import into

pKey dottedKey
  • optional

Parent Key

throwable boolean
  • optional
  • default: true

If true, getset will throw an exception if key doesn't exist in getter mode else it will return undefined

Return:

GetSetObject

getset return

Throw:

TypeError

If key does not exist and throwable is true

Example:

 // Getter mode
 i.getset(); // returns all data
 i.getset('a'); // returns data for key a
 i.getset('c'); // will throw a TypeError exception if key c doesn't exist
 i.getset('c', undefined, '', false) // returns undefined
 // Better use
 i.pull('c', '', false); // returns undefined

 // Setter mode
 i.getset('a', 'valueA'); // Set 'valueA' to key a
 i.getset('b', 'valueB', 'a'); // set 'valueB' to key a.b

 // Import mode
 i.getset({a: 'valueA', 'a.b': 'valueB'}); // Import key/values at root level
 i.getset({'b', 'valueB'}, 'a'); // Import key/values at key a

public has(key: dottedKey, pKey: dottedKey): Boolean version 1.1.2 since 1.0.0 source

Check if a given key exists in the ObjectArray

Params:

NameTypeAttributeDescription
key dottedKey

Key

pKey dottedKey
  • optional

Parent key

Return:

Boolean

true if key exists, false otherwise

public import(data: Object, pKey: dottedKey): this version 1.1.0 since 1.0.0 source

Import an object as data

Params:

NameTypeAttributeDescription
data Object

Object to import

pKey dottedKey
  • optional

Parent key

Return:

this

Chainable

public keys(key: dottedKey, pKey: dottedKey, throwable: boolean): Array version 3.0.0 since 1.0.0 source

Returns keys of a given dataset in the ObjectArray If no key is provided, it will output the keys of the root data object

Params:

NameTypeAttributeDescription
key dottedKey

Key

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

Array

Array of keys for the dataset or empty array if key is not defined

Throw:

TypeError

If key does not exist

public length(key: dottedKey, pKey: dottedKey, throwable: boolean): Number | undefined version 3.0.0 since 1.0.0 source

Returns length of a given dataset in the ObjectArray If no key is provided, it will output the length of the root data object

Params:

NameTypeAttributeDescription
key dottedKey

Key

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

Number | undefined

Length of the dataset

Throw:

TypeError

If key does not exist

Test:

public pKey(pKey: dottedKey): ObjectArray version 1.0.0 since 3.0.0 source

Set the parent key global memorizer. it works like giving a pKey argument to methods

Params:

NameTypeAttributeDescription
pKey dottedKey

Parent key to define working subset

Return:

ObjectArray

Chainable

Test:

public parentKey(key: dottedKey): String version 1.0.1 since 1.0.0 source

Returns e level up key for a given dotted key

Params:

NameTypeAttributeDescription
key dottedKey

Key

Return:

String

Parent key

public pull(key: dottedKey, pKey: dottedKey, throwable: boolean): Object | undefined version 2.0.0 since 1.4.0 source

Alias for dataset method.

Params:

NameTypeAttributeDescription
key dottedKey
  • optional

Key

pKey dottedKey
  • optional

Parent Key

throwable boolean
  • optional
  • default: false

Wether to throw an exception if key doesn't exist

Return:

Object | undefined

Data object

Throw:

TypeError

If key does not exist

See:

public push(key: dottedKey, val: Number | String | Array | Object, pKey: dottedKey): this version 1.1.0 since 1.0.0 source

Push a new key/value pair

Params:

NameTypeAttributeDescription
key dottedKey

Key of the added item

val Number | String | Array | Object

Value of the added item

pKey dottedKey
  • optional

Parent key to push into

Return:

this

Chainable

Throw:

TypeError

If key or parent key are not a string

public reduce(reducer: ReducerCallback, start: Mixed, key: dottedKey, pKey: dottedKey, throwable: boolean): Mixed version 2.0.0 since 1.0.0 source

Reduce the ObjectArray data given a callback

Params:

NameTypeAttributeDescription
reducer ReducerCallback

Callback to apply to each key/value (from left)

start Mixed
  • optional

Initial value for iteration

key dottedKey
  • optional

Dotted key to limit iterations through its subset if empty, the global data object will be used

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

Mixed

Callback iteration returned value

Throw:

TypeError

If key does not exist

Test:

public remove(key: dottedKey, pKey: dottedKey, throwable: boolean): this version 2.0.0 since 1.1.0 source

Remove key/value data

Params:

NameTypeAttributeDescription
key dottedKey

Key of the added item

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

this

Chainable

Throw:

TypeError

If key does not exist

public stringToStyles(str: String, pKey: dottedKey): this version 1.0.3 since 1.2.0 source

Imports a string from a style attribute. ObjectArray will camelize key from spaces and/or dashes

Params:

NameTypeAttributeDescription
str String

String to import

pKey dottedKey
  • optional

Dotted key to import styles into. If omitted, the object will be available at top-level

Return:

this

Chainable

public stylesToString(key: dottedKey, pKey: dottedKey, throwable: boolean): String | undefined version 2.0.0 since 1.0.0 source

Returns a string suitable for a style attribute. ObjectArray will convert camel-cased key to dashed key.

Params:

NameTypeAttributeDescription
key dottedKey
  • optional

Dotted key to limit iterations through its subset if empty, the global data object will be used

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

String | undefined

style string

Throw:

TypeError

If key does not exist

public throwing(v: Boolean | undefined): ObjectArray version 1.0.0 since 3.0.0 source

Overrides all throwable params settings of the object array

You can revert back to no effect by setting it to undefined

Params:

NameTypeAttributeDescription
v Boolean | undefined

Set to :

  • true to enable always throwing behaviour
  • false to enable always returning undefined
  • undefined to revert to a per method behaviour setting

Return:

ObjectArray

Chainable

Test:

public urlEncode(key: dottedKey, pKey: dottedKey, throwable: boolean): String | undefined version 2.0.0 since 1.0.0 source

Returns a string suitable for a URI query string

Params:

NameTypeAttributeDescription
key dottedKey
  • optional

Dotted key to limit iterations through its subset if empty, the global data object will be used

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

String | undefined

style string

Throw:

TypeError

If key does not exist

public values(key: dottedKey, pKey: dottedKey, throwable: boolean): Array version 3.0.0 since 1.0.0 source

Returns values of a given dataset in the ObjectArray If no key is provided, it will output the values of the root data object

Params:

NameTypeAttributeDescription
key dottedKey

Key

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional
  • default: true

Wether to throw an exception if key doesn't exist

Return:

Array

Array of values for the dataset or empty array if key is not defined

Throw:

TypeError

If key does not exist

Private Methods

private _key(key: dottedKey, pKey: dottedKey, throwable: boolean): dottedKey version 2.0.0 since 2.0.0 source

Returns a joined dotted key from key and parent key.

If not parent key is provided, it will try to use global parent key memorizer

If the global _throw trigger is set, it will overrides any throwable parameter. Use it with care !

Params:

NameTypeAttributeDescription
key dottedKey

Key

pKey dottedKey
  • optional

Parent key

throwable boolean
  • optional

Wether to trigger an exception if key doesn't exist

Return:

dottedKey

Joined dotted key

Throw:

TypeError

If key does not exist and throwable set to true or global _throw set to true

Test:

See:

private _recurseFlatten(ret: Object, dotted: Boolean, pKey: dottedKey): Object version 1.0.0 since 1.3.0 source

Private method to recurse flatten

Params:

NameTypeAttributeDescription
ret Object

Object to complete

dotted Boolean

Should the new key be dotted or not

pKey dottedKey

Parent key defining current subdataset

Return:

Object

Completed result with non-object data