returndotjs 1.0.0 → 1.1.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 +5 -3
- package/index.js +2 -8
- package/package.json +24 -6
- package/safe.d.ts +13 -0
- package/safe.js +29 -17
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ function add(a, b) {
|
|
|
12
12
|
...you can now do this!
|
|
13
13
|
|
|
14
14
|
```js
|
|
15
|
-
require('returndotjs')
|
|
15
|
+
const { functions, _return } = require('returndotjs/safe')
|
|
16
16
|
|
|
17
17
|
functions.add = function(a, b) {
|
|
18
18
|
_return(a + b)
|
|
@@ -23,7 +23,7 @@ const add = functions.add
|
|
|
23
23
|
Or this:
|
|
24
24
|
|
|
25
25
|
```js
|
|
26
|
-
|
|
26
|
+
require('returndotjs')
|
|
27
27
|
|
|
28
28
|
functions.add = function(a, b) {
|
|
29
29
|
_return(a + b)
|
|
@@ -42,7 +42,7 @@ which outputs `3`!
|
|
|
42
42
|
|
|
43
43
|
Using Return.js will work with the following examples:
|
|
44
44
|
```js
|
|
45
|
-
require('returndotjs')
|
|
45
|
+
const { functions, _return } = require('returndotjs/safe')
|
|
46
46
|
|
|
47
47
|
functions.hi = function () {
|
|
48
48
|
_return("hi")
|
|
@@ -59,6 +59,8 @@ functions.hi = () => {
|
|
|
59
59
|
But it won't work like this:
|
|
60
60
|
|
|
61
61
|
```js
|
|
62
|
+
const { functions, _return } = require('returndotjs/safe')
|
|
63
|
+
|
|
62
64
|
const hi = functions.hi = () => {
|
|
63
65
|
_return("hi")
|
|
64
66
|
}
|
package/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
const { _return, functions } = require(
|
|
1
|
+
const { _return, functions } = require("./safe")
|
|
2
2
|
|
|
3
|
-
var _myGlobal
|
|
4
|
-
|
|
5
|
-
if (typeof global !== 'undefined') {
|
|
6
|
-
_myGlobal = global
|
|
7
|
-
} else {
|
|
8
|
-
_myGlobal = window
|
|
9
|
-
}
|
|
3
|
+
var _myGlobal = require("@10xly/global")
|
|
10
4
|
|
|
11
5
|
_myGlobal._return = _return
|
|
12
6
|
_myGlobal.functions = functions
|
package/package.json
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "returndotjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Return it.",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
5
|
"keywords": [
|
|
10
6
|
"return"
|
|
11
7
|
],
|
|
8
|
+
"homepage": "https://github.com/10xly/returndotjs#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/10xly/returndotjs/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/10xly/returndotjs.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
12
17
|
"author": "tj-commits",
|
|
13
|
-
"
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"main": "index.js",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@10xly/global": "^1.0.0",
|
|
25
|
+
"amp-is-function": "^1.0.1",
|
|
26
|
+
"array-get-member": "^1.0.3",
|
|
27
|
+
"construct-new": "^2.0.5",
|
|
28
|
+
"es-intrinsic-cache": "^1.0.4",
|
|
29
|
+
"lodash.stubobject": "^4.13.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {}
|
|
14
32
|
}
|
package/safe.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets the value to be returned by the next intercepted function call.
|
|
3
|
+
* @param value - The value that the next call to a member of `functions` will return.
|
|
4
|
+
*/
|
|
5
|
+
export function _return(value: any): void
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Proxy-wrapped object where every property access that results in a function
|
|
9
|
+
* will execute the underlying function but return the value previously set via `_return`.
|
|
10
|
+
*/
|
|
11
|
+
export const functions: {
|
|
12
|
+
[key: string]: (...args: any[]) => any
|
|
13
|
+
}
|
package/safe.js
CHANGED
|
@@ -3,24 +3,36 @@ function _return(value) {
|
|
|
3
3
|
returnValue = value
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
get(target, p, reciever) {
|
|
12
|
-
if (typeof target[p] === 'function') {
|
|
13
|
-
return function (...args) {
|
|
14
|
-
target[p](...args)
|
|
15
|
-
var result = returnValue
|
|
16
|
-
returnValue = undefined
|
|
17
|
-
return result
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return target[p]
|
|
21
|
-
}
|
|
22
|
-
})
|
|
6
|
+
const getIntrinsic = require("es-intrinsic-cache")
|
|
7
|
+
const proxy = getIntrinsic("%Proxy%")
|
|
8
|
+
const construct = require("construct-new")
|
|
9
|
+
const isFunc = require("amp-is-function")
|
|
10
|
+
const { True } = require("array-get-member")
|
|
23
11
|
|
|
12
|
+
var functions = construct({
|
|
13
|
+
target: proxy,
|
|
14
|
+
args: [
|
|
15
|
+
require("lodash.stubobject")(),
|
|
16
|
+
{
|
|
17
|
+
set(target, p, newValue, reciever) {
|
|
18
|
+
target[p] = newValue
|
|
19
|
+
return True
|
|
20
|
+
},
|
|
21
|
+
get(target, p, reciever) {
|
|
22
|
+
if (isFunc(target[p])) {
|
|
23
|
+
return function (...args) {
|
|
24
|
+
target[p](...args)
|
|
25
|
+
var result = returnValue
|
|
26
|
+
returnValue = void void void void void // so much void
|
|
27
|
+
null
|
|
28
|
+
return result
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return target[p]
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
})
|
|
24
36
|
|
|
25
37
|
exports._return = _return
|
|
26
38
|
exports.functions = functions
|