@enact/core
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,2 +5,8 @@ # Change Log | ||
## [1.2.0] - 2017-05-17 | ||
### Added | ||
- `core/handle.oneOf` to support branching event handlers | ||
## [1.1.0] - 2017-04-21 | ||
@@ -7,0 +13,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.stopImmediate = exports.stop = exports.preventDefault = exports.log = exports.handle = exports.forProp = exports.forKeyCode = exports.forKey = exports.forEventProp = exports.forward = exports.callOnEvent = undefined; | ||
exports.stopImmediate = exports.stop = exports.preventDefault = exports.oneOf = exports.log = exports.handle = exports.forProp = exports.forKeyCode = exports.forKey = exports.forEventProp = exports.forward = exports.callOnEvent = undefined; | ||
@@ -21,2 +21,6 @@ var _allPass = require('ramda/src/allPass'); | ||
var _cond = require('ramda/src/cond'); | ||
var _cond2 = _interopRequireDefault(_cond); | ||
var _curry = require('ramda/src/curry'); | ||
@@ -52,2 +56,5 @@ | ||
// do not impede the event flow | ||
var makeSafeHandler = (0, _ifElse2.default)((0, _is2.default)(Function), _identity2.default, (0, _always2.default)(_T2.default)); | ||
// Accepts an array of handlers, sanitizes them, and returns a handler function | ||
/** | ||
@@ -109,5 +116,2 @@ * `core/handle` provides a set of utilities to support handling events for `kind()`s and | ||
var makeSafeHandler = (0, _ifElse2.default)((0, _is2.default)(Function), _identity2.default, (0, _always2.default)(_T2.default)); | ||
// Accepts an array of handlers, sanitizes them, and returns a handler function | ||
var makeHandler = (0, _compose2.default)(_allPass2.default, (0, _map2.default)(makeSafeHandler)); | ||
@@ -148,2 +152,27 @@ | ||
/** | ||
* Calls the first handler whose condition passes. Each branch must be passed as an array with the | ||
* first element being the condition function and the second being the handler function. The same | ||
* arguments are passed to both the condition function and the handler function. The value returned | ||
* from the handler is returned. | ||
* | ||
* ``` | ||
* const handler = oneOf( | ||
* [forKey('enter'), handleEnter], | ||
* [forKey('left'), handleLeft], | ||
* [forKey('right'), handleRight] | ||
* ); | ||
* | ||
* @param {...Function} handlers List of handlers to process the event | ||
* @returns {Function} A function that accepts an event which is dispatched to each of the | ||
* conditions and, if it passes, onto the provided handler. | ||
*/ | ||
var oneOf = handle.oneOf = function () { | ||
for (var _len2 = arguments.length, handlers = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
handlers[_key2] = arguments[_key2]; | ||
} | ||
return (0, _cond2.default)(handlers); | ||
}; | ||
/** | ||
* Calls a named function on the event and returns `true` | ||
@@ -378,4 +407,4 @@ * | ||
var log = handle.log = (0, _curry2.default)(function (message, ev) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | ||
args[_key2 - 2] = arguments[_key2]; | ||
for (var _len3 = arguments.length, args = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { | ||
args[_key3 - 2] = arguments[_key3]; | ||
} | ||
@@ -402,4 +431,5 @@ | ||
exports.log = log; | ||
exports.oneOf = oneOf; | ||
exports.preventDefault = preventDefault; | ||
exports.stop = stop; | ||
exports.stopImmediate = stopImmediate; |
@@ -216,2 +216,81 @@ 'use strict'; | ||
}); | ||
describe('#oneOf', function () { | ||
it('should call each handler until one passes', function () { | ||
var handler = _sinon2.default.spy(returnsTrue); | ||
var h1 = [returnsFalse, handler]; | ||
var h2 = [returnsTrue, handler]; | ||
var callback = (0, _handle.oneOf)(h1, h1, h2); | ||
callback(); | ||
var expected = 1; | ||
var actual = handler.callCount; | ||
expect(actual).to.equal(expected); | ||
}); | ||
it('should stop if the first handler passes', function () { | ||
var handler = _sinon2.default.spy(returnsTrue); | ||
var callback = (0, _handle.oneOf)([returnsTrue, handler], [returnsTrue, handler], [returnsTrue, handler]); | ||
callback(); | ||
var expected = 1; | ||
var actual = handler.callCount; | ||
expect(actual).to.equal(expected); | ||
}); | ||
it('should pass args to condition', function () { | ||
var handler = _sinon2.default.spy(returnsTrue); | ||
var callback = (0, _handle.oneOf)([handler, returnsTrue]); | ||
var ev = { value: 1 }; | ||
callback(ev); | ||
var expected = ev; | ||
var actual = handler.firstCall.args[0]; | ||
expect(actual).to.equal(expected); | ||
}); | ||
it('should pass args to handlers', function () { | ||
var handler = _sinon2.default.spy(returnsTrue); | ||
var callback = (0, _handle.oneOf)([returnsTrue, handler]); | ||
var ev = { value: 1 }; | ||
callback(ev); | ||
var expected = ev; | ||
var actual = handler.firstCall.args[0]; | ||
expect(actual).to.equal(expected); | ||
}); | ||
it('should return the value from the passed condition branch', function () { | ||
var handler = _sinon2.default.spy(function () { | ||
return 'ok'; | ||
}); | ||
var callback = (0, _handle.oneOf)([returnsTrue, handler]); | ||
var expected = callback(); | ||
var actual = 'ok'; | ||
expect(actual).to.equal(expected); | ||
}); | ||
it('should support bound handlers', function () { | ||
var componentInstance = { | ||
context: { | ||
value: 1 | ||
} | ||
}; | ||
var handler = _sinon2.default.spy(); | ||
var h = _handle.handle.bind(componentInstance); | ||
var callback = (0, _handle.oneOf)([returnsTrue, h(handler)]); | ||
callback(); | ||
var expected = 1; | ||
var actual = handler.firstCall.args[2].value; | ||
expect(actual).to.equal(expected); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@enact/core", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.", | ||
@@ -33,7 +33,6 @@ "main": "index.js", | ||
"prop-types": "~15.5.0", | ||
"ramda": "~0.22.1", | ||
"ramda": "~0.23.0", | ||
"react": "~15.5.0", | ||
"react-addons-update": "~15.5.0", | ||
"react-dom": "~15.5.0" | ||
} | ||
} |
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
102101
5
2806
+ Addedramda@0.23.0(transitive)
- Removedreact-addons-update@~15.5.0
- Removedramda@0.22.1(transitive)
- Removedreact-addons-update@15.5.2(transitive)
Updatedramda@~0.23.0