while2 1.0.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 +71 -0
- package/index.js +1 -0
- package/lib/while2.js +32 -0
- package/package.json +14 -0
- package/test.js +7 -0
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# While2
|
|
2
|
+
|
|
3
|
+
### What is while2
|
|
4
|
+
While2 is while statements, but different syntax. Also it includes a built-in index value.
|
|
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()
|
|
15
|
+
```
|
|
16
|
+
### Installation
|
|
17
|
+
|
|
18
|
+
```npm install while2```
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
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".
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
const w2 = require('while2')
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For every while2 statement, you will create a new instance of w2, as it is a class, and pass a condition, and 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 in the background.
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const w2 = require('while2')
|
|
36
|
+
|
|
37
|
+
new w2(true)
|
|
38
|
+
.do(() => {
|
|
39
|
+
// anything here will run again and again forever
|
|
40
|
+
})
|
|
41
|
+
.end() // this line makes sure that the while loop is ran in the background. if you don't include this nothing will happen
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Features: Builtin Index
|
|
45
|
+
|
|
46
|
+
While2 includes an i parameter that will automatically increment every time the do function is run.
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
const w2 = require('while2')
|
|
50
|
+
|
|
51
|
+
new w2(true)
|
|
52
|
+
.do(i => {
|
|
53
|
+
console.log(i) /* will print 1 2 3 4 5 6 7... */
|
|
54
|
+
})
|
|
55
|
+
.end()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Features: BreakLoop Function
|
|
59
|
+
|
|
60
|
+
While2 includes a breakLoop parameter function so that you can break your while loop.
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const w2 = require('./lib/while2')
|
|
64
|
+
|
|
65
|
+
new w2(true)
|
|
66
|
+
.do((i, breakLoop) => {
|
|
67
|
+
if (i > 3) breakLoop() // prints 0, 1, 2, 3 then stops
|
|
68
|
+
console.log(i)
|
|
69
|
+
})
|
|
70
|
+
.end()
|
|
71
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/while2')
|
package/lib/while2.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "while2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "for when while loops aren't verbose enoughs",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"directories": {
|
|
7
|
+
"lib": "lib"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"author": "me",
|
|
13
|
+
"license": "UNLICENSED"
|
|
14
|
+
}
|