Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created February 3, 2013 06:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rauschma/4700689 to your computer and use it in GitHub Desktop.
Save rauschma/4700689 to your computer and use it in GitHub Desktop.
// Proof of concept only!
function isInstance(value, Type) {
if (typeof Type.hasInstance === 'function') {
return Type.hasInstance(value);
} else {
return value instanceof Type;
}
}
isInstance.Primitive = {
hasInstance: function (value) {
if (value === null) {
return true;
}
switch(typeof value) {
case 'undefined':
case 'boolean':
case 'number':
case 'string':
return true;
default:
return false;
}
}
};
isInstance.PrimitiveBoolean = {
hasInstance: function (value) {
return typeof value === 'boolean';
}
};
isInstance.PrimitiveNumber = {
hasInstance: function (value) {
return typeof value === 'number';
}
};
isInstance.PrimitiveString = {
hasInstance: function (value) {
return typeof value === 'string';
}
};
isInstance.ValueType = isInstance.Primitive; // for now
isInstance.ReferenceType = {
hasInstance: function (value) {
switch(value) {
case 'array':
return true;
case 'object':
return value !== null;
default:
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment