Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit ab6fa71

Browse files
Closure Teamcopybara-github
authored andcommitted
Remove the defaultImpl parameter from Closure Delegates helper methods.
Callers are instead encouraged to explicitly check the number of registered delegates, or use the nullish coalescing operator on the result of the helper method as in `callFirst(delegates, (d) => ...) ?? defaultValue` RELNOTES[INC]: Remove the `defaultImpl` parameter from Closure Delegates helper methods. PiperOrigin-RevId: 510550303 Change-Id: Ia405b30a0bab5b8bf995b05870f74ce933252d1f
1 parent 1703a95 commit ab6fa71

File tree

2 files changed

+10
-51
lines changed

2 files changed

+10
-51
lines changed

closure/goog/delegate/delegates.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ goog.module('goog.delegate.delegates');
1818
* Calls the first delegate, or returns undefined if none are given.
1919
* @param {!Array<T>} delegates
2020
* @param {function(T): R} mapper
21-
* @param {function(): (R|undefined)=} defaultImpl
2221
* @return {R|undefined}
2322
* @template T, R
2423
*/
25-
exports.callFirst = (delegates, mapper, defaultImpl = undefined) => {
24+
exports.callFirst = (delegates, mapper) => {
2625
if (delegates.length === 0) {
27-
return defaultImpl ? defaultImpl() : undefined;
26+
return undefined;
2827
}
2928
return mapper(delegates[0]);
3029
};
@@ -35,36 +34,27 @@ exports.callFirst = (delegates, mapper, defaultImpl = undefined) => {
3534
* undefined if no such element is found.
3635
* @param {!Array<T>} delegates
3736
* @param {function(T): R|undefined} mapper
38-
* @param {function(): (R|undefined)=} defaultImpl
3937
* @return {R|undefined}
4038
* @template T, R
4139
*/
42-
exports.callUntilDefinedAndNotNull =
43-
(delegates, mapper, defaultImpl = undefined) => {
44-
if (delegates.length === 0) {
45-
return defaultImpl ? defaultImpl() : undefined;
46-
}
47-
for (const delegate of delegates) {
48-
const result = mapper(delegate);
49-
if (result != null) return result;
50-
}
51-
return undefined;
52-
};
40+
exports.callUntilDefinedAndNotNull = (delegates, mapper) => {
41+
for (const delegate of delegates) {
42+
const result = mapper(delegate);
43+
if (result != null) return result;
44+
}
45+
return undefined;
46+
};
5347

5448

5549
/**
5650
* Calls delegates until one returns a truthy result. Returns false if no such
5751
* element is found.
5852
* @param {!Array<T>} delegates
5953
* @param {function(T): R} mapper
60-
* @param {function(): (R|boolean)=} defaultImpl
6154
* @return {boolean|R}
6255
* @template T, R
6356
*/
64-
exports.callUntilTruthy = (delegates, mapper, defaultImpl = undefined) => {
65-
if (delegates.length === 0) {
66-
return defaultImpl ? defaultImpl() : false;
67-
}
57+
exports.callUntilTruthy = (delegates, mapper) => {
6858
for (const delegate of delegates) {
6959
const result = mapper(delegate);
7060
if (result) return result;

closure/goog/delegate/delegates_test.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,6 @@ testSuite({
3737
assertCallCounts(1, 1, 1, 0);
3838
},
3939

40-
testWithDelegatesRegistered_defaultFunctionsAreNotCalled() {
41-
const funcs = [
42-
() => 'non-default',
43-
];
44-
45-
assertEquals('non-default', delegates.callFirst(funcs, f => f(), () => {
46-
throw new Error('Default should not have been called');
47-
}));
48-
assertEquals(
49-
'non-default',
50-
delegates.callUntilDefinedAndNotNull(funcs, f => f(), () => {
51-
throw new Error('Default should not have been called');
52-
}));
53-
assertEquals(
54-
'non-default', delegates.callUntilTruthy(funcs, f => f(), () => {
55-
throw new Error('Default should not have been called');
56-
}));
57-
},
58-
59-
testWithNoDelegatesRegistered_defaultFunctionsAreCalled() {
60-
const funcs = [];
61-
62-
assertEquals(
63-
'default', delegates.callFirst(funcs, f => f(), () => 'default'));
64-
assertEquals(
65-
'default',
66-
delegates.callUntilDefinedAndNotNull(funcs, f => f(), () => 'default'));
67-
assertEquals(
68-
'default', delegates.callUntilTruthy(funcs, f => f(), () => 'default'));
69-
},
70-
7140
testResultNeverDefined() {
7241
const funcs = [
7342
recordFunction(),

0 commit comments

Comments
 (0)