-
Notifications
You must be signed in to change notification settings - Fork 100
added File object conditional check to _processKeys #19
base: master
Are you sure you want to change the base?
Conversation
👍 |
@domchristie any comments on getting this merged? |
@bitwurx @lightswitch05 sorry for the delay in responding. It has been a while since I looked at this part of the code, and I’m trying to work out what its real purpose is! I think what we want to do is
and
What do you reckon? |
It would clean up the code, but we often use humps on custom objects (decamelize our JavaScript models for a snake_case API). I think |
Interesting. Well the issue is that if we check for |
You have some great points. After looking into it more with @bitwurx , this pull request (and the other type checking already in the library) is really a work around for the behavior of returning an entirely new object (line 30). I think having File and Date objects would be fine as long as they were preserved instead of turned into a generic Object. This would preserve backwards compatibility with the existing type checks (Date, Regex) - and allow support for all the other JavaScript types. |
Yeh, the reason for creating a new object was to effectively clone the given object, though I suppose you’re saying that this may not be necessary? (again, apologies: I wrote this a while ago, and so cannot be sure if there was another reason for this!). We may run into issues if we alter the given object, particularly if it’s of a native type or if the property is read-only. In terms of preserving some objects, would you be able to show me what you have in mind (e.g. with code/pseudocode) Thanks! |
I think this is the current pseudo-code for
Here is what I have in mind for preserving the object type. It wouldn't work as-is, but will hopefully explain my logic: if(_isArray(obj)) {
for(l=obj.length; i<l; i++) {
// no need to duplicate the array
obj[i] = (_processKeys(convert, obj[i], options));
}
}
else { // will still need to do some primitive type checking
for(var key in obj) {
try {
var convertedKey = convert(key, options);
obj[convertedKey] = _processKeys(convert, obj[key], options);
delete obj[key];
} catch(err) {
// don't care - must a special object ignore and continue
}
}
} Here is a quick mockup that uses Date, RegExp, custom objects, and plain objects: https://repl.it/BZSY/2 |
Excellent! I’m slightly concerned about the mutation, but happy to go for this direction in v2 👍 |
I've been thinking about this more:
I think the best way forward is to define a strategy for humps to use:
Stringify would be an extremely safe method to normalize the objects before trying to modify them - but it also wouldn't preserve types or preserve currently functionality. Having a way to define hump's strategy would keep everyone happy and current functionality could be retained by default. |
@lightswitch05 Thanks for putting some though into this, and apologies again for getting back sooner.
Do you mean as a configuration? I’m in two minds about stringifying and parsing. I like it in that it’s an elegant way to normalise input (especially native types), but as with mutation, it feels a bit destructive. |
Yes, as a configuration. Each conversation method has its own strengths and weaknesses. The only way to support all object types is to support different conversion stratagies. It would be great if you could think of a way to auto detect which strategy is going to work best depending on the object type- but I don't know how that would work. |
How about this: var _processKeys = function(convert, obj, options) {
var output,
i = 0,
l = 0;
if (_isArray(obj)) {
output = options.strategy === 'mutate' ? obj : [];
for (l = obj.length; i < l; i++) {
output[i] = _processKeys(convert, obj[i], options);
}
return output;
}
if (_isObject(obj)) {
output = options.strategy === 'mutate' ? obj : {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var convertedKey = convert(key, options)
output[convertedKey] = _processKeys(convert, obj[key], options);
if (options.strategy === 'mutate') {
delete obj[key]
}
}
}
return output;
}
return obj;
}
var _isObject = function(obj) {
return toString.call(obj) === '[object Object]';
}; I think this addresses everyone's concerns and is backward compatible |
To maximize performance mutation should be implemented in a separate function. Preferably it should have interface like so: edit: I created xcase https://github.com/encharm/xcase which has |
Hi guys, unfortunately the operation with the files still does not work. Maybe now it makes sense to add support for files, and then think about what to do with all the other JavaScript types |
No description provided.