-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (54 loc) · 1.3 KB
/
index.js
File metadata and controls
62 lines (54 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
/**
* Simple linear increasing set of nummeric values.
*
* @constructor
* @param {Number} value Starting value.
* @public
*/
function Diffset(value) {
this.last = 'number' === typeof value ? value : 0;
this.set = [];
}
/**
* Calculate the difference between the supplied value and our last known value.
*
* @param {Number} value Value to calculate
* @returns {Number} Positive or negative difference between our last value.
* @private
*/
Diffset.prototype.diff = function diff(value) {
var currently = this.last;
this.last = value;
return value - currently;
};
/**
* Add new values to the set.
*
* @param {Number} arguments Numbers to add to the set.
* @returns {Diffset}
* @public
*/
Diffset.prototype.push = function push() {
for (var i = 0, l = arguments.length; i < l; i++) {
this.set.push(this.diff(arguments[i]));
}
return this;
};
/**
* Flush the internal set and return a copy of it.
*
* @param {Number} value Optional value to set the `last` known value to.
* @returns {Array} Copy of the internal set we just flushed.
*
*/
Diffset.prototype.flush = function flush(value) {
var copy = this.set.slice(0);
this.set.length = 0;
if ('number' === typeof value) this.last = value;
return copy;
};
//
// Expose the module.
//
module.exports = Diffset;