@enact/core
Advanced tools
Comparing version 3.3.0-alpha.13 to 3.3.0-alpha.14
@@ -5,2 +5,8 @@ # Change Log | ||
## [3.3.0-alpha.14] - 2020-06-29 | ||
### Added | ||
- `core/util` function `mapAndFilterChildren` to safely iterate over React `children` | ||
## [3.3.0-alpha.13] - 2020-06-22 | ||
@@ -7,0 +13,0 @@ |
{ | ||
"name": "@enact/core", | ||
"version": "3.3.0-alpha.13", | ||
"version": "3.3.0-alpha.14", | ||
"description": "Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,2 +61,64 @@ "use strict"; | ||
}); | ||
describe('mapAndFilterChildren', function () { | ||
test('Returns null if null passed', function () { | ||
var expected = null; | ||
var actual = (0, _util.mapAndFilterChildren)(null, function (val) { | ||
return val; | ||
}); | ||
expect(actual).toBe(expected); | ||
}); | ||
test('Returns passed array if identity filter', function () { | ||
var children = [1, 2, 3]; | ||
var expected = children; | ||
var actual = (0, _util.mapAndFilterChildren)(children, function (val) { | ||
return val; | ||
}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
test('Returns passed array without nullish or false entries with identity filter', function () { | ||
// eslint-disable-next-line no-undefined | ||
var children = [1, 2, null, 3, undefined, false]; | ||
var expected = [1, 2, 3]; | ||
var actual = (0, _util.mapAndFilterChildren)(children, function (val) { | ||
return val; | ||
}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
test('Does not call filter with nullish or false entries', function () { | ||
var spy = jest.fn(); // eslint-disable-next-line no-undefined | ||
var children = [1, 2, null, 3, undefined, false]; | ||
(0, _util.mapAndFilterChildren)(children, spy); | ||
var expected = 3; | ||
var actual = spy.mock.calls.length; | ||
expect(actual).toBe(expected); | ||
}); | ||
test('Returns without null entries from filter', function () { | ||
var children = [1, 2, 3]; | ||
var expected = [1, 3]; | ||
var actual = (0, _util.mapAndFilterChildren)(children, function (val) { | ||
return val === 2 ? null : val; | ||
}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
test('Runs custom filter', function () { | ||
var children = [1, 2, 3]; | ||
var expected = [1]; | ||
var actual = (0, _util.mapAndFilterChildren)(children, function (val) { | ||
return val === 2 ? null : val; | ||
}, function (val) { | ||
return val === 1; | ||
}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
test('should forward value and index to callback', function () { | ||
var spy = jest.fn(); | ||
(0, _util.mapAndFilterChildren)([1], spy); | ||
var expected = [1, // value | ||
0 // index | ||
]; | ||
var actual = spy.mock.calls[0]; | ||
expect(expected).toEqual(actual); | ||
}); | ||
}); | ||
}); |
@@ -149,1 +149,11 @@ // Type definitions for core/util | ||
export function memoize(fn: Function): Function; | ||
/** | ||
* Maps over the `children` , discarding any `null` children before and after calling the callback. | ||
* | ||
* A replacement for `React.Children.map` . | ||
*/ | ||
export function mapAndFilterChildren( | ||
children: any, | ||
callback: Function, | ||
filter?: Function | ||
): any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
}); | ||
exports.perfNow = exports.mergeClassNameMaps = exports.memoize = exports.isRenderable = exports.extractAriaProps = exports.coerceFunction = exports.coerceArray = exports.clamp = exports.cap = void 0; | ||
exports.mapAndFilterChildren = exports.perfNow = exports.mergeClassNameMaps = exports.memoize = exports.isRenderable = exports.extractAriaProps = exports.coerceFunction = exports.coerceArray = exports.clamp = exports.cap = void 0; | ||
@@ -21,2 +21,4 @@ var _always = _interopRequireDefault(require("ramda/src/always")); | ||
var _react = _interopRequireDefault(require("react")); | ||
var ReactIs = _interopRequireWildcard(require("react-is")); | ||
@@ -265,3 +267,44 @@ | ||
}; | ||
/** | ||
* Maps over the `children`, discarding any `null` children before and after calling the callback. | ||
* | ||
* A replacement for `React.Children.map`. | ||
* | ||
* @function | ||
* @param {*} children Children to map over | ||
* @param {Function} callback Function to apply to each child. Will not be called if the child is | ||
* `null`. If `callback` returns `null`, the child will be removed from | ||
* the result. If `null` is returned, the item will not be included in | ||
* the final output, regardless of the filter function. | ||
* @param {Function} [filter] Filter function applied after mapping. | ||
* | ||
* @returns {*} The processed children or the value of `children` if not an array. | ||
* @memberof core/util | ||
* @see https://reactjs.org/docs/react-api.html#reactchildrenmap | ||
* @public | ||
*/ | ||
exports.memoize = memoize; | ||
exports.memoize = memoize; | ||
var mapAndFilterChildren = function mapAndFilterChildren(children, callback, filter) { | ||
var result = _react["default"].Children.map(children, function (child) { | ||
if (child != null) { | ||
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
rest[_key - 1] = arguments[_key]; | ||
} | ||
return callback.apply(void 0, [child].concat(rest)); | ||
} else { | ||
return child; | ||
} | ||
}); | ||
if (result && filter) { | ||
return result.filter(filter); | ||
} else { | ||
return result; | ||
} | ||
}; | ||
exports.mapAndFilterChildren = mapAndFilterChildren; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
227309
5772