Open vitaly-t opened 8 years ago
Thanks for this. As it happens we’re currently discussing this in #19
Very well! :) I would suggest further, a generic method:
camelize(value, [options]);
where value can be a string or an object (to camelize keys). options
, in case of keys, should be able to accept flag nested
(false
by default) to provide nested keys processing. Maybe even support key copy
there, so a copy is created only when set.
Plus whatever other options you may have for processing.
Also, consider this, you have a variable that contains an array of strings and objects - all need to be camelized, is when it gets more interesting.
As an update, In my usage case I came to conclusion that such a functionality would not help me.
It really depends on the object's heterogeneous nature. In my case I use the library to process data rows that arrive from a database. That means I am dealing with lots of data rows, each has the same set of columns. Obviously, you would want to camelize all columns only once, for a single row, and then re-apply those to all the rows. In such case doing camelize for every row would be unacceptable, performance-wise.
And I would not recommend adding support for heterogeneous object collections, it would be too complicated.
As of now, I'm very happy with the basic camelize method. Here's how I am using it:
function camelizeColumns(data) {
var template = data[0];
for (var prop in template) {
var camel = humps.camelize(prop);
if (!(camel in template)) {
for (var i = 0; i < data.length; i++) {
var d = data[i];
d[camel] = d[prop];
delete d[prop];
}
}
}
}
As you can see from implementation, I camelize only a single row, and then re-use it, which is critical when processing large collections of same-type objects.
It would be a very good idea to have methods
camelizeKeys
andpascalizeKeys
perform the operation on the original object that's passed, without creating a copy. As an option that is.