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 |
constructor(data: Object): ObjectArray |
Member Summary
Public Members | ||
public get |
Returns the underlying |
since 1.0.0 |
public set |
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 |
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 |
Returns dataset for the key. |
version 2.0.0 since 1.0.0 |
public |
Coalesce method to define a default value if a key is not set. |
version 1.0.0 since 3.1.0 |
public |
Empty the ObjectArray data. |
version 3.0.0 since 1.2.0 |
public |
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 |
version 2.0.0 since 1.0.0 |
public |
Returns a string suitable for a |
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 |
Check if a given key exists in the ObjectArray |
version 1.1.2 since 1.0.0 |
public |
Import an object as data |
version 1.1.0 since 1.0.0 |
public |
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 |
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 |
pKey(pKey: dottedKey): ObjectArray 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 |
Alias for dataset method. |
version 2.0.0 since 1.4.0 |
public |
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/value data |
version 2.0.0 since 1.1.0 |
public |
stringToStyles(str: String, pKey: dottedKey): this Imports a string from a |
version 1.0.3 since 1.2.0 |
public |
Returns a string suitable for a |
version 2.0.0 since 1.0.0 |
public |
throwing(v: Boolean | undefined): ObjectArray Overrides all throwable params settings of the object array |
version 1.0.0 since 3.0.0 |
public |
Returns a string suitable for a URI query string |
version 2.0.0 since 1.0.0 |
public |
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 |
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:
Name | Type | Attribute | Description |
data | Object |
|
Initial data to import |
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
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:
Name | Type | Attribute | Description |
s | String | String to camelize |
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
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:
Name | Type | Attribute | Description |
key | dottedKey | 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:
Name | Type | Attribute | Description |
keepFlattened | Boolean |
|
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:
Name | Type | Attribute | Description |
s | String |
|
String to dashize |
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
Throw:
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
Return:
this | Chainable |
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
Return:
this | Chainable |
Throw:
If key does not exist |
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.
Return:
this | Chainable |
Throw:
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:
Name | Type | Attribute | Description |
cb | ForEachCallback | Callback to be run |
|
key | dottedKey |
|
Dotted key to limit iterations through its subset if empty, the global data object will be used |
pKey | dottedKey |
|
Parent key |
throwable | boolean |
|
Wether to throw an exception if key doesn't exist |
Return:
void |
Throw:
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
Throw:
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:
Name | Type | Attribute | Description |
key | dottedKey | Object |
|
Key / Object to import |
val | number | string | array | object | dottedKey |
|
Value to push / Parent key to import into |
pKey | dottedKey |
|
Parent Key |
throwable | boolean |
|
If |
Throw:
If key does not exist and throwable is |
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
public import(data: Object, pKey: dottedKey): this version 1.1.0 since 1.0.0 source
Import an object as data
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
Throw:
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
Throw:
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:
Name | Type | Attribute | Description |
pKey | dottedKey | Parent key to define working subset |
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:
Name | Type | Attribute | Description |
key | dottedKey | Key |
public pull(key: dottedKey, pKey: dottedKey, throwable: boolean): Object | undefined version 2.0.0 since 1.4.0 source
Alias for dataset method.
Throw:
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
Return:
this | Chainable |
Throw:
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:
Name | Type | Attribute | Description |
reducer | ReducerCallback | Callback to apply to each key/value (from left) |
|
start | Mixed |
|
Initial value for iteration |
key | dottedKey |
|
Dotted key to limit iterations through its subset if empty, the global data object will be used |
pKey | dottedKey |
|
Parent key |
throwable | boolean |
|
Wether to throw an exception if key doesn't exist |
Return:
Mixed | Callback iteration returned value |
Throw:
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
Return:
this | Chainable |
Throw:
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
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.
Throw:
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
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
Throw:
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
Throw:
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 !
Throw:
If key does not exist and throwable set to |