@wizhut_tech/wizjs 0.1.1 → 0.1.3
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const t = require('tap');
|
|
2
2
|
|
|
3
|
-
const { compact } = require('../src/lang/arrays.js');
|
|
3
|
+
const { compact, accumulate, Operator } = require('../src/lang/arrays.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
t.test('arrays/compact', (t) => {
|
|
@@ -11,4 +11,15 @@ t.test('arrays/compact', (t) => {
|
|
|
11
11
|
t.match(compact(null), []);
|
|
12
12
|
t.match(compact(undefined), []);
|
|
13
13
|
t.end();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
t.test('arrays/accumulate', (t) => {
|
|
18
|
+
const arr = [1, 2, 3];
|
|
19
|
+
|
|
20
|
+
t.equal(accumulate(arr, Operator.plus, 0), 6);
|
|
21
|
+
t.equal(accumulate(arr, Operator.minus, 0), -6);
|
|
22
|
+
t.equal(accumulate(arr, Operator.multiply, 0), 0);
|
|
23
|
+
t.equal(accumulate(arr, Operator.divide, 0), 0);
|
|
24
|
+
t.end();
|
|
14
25
|
});
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
const t = require('tap');
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { count, fn_count } = require('../src/lang/itertools.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
t.test('itertools/
|
|
6
|
+
t.test('itertools/count', (t) => {
|
|
7
7
|
// simple integer
|
|
8
|
-
const genOne =
|
|
8
|
+
const genOne = count(0, 1);
|
|
9
9
|
t.equal(genOne.next().value, 0);
|
|
10
10
|
t.equal(genOne.next().value, 1);
|
|
11
11
|
t.equal(genOne.next().value, 2);
|
|
12
12
|
|
|
13
13
|
// simple floating-point
|
|
14
|
-
const genTwo =
|
|
14
|
+
const genTwo = count(0, 0.5);
|
|
15
15
|
t.equal(genTwo.next().value, 0);
|
|
16
16
|
t.equal(genTwo.next().value, 0.5);
|
|
17
17
|
t.equal(genTwo.next().value, 1);
|
package/docs/lang_arrays.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
# Lang / Arrays
|
|
2
2
|
|
|
3
|
-
*
|
|
3
|
+
* **compact(arg)**: removes *null* and *undefined* values from an array, returns always an array. If the *arg* is invalid, it returns an empty array.
|
|
4
|
+
* **accumulate(operator, a, b)**: applies the operator to *a* and *b* then returns the result.
|
|
5
|
+
|
|
6
|
+
## Classes
|
|
7
|
+
|
|
8
|
+
* **Operator**: Static class that configures *accumulate* method. Support *addition (Operator.plus)*, *subtraction (Operator.minus)*, *multiplication (Operator.multiply)* and *division (Operator.division)*.
|
package/docs/lang_itertools.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# Lang / itertools
|
|
2
2
|
|
|
3
|
-
* **
|
|
4
|
-
* **
|
|
3
|
+
* **count(start, step=1)**: A generator that counts from *start* with a specified *step*. Default stepping is *1*.
|
|
4
|
+
* **fn_count(start, step=1, fn)**: A generator that counts from *start* with a specified *step*. Default stepping is *1*. For each iteration the *fn* is called. To continue to the next iteration, the *fn* should return *true*. If not the iteration breaks,.
|
package/package.json
CHANGED
package/src/lang/arrays.js
CHANGED
|
@@ -19,7 +19,31 @@ function compact(arr) {
|
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
class Operator {
|
|
23
|
+
static plus = (a, b) => a + b;
|
|
24
|
+
static minus = (a, b) => a - b;
|
|
25
|
+
static multiply = (a, b) => a * b;
|
|
26
|
+
static divide = (a, b) => a / b;
|
|
27
|
+
|
|
28
|
+
static apply(op, a, b) {
|
|
29
|
+
return op(a, b);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function accumulate(arr, operator, initial=0) {
|
|
35
|
+
let accumulator = initial;
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < arr.length; i++) {
|
|
38
|
+
accumulator = Operator.apply(operator, accumulator, arr[i]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return accumulator;
|
|
42
|
+
}
|
|
43
|
+
|
|
22
44
|
|
|
23
45
|
module.exports = {
|
|
24
|
-
compact
|
|
46
|
+
compact,
|
|
47
|
+
accumulate,
|
|
48
|
+
Operator
|
|
25
49
|
}
|
package/src/lang/itertools.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const {toInteger} = require("../math/numbers");
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
function*
|
|
5
|
+
function* count(start=0, step=1) {
|
|
6
6
|
let iCount = toInteger(start);
|
|
7
7
|
const iStep = toInteger(step);
|
|
8
8
|
|
|
@@ -12,14 +12,14 @@ function* cycle(start=0, step=1) {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function
|
|
16
|
-
const
|
|
15
|
+
function fn_count(start, fn, step=1) {
|
|
16
|
+
const countGenerator = count(start, step);
|
|
17
17
|
|
|
18
|
-
while (fn(
|
|
18
|
+
while (fn(countGenerator.next().value)) { }
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
module.exports = {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
count,
|
|
24
|
+
fn_count
|
|
25
25
|
};
|