attempt-statement 1.1.0 → 1.2.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 -10
- package/attempt.js +1 -43
- package/index.js +44 -0
- package/package.json +30 -11
- package/test.js +0 -10
package/README.md
CHANGED
|
@@ -1,31 +1,38 @@
|
|
|
1
1
|
# attempt-statement
|
|
2
|
+
Never use try/catch/finally again, use attempt/rescue/else/ensure instead.
|
|
3
|
+
|
|
4
|
+
## usage
|
|
2
5
|
```javascript
|
|
3
6
|
const attempt = require('attempt-statement')
|
|
4
7
|
attempt(() => {
|
|
5
|
-
consoe.log('
|
|
8
|
+
consoe.log('this line throws an error because i mispelled console as consoe')
|
|
6
9
|
}).rescue((error) => {
|
|
7
10
|
console.log('Error: ' + error)
|
|
8
11
|
}).else(() => {
|
|
9
|
-
console.log('
|
|
12
|
+
console.log('this will never be run because there\'s always an error')
|
|
10
13
|
}).ensure(() => {
|
|
11
14
|
console.log('This will always be run')
|
|
12
15
|
}).end()
|
|
13
16
|
// Output:
|
|
17
|
+
|
|
14
18
|
// Error: ReferenceError: consoe is not defined
|
|
15
19
|
// This will always be run
|
|
16
20
|
```
|
|
17
|
-
equivalent to
|
|
21
|
+
above is equivalent to
|
|
18
22
|
```javascript
|
|
19
23
|
let error = false
|
|
20
24
|
try {
|
|
21
|
-
consoe.log('
|
|
25
|
+
consoe.log('this line throws an error because i mispelled console as consoe')
|
|
26
|
+
|
|
27
|
+
console.log('this will never be run because there\'s always an error')
|
|
22
28
|
} catch(error) {
|
|
23
29
|
console.log('Error: ' + error)
|
|
24
30
|
error = true
|
|
31
|
+
} finally {
|
|
32
|
+
console.log('This will always be run')
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
there is no built in way in javascript to achieve the else behavior so attempt-statement is the way to go.
|
|
34
|
+
// Output:
|
|
35
|
+
|
|
36
|
+
// Error: ReferenceError: consoe is not defined
|
|
37
|
+
// This will always be run
|
|
38
|
+
```
|
package/attempt.js
CHANGED
|
@@ -1,43 +1 @@
|
|
|
1
|
-
|
|
2
|
-
constructor(handler) {
|
|
3
|
-
if (!(this instanceof Attempt)) return new Attempt(handler)
|
|
4
|
-
this.trycode = handler
|
|
5
|
-
return this
|
|
6
|
-
}
|
|
7
|
-
rescue(handler) {
|
|
8
|
-
this.rescuehandler = handler
|
|
9
|
-
return this
|
|
10
|
-
}
|
|
11
|
-
else(handler) {
|
|
12
|
-
this.elsehandler = handler
|
|
13
|
-
return this
|
|
14
|
-
}
|
|
15
|
-
ensure(handler) {
|
|
16
|
-
this.ensurehandler = handler
|
|
17
|
-
return this
|
|
18
|
-
}
|
|
19
|
-
end() {
|
|
20
|
-
const dis = this
|
|
21
|
-
let error = false
|
|
22
|
-
try {
|
|
23
|
-
if (this.trycode) this.trycode()
|
|
24
|
-
} catch (e) {
|
|
25
|
-
error = true
|
|
26
|
-
if (this.rescuehandler) this.rescuehandler(e)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!error && this.elsehandler) {
|
|
30
|
-
this.elsehandler()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (this.ensurehandler) this.ensurehandler()
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function attempt(handler) {
|
|
38
|
-
this.______attemptfn = true
|
|
39
|
-
return new Attempt(handler)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
module.exports = attempt
|
|
1
|
+
module.exports = require("./index")
|
package/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var t = require("true-value")
|
|
2
|
+
|
|
3
|
+
class Attempt {
|
|
4
|
+
constructor(handler) {
|
|
5
|
+
if (!(this instanceof Attempt)) return new Attempt(handler)
|
|
6
|
+
this.trycode = handler
|
|
7
|
+
return this
|
|
8
|
+
}
|
|
9
|
+
rescue(handler) {
|
|
10
|
+
this.rescuehandler = handler
|
|
11
|
+
return this
|
|
12
|
+
}
|
|
13
|
+
else(handler) {
|
|
14
|
+
this.elsehandler = handler
|
|
15
|
+
return this
|
|
16
|
+
}
|
|
17
|
+
ensure(handler) {
|
|
18
|
+
this.ensurehandler = handler
|
|
19
|
+
return this
|
|
20
|
+
}
|
|
21
|
+
end() {
|
|
22
|
+
let error = false
|
|
23
|
+
try {
|
|
24
|
+
if (this.trycode) this.trycode()
|
|
25
|
+
} catch (e) {
|
|
26
|
+
error = t()
|
|
27
|
+
if (this.rescuehandler) this.rescuehandler(e)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!error && this.elsehandler) {
|
|
31
|
+
this.elsehandler()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (this.ensurehandler) this.ensurehandler()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function attempt(handler) {
|
|
39
|
+
this.________true_value_ignore________ = t()
|
|
40
|
+
return new Attempt(handler)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
module.exports = attempt
|
package/package.json
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "attempt-statement",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "attempt-statement",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "try in fp",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"attempt",
|
|
7
|
+
"try",
|
|
8
|
+
"catch",
|
|
9
|
+
"lua",
|
|
10
|
+
"rescue"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/in-fp/attempt-statement#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/in-fp/attempt-statement/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/in-fp/attempt-statement.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "UNLICENSED",
|
|
21
|
+
"author": "fox",
|
|
22
|
+
"type": "commonjs",
|
|
23
|
+
"main": "attempt.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"true-value": "^2.0.5"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/test.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const attempt = require('./attempt')
|
|
2
|
-
attempt(() => {
|
|
3
|
-
consoe.log('I misspelled console')
|
|
4
|
-
}).rescue((error) => {
|
|
5
|
-
console.log('Error: ' + error)
|
|
6
|
-
}).else(() => {
|
|
7
|
-
console.log('No error yay')
|
|
8
|
-
}).ensure(() => {
|
|
9
|
-
console.log('This will always be run')
|
|
10
|
-
}).end()
|