2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2016-05-16 11:04:37 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-05-16 11:04:37 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventValidator is designed to validate event types to make it easier to catch
|
|
|
|
* common mistakes. It accepts a map of all of the different types of events
|
|
|
|
* that the emitter can emit. Then, if a user attempts to emit an event that is
|
|
|
|
* not one of those specified types the emitter will throw an error. Also, it
|
|
|
|
* provides a relatively simple matcher so that if it thinks that you likely
|
|
|
|
* mistyped the event name it will suggest what you might have meant to type in
|
|
|
|
* the error message.
|
|
|
|
*/
|
2016-05-16 11:04:37 +00:00
|
|
|
const EventValidator = {
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} emitter - The object responsible for emitting the actual
|
|
|
|
* events
|
|
|
|
* @param {Object} types - The collection of valid types that will be used to
|
|
|
|
* check for errors
|
|
|
|
* @return {Object} A new emitter with event type validation
|
|
|
|
* @example
|
2016-05-16 11:04:37 +00:00
|
|
|
* const types = {someEvent: true, anotherEvent: true};
|
|
|
|
* const emitter = EventValidator.addValidation(emitter, types);
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
addValidation: function(emitter: Object, types: Object) {
|
2016-05-16 11:04:37 +00:00
|
|
|
const eventTypes = Object.keys(types);
|
|
|
|
const emitterWithValidation = Object.create(emitter);
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-03-21 14:49:54 +00:00
|
|
|
Object.assign(emitterWithValidation, {
|
2015-01-30 01:10:49 +00:00
|
|
|
emit: function emit(type, a, b, c, d, e, _) {
|
|
|
|
assertAllowsEventType(type, eventTypes);
|
|
|
|
return emitter.emit.call(this, type, a, b, c, d, e, _);
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return emitterWithValidation;
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function assertAllowsEventType(type, allowedTypes) {
|
|
|
|
if (allowedTypes.indexOf(type) === -1) {
|
|
|
|
throw new TypeError(errorMessageFor(type, allowedTypes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorMessageFor(type, allowedTypes) {
|
2016-05-16 11:04:37 +00:00
|
|
|
let message = 'Unknown event type "' + type + '". ';
|
2015-01-30 01:10:49 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
message += recommendationFor(type, allowedTypes);
|
|
|
|
}
|
|
|
|
message += 'Known event types: ' + allowedTypes.join(', ') + '.';
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow for good error messages
|
|
|
|
if (__DEV__) {
|
2018-05-11 02:06:46 +00:00
|
|
|
var recommendationFor = function(type, allowedTypes) {
|
2016-05-16 11:04:37 +00:00
|
|
|
const closestTypeRecommendation = closestTypeFor(type, allowedTypes);
|
2015-01-30 01:10:49 +00:00
|
|
|
if (isCloseEnough(closestTypeRecommendation, type)) {
|
|
|
|
return 'Did you mean "' + closestTypeRecommendation.type + '"? ';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
var closestTypeFor = function(type, allowedTypes) {
|
2016-05-16 11:04:37 +00:00
|
|
|
const typeRecommendations = allowedTypes.map(
|
2018-05-11 02:06:46 +00:00
|
|
|
typeRecommendationFor.bind(this, type),
|
2015-01-30 01:10:49 +00:00
|
|
|
);
|
|
|
|
return typeRecommendations.sort(recommendationSort)[0];
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
var typeRecommendationFor = function(type, recommendedType) {
|
2015-01-30 01:10:49 +00:00
|
|
|
return {
|
2018-01-30 03:10:33 +00:00
|
|
|
type: recommendedType,
|
2018-05-11 02:06:46 +00:00
|
|
|
distance: damerauLevenshteinDistance(type, recommendedType),
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
var recommendationSort = function(recommendationA, recommendationB) {
|
2015-01-30 01:10:49 +00:00
|
|
|
if (recommendationA.distance < recommendationB.distance) {
|
|
|
|
return -1;
|
|
|
|
} else if (recommendationA.distance > recommendationB.distance) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
var isCloseEnough = function(closestType, actualType) {
|
|
|
|
return closestType.distance / actualType.length < 0.334;
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
var damerauLevenshteinDistance = function(a, b) {
|
2016-05-16 11:04:37 +00:00
|
|
|
let i, j;
|
|
|
|
const d = [];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i <= a.length; i++) {
|
|
|
|
d[i] = [i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j <= b.length; j++) {
|
|
|
|
d[0][j] = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i <= a.length; i++) {
|
|
|
|
for (j = 1; j <= b.length; j++) {
|
2016-05-16 11:04:37 +00:00
|
|
|
const cost = a.charAt(i - 1) === b.charAt(j - 1) ? 0 : 1;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
d[i][j] = Math.min(
|
|
|
|
d[i - 1][j] + 1,
|
|
|
|
d[i][j - 1] + 1,
|
2018-05-11 02:06:46 +00:00
|
|
|
d[i - 1][j - 1] + cost,
|
2015-01-30 01:10:49 +00:00
|
|
|
);
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
if (
|
|
|
|
i > 1 &&
|
|
|
|
j > 1 &&
|
|
|
|
a.charAt(i - 1) === b.charAt(j - 2) &&
|
|
|
|
a.charAt(i - 2) === b.charAt(j - 1)
|
|
|
|
) {
|
2015-01-30 01:10:49 +00:00
|
|
|
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d[a.length][b.length];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EventValidator;
|