Message from JavaScript discussions
August 2017
— Huh. I need help!
I feed a RegExp
instance into my diff function, and it gets type checked as a traversable object, but then when it gets added to the tuple it somehow isn't an object any more
— Since I rely on a type check prior to adding an object to the traversal stack, there is none at the top of it and it chokes
— How I traverse:
// Node has not been seen before, so traverse it
var nextTuple = {};
// Travese the Tuple's properties
for (var unit in state.tuple) {
if (unit === "search" || unit === "subject" || state.accessor in state.tuple[unit]) {
nextTuple[unit] = state.tuple[unit][state.accessor];
}
}
— How I type check the RegExp
which confirms it is an object:
/**
* isContainer - Returns `true` if `input` is an Object or Array, otherwise returns `false`.
* param {any} input
* returns {Boolean}
*/
function isContainer(input) {
return (input !== null && (isObject(input) || Array.isArray(input)));
}
—
function isObject(input) {
return (input !== null && typeof (input) === "object" && !Array.isArray(input));
}
— Yes I know those two little functions check the same thing twice, they are on the todo list
— I can see cross sections of traversal state and see this right before I get errors about type, as some other functions require objects and throw TypeErrors.
— The upper props are from the previous iteration and get changed later execution, so ignore those
— But the bottom tuple
prop shows the result of traversal
— It chokes on some polymorphic length checking function that fails to ID the tuple elements as objects
—
function getContainerLength(input) {
if (Array.isArray(input)) {
return input.length;
}
if (input !== null && typeof input === "object") {
return Object.keys(input).length;
}
throw new TypeError("The given parameter must be an Object or Array");
}