while2 1.0.0 → 2.0.1
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 +17 -29
- package/index.js +32 -1
- package/package.json +20 -5
- package/lib/while2.js +0 -32
- package/test.js +0 -7
package/README.md
CHANGED
|
@@ -1,47 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `while2`
|
|
2
|
+
`while2` is a while statement, but 10x and function-oriented. Also, it includes a built-in index value.
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
```javascript
|
|
7
|
-
const w2 = require('./lib/while2')
|
|
8
|
-
|
|
9
|
-
new w2(true)
|
|
10
|
-
.do((i, breakLoop) => {
|
|
11
|
-
if (i > 3) breakLoop() // prints 0, 1, 2, 3 then stops
|
|
12
|
-
console.log(i)
|
|
13
|
-
})
|
|
14
|
-
.end()
|
|
4
|
+
## Installation
|
|
5
|
+
```bash
|
|
6
|
+
$ npm install while2
|
|
15
7
|
```
|
|
16
|
-
### Installation
|
|
17
8
|
|
|
18
|
-
|
|
9
|
+
## Usage
|
|
19
10
|
|
|
20
|
-
|
|
21
|
-
const w2 = require('while2')
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
### Syntax
|
|
25
|
-
|
|
26
|
-
To use while2, first import the module. It is recommended to call it "w2".
|
|
11
|
+
To use while2, first import the module. It is recommended to call it `w2`.
|
|
27
12
|
|
|
28
13
|
```javascript
|
|
29
14
|
const w2 = require('while2')
|
|
30
15
|
```
|
|
31
16
|
|
|
32
|
-
For every while2 statement, you will create a new instance of w2
|
|
17
|
+
For every while2 statement, you will create a new instance of `w2`, as it is a class, and pass a condition. For everything that you want to run while the condition is true, put `.do(...)` and then pass in a function in the parentheses. Always type `.end()`at the end to run the while loop or it won't work
|
|
33
18
|
|
|
34
19
|
```javascript
|
|
35
20
|
const w2 = require('while2')
|
|
36
21
|
|
|
37
|
-
new w2(true)
|
|
22
|
+
new w2(true) // equivalent to while (true)
|
|
38
23
|
.do(() => {
|
|
39
24
|
// anything here will run again and again forever
|
|
40
25
|
})
|
|
41
26
|
.end() // this line makes sure that the while loop is ran in the background. if you don't include this nothing will happen
|
|
42
27
|
```
|
|
43
28
|
|
|
44
|
-
###
|
|
29
|
+
### Built-in index
|
|
45
30
|
|
|
46
31
|
While2 includes an i parameter that will automatically increment every time the do function is run.
|
|
47
32
|
|
|
@@ -55,12 +40,12 @@ new w2(true)
|
|
|
55
40
|
.end()
|
|
56
41
|
```
|
|
57
42
|
|
|
58
|
-
###
|
|
43
|
+
### Breaking the loop
|
|
59
44
|
|
|
60
|
-
While2 includes a breakLoop parameter function so that you can break your while loop.
|
|
45
|
+
While2 includes a `breakLoop` parameter function so that you can break your while loop.
|
|
61
46
|
|
|
62
47
|
```javascript
|
|
63
|
-
const w2 = require('
|
|
48
|
+
const w2 = require('while2')
|
|
64
49
|
|
|
65
50
|
new w2(true)
|
|
66
51
|
.do((i, breakLoop) => {
|
|
@@ -68,4 +53,7 @@ new w2(true)
|
|
|
68
53
|
console.log(i)
|
|
69
54
|
})
|
|
70
55
|
.end()
|
|
71
|
-
```
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
No license, do whatever you want
|
package/index.js
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
const breakError = {}
|
|
2
|
+
|
|
3
|
+
class w2 {
|
|
4
|
+
constructor(condition) {
|
|
5
|
+
if (!this instanceof w2) return new w2()
|
|
6
|
+
this.condition = condition
|
|
7
|
+
this.i = 0
|
|
8
|
+
return this
|
|
9
|
+
}
|
|
10
|
+
do(cb) {
|
|
11
|
+
this.doHandler = cb
|
|
12
|
+
return this
|
|
13
|
+
}
|
|
14
|
+
end() {
|
|
15
|
+
try {
|
|
16
|
+
while (this.condition) {
|
|
17
|
+
this.doHandler(this.i, () => {
|
|
18
|
+
throw breakError
|
|
19
|
+
}) // pass in the function to break the loop
|
|
20
|
+
this.i += 1
|
|
21
|
+
}
|
|
22
|
+
} catch(err) {
|
|
23
|
+
if (err === breakError) return
|
|
24
|
+
|
|
25
|
+
throw err
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
module.exports = w2
|
package/package.json
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "while2",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "while in fp",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"while",
|
|
7
|
+
"while2",
|
|
8
|
+
"declaration",
|
|
9
|
+
"statement"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/in-fp/while2#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/in-fp/while2/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/in-fp/while2.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "UNLICENSED",
|
|
20
|
+
"author": "me",
|
|
21
|
+
"type": "commonjs",
|
|
5
22
|
"main": "index.js",
|
|
6
23
|
"directories": {
|
|
7
24
|
"lib": "lib"
|
|
8
25
|
},
|
|
9
26
|
"scripts": {
|
|
10
27
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
-
}
|
|
12
|
-
"author": "me",
|
|
13
|
-
"license": "UNLICENSED"
|
|
28
|
+
}
|
|
14
29
|
}
|
package/lib/while2.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const breakError = {}
|
|
2
|
-
|
|
3
|
-
class w2 {
|
|
4
|
-
constructor(condition) {
|
|
5
|
-
if (!this instanceof w2) return new w2()
|
|
6
|
-
this.condition = condition
|
|
7
|
-
this.i = 0
|
|
8
|
-
return this
|
|
9
|
-
}
|
|
10
|
-
do(cb) {
|
|
11
|
-
this.doHandler = cb
|
|
12
|
-
return this
|
|
13
|
-
}
|
|
14
|
-
end() {
|
|
15
|
-
try {
|
|
16
|
-
while (this.condition) {
|
|
17
|
-
this.doHandler(this.i, () => {
|
|
18
|
-
throw breakError
|
|
19
|
-
}) // pass in the function to break the loop
|
|
20
|
-
this.i += 1
|
|
21
|
-
}
|
|
22
|
-
} catch(err) {
|
|
23
|
-
if (err === breakError) return
|
|
24
|
-
|
|
25
|
-
throw err
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
module.exports = w2
|