Home Manual Reference Source Test
import VariableStack from 'corrode/src/variable-stack.js'
public class | source

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: {}|
         +----------+

Test:

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

internal storage for the stack

public

top: *

public get

value: Object | *: *

retrieve the value of the top.layer

public set

value(val: Object | *)

set the current value this also updates the value in the parent-layer

Method Summary

Public Methods
public

peek(layerCount: number): Object | *

get the value of a layer below the current one

public

peekLayer(layerCount: number): Object

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(name: string, value: Object | *)

push a value onto the stack

Public Constructors

Public Members

public stack: Array<Object> source

internal storage for the stack

public get value: Object | *: * source

retrieve the value of the top.layer

Return:

Object | *

the current value

Test:

public set value(val: Object | *) source

set the current value this also updates the value in the parent-layer

Public Methods

public peek(layerCount: number): Object | * source

get the value of a layer below the current one

Params:

NameTypeAttributeDescription
layerCount number

how many layers deeper relative from the current

Return:

Object | *

value

Test:

public peekLayer(layerCount: number): Object source

get a layer below the current one

Params:

NameTypeAttributeDescription
layerCount number

how many layers deeper relative from the current

Return:

Object

layer-object

Test:

public popAll() source

pop all layers until the root-layer is reached

Test:

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.

Params:

NameTypeAttributeDescription
name string

name of the new layer

value Object | *
  • optional
  • default: {}

value-object of the new layer

Example:

varStack.push('foo');
varStack.value.bar = 'baz';
// varStack.value => { bar: 'baz' }
// varStack.peek() => { foo: { bar: 'baz' } }

Test: