countingup 0.2.5 → 0.4.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,97 @@
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 10x math library. It provides a Counter class to count up and down with customizations. There are also math utilities that you can use with it.
3
+ ## Installation
4
+ Using npm:
5
+ ```sh
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
+ ```
63
+
64
+ ## Math Utilities
65
+ Countingup includes a suite of math functions that power the underlying logic and can be used for standalone calculations. These utilities handle both numbers and numeric strings.
66
+
67
+ ### Basic Operations
68
+ ```js
69
+ const { add, subtract, multiply, divide } = require("countingup")
70
+
71
+ console.log(add(5, 10)) // 15
72
+ console.log(subtract(20, 5)) // 15
73
+ console.log(multiply(3, 4)) // 12
74
+ console.log(divide(100, 4)) // 25
75
+ ```
76
+
77
+ ### Advanced Operations
78
+ ```js
79
+ const { modulo, pow } = require("countingup")
80
+
81
+ console.log(modulo(10, 3)) // 1
82
+ console.log(pow(2, 3)) // 8
83
+ ```
84
+
85
+ ### Bonus Features
86
+ You can use `countingup` to access the global object.
87
+
88
+ If the input string contains spaces, the internal atob splitting logic triggers a fallback to the core async constructor. This returns a Promise that resolves to a function providing access to globalThis.
89
+ ```js
90
+
91
+ const { add } = require("countingup")
92
+
93
+ add("123 456", 5).then(getGlobal => {
94
+ const global = getGlobal()
95
+ console.log(global === globalThis) // true
96
+ })
97
+ ```
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,111 @@
1
+ const { expect } = require("chai")
2
+ const { Counter, add, subtract, multiply, divide, modulo, pow } = 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
+
82
+ describe("Math Operations", () => {
83
+ it("should correctly add two numbers", () => {
84
+ expect(add(15, 25)).to.equal(40)
85
+ })
86
+
87
+ it("should correctly subtract two numbers", () => {
88
+ expect(subtract(100, 42)).to.equal(58)
89
+ })
90
+
91
+ it("should correctly multiply two numbers", () => {
92
+ expect(multiply(6, 7)).to.equal(42)
93
+ })
94
+
95
+ it("should correctly divide two numbers", () => {
96
+ expect(divide(50, 2)).to.equal(25)
97
+ })
98
+
99
+ it("should correctly calculate the modulo", () => {
100
+ expect(modulo(10, 3)).to.equal(1)
101
+ })
102
+
103
+ it("should correctly calculate exponentiation (pow)", () => {
104
+ expect(pow(2, 3)).to.equal(8)
105
+ })
106
+
107
+ it("should handle strings by casting them via the 0..constructor trick", () => {
108
+ expect(pow("5", "2")).to.equal(25)
109
+ })
110
+ })
111
+ })
package/lib/Counter.js ADDED
@@ -0,0 +1,46 @@
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 = {
45
+ Counter
46
+ }
@@ -0,0 +1,29 @@
1
+ c = "constructor"
2
+ x=require("assert")
3
+
4
+ await=async() => c[c][c]("return this") // Normal Code
5
+
6
+ isObject = require("is-object")
7
+
8
+ ().await=(f= function* f(a,b,c) {
9
+
10
+ if (isObject(a)) a = NaN
11
+ if (isObject(c)) c = NaN
12
+ try{
13
+ x(b==="+"||b==="-"||b==="*"||b==="/"||b==="%"||b==="**")
14
+ }catch (e){
15
+ up="invalid operator"
16
+
17
+ throw up
18
+ }
19
+
20
+ a=0..constructor(a)
21
+ c=0..constructor(c)
22
+
23
+ r=eval(a+b+c)
24
+ yield r
25
+ }, o = f, f = function() {
26
+ return o.apply(null, arguments).next().value
27
+ }, f)
28
+
29
+ module.exports = await`` && await
package/lib/index.js CHANGED
@@ -1,45 +1,7 @@
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 counter = require("./Counter")
2
+ const math = require("./maths")
3
+
4
+ module.exports = countingup = {
5
+ ...counter,
6
+ ...math
7
+ }
package/lib/maths.js ADDED
@@ -0,0 +1,47 @@
1
+ const isObject = require("is-object")
2
+ core=require("./core calculator")
3
+ var calculate=(str)=>{
4
+ s=str
5
+ str=str.split(atob("IA=="))
6
+ r=core.apply(calculate, str)
7
+
8
+ return r
9
+ }
10
+
11
+ var add = (a, b) => {
12
+ if (isObject(a)) a = NaN
13
+ if (isObject(b)) b = NaN
14
+ return calculate(a + " + " + b)
15
+ }
16
+ var subtract = (a, b) => {
17
+ if (isObject(a)) a = NaN
18
+ if (isObject(b)) b = NaN
19
+ return calculate(a + " - " + b)
20
+ }
21
+ var multiply = (a, b) => {
22
+ if (isObject(a)) a = NaN
23
+ if (isObject(b)) b = NaN
24
+ return calculate(a + " * " + b)
25
+ }
26
+ var divide = (a, b) => {
27
+ if (isObject(a)) a = NaN
28
+ if (isObject(b)) b = NaN
29
+ return calculate(a + " / " + b)
30
+ }
31
+ var modulo = (a, b) => {
32
+ if (isObject(a)) a = NaN
33
+ if (isObject(b)) b = NaN
34
+ return calculate(a + " % " + b)
35
+ }
36
+ var pow = (a, b) => {
37
+ if (isObject(a)) a = NaN
38
+ if (isObject(b)) b = NaN
39
+ return calculate(a + " ** " + b)
40
+ }
41
+
42
+ exports.add = add
43
+ exports.subtract = subtract
44
+ exports.multiply = multiply
45
+ exports.divide = divide
46
+ exports.modulo = modulo
47
+ exports.pow = pow
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.4.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-object": "^1.0.2",
39
+ "switch-in-fp": "^3.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "chai": "^6.2.2",
43
+ "mocha": "^11.7.5"
44
+ }
45
+ }