countingup 0.2.5 → 0.3.0

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 CHANGED
@@ -1,62 +1,62 @@
1
- # Countingup
2
- Countingup is a library that provides a Counter class to count up and down with customizations.
3
- ## Installation
4
- Using npm:
5
- ```
6
- $ npm install countingup
7
- ```
8
-
9
- In Node.js
10
- ```javascript
11
- const countingup = require('countingup')
12
- ```
13
-
14
- # Counter class
15
- The ``Counter`` class provides a tool to count numbers.
16
-
17
- Initializing
18
- ```javascript
19
- const Counter = countingup.Counter
20
- const myCounter = new Counter()
21
- ```
22
-
23
- Counting
24
- ```javascript
25
- myCounter.count()
26
- console.log(myCounter.getCurrentNumber()) // 1
27
- ```
28
-
29
- Resetting
30
- ```javascript
31
- myCounter.reset()
32
- console.log(myCounter.getCurrentNumber()) // 0
33
- ```
34
-
35
- # Bonus Features
36
- Customizing the Increment
37
- ```javascript
38
- myCounter.count()
39
- console.log(myCounter.getCurrentNumber()) // 1
40
- myCounter.count(3)
41
- console.log(myCounter.getCurrentNumber()) // 4
42
- ```
43
-
44
- Customizing the Direction
45
-
46
- This allows you to change the direction so it counts down and subtracts
47
- ```javascript
48
- myCounter.reset()
49
- myCounter.count(5)
50
- console.log(myCounter.getCurrentNumber()) // 5
51
- myCounter.count(5, countingup.Counter.DIRECTION.REVERSE) // 0
52
- ```
53
- By default it will be forwards (countingup.Counter.DIRECTION.FORWARDS)
54
-
55
- Customizing the Starting Number
56
-
57
- ```javascript
58
- const myCounter2 = new Counter(4)
59
- console.log(myCounter2.getCurrentNumber()) // 4
60
- myCounter2.reset(3)
61
- console.log(myCounter2.getCurrentNumber()) // 3
62
- ```
1
+ # Countingup
2
+ Countingup is a library that provides a Counter class to count up and down with customizations.
3
+ ## Installation
4
+ Using npm:
5
+ ```
6
+ $ npm install countingup
7
+ ```
8
+
9
+ In Node.js
10
+ ```javascript
11
+ const countingup = require('countingup')
12
+ ```
13
+
14
+ # Counter class
15
+ The ``Counter`` class provides a tool to count numbers.
16
+
17
+ Initializing
18
+ ```javascript
19
+ const Counter = countingup.Counter
20
+ const myCounter = new Counter()
21
+ ```
22
+
23
+ Counting
24
+ ```javascript
25
+ myCounter.count()
26
+ console.log(myCounter.getCurrentNumber()) // 1
27
+ ```
28
+
29
+ Resetting
30
+ ```javascript
31
+ myCounter.reset()
32
+ console.log(myCounter.getCurrentNumber()) // 0
33
+ ```
34
+
35
+ # Bonus Features
36
+ Customizing the Increment
37
+ ```javascript
38
+ myCounter.count()
39
+ console.log(myCounter.getCurrentNumber()) // 1
40
+ myCounter.count(3)
41
+ console.log(myCounter.getCurrentNumber()) // 4
42
+ ```
43
+
44
+ Customizing the Direction
45
+
46
+ This allows you to change the direction so it counts down and subtracts
47
+ ```javascript
48
+ myCounter.reset()
49
+ myCounter.count(5)
50
+ console.log(myCounter.getCurrentNumber()) // 5
51
+ myCounter.count(5, countingup.Counter.DIRECTION.REVERSE) // 0
52
+ ```
53
+ By default it will be forwards (countingup.Counter.DIRECTION.FORWARDS)
54
+
55
+ Customizing the Starting Number
56
+
57
+ ```javascript
58
+ const myCounter2 = new Counter(4)
59
+ console.log(myCounter2.getCurrentNumber()) // 4
60
+ myCounter2.reset(3)
61
+ console.log(myCounter2.getCurrentNumber()) // 3
62
+ ```
package/index.js CHANGED
@@ -1 +1 @@
1
- module.exports = require('countingup/lib')
1
+ module.exports = require('./lib/index')
package/index.test.js ADDED
@@ -0,0 +1,81 @@
1
+ const { expect } = require("chai")
2
+ const { Counter } = require("./index")
3
+
4
+ describe("Counter Library", () => {
5
+ let counter
6
+
7
+ beforeEach(() => {
8
+ counter = new Counter(10)
9
+ })
10
+
11
+ describe("Initialization", () => {
12
+ it("should initialize with a base value", () => {
13
+ expect(counter.getCurrentNumber()).to.equal(10)
14
+ })
15
+
16
+ it("should default to 0 if no base is provided or is invalid", () => {
17
+ const defaultCounter = new Counter()
18
+ const invalidCounter = new Counter("invalid")
19
+ expect(defaultCounter.getCurrentNumber()).to.equal(0)
20
+ expect(invalidCounter.getCurrentNumber()).to.equal(0)
21
+ })
22
+ })
23
+
24
+ describe("Counting Logic", () => {
25
+ it("should increment by 1 when count is called with no arguments", () => {
26
+ counter.count()
27
+ expect(counter.getCurrentNumber()).to.equal(11)
28
+ })
29
+
30
+ it("should increment by a specific amount in FORWARDS direction", () => {
31
+ counter.count(5, Counter.DIRECTION.FORWARDS)
32
+ expect(counter.getCurrentNumber()).to.equal(15)
33
+ })
34
+
35
+ it("should decrement by a specific amount in REVERSE direction", () => {
36
+ counter.count(5, Counter.DIRECTION.REVERSE)
37
+ expect(counter.getCurrentNumber()).to.equal(5)
38
+ })
39
+ })
40
+
41
+ describe("Reset Functionality", () => {
42
+ it("should reset the counter to a new base value", () => {
43
+ counter.reset(50)
44
+ expect(counter.getCurrentNumber()).to.equal(50)
45
+ })
46
+
47
+ it('should return "this" to allow chaining', () => {
48
+ const result = counter.reset(5)
49
+ expect(result).to.equal(counter)
50
+ })
51
+ })
52
+
53
+ describe("Error Handling (Throws)", () => {
54
+ it("should throw an error for non-integer increments", () => {
55
+ expect(() => {
56
+ counter.count(1.5)
57
+ }).to.throw("Invalid increment")
58
+ })
59
+
60
+ it("should throw an error for non-finite increments", () => {
61
+ expect(() => {
62
+ counter.count(Infinity)
63
+ }).to.throw("Invalid increment")
64
+ })
65
+
66
+ it("should throw an error for invalid directions", () => {
67
+ expect(() => {
68
+ counter.count(1, "INVALID_DIR")
69
+ }).to.throw("Invalid direction")
70
+ })
71
+
72
+ it("should not change the counter value if an error is thrown", () => {
73
+ try {
74
+ counter.count(1.5)
75
+ } catch (e) {
76
+ // Error expected
77
+ }
78
+ expect(counter.getCurrentNumber()).to.equal(10)
79
+ })
80
+ })
81
+ })
package/lib/index.js CHANGED
@@ -1,45 +1,46 @@
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) {
11
- if (base == null || !Number.isFinite(base)) base = 0
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')
23
- }
24
- switch (direction) {
25
- case DIRECTION.FORWARDS: {
26
- counter += increment
27
- return
28
- }
29
-
30
- case DIRECTION.REVERSE: {
31
- counter -= increment
32
- return
33
- }
34
-
35
- default: {
36
- return console.error('Invalid direction')
37
- }
38
- }
39
- }
40
- }
41
-
42
- Counter.DIRECTION = DIRECTION
43
- module.exports = countingup = {
44
- Counter
45
- }
1
+ const DIRECTION = {
2
+ FORWARDS: 'forwards',
3
+ REVERSE: 'reverse'
4
+ }
5
+
6
+ const zero = require("@positive-numbers/zero")
7
+ const one = require("@positive-numbers/one")
8
+ const isNil = require("@is-(unknown)/is-nil")
9
+ const isFinite = require("@is-(unknown)/is-finite")
10
+ const isInteger = require("is-integer")
11
+ const not = require("es-logical-not-operator")
12
+ const or = require("es-logical-or-operator")
13
+ const { immediateError, ErrorType } = require("immediate-error")
14
+ const { Switch } = require("switch-in-fp")
15
+
16
+ function Counter(base) {
17
+ if (or(isNil(base), not(isFinite(base)))) base = zero
18
+ var counter = base
19
+ this.reset = function(base) {
20
+ if (or(isNil(base), not(isFinite(base)))) base = zero
21
+ counter = base
22
+ return this
23
+ }
24
+ this.getCurrentNumber = function() {
25
+ return counter
26
+ }
27
+ this.count = function(increment, direction) {
28
+ if (isNil(increment)) increment = one
29
+ if (isNil(direction)) direction = DIRECTION.FORWARDS
30
+ if (or(not(isFinite(increment)), not(isInteger(increment)))) {
31
+ immediateError("Invalid increment (increment was not a finite integer)", ErrorType.RangeError)
32
+ }
33
+ Switch(direction).case(DIRECTION.FORWARDS, function() {
34
+ counter += increment
35
+ }).case(DIRECTION.REVERSE, function() {
36
+ counter -= increment
37
+ }).else(function() {
38
+ immediateError("Invalid direction (direction was expected to be \"forwards\" or \"reverse\"")
39
+ }).execute()
40
+ }
41
+ }
42
+
43
+ Counter.DIRECTION = DIRECTION
44
+ module.exports = countingup = {
45
+ Counter
46
+ }
package/package.json CHANGED
@@ -1,27 +1,45 @@
1
- {
2
- "name": "countingup",
3
- "version": "0.2.5",
4
- "description": "Counter Class for JavaScrippt",
5
- "main": "index.js",
6
- "directories": {
7
- "lib": "lib"
8
- },
9
- "scripts": {},
10
- "author": "Samuel Fox J.",
11
- "license": "UNLICENSED",
12
- "keywords": [
13
- "math",
14
- "js",
15
- "utils",
16
- "problems",
17
- "calculations"
18
- ],
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/tj-commits/countingup.git"
22
- },
23
- "bugs": {
24
- "url": "https://github.com/tj-commits/countingup/issues"
25
- },
26
- "homepage": "https://github.com/tj-commits/countingup#readme"
27
- }
1
+ {
2
+ "name": "countingup",
3
+ "version": "0.3.0",
4
+ "description": "Counter Class for JavaScript",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "lib": "lib"
8
+ },
9
+ "scripts": {
10
+ "test": "mocha index.test.js"
11
+ },
12
+ "author": "Samuel Fox J.",
13
+ "license": "UNLICENSED",
14
+ "keywords": [
15
+ "math",
16
+ "js",
17
+ "utils",
18
+ "problems",
19
+ "calculations"
20
+ ],
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/tj-commits/countingup.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/tj-commits/countingup/issues"
27
+ },
28
+ "homepage": "https://github.com/tj-commits/countingup#readme",
29
+ "dependencies": {
30
+ "@is-(unknown)/is-finite": "^1.0.0",
31
+ "@is-(unknown)/is-nil": "^1.2.0",
32
+ "@positive-numbers/one": "^3.0.0",
33
+ "@positive-numbers/zero": "^3.0.0",
34
+ "es-logical-not-operator": "^1.0.0",
35
+ "es-logical-or-operator": "^1.0.0",
36
+ "immediate-error": "^7.1.0",
37
+ "is-integer": "^1.0.7",
38
+ "is-null": "^1.0.1",
39
+ "switch-in-fp": "^3.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "chai": "^6.2.2",
43
+ "mocha": "^11.7.5"
44
+ }
45
+ }