VariableStack
The VariableStack is a special kind of stack. It allows corrode to do black magic like loops in loops and other crazy stuff.
To enable this we define a stack as an object, containing other objects. Seen this way it looks more like a TreeStructure which layers you can push and pop as you like.
The VariableStack starts as an "empty" object. "empty" meaning, that each layer is an object consisting of two/three values:
isRoot
telling the user whether this layer is the uppermost one.value
holding the value for this layer.[name]
name of this layer (root-layer won't have one)
Pushing a new layer means adding a new object to the value
-object of
the current layer and setting the current layer to our newly created one.
Actually, the object getting added to the value
-object is not just any object.
It itself is an object like { isRoot: false, value: {} }
.
The root-layer (layer-object where isRoot === true
) is the lowest layer.
The current layer is the topmost one.
Example:
+--------------------------+ +-----------------+
| VariableStack#value: | | | current layer
| { | | {} |
| value_1: { foo: 'bar' }| | | isRoot: false
| } | +-----------------+
+------------+-------------+ | |
| | { |
+ | foo: 'bar', | VariableStack
.push('value_1') | value_2: {} | #peek(1)
+ +----------> | } |
| | | isRoot: false
v +----------> +-----------------+
+-----------+----------+ | |
|#value: { foo: 'bar' }| | { |
+-----------+----------+ | value_1: { | VariableStack
| | foo: 'bar', | #peek(2)
+ | value_2: {} |
.push('value_2') | } | isRoot: true
+ | } |
| | |
v +-----------------+
+----+-----+
|#value: {}|
+----------+
Constructor Summary
Public Constructor | ||
public |
|
Member Summary
Public Members | ||
public |
internal storage for the stack |
|
public |
top: * |
|
public get |
retrieve the value of the top.layer |
|
public set |
set the current value this also updates the value in the parent-layer |
Method Summary
Public Methods | ||
public |
get the value of a layer below the current one |
|
public |
get a layer below the current one |
|
public |
pop() pop the current layer |
|
public |
popAll() pop all layers until the root-layer is reached |
|
public |
push a value onto the stack |
Public Constructors
Public Methods
public peek(layerCount: number): Object | * source
get the value of a layer below the current one
Params:
Name | Type | Attribute | Description |
layerCount | number | how many layers deeper relative from the current |
public peekLayer(layerCount: number): Object source
get a layer below the current one
Params:
Name | Type | Attribute | Description |
layerCount | number | how many layers deeper relative from the current |
public push(name: string, value: Object | *) source
push a value onto the stack
The value doesn't have to be a object, but only objects will properly support child-layers. When pushing the new layer the current one will receive a reference to the pushed object as a value at the given name.
Note, that if you're pushing a non-object value this reference will not work, as only arrays & objects are passed by reference. Instead the value in the layer above will be updated, when the current layer's value will be set.
If the value you want to push already exists at the current layer VariableStack ignores your value and just re-uses the old one, so no layer will be replaced.
Example:
varStack.push('foo');
varStack.value.bar = 'baz';
// varStack.value => { bar: 'baz' }
// varStack.peek() => { foo: { bar: 'baz' } }
Test:
- VariableStack allows object-descension
- VariableStack haves correct value-shortcut
- VariableStack allows deep-adding
- VariableStack allows object-popping
- VariableStack allows object-popping with values
- VariableStack allows multiple pushed objects
- VariableStack allows object-repushing
- VariableStack allows array-repushing
- VariableStack allows primitive-repushing
- VariableStack repushes the same instance
- VariableStack allows object-repushing on user-defined objects
- VariableStack allows array pushing
- VariableStack allows full primitive value-replacing
- VariableStack allows full object value-replacing
- VariableStack allows full array value-replacing