Class: Tree

sntls. Tree

Accesses, traverses, and modifies tree-like object structures.
Source:

Extends

Members

items :Object|Array

Object buffer that stores items. Technically writable and public for performance and transparency reasons, but should not be changed externally as may lead to inconsistent state especially in `Hash`-based subclasses.
Inherited From:
Source:

keyCount :number

Tracks number of distinct keys in the hash. Uninitialized until first queried. (by either `.getKeys()` or `.getKeyCount()`), therefore it is safer to use its getter method. Should not be modified externally.
Inherited From:
Source:

Methods

<static> create(items) → {sntls.Tree}

Instantiates class
Parameters:
Name Type Description
items object
Source:
Returns:
Type
sntls.Tree

changeBufferTypeTo(bufferType=Object) → {sntls.Hash}

Changes buffer type from Object to Array or vice versa. Changes the current hash instance!
Parameters:
Name Type Description
bufferType=Object function `Array` or `Object`, specifying new buffer type.
Inherited From:
Source:
Returns:
Type
sntls.Hash
Example
sntls.Hash.create({0: 'foo', 1: 'bar'}).changeBufferTypeTo(Array).items // ['foo', 'bar']

clear() → {sntls.Hash}

Clears hash by replacing items buffer with an empty one. Observes current buffer type, ie. if hash was array based, the new buffer will be also array.
Inherited From:
Source:
Returns:
Type
sntls.Hash

clone() → {sntls.Hash}

Clones hash. Creates an instance of the same class (for subclasses of `Hash`) and initializes it with a shallow copy of the current items buffer and item count.
Inherited From:
Source:
Returns:
New hash with identical contents.
Type
sntls.Hash

getFirstKey() → {string}

Retrieves the first available key it can find. If hash has more than one items, any of the hash's keys may be returned. Result does not necessarily match up with the return value of `.getFirstValue()`.
Inherited From:
Source:
See:
Returns:
Type
string

getFirstValue() → {*}

Retrieves the first available value it can find. If hash has more than one items, any value from the hash may be returned. Result does not necessarily match up with the return value of `.getFirstKey()`.
Inherited From:
Source:
See:
Returns:
Type
*

getItem(itemKey) → {*}

Retrieves item from the hash.
Parameters:
Name Type Description
itemKey string Item key.
Inherited From:
Source:
Returns:
Item variable.
Type
*

getKeyCount() → {number}

Retrieves the number of keys in hash.
Inherited From:
Source:
Returns:
Type
number
Example
var c = sntls.Hash.create({foo: 1, bar: 2});
c.getKeyCount() // 2

getKeys() → {string[]}

Retrieves item keys as an array. The order in which keys appear in the resulting array is not deterministic.
Inherited From:
Source:
Returns:
Type
string[]

getKeysAsHash() → {sntls.Hash}

Retrieves item keys wrapped in a hash.
Inherited From:
Source:
See:
Returns:
Type
sntls.Hash

getNode(path) → {*}

Retrieves the value at the specified path.
Parameters:
Name Type Description
path sntls.Path Path to node
Source:
Returns:
Whatever value is found at path
Type
*

getNodeAsHash(path) → {sntls.Hash}

Retrieves object at the specified path wrapped in a Hash object.
Parameters:
Name Type Description
path sntls.Path Path to node
Source:
Returns:
Type
sntls.Hash

getOrSetNode(path, generator, handler) → {*}

Retrieves the value at the specified path, or when the path does not exist, creates path and assigns the return value of the generator.
Parameters:
Name Type Argument Description
path sntls.Path Path to node
generator function Generator function returning value
handler function <optional>
Callback receiving the path and value affected by change.
Source:
Returns:
Type
*

getSafeNode(path, handler) → {object}

Retrieves the value at the specified path, or when the path does not exist, creates path and assigns an empty object.
Parameters:
Name Type Argument Description
path sntls.Path
handler function <optional>
Callback receiving the path and value affected by change.
Source:
Returns:
Type
object

getSafeNodeAsHash(path, handler) → {sntls.Hash}

Retrieves safe value at path, wrapped in a hash.
Parameters:
Name Type Argument Description
path sntls.Path
handler function <optional>
Callback receiving the path affected by change.
Source:
Returns:
Type
sntls.Hash

getValues() → {Array}

Retrieves collection items in an array, without key information. The order in which keys appear in the resulting array is not deterministic.
Inherited From:
Source:
Returns:
Type
Array

getValuesAsHash() → {sntls.Hash}

Retrieves item values wrapped in a hash.
Inherited From:
Source:
See:
Returns:
Type
sntls.Hash

moveNode(fromPath, toPath) → {sntls.Tree}

Moves node from one path to another.
Parameters:
Name Type Description
fromPath sntls.Path
toPath sntls.Path
Source:
Returns:
Type
sntls.Tree

passItemsTo(handler, context, argIndex) → {*}

Passes the items buffer to the specified function.
Parameters:
Name Type Argument Default Description
handler function External handler accepting the buffer.
context * <optional>
Context in which to call the handler. If handler is a method, the context should be the owner (instance or class) of the method.
argIndex number <optional>
0 Argument index taken by buffer when calling the function.
Inherited From:
Source:
Returns:
Whatever is returned by the handler.
Type
*

