@rybr/lenses
Version:
a simple and light package meant to ease data traversal, manipulation, mutation, and transformation
67 lines (61 loc) • 1.76 kB
JavaScript
const getConstructorName = input =>
input == null ? `${input}` : input.constructor ? input.constructor.name : 'Unknown'
const getOperationType = operation =>
TYPES.STRING.is(operation) && operation.trim().length > 0
? TYPES.STRING
: TYPES.NUMBER.is(operation) && operation >= 0
? TYPES.NUMBER
: TYPES.FUNCTION.is(operation)
? TYPES.FUNCTION
: TYPES.INVALID
const isType = (input, type, typeofName, constructor) =>
input === type ||
input instanceof constructor ||
getConstructorName(input) === constructor.name ||
typeof input === typeofName
const TYPES = {
STRING: {
is: input => isType(input, TYPES.STRING, 'string', String),
toString: () => 'STRING',
},
FUNCTION: {
is: input => isType(input, TYPES.FUNCTION, 'function', Function),
toString: () => 'FUNCTION',
},
NUMBER: {
is: input => isType(input, TYPES.NUMBER, 'number', Number),
toString: () => 'NUMBER',
},
OBJECT: {
is: input => isType(input, TYPES.OBJECT, 'object', Object),
toString: () => 'OBJECT',
},
ARRAY: {
is: input => input === TYPES.ARRAY || Array.isArray(input),
toString: () => 'ARRAY',
},
//TODO: figure out how to handle this correctly
HTML_ELEMENT: {
is: input => isType(input, TYPES.HTML_ELEMENT, 'object', HTMLElement),
toString: () => 'HTML_ELEMENT',
},
INVALID: {
is: input => input === TYPES.INVALID,
toString: () => 'INVALID',
},
}
//for browser static import
const loadGlobal = (globals = {}) => {
if (typeof window !== 'undefined') {
window.L = {
...window.L,
...globals,
}
}
}
module.exports = {
getConstructorName,
getOperationType,
TYPES,
loadGlobal,
}