countingup 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/index.js +36 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,9 +48,9 @@ This allows you to change the direction so it counts down and subtracts
|
|
|
48
48
|
myCounter.reset()
|
|
49
49
|
myCounter.count(5)
|
|
50
50
|
console.log(myCounter.getCurrentNumber()) // 5
|
|
51
|
-
myCounter.count(5, countingup.DIRECTION.REVERSE) // 0
|
|
51
|
+
myCounter.count(5, countingup.Counter.DIRECTION.REVERSE) // 0
|
|
52
52
|
```
|
|
53
|
-
By default it will be forwards (countingup.DIRECTION.FORWARDS)
|
|
53
|
+
By default it will be forwards (countingup.Counter.DIRECTION.FORWARDS)
|
|
54
54
|
|
|
55
55
|
Customizing the Starting Number
|
|
56
56
|
|
package/lib/index.js
CHANGED
|
@@ -1,40 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
const DIRECTION = {
|
|
2
|
+
FORWARDS: 'forwards',
|
|
3
|
+
REVERSE: 'reverse'
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function Counter(base) {
|
|
8
|
+
if (base == null || !Number.isFinite(base)) base = 0
|
|
9
|
+
var counter = base
|
|
10
|
+
this.reset = function(base) {
|
|
7
11
|
if (base == null || !Number.isFinite(base)) base = 0
|
|
8
|
-
|
|
9
|
-
this
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
counter = base
|
|
13
|
+
return this
|
|
14
|
+
}
|
|
15
|
+
this.getCurrentNumber = function() {
|
|
16
|
+
return counter
|
|
17
|
+
}
|
|
18
|
+
this.count = function(increment, direction) {
|
|
19
|
+
if (increment == null) increment = 1
|
|
20
|
+
if (direction == null) direction = DIRECTION.FORWARDS
|
|
21
|
+
if (!Number.isFinite(increment) || !Number.isInteger(increment)) {
|
|
22
|
+
return console.error('Invalid increment')
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return console.error('Invalid increment')
|
|
24
|
+
switch (direction) {
|
|
25
|
+
case DIRECTION.FORWARDS: {
|
|
26
|
+
counter += increment
|
|
27
|
+
return
|
|
22
28
|
}
|
|
23
|
-
switch (direction) {
|
|
24
|
-
case countingup.DIRECTION.FORWARDS: {
|
|
25
|
-
counter += increment
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
case DIRECTION.REVERSE: {
|
|
31
|
+
counter -= increment
|
|
32
|
+
return
|
|
33
|
+
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
35
|
+
default: {
|
|
36
|
+
return console.error('Invalid direction')
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
+
|
|
42
|
+
Counter.DIRECTION = DIRECTION
|
|
43
|
+
module.exports = countingup = {
|
|
44
|
+
Counter
|
|
45
|
+
}
|