passSelfTo(handler, context, argIndex) → {*}

Passes itself to the specified function.
Parameters:
Name Type Argument Default Description
handler function External handler accepting the hash.
context * <optional>
Context in which to call the handler. If handler is a method, the context should be the owner (instance or class) of the method.
argIndex number <optional>
0 Argument index taken by buffer when calling the function.
Inherited From:
Source:
Returns:
Whatever is returned by the handler.
Type
*

queryKeys(query) → {Array}

Queries node keys from tree
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
Array

queryKeysAsHash(query) → {sntls.Hash}

Queries node keys from tree wrapped in a hash
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
sntls.Hash

queryKeyValuePairs(query) → {object}

Queries key-value associations from tree as an object
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
object

queryKeyValuePairsAsHash(query) → {sntls.Hash}

Queries key-value associations from tree as an object wrapped in a hash
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
sntls.Hash

queryPaths(query) → {Array}

Queries paths from tree
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
Array

queryPathsAsHash(query) → {sntls.Hash}

Queries paths from tree wrapped in a hash
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
sntls.Hash

queryPathValuePairs(query) → {object}

Queries pat-value associations from tree as object
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
object

queryPathValuePairsAsHash(query) → {sntls.Hash}

Queries pat-value associations from tree as object wrapped in a hash
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
sntls.Hash

queryValues(query) → {Array}

Queries node values from tree
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
Array

queryValuesAsHash(query) → {sntls.Hash}

Queries node values from tree wrapped in a hash
Parameters:
Name Type Description
query sntls.Query
Source:
Returns:
Type
sntls.Hash

setNode(path, value) → {sntls.Tree}

Sets the node at the specified path to the given value.
Parameters:
Name Type Description
path sntls.Path Path to node
value * Node value to set
Source:
Returns:
Type
sntls.Tree

toArrayCollection() → {sntls.ArrayCollection}

Reinterprets hash as array collection.
Inherited From:
Source:
Returns:
Type
sntls.ArrayCollection

toCollection(subClass) → {sntls.Collection}

Reinterprets hash as collection, optionally as the specified subclass.
Parameters:
Name Type Argument Description
subClass sntls.Collection <optional>
Collection subclass.
Inherited From:
Source:
Returns:
Type
sntls.Collection

toDateCollection() → {sntls.DateCollection}

Reinterprets hash as date collection.
Inherited From:
Source:
Returns:
Type
sntls.DateCollection

toDictionary() → {sntls.Dictionary}

Reinterprets hash as a dictionary.
Inherited From:
Source:
Returns:
Type
sntls.Dictionary

toJournalingCollection() → {sntls.JournalingCollection}

Reinterprets hash as a journaling collection.
Inherited From:
Source:
Returns:
Type
sntls.JournalingCollection

toOrderedList() → {sntls.OrderedList}

Reinterprets hash as an ordered list.
Inherited From:
Source:
Returns:
Type
sntls.OrderedList

toOrderedStringList() → {sntls.OrderedStringList}

Reinterprets hash as ordered string list.
Inherited From:
Source:
Returns:
Type
sntls.OrderedStringList

toSet() → {sntls.Set}

Reinterprets hash as a string dictionary.
Inherited From:
Source:
Returns:
Type
sntls.Set

toStringCollection() → {sntls.StringCollection}

Reinterprets hash as string collection.
Inherited From:
Source:
Returns:
Type
sntls.StringCollection

toStringDictionary() → {sntls.StringDictionary}

Reinterprets hash as a string dictionary.
Inherited From:
Source:
Returns:
Type
sntls.StringDictionary

toTree() → {sntls.Tree}

Reinterprets hash as a tree.
Inherited From:
Source:
Returns:
Type
sntls.Tree

traverseAllNodes(handler) → {sntls.Tree}

Traverses tree iteratively, calling handler on every node unless interrupted by returning false from handler.
Parameters:
Name Type Description
handler function
Source:
Returns:
Type
sntls.Tree

traverseByQuery(query, handler) → {sntls.Tree}

Traverses tree recursively, guided by the specified query array
Parameters:
Name Type Description
query sntls.Query
handler function
Source:
Returns:
Type
sntls.Tree

unsetKey(path, splice, handler) → {sntls.Tree}

Removes key from the specified path.
Parameters:
Name Type Argument Default Description
path sntls.Path Path to node
splice boolean <optional>
false Whether to use splice when removing key from array.
handler function <optional>
Callback receiving the path affected by change.
Source:
Returns:
Type
sntls.Tree

unsetNode(path) → {sntls.Tree}

Removes node from the specified path, ie. the node will be overwritten with an undefined value.
Parameters:
Name Type Description
path sntls.Path
Source:
Returns:
Type
sntls.Tree

unsetPath(path, splice, handler) → {sntls.Tree}

Removes nodes from tree that have no children other than the one specified by the path.
Parameters:
Name Type Argument Default Description
path sntls.Path Datastore path
splice boolean <optional>
false Whether to use splice when removing key from array.
handler function <optional>
Callback receiving the path affected by change.
Source:
Returns:
Type
sntls.Tree