-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding a custom type, typeMap is frozen #15
Comments
Modifying However, // custom-typechecker.js
const typechecker = require('typechecker')
const customTypeChecker = Object.assign({}, typechecker)
const customTypeMap = {
array: typechecker.isArray,
boolean: typechecker.isBoolean,
date: typechecker.isDate,
error: typechecker.isError,
class: typechecker.isClass,
function: typechecker.isFunction,
null: typechecker.isNull,
number: typechecker.isNumber,
regexp: typechecker.isRegExp,
string: typechecker.isString,
'undefined': typechecker.isUndefined,
buffer: Buffer.isBuffer, // must be before isObject, as otherwise isObject will catch it
map: typechecker.isMap,
weakmap: typechecker.isWeakMap,
object: typechecker.isObject
}
customTypeChecker.getType = function customGetType (value, _typeMap) {
return typechecker.getType(value, _typeMap || customTypeMap)
}
module.exports = customTypeChecker // app.js
var result = require('./custom-typechecker.js').getType(Buffer.from(''))
console.log(result) // buffer |
That didn't work as written. customTypeMap is neither exported nor passed into the new getType. I tried a few variations on this to no avail. Did you mean this?? I tried that and that still fails. Got time to get this working for yourself first? I'll write a pr for the readme once it's working.
|
Update previous example, should work. |
@dkebler okay, my updated code should work, but there is a bug in typechecker, so I'll update the code |
@dkebler okay, v4.4.1 is fixed, which makes the above work |
the customTypeMap definition could also be done like so: // Add our custom types
const customTypeMap = {
buffer: Buffer.isBuffer
}
// Add original types
Object.keys(typechecker.typeMap).forEach(function (key) {
customTypeMap[key] = customTypeMap[key] || typechecker.typeMap[key]
}) |
@balupton thanks! Now I can all add my classes as types :-). I definitely preferred merging the maps (cleaner) but your object.keys statement above wasn't doing it. So here is the final working module. I'll do a PR for the readme and add this with some explanation. //mytypechecker.js
const typechecker = require('typechecker')
const customTypeChecker = Object.assign({}, typechecker)
// add custom types, a key and corresponding function for each
customTypeChecker.isBuffer = function isBuffer(obj) {
return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
}
// Create a custom type map and make an key/property for each new type
// the key/property used will be the string returned by getType.
let customTypeMap = {
buffer: customTypeChecker.isBuffer
}
// merge base typeMap with customTypeMap
customTypeMap = Object.assign(customTypeMap, customTypeChecker.typeMap)
//overwrite getType to use customTypeMap if available
customTypeChecker.getType = function customGetType(value, _typeMap) {
return typechecker.getType(value, _typeMap || customTypeMap)
}
module.exports = customTypeChecker |
Glad it works :-)
Whoops fixed. |
Little help please, I'd like to do this.
mytypechecker.js
that might work but can't try it because the typeMap object is frozen.
Not sure why this was done (save fools from themselves?)
So how can I add my custom types to the library so I can use
getTypes
without having to pass it an additional map every timei.e. don't want to do this every time.
expect(u.getType(Buffer.from('this is a test'), { buffer: u.isBuffer })).to.equal('buffer')
if
typeMap
needs to be frozen why bother to export it? How about not and providing a registration function to register additional custom types for use withgetType
?Thx.
The text was updated successfully, but these errors were encountered: