thaw 2.0.6 → 2.1.2
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 +51 -51
- package/dist/index.js +24 -48
- package/package.json +9 -10
package/README.md
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
The narrow belt for AOP 🎀
|
|
2
|
-
---
|
|
3
|
-
This package provides narrow methods for aspect-oriented programming (AOP).
|
|
4
|
-
|
|
5
|
-
## Prerequisites
|
|
6
|
-
|
|
7
|
-
* Node.js `>= 14.x`
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install thaw --save
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
### Usage
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
import * as thaw from 'thaw';
|
|
19
|
-
|
|
20
|
-
const around = thaw.around((val) => val + 1, (val) => val + val);
|
|
21
|
-
const after = thaw.after((val) => val + 1, (val) => val + val);
|
|
22
|
-
const before = thaw.before((val) => val + 1, (val) => val + val);
|
|
23
|
-
const compose = thaw.compose(around, after, before);
|
|
24
|
-
|
|
25
|
-
console.assert(around(1) === 6, 'around');
|
|
26
|
-
console.assert(after(1) === 4, 'after');
|
|
27
|
-
console.assert(before(1) === 3, 'before');
|
|
28
|
-
console.assert(compose(1) === 29, 'compose');
|
|
29
|
-
|
|
30
|
-
let bip = 0;
|
|
31
|
-
const debounce = thaw.debounce(() => bip++, 100);
|
|
32
|
-
|
|
33
|
-
setTimeout(debounce, 0);
|
|
34
|
-
setTimeout(debounce, 100);
|
|
35
|
-
setTimeout(debounce, 250); // will fire
|
|
36
|
-
setTimeout(debounce, 300);
|
|
37
|
-
setTimeout(debounce, 350); // will fire
|
|
38
|
-
setTimeout(debounce, 400);
|
|
39
|
-
setTimeout(() => console.assert(bip === 2, 'debounce'), 1e3);
|
|
40
|
-
|
|
41
|
-
let pib = 0;
|
|
42
|
-
const throttle = thaw.throttle(() => pib++, 100);
|
|
43
|
-
|
|
44
|
-
setTimeout(throttle, 0); // will fire
|
|
45
|
-
setTimeout(throttle, 100);
|
|
46
|
-
setTimeout(throttle, 250); // will fire
|
|
47
|
-
setTimeout(throttle, 300);
|
|
48
|
-
setTimeout(throttle, 350); // will fire
|
|
49
|
-
setTimeout(throttle, 400);
|
|
50
|
-
setTimeout(() => console.assert(pib === 3, 'throttle'), 1e3);
|
|
51
|
-
```
|
|
1
|
+
The narrow belt for AOP 🎀
|
|
2
|
+
---
|
|
3
|
+
This package provides **narrow-trench** methods for aspect-oriented programming (AOP).
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
* Node.js `>= 14.x`
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install thaw --save
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Usage
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import * as thaw from 'thaw';
|
|
19
|
+
|
|
20
|
+
const around = thaw.around((val) => val + 1, (val) => val + val);
|
|
21
|
+
const after = thaw.after((val) => val + 1, (val) => val + val);
|
|
22
|
+
const before = thaw.before((val) => val + 1, (val) => val + val);
|
|
23
|
+
const compose = thaw.compose(around, after, before);
|
|
24
|
+
|
|
25
|
+
console.assert(around(1) === 6, 'around');
|
|
26
|
+
console.assert(after(1) === 4, 'after');
|
|
27
|
+
console.assert(before(1) === 3, 'before');
|
|
28
|
+
console.assert(compose(1) === 29, 'compose');
|
|
29
|
+
|
|
30
|
+
let bip = 0;
|
|
31
|
+
const debounce = thaw.debounce(() => bip++, 100);
|
|
32
|
+
|
|
33
|
+
setTimeout(debounce, 0);
|
|
34
|
+
setTimeout(debounce, 100);
|
|
35
|
+
setTimeout(debounce, 250); // will fire
|
|
36
|
+
setTimeout(debounce, 300);
|
|
37
|
+
setTimeout(debounce, 350); // will fire
|
|
38
|
+
setTimeout(debounce, 400);
|
|
39
|
+
setTimeout(() => console.assert(bip === 2, 'debounce'), 1e3);
|
|
40
|
+
|
|
41
|
+
let pib = 0;
|
|
42
|
+
const throttle = thaw.throttle(() => pib++, 100);
|
|
43
|
+
|
|
44
|
+
setTimeout(throttle, 0); // will fire
|
|
45
|
+
setTimeout(throttle, 100);
|
|
46
|
+
setTimeout(throttle, 250); // will fire
|
|
47
|
+
setTimeout(throttle, 300);
|
|
48
|
+
setTimeout(throttle, 350); // will fire
|
|
49
|
+
setTimeout(throttle, 400);
|
|
50
|
+
setTimeout(() => console.assert(pib === 3, 'throttle'), 1e3);
|
|
51
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -3,44 +3,36 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.throttle = exports.debounce = exports.compose = exports.before = exports.around = exports.after = void 0;
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
return
|
|
8
|
-
return cb(fn(cb
|
|
6
|
+
const around = (fn, cb) => {
|
|
7
|
+
return (...args) => {
|
|
8
|
+
return cb(fn(cb(...args)));
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
exports.around = around;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
-
return cb(fn
|
|
14
|
+
const after = (fn, cb) => {
|
|
15
|
+
return (...args) => {
|
|
16
|
+
return cb(fn(...args));
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
exports.after = after;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
return
|
|
24
|
-
return fn(cb
|
|
22
|
+
const before = (fn, cb) => {
|
|
23
|
+
return (...args) => {
|
|
24
|
+
return fn(cb(...args));
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
exports.before = before;
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
30
|
+
const compose = (...fns) => {
|
|
31
|
+
return (...args) => {
|
|
32
|
+
let result = null;
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
39
|
-
args[_key2] = arguments[_key2];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (var i = 0; i < fns.length; i++) {
|
|
43
|
-
result = fns[i].apply(fns, i ? [result] : args);
|
|
34
|
+
for (let i = 0; i < fns.length; i++) {
|
|
35
|
+
result = fns[i](...(i ? [result] : args));
|
|
44
36
|
}
|
|
45
37
|
|
|
46
38
|
return result;
|
|
@@ -49,40 +41,24 @@ var compose = function compose() {
|
|
|
49
41
|
|
|
50
42
|
exports.compose = compose;
|
|
51
43
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
var tick;
|
|
58
|
-
return function () {
|
|
59
|
-
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
60
|
-
args[_key3] = arguments[_key3];
|
|
61
|
-
}
|
|
62
|
-
|
|
44
|
+
const debounce = (fn, wait = 0) => {
|
|
45
|
+
let tick;
|
|
46
|
+
return (...args) => {
|
|
63
47
|
clearTimeout(tick);
|
|
64
|
-
tick = setTimeout(
|
|
65
|
-
fn
|
|
48
|
+
tick = setTimeout(() => {
|
|
49
|
+
fn(...args);
|
|
66
50
|
}, wait);
|
|
67
51
|
};
|
|
68
52
|
};
|
|
69
53
|
|
|
70
54
|
exports.debounce = debounce;
|
|
71
55
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
var tick;
|
|
78
|
-
return function () {
|
|
79
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
80
|
-
args[_key4] = arguments[_key4];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
tick = !tick && setTimeout(function () {
|
|
56
|
+
const throttle = (fn, wait = 0) => {
|
|
57
|
+
let tick;
|
|
58
|
+
return (...args) => {
|
|
59
|
+
tick = !tick && setTimeout(() => {
|
|
84
60
|
tick = clearTimeout(tick);
|
|
85
|
-
fn
|
|
61
|
+
fn(...args);
|
|
86
62
|
}, wait);
|
|
87
63
|
};
|
|
88
64
|
};
|
package/package.json
CHANGED
|
@@ -8,15 +8,14 @@
|
|
|
8
8
|
"url": "https://github.com/bricss/thaw/issues"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@babel/cli": "^7.
|
|
12
|
-
"@babel/core": "^7.
|
|
13
|
-
"@babel/eslint-parser": "^7.
|
|
14
|
-
"@babel/
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"eslint": "^
|
|
18
|
-
"
|
|
19
|
-
"mocha": "^9.1.3"
|
|
11
|
+
"@babel/cli": "^7.17.6",
|
|
12
|
+
"@babel/core": "^7.17.9",
|
|
13
|
+
"@babel/eslint-parser": "^7.17.0",
|
|
14
|
+
"@babel/preset-env": "^7.16.11",
|
|
15
|
+
"c8": "^7.11.2",
|
|
16
|
+
"eslint": "^8.14.0",
|
|
17
|
+
"eslint-config-ultra-refined": "^2.5.0",
|
|
18
|
+
"mocha": "^9.2.2"
|
|
20
19
|
},
|
|
21
20
|
"description": "The narrow belt for AOP 🎀",
|
|
22
21
|
"engines": {
|
|
@@ -51,5 +50,5 @@
|
|
|
51
50
|
"test": "mocha --exit --recursive",
|
|
52
51
|
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
|
|
53
52
|
},
|
|
54
|
-
"version": "2.
|
|
53
|
+
"version": "2.1.2"
|
|
55
54
|
}
|