attempt-statement 1.0.0 → 1.2.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 +38 -0
- package/{attempt.js → index.js} +10 -4
- package/package.json +30 -11
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# attempt-statement
|
|
2
|
+
Never use try/catch/finally again, use attempt/rescue/else/ensure instead.
|
|
3
|
+
|
|
4
|
+
## usage
|
|
5
|
+
```javascript
|
|
6
|
+
const attempt = require('attempt-statement')
|
|
7
|
+
attempt(() => {
|
|
8
|
+
consoe.log('this line throws an error because i mispelled console as consoe')
|
|
9
|
+
}).rescue((error) => {
|
|
10
|
+
console.log('Error: ' + error)
|
|
11
|
+
}).else(() => {
|
|
12
|
+
console.log('this will never be run because there\'s always an error')
|
|
13
|
+
}).ensure(() => {
|
|
14
|
+
console.log('This will always be run')
|
|
15
|
+
}).end()
|
|
16
|
+
// Output:
|
|
17
|
+
|
|
18
|
+
// Error: ReferenceError: consoe is not defined
|
|
19
|
+
// This will always be run
|
|
20
|
+
```
|
|
21
|
+
above is equivalent to
|
|
22
|
+
```javascript
|
|
23
|
+
let error = false
|
|
24
|
+
try {
|
|
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')
|
|
28
|
+
} catch(error) {
|
|
29
|
+
console.log('Error: ' + error)
|
|
30
|
+
error = true
|
|
31
|
+
} finally {
|
|
32
|
+
console.log('This will always be run')
|
|
33
|
+
}
|
|
34
|
+
// Output:
|
|
35
|
+
|
|
36
|
+
// Error: ReferenceError: consoe is not defined
|
|
37
|
+
// This will always be run
|
|
38
|
+
```
|
package/{attempt.js → index.js}
RENAMED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
var t = require("true-value")
|
|
2
|
+
|
|
3
|
+
class Attempt {
|
|
2
4
|
constructor(handler) {
|
|
3
|
-
if (!(this instanceof
|
|
5
|
+
if (!(this instanceof Attempt)) return new Attempt(handler)
|
|
4
6
|
this.trycode = handler
|
|
5
7
|
return this
|
|
6
8
|
}
|
|
@@ -17,12 +19,11 @@ class attempt {
|
|
|
17
19
|
return this
|
|
18
20
|
}
|
|
19
21
|
end() {
|
|
20
|
-
const dis = this
|
|
21
22
|
let error = false
|
|
22
23
|
try {
|
|
23
24
|
if (this.trycode) this.trycode()
|
|
24
25
|
} catch (e) {
|
|
25
|
-
error =
|
|
26
|
+
error = t()
|
|
26
27
|
if (this.rescuehandler) this.rescuehandler(e)
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -34,5 +35,10 @@ class attempt {
|
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
function attempt(handler) {
|
|
39
|
+
this.________true_value_ignore________ = t()
|
|
40
|
+
return new Attempt(handler)
|
|
41
|
+
}
|
|
42
|
+
|
|
37
43
|
|
|
38
44
|
module.exports = attempt
|
package/package.json
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "attempt-statement",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "attempt-statement",
|
|
3
|
+
"version": "1.2.0",
|
|
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
|
+
}
|