countingup 0.3.0 → 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 +40 -5
- package/index.test.js +33 -3
- package/lib/Counter.js +46 -0
- package/lib/core calculator.js +29 -0
- package/lib/index.js +6 -45
- package/lib/maths.js +47 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Countingup
|
|
2
|
-
Countingup is a library
|
|
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
3
|
## Installation
|
|
4
4
|
Using npm:
|
|
5
|
-
```
|
|
6
|
-
|
|
5
|
+
```sh
|
|
6
|
+
npm install countingup
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
In Node.js
|
|
@@ -11,7 +11,7 @@ In Node.js
|
|
|
11
11
|
const countingup = require('countingup')
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## Counter class
|
|
15
15
|
The ``Counter`` class provides a tool to count numbers.
|
|
16
16
|
|
|
17
17
|
Initializing
|
|
@@ -32,7 +32,7 @@ myCounter.reset()
|
|
|
32
32
|
console.log(myCounter.getCurrentNumber()) // 0
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
### Bonus Features
|
|
36
36
|
Customizing the Increment
|
|
37
37
|
```javascript
|
|
38
38
|
myCounter.count()
|
|
@@ -60,3 +60,38 @@ console.log(myCounter2.getCurrentNumber()) // 4
|
|
|
60
60
|
myCounter2.reset(3)
|
|
61
61
|
console.log(myCounter2.getCurrentNumber()) // 3
|
|
62
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.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { expect } = require("chai")
|
|
2
|
-
const { Counter } = require("./index")
|
|
2
|
+
const { Counter, add, subtract, multiply, divide, modulo, pow } = require("./index")
|
|
3
3
|
|
|
4
4
|
describe("Counter Library", () => {
|
|
5
5
|
let counter
|
|
@@ -44,7 +44,7 @@ describe("Counter Library", () => {
|
|
|
44
44
|
expect(counter.getCurrentNumber()).to.equal(50)
|
|
45
45
|
})
|
|
46
46
|
|
|
47
|
-
it(
|
|
47
|
+
it("should return \"this\" to allow chaining", () => {
|
|
48
48
|
const result = counter.reset(5)
|
|
49
49
|
expect(result).to.equal(counter)
|
|
50
50
|
})
|
|
@@ -78,4 +78,34 @@ describe("Counter Library", () => {
|
|
|
78
78
|
expect(counter.getCurrentNumber()).to.equal(10)
|
|
79
79
|
})
|
|
80
80
|
})
|
|
81
|
-
|
|
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,46 +1,7 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
REVERSE: 'reverse'
|
|
4
|
-
}
|
|
1
|
+
const counter = require("./Counter")
|
|
2
|
+
const math = require("./maths")
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
}
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "countingup",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Counter Class for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"es-logical-or-operator": "^1.0.0",
|
|
36
36
|
"immediate-error": "^7.1.0",
|
|
37
37
|
"is-integer": "^1.0.7",
|
|
38
|
-
"is-
|
|
38
|
+
"is-object": "^1.0.2",
|
|
39
39
|
"switch-in-fp": "^3.0.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|