You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by do...@apache.org on 2021/06/03 01:58:47 UTC
[arrow] branch master updated: ARROW-12799: [JS] Use Nullish
Coalescing Operator (??) For Defaults
This is an automated email from the ASF dual-hosted git repository.
domoritz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new e6e437a ARROW-12799: [JS] Use Nullish Coalescing Operator (??) For Defaults
e6e437a is described below
commit e6e437a14e9eec16707944ea3b3a3a298e0d8dec
Author: P42 <72...@users.noreply.github.com>
AuthorDate: Wed Jun 2 18:57:42 2021 -0700
ARROW-12799: [JS] Use Nullish Coalescing Operator (??) For Defaults
**The nullish coalescing operator (`??`) returns its right side when its left side is nullish** (`null` or `undefined`), and its left side otherwise.
For example, `const x = a ?? b` would set `x` to `a` if `a` has a value, and to `b` if `a` is `null` or `undefined`.
The nullish coalescing operator is very useful to **provide default values when a value or an expression is nullish**.
Before its introduction in ES2020, this default value pattern was often expressed using the conditional operator.
This refactoring simplifies conditional (ternary) checks to nullish coalescing operator expressions:
* `a == null ? x : a` becomes `a ?? x`
* `a != null ? a : x` becomes `a ?? x`
* `a === null || a === undefined ? x : a` becomes `a ?? x`
* `a !== null && a !== undefined ? a : x` becomes `a ?? x`
* `f(1) != null ? f(1) : x` becomes `f(1) ?? x`
* etc.
Learn More: [Nullish coalescing operator (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator)
When two similar-looking function calls have a side effect, this refactoring can change the behavior of the code.
For example, the refactoring changes:
```javascript
let a = f(1) === null || f(1) === undefined ? 'default' : f(1);
```
into
```javascript
let a = f(1) ?? 'default';
```
If `f(1)` has a side effect, it would have been called one, two or three times before the refactoring, and once after the refactoring.
This means that the side effect would have been called a different number of times, potentially changing the behavior.
Closes #10339 from domoritz/p42/nullish_coalescing_operator/1621098556103
Authored-by: P42 <72...@users.noreply.github.com>
Signed-off-by: Dominik Moritz <do...@gmail.com>
---
js/bin/integration.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/js/bin/integration.js b/js/bin/integration.js
index c357c12..507514e 100755
--- a/js/bin/integration.js
+++ b/js/bin/integration.js
@@ -189,7 +189,7 @@ function compareTableIsh(actual, expected) {
function compareVectors(actual, expected) {
if ((actual == null && expected != null) || (expected == null && actual != null)) {
- throw new Error(`${actual == null ? `actual` : `expected`} is null, was expecting ${actual == null ? expected : actual} to be that also`);
+ throw new Error(`${actual == null ? `actual` : `expected`} is null, was expecting ${actual ?? expected} to be that also`);
}
let props = ['type', 'length', 'nullCount'];