Skip to content

Instantly share code, notes, and snippets.

@furf
Forked from 140bytes/LICENSE.txt
Created September 20, 2011 22:23
Show Gist options
  • Save furf/1230612 to your computer and use it in GitHub Desktop.
Save furf/1230612 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

// If you've done this:
//
// if (typeof data.deeply !== 'undefined' &&
// typeof data.deeply.nested !== 'undefined' &&
// typeof data.deeply.nested.value !== 'undefined') { /* ... */ }
//
// You need this:
function (
object, // JavaScript object w/ iterable properties
path, // dot|bracket-delimited path to property
// e.g. 'deeply.nested.array[1].value'
index // iteration index
) {
path = path
.replace(
/\[(["']?)([^\1]+?)\1?\]/g, // convert bracket notation
'.$2') // to dot notation
.replace(/^\./, '') // remove leading dot if the first property
// uses bracket notation
.split('.'); // split dot-delimited notation to array
for ( // iterate list of nested properties
index = 0; // from first property
path[index] && // to last specified property
(object = object[ // setting current object to value of
path[index++] // current property (and bumping index)
]) != []._; // while value is defined
);
return object; // return defined value or undefined
}
function(b,c,a){c=c.replace(/\[(["']?)([^\1]+?)\1?\]/g,".$2").replace(/^\./,"").split(".");for(a=0;c[a]&&(b=b[c[a++]])!=[]._;);return b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Dave Furfero <http://furf.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "getDeepValue",
"description": "Safely traverse an object for nested (and possibly undefined) property values",
"keywords": [
"object",
"nested",
"property",
"traversal",
"undefined"
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<title>getDeepValue</title>
<dl>
<dt>dots</dt>
<dd>
<div>Expected value: <b>foo</b></div>
<div>Actual value: <b id="dots"></b></div>
</dd>
<dt>brackets (numeric)</dt>
<dd>
<div>Expected value: <b>bar</b></div>
<div>Actual value: <b id="brackets"></b></div>
</dd>
<dt>brackets (quoted)</dt>
<dd>
<div>Expected value: <b>☺</b></div>
<div>Actual value: <b id="quoted"></b></div>
</dd>
<dt>undefined</dt>
<dd>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="undefined"></b></div>
</dd>
<dt>non-iterable object</dt>
<dd>
<div>Expected value: <b>error</b></div>
<div>Actual value: <b id="null"></b></div>
</dd>
</dl>
<script>
var getDeepValue = function(b,c,a){c=c.replace(/\[(["']?)([^\1]+?)\1?\]/g,".$2").replace(/^\./,"").split(".");for(a=0;c[a]&&(b=b[c[a++]])!=[]._;);return b};
var data = {
deeply: {
nested: {
value: 'foo'
}
},
'♥ ♫ ★': '☺',
array: ['foo',['bar','baz']]
};
// Test dots
document.getElementById('dots').innerHTML = getDeepValue(data, 'deeply.nested.value');
// Test numeric brackets
document.getElementById('brackets').innerHTML = getDeepValue(data, 'array[1][0]');
// Test quoted brackets
document.getElementById('quoted').innerHTML = getDeepValue(data, '♥ ♫ ★');
// Test undefined
document.getElementById('undefined').innerHTML = getDeepValue(data, 'deeply.held.belief');
// Test null
try {
document.getElementById('null').innerHTML = getDeepValue(null, 'deeply.nested.value');
} catch (e) {
document.getElementById('null').innerHTML = 'error';
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment