Skip to content

Instantly share code, notes, and snippets.

@reissbaker
Created January 12, 2012 19:04
Show Gist options
  • Save reissbaker/1602419 to your computer and use it in GitHub Desktop.
Save reissbaker/1602419 to your computer and use it in GitHub Desktop.
key selection, remapping
var selectKeys = function(hash, keys) {
var output, prop, index;
output = {};
if(!keys) return {};
if(keys instanceof Array) {
for(index = 0; index < keys.length; index++) {
output[keys[index]] = hash[keys[index]];
}
return output;
}
for(prop in keys) {
if(keys.hasOwnProperty(prop)) {
if(typeof keys[prop] === 'boolean') {
if(keys[prop]) output[prop] = hash[prop];
// if false, don't copy the property
} else {
output[keys[prop]] = hash[prop];
}
}
}
return output;
};
@reissbaker
Copy link
Author

Call like this:

var selected = selectKeys(optionsHash, ['key1', 'key2']);

or,

var remappedSelected = selectKeys(options, {
  flying_metal_bird: 'airplane'
});

The keys are what the keys in the input value are; the values are what they should be remapped to.

For convenience in the remapping version, if the key in the output should be the same as the key in the input, you can do:

selectKeys(options, {
  keyName: true,
  key_2: key2
});

Passing in false skips the key entirely, although a more concise way to do that would be to not include the key name at all.

@clizzin
Copy link

clizzin commented Jan 12, 2012

Can you provide an example of input and output for this function? I don't really understand what it's doing / used for.

@reissbaker
Copy link
Author

Sure thing.

var options = {
  some_value: 6,
  flying_metal_bird: false
};
var parsedOptions = selectKeys(options, {
  some_value: 'someValue',
  flying_metal_bird: 'airplane'
});

/* 
parsedOptions is now: {
  someValue: 6,
  airplane: false
}
*/

or, more to the point of why I wrote it:

var paramsFromRuby = <%= stuff_in_a_format_that_some_JS_code_hates%>;
var options = selectKeys(paramsFromRuby, {
  feet_in_the_air: 'jumping'
});

@clizzin
Copy link

clizzin commented Jan 12, 2012

Oh I see, it's renaming the keys. Maybe this function would be better named "rename-keys"? I got thrown by the use of "parse" and "options"; "parse options" sounds to me like parsing command-line flags.

@reissbaker
Copy link
Author

Yep. Good point. Renaming it to selectKeys, made it select (and not remap) keys if given an array, and if given an object can remap them.

@ssorallen
Copy link

Line 4 you can use if (!keys) instead of checking for undefined. That will catch null too.

@reissbaker
Copy link
Author

Nice. Changed it. That's a win for compression, too, since typestrings can't be compressed but variable names can.

@ssorallen
Copy link

Line 1 has a syntax error. You can drop the selectKeys:.

var selectKeys = selectKeys: function(hash, keys) {

@reissbaker
Copy link
Author

That's what I get for copying+pasting out of an object literal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment