Class: NumberGenerator

numgen~ NumberGenerator

new NumberGenerator(initValueopt)

Class for objects that generate number sequences.
Numbers of sequence are calculated by the following formula:
f * v
where f is constant factor (см. _factor), v is variable value that changes depending on generator properties (см. _value).
Objects are ES6-iterable.
Parameters:
Name Type Attributes Description
initValue Object <optional>
Specifies initial property values. Keys are property names, their values are values of corresponding properties. The following keys (properties) can be specified:
Name Type Description
factor Number Constant factor that is used to calculate numbers of sequence.
maxValue Number Maximal value of variable.
passToChanger Boolean Indicates whether reference to this object should be passed into function that is used to calculate new value of variable.
resetValueOnMax Boolean Indicates whether variable value should be set to initial value when variable reaches maximal value.
startValue Number Initial value of variable.
valueChange Number, Function, Object, null, undefined Specifies the way to calculate new value of variable (see _valueChange).
valueChangePeriod Integer Time interval in milliseconds during which it is possible to calculate new value of variable (see _valueChangePeriod).
valueSavePeriod Integer Time interval in milliseconds during which it is possible to change or preserve value of variable (see _valueSavePeriod).
Source:

Members

(protected) _current :Number

The current item of sequence (the last requested item by getNext).
Type:
  • Number
Source:
See:

(protected) _factor :Number

Constant factor that is used to calculate numbers of sequence.
Type:
  • Number
Source:

(protected) _index :Integer

The index of the current item of sequence.
Type:
  • Integer
Source:
See:

(protected) _lastNumberTime :Integer

Time where the last calculated item of sequence was requested (represented in milliseconds from start of epoch).
Type:
  • Integer
Source:
See:

(protected) _maxValue :Number

Maximal value of variable.
Type:
  • Number
Source:
See:

(protected) _passToChanger :Boolean

Indicates whether reference to this object should be passed into function that is used to calculate new value of variable.
Type:
  • Boolean
Source:
See:

(protected) _prev :Number

The previous item of sequence.
Type:
  • Number
Source:
See:

(protected) _resetValueOnMax :Boolean

Indicates whether variable value should be set to initial value when variable reaches maximal value.
Type:
  • Boolean
Source:
See:

(protected) _startValue :Number

Initial value of variable.
Type:
  • Number
Source:
See:

(protected) _state :Object

Contains any data necessary for generator's work. Can be used by function that calculates value of variable to save intermediate values e.g. several preceding items of sequence.
Type:
  • Object
Source:
See:

(protected) _value :Number

Variable value that is used to calculate numbers of sequence.
Type:
  • Number
Source:
See:

(protected) _valueChange :Number|function|Object|null|undefined

Specifies the way to calculate new value of variable.
The following values can be used:
  • null or undefined means that variable's value does not change (i.e. variable is constant);
  • a number means that new value is the sum of previous value and the number;
  • a function or an object having execute method means that new value is result of function/method call.
In the latter case the following object is passed as argument into function/method:
Field Type Description
current Number The current item of sequence (i.e. the last item that was returned by getNext method).
factor Number Constant factor that is used to calculate numbers of sequence.
generator Object Reference to this object that represents number generator. This field is available only when passToChanger property is true (i.e. when isPassToChanger returns true).
index Integer The index of sequence's number that is being calculated.
maxValue Number Maximal value of variable.
prev Number The previous item of sequence (i.e. the result that is returned by getPrev method).
startValue Number Initial value of variable.
state Object The data that are necessary to generator's work. The object can be used to save intermediate values (e.g. several preceding items of sequence).
value Number The current value of variable.
Type:
  • Number | function | Object | null | undefined
Source:
See:

(protected) _valueChangePeriod :Integer

Time interval in milliseconds during which it is possible to calculate new value of variable. Negative value indicates that there are no time restrictions for calculation of new value.
Type:
  • Integer
Source:
See:

(protected) _valueSavePeriod :Integer

Time interval in milliseconds during which it is possible to change or preserve value of variable. When the interval ends the variable is reset to initial value. Negative value indicates that reset of variable's value is not used.
Type:
  • Integer
Source:
See:

Methods

clone() → {Object}

Create new generator object that is similar to this one but is in initial state.
Source:
See:
Returns:
Reference to the newly created object.
Type
Object

dispose()

Free resources that are allocated for object.
Source:
See:

getCurrent() → {Number}

Return the current item of sequence (the last requested item by getNext).
Source:
See:
Returns:
The current item of sequence (the last requested item by getNext).
Type
Number

getFactor() → {Number}

Return constant factor that is used to calculate numbers of sequence.
Source:
See:
Returns:
Constant factor that is used to calculate numbers of sequence.
Type
Number

getIndex() → {Integer}

Return the index of the current item of sequence.
Source:
See:
Returns:
The index of the current item of sequence.
Type
Integer

getMaxValue() → {Number}

Return maximal value of variable.
Source:
See:
Returns:
Maximal value of variable.
Type
Number

getNext() → {Number}

Return next item of sequence.
Source:
See:
Returns:
Next item of sequence.
Type
Number

getNextPart(nPartSize) → {Array}

Return array containing several next items of sequence.
Parameters:
Name Type Description
nPartSize Integer Quantity of next items of sequence that should be included in result.
Source:
See:
Returns:
Array containing several next items of sequence.
Type
Array

getPart(nFirstIndex, nLastIndex) → {Array}

Return array containing several consecutive items of sequence (subsequence containing items with indexes from nFirstIndex up to nLastIndex).
Unlike getNextPart this method does not change object's state (i.e. _current and _index fields preserve their values).
Parameters:
Name Type Description
nFirstIndex Integer Index of first item that should be included in result.
nLastIndex Integer Index of last item that should be included in result.
Source:
See:
Returns:
Subsequence containing items with indexes from nFirstIndex up to nLastIndex.
Type
Array

getPrev() → {Number}

Return the previous item of sequence.
Source:
See:
Returns:
The previous item of sequence.
Type
Number

getStartValue() → {Number}

Return initial value of variable.
Source:
See:
Returns:
Initial value of variable.
Type
Number

getValue() → {Number}

Return current variable value.
Source:
See:
Returns:
Current variable value.
Type
Number

getValueChange() → {Number|function|Object|null|undefined}

Return value that specifies the way to calculate new value of variable.
Source:
See:
Returns:
Value that specifies the way to calculate new value of variable.
Type
Number | function | Object | null | undefined

getValueChangePeriod() → {Integer}

Return time interval in milliseconds during which it is possible to calculate new value of variable.
Source:
See:
Returns:
Time interval in milliseconds during which it is possible to calculate new value of variable.
Type
Integer

getValueSavePeriod() → {Integer}

Return time interval in milliseconds during which it is possible to change or preserve value of variable.
Source:
See:
Returns:
Time interval in milliseconds during which it is possible to change or preserve value of variable.
Type
Integer

isPassToChanger() → {Boolean}

Test whether reference to this object should be passed into function that is used to calculate new value of variable.
Source:
See:
Returns:
true, if reference to this object should be passed into function, otherwise false.
Type
Boolean

isResetValueOnMax() → {Boolean}

Test whether variable value should be set to initial value when variable reaches maximal value.
Source:
See:
Returns:
true, if variable value will be reset, otherwise false.
Type
Boolean

reset() → {Object}

Reset parameters of generator to initial values.
This method should be called after generator's creation when properties are changed that influence on generator's work. This method shouldn't be called when generator's properties are set by constructor's parameters.
Source:
See:
Returns:
Reference to this object.
Type
Object

setFactor(nFactor) → {Object}

Set constant factor that is used to calculate numbers of sequence.
Parameters:
Name Type Description
nFactor Number Constant factor that is used to calculate numbers of sequence.
Source:
See:
Returns:
Reference to this object.
Type
Object

setMaxValue(nValue) → {Object}

Set maximal value of variable.
Parameters:
Name Type Description
nValue Number Maximal value of variable.
Source:
See:
Returns:
Reference to this object.
Type
Object

setPassToChanger(bPass) → {Object}

Change the property that indicates whether reference to this object should be passed into function that is used to calculate new value of variable.
Parameters:
Name Type Description
bPass Boolean true, if reference to this object should be passed into function, otherwise false.
Source:
See:
Returns:
Reference to this object.
Type
Object

setResetValueOnMax(bReset) → {Object}

Change the property that indicates whether variable value should be set to initial value when variable reaches maximal value.
Parameters:
Name Type Description
bReset Boolean true, if variable value should be reset, otherwise false.
Source:
See:
Returns:
Reference to this object.
Type
Object

setStartValue(nValue) → {Object}

Set initial value of variable.
Parameters:
Name Type Description
nValue Number Initial value of variable.
Source:
See:
Returns:
Reference to this object.
Type
Object

setValueChange(change) → {Object}

Set the way to calculate new value of variable.
Parameters:
Name Type Description
change Number | function | Object | null | undefined Specifies the way to calculate new value of variable.
Source:
See:
Returns:
Reference to this object.
Type
Object

setValueChangePeriod(nPeriod) → {Object}

Set time interval in milliseconds during which it is possible to calculate new value of variable.
Parameters:
Name Type Description
nPeriod Integer Time interval in milliseconds during which new value of variable can be calculated.
Source:
See:
Returns:
Reference to this object.
Type
Object

setValueSavePeriod(nPeriod) → {Object}

Set time interval in milliseconds during which it is possible to change or preserve value of variable.
Parameters:
Name Type Description
nPeriod Integer Time interval in milliseconds during which value of variable can be changed or preserved.
Source:
See:
Returns:
Reference to this object.
Type
Object

toArray(nSize) → {Array}

Convert object into array.
Essentially this method is the wrap for the following call: this.getPart(1, nSize);
Parameters:
Name Type Description
nSize Integer Size of array.
Source:
See:
Returns:
Subsequence containing items with indexes from 1 up to nSize.
Type
Array

toString() → {String}

Convert object into string.
Source:
See:
Returns:
String representation of the object.
Type
String