@carbon/ibm-security
Version:
Carbon for Cloud & Cognitive IBM Security UI components
82 lines (76 loc) • 3.56 kB
JavaScript
/**
* @file Deprecation warning.
* @copyright IBM Security 2019 - 2021
*/
import isDevelopment from '../env';
/**
* Filters, compares, and logs the appropriate properties with a description.
* @param {string} description The description to use when logging.
* @param {string} type The property type to log.
* @param {Array.<string>} componentKeys The component keys to filter.
* @param {Function} filterCondition The condition to filter the component keys.
* @returns {string} The description and appropriate properties.
*/
var logDeviations = function logDeviations(description, type, componentKeys, filterCondition) {
return console.log("".concat(description, " ").concat(type), componentKeys.filter(filterCondition));
};
/**
* Helper for comparing and logging deviations between multiple components.
* @param {string} carbonComponent The Carbon component to compare.
* @param {string} securityComponent The Security-specific component to compare.
*/
var compareDeviations = function compareDeviations(carbonComponent, securityComponent) {
['defaultProps', 'propTypes'].forEach(function (type) {
var carbonComponentKeys = Object.keys(carbonComponent[type]);
var securityComponentKeys = Object.keys(securityComponent[type]);
logDeviations('Common', type, carbonComponentKeys, function (key) {
return securityComponentKeys.indexOf(key) !== -1;
});
logDeviations('Carbon', type, carbonComponentKeys, function (key) {
return securityComponentKeys.indexOf(key) === -1;
});
logDeviations('Security', type, securityComponentKeys, function (key) {
return carbonComponentKeys.indexOf(key) === -1;
});
});
};
/**
* Returns a standardized deprecation warning for development.
* @param {string} warning - The warning value supplied.
* @returns {console.trace} A stack trace containing the created deprecation details.
*/
function deprecationWarning(warning) {
if (isDevelopment()) {
try {
throw new Error("Warning: ".concat(warning, " and will be removed in an upcoming major release"));
} catch (error) {
console.warn(error);
}
}
}
/**
* Returns a standardized deprecation warning.
* @param {string} actual - The actual value supplied.
* @param {string} expected - The expected value to be supplied.
* @returns {console.trace} A stack trace containing the created deprecation details.
*/
var deprecate = function deprecate(actual, expected) {
return deprecationWarning("'".concat(actual, "' has been deprecated in favor of '").concat(expected, "'"));
};
/**
* Returns a standardized component deprecation warning.
* @param {Record<string, string>} deprecationWarning - An object containing properties that construct the deprecation warning.
* @param {string} deprecationWarning.actual - The actual value supplied.
* @param {string} deprecationWarning.componentName - The component name it was supplied to.
* @param {string} deprecationWarning.description - The description of the deprecation warning.
* @param {string} deprecationWarning.expected - The expected value to be supplied.
* @returns {console.trace} A stack trace containing the created deprecation details.
*/
export default (function (_ref) {
var actual = _ref.actual,
componentName = _ref.componentName,
description = _ref.description,
expected = _ref.expected;
return deprecationWarning("Deprecated ".concat(description, " '").concat(actual, "' supplied to '").concat(componentName, "', expected '").concat(expected, "'"));
});
export { compareDeviations, deprecate };