Skip to content

Commit ebdada7

Browse files
committed
feature: @putout/plugin-maybe: declare: maybeCall
1 parent 26b0e57 commit ebdada7

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

packages/plugin-maybe/.putout.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"*.md{js}": {
44
"maybe": "off"
55
}
6+
},
7+
"rules": {
8+
"optional-chaining/convert-logical-to-optional": "off"
69
}
7-
}
10+
}

packages/plugin-maybe/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,20 @@ When you not sure is `f` a function, but you want to use it as function anyways:
9898

9999
```js
100100
const fn = maybeFn(f);
101+
102+
maybeCall(fn);
101103
```
102104

103105
### ✅ Example of correct code
104106

105107
```js
106108
const isFn = (a) => typeof a === 'function';
107109
const noop = () => {};
108-
const maybeFn = isFn(a) ? a : noop;
110+
const maybeFn = (a) => isFn(a) ? a : noop;
111+
const maybeCall = (a, ...b) => isFn(a) && a(...b);
109112

110113
const fn = maybeFn(f);
114+
maybeCall(fn);
111115
```
112116

113117
When you not sure is `a` is an array or not, but you want to get first element of it.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const maybeCall = (a, ...b) => isFn(a) && a(...b);
2+
maybeCall(fn, 'hello');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
maybeCall(fn, 'hello');
2+

packages/plugin-maybe/lib/declare/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports.declare = () => ({
55
maybeEmptyArray: 'const maybeEmptyArray = (a) => !a ? [] : a',
66
maybeFn: 'const maybeFn = (a) => isFn(a) ? a : noop',
77
maybeFirst: 'const maybeFirst = (a) => isArray(a) ? a[0] : a',
8+
maybeCall: 'const maybeCall = (a, ...b) => isFn(a) && a(...b)',
89
});

packages/plugin-maybe/lib/declare/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ test('plugin-maybe: declare: transform: maybe-first', (t) => {
2323
t.transform('maybe-first');
2424
t.end();
2525
});
26+
27+
test('plugin-maybe: declare: transform: maybe-call', (t) => {
28+
t.transform('maybe-call');
29+
t.end();
30+
});

0 commit comments

Comments
 (0)