inferno-shared
Version:
Helpers functions for Inferno
70 lines (68 loc) • 1.78 kB
JavaScript
const ERROR_MSG = 'a runtime error occured! Use Inferno in development environment to find the error.';
const isArray = Array.isArray;
function isStringOrNumber(o) {
const type = typeof o;
return type === 'string' || type === 'number';
}
function isNullOrUndef(o) {
return o === void 0 || o === null;
}
function isInvalid(o) {
return o === null || o === false || o === true || o === void 0;
}
function isFunction(o) {
return typeof o === 'function';
}
function isString(o) {
return typeof o === 'string';
}
function isNumber(o) {
return typeof o === 'number';
}
function isNull(o) {
return o === null;
}
function isUndefined(o) {
return o === void 0;
}
function throwError(message) {
if (!message) {
message = ERROR_MSG;
}
throw new Error(`Inferno Error: ${message}`);
}
function warning(message) {
console.error(message);
}
const KNOWN_STATICS = {
childContextTypes: true,
contextType: true,
contextTypes: true,
defaultProps: true,
displayName: true,
getDefaultProps: true,
getDerivedStateFromError: true,
getDerivedStateFromProps: true,
mixins: true,
propTypes: true,
type: true,
// KNOWN STATICS
name: true,
length: true,
prototype: true,
caller: true,
callee: true,
arguments: true,
arity: true
};
function hoistStaticProperties(targetComponent, sourceComponent) {
// don't hoist over string (html) components
const keys = Object.getOwnPropertyNames(sourceComponent);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (!KNOWN_STATICS[key]) {
targetComponent[key] = sourceComponent[key];
}
}
}
export { ERROR_MSG, hoistStaticProperties, isArray, isFunction, isInvalid, isNull, isNullOrUndef, isNumber, isString, isStringOrNumber, isUndefined, throwError, warning };