attempt-statement 1.1.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 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('I misspelled console')
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('No error yay')
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('I misspelled console')
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
- if (error === false) {
27
- console.log('No error yay')
28
- }
29
- console.log('This will always be run')
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
+ ```
@@ -1,3 +1,5 @@
1
+ var t = require("true-value")
2
+
1
3
  class Attempt {
2
4
  constructor(handler) {
3
5
  if (!(this instanceof Attempt)) return new Attempt(handler)
@@ -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 = true
26
+ error = t()
26
27
  if (this.rescuehandler) this.rescuehandler(e)
27
28
  }
28
29
 
@@ -35,7 +36,7 @@ class Attempt {
35
36
  }
36
37
 
37
38
  function attempt(handler) {
38
- this.______attemptfn = true
39
+ this.________true_value_ignore________ = t()
39
40
  return new Attempt(handler)
40
41
  }
41
42
 
package/package.json CHANGED
@@ -1,11 +1,30 @@
1
- {
2
- "name": "attempt-statement",
3
- "version": "1.1.0",
4
- "description": "An attempt statement for js",
5
- "main": "attempt.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "author": "fox",
10
- "license": "UNLICENSED"
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
+ }
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()