@wizhut_tech/wizjs 0.1.3 → 0.1.4
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 { count,
|
|
3
|
+
const { count, repeat } = require('../src/lang/itertools.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
t.test('itertools/count', (t) => {
|
|
@@ -19,3 +19,18 @@ t.test('itertools/count', (t) => {
|
|
|
19
19
|
|
|
20
20
|
t.end();
|
|
21
21
|
});
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
t.test('itertools/repeat', (t) => {
|
|
25
|
+
// single repeat
|
|
26
|
+
const repeatGen = repeat(10, 1);
|
|
27
|
+
t.equal(repeatGen.next().value, 10);
|
|
28
|
+
t.equal(repeatGen.return().done, true);
|
|
29
|
+
|
|
30
|
+
// eternal repeat
|
|
31
|
+
const repeatGenTwo = repeat(15, 0);
|
|
32
|
+
t.equal(repeatGenTwo.next().value, 15);
|
|
33
|
+
t.equal(repeatGenTwo.next().value, 15);
|
|
34
|
+
|
|
35
|
+
t.end();
|
|
36
|
+
});
|
package/docs/lang_itertools.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# Lang / itertools
|
|
2
2
|
|
|
3
3
|
* **count(start, step=1)**: A generator that counts from *start* with a specified *step*. Default stepping is *1*.
|
|
4
|
-
* **
|
|
4
|
+
* **repeat(arg, times=0)**: A generator that repeats *arg* for *times* times. If times is 0 then it returns *arg* forever.
|
package/package.json
CHANGED
package/src/lang/itertools.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
const {toInteger} = require("../math/numbers");
|
|
1
|
+
const { toInteger } = require('../math/numbers.js');
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
function* count(start=0, step=1) {
|
|
6
5
|
let iCount = toInteger(start);
|
|
7
|
-
const iStep = toInteger(step);
|
|
8
6
|
|
|
9
7
|
while (true) {
|
|
10
8
|
yield iCount;
|
|
@@ -12,14 +10,28 @@ function* count(start=0, step=1) {
|
|
|
12
10
|
}
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
function
|
|
16
|
-
|
|
13
|
+
function *repeat(arg, times=0) {
|
|
14
|
+
let iTimes = toInteger(times);
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
if (iTimes < 0) {
|
|
17
|
+
iTimes = 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let iTimesLeft = iTimes;
|
|
20
21
|
|
|
22
|
+
while (true) {
|
|
23
|
+
if (iTimes === 0) {
|
|
24
|
+
yield arg;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (iTimesLeft > 0) {
|
|
28
|
+
iTimesLeft -= 1;
|
|
29
|
+
yield arg;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
21
33
|
|
|
22
34
|
module.exports = {
|
|
23
35
|
count,
|
|
24
|
-
|
|
36
|
+
repeat
|
|
25
37
|
};
|