-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setFieldValue to accept a dotted path ...
e.g. you can do setFieldValue('replicator.1.foo', 'bar') to update the foo field on the second replicator set to bar This uses the eventual underscore 'set' method from jashkenas/underscore#2961. Once it's merged we can call the method directly and not need to include these files.
- Loading branch information
1 parent
5ff2732
commit 20a4d90
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import isObject from 'underscore/modules/isObject.js'; | ||
import toPath from 'underscore/modules/_toPath.js'; | ||
import contains from 'underscore/modules/contains.js'; | ||
|
||
|
||
var arrayIndex = /^\d+$/; | ||
|
||
// Internal function of `set`. | ||
function deepSet(obj, path, value) { | ||
var key = path[0]; | ||
|
||
if (path.length === 1) { | ||
obj[key] = value; | ||
return; | ||
} | ||
|
||
if (!isObject(obj[key])) { | ||
var nextKey = path[1]; | ||
obj[key] = arrayIndex.test(nextKey) ? [] : {}; | ||
} | ||
|
||
return deepSet(obj[key], path.slice(1), value); | ||
} | ||
|
||
// Set the value on `path` of `object`. | ||
// If any property in `path` does not exist it will be created. | ||
// Returns mutated object (`obj`). | ||
export default function set(obj, path, value) { | ||
path = toPath(path); | ||
|
||
if (!isObject(obj) || !path.length) return obj; | ||
if (contains(path, '__proto__')) throw new Error('Prototype assignment attempted'); | ||
|
||
deepSet(obj, path, value); | ||
|
||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20a4d90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasonvarga If you are very sure, you can override _.toPath in order to enable dotted paths everywhere, no wrappers required.
20a4d90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not override Underscore functions. But maybe I'm misunderstanding what you're suggesting.
20a4d90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it's up to you whether you want to do it, I don't necessarily recommend it.
_.toPath
is one of the configurable parts of Underscore, similar to_.iteratee
,_.templateSettings
and_.partial.placeholder
. Apart from providing consistency, it exists so that users can adjust the way Underscore interprets property paths to their needs.Your hesitation is justified, because it will also change the way Underscore works for other dependents. I'm just pointing out that the option is there, and if you want to have dotted string paths everywhere, overriding
_.toPath
is definitely the most efficient way to achieve that.