forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] [@kbn/tinymath] Add new defaults function (elastic#142087)
* ✨ Add new defaults function * ✨ Add new defaults function * 📝 Rewrite the doc Co-authored-by: Joe Reuter <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
/** | ||
* Returns the default provided value when the first value if null. If at least one array is passed into the function, the function will be applied index-wise to each element. | ||
* @param {(any|any[])} a array of any values | ||
* @param {(any)} b a value to use as fallback. | ||
* @return {(any|any[])} The `a` value if not null, `b` otherwise. Returns an array where each element is default to `b` when null, or kept the original value if `a` is an array. | ||
* | ||
* @example | ||
* defaults(null, 1) // returns 1 | ||
* defaults([3, null, 5], 1) // returns [3, 1, 5] | ||
* defaults(5, 1) // returns 5 | ||
*/ | ||
|
||
module.exports = { defaults }; | ||
|
||
function defaults(a, b) { | ||
if (Array.isArray(a)) { | ||
return a.map((v) => (v == null ? b : v)); | ||
} | ||
return a == null ? b : a; | ||
} | ||
|
||
defaults.skipNumberValidation = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
const { defaults } = require('../../src/functions/defaults'); | ||
|
||
describe('Defaults', () => { | ||
it('number, number', () => { | ||
expect(defaults(10, 2)).toEqual(10); | ||
}); | ||
|
||
it('null, number', () => { | ||
expect(defaults(null, 2)).toEqual(2); | ||
}); | ||
|
||
it('number, null', () => { | ||
expect(defaults(2, null)).toEqual(2); | ||
}); | ||
|
||
it('array, number', () => { | ||
expect(defaults([10, 20, 30, 40], 10)).toEqual([10, 20, 30, 40]); | ||
}); | ||
|
||
it('arrays with null, number', () => { | ||
expect(defaults([null, 20, 30, null], 10)).toEqual([10, 20, 30, 10]); | ||
}); | ||
|
||
it('empty array, number', () => { | ||
expect(defaults([], 10)).toEqual([]); | ||
}); | ||
|
||
it('skips number validation', () => { | ||
expect(defaults).toHaveProperty('skipNumberValidation', true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters