Skip to content

Instantly share code, notes, and snippets.

@nasser
Created July 17, 2013 18:49
Show Gist options
  • Save nasser/6023321 to your computer and use it in GitHub Desktop.
Save nasser/6023321 to your computer and use it in GitHub Desktop.
// Parse string +path+ into nested objects within +object+ and assign
// the deepest key to +value+, without overwriting existing keys.
// Examples follow.
function parse(path, value, object) { }
parse("foo", 42, {}); // => { foo: 42 }
parse("foo.bar", 42, {}); // => { foo: { bar: 42 } }
parse("foo.bar.baz", 42, {}); // => { foo: { bar: { baz: 42 } } }
parse("foo", 42, { challenge: "accepted" }); // => { foo: 42, challenge: 'accepted' }
parse("foo.bar", 42, { challenge: "accepted" }); // => { foo: { bar: 42 }, challenge: 'accepted' }
parse("foo", 42, { foo: "accepted" }); // => { foo: 'accepted' }
parse("foo.bar", 42, { foo: { baz: "qux" } }); // => { foo: { baz: 'qux', bar: 42 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment