Skip to content

Commit

Permalink
added meaningful error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Alger committed Dec 12, 2014
1 parent bba1ebb commit ff6a26f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
40 changes: 29 additions & 11 deletions lodash-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
* @returns {Object}
*/
deepMapValues: function(object, callback, propertyPath){
var properties = getProperties(propertyPath)
var properties = getProperties(propertyPath);
if(_.isObject(object) && !_.isDate(object) && !_.isRegExp(object)){
return _.extend(object, _.mapValues(object, function(value, key){
return _.deepMapValues(value, callback, _.flatten([properties, key]));
Expand Down Expand Up @@ -202,38 +202,56 @@
return [];
}

var escape = false;
var i = 0;
var ch = '';
var len = propertyPath.length;
var path = [];
var step = '';
var len = propertyPath.length;
var ch = '';
var i = 0;
var error = null;
var escape = false;
var control = false;
var brackets = false;

// walk through the path and find backslashes that escape
// periods or other backslashes, and split on unescaped periods
stepper:
// and brackets
for (; i < len; i++) {
ch = propertyPath.charAt(i);
ch = propertyPath[i];
control = (ch === '\\' || ch === '[' || ch === ']' || ch === '.');

if (control && !escape) {
if (brackets && ch !== ']') {
error = 'unexpected "' + ch + '" within brackets';
break;
}

if (!escape) {
switch (ch) {
case '\\':
escape = true;
continue stepper;
break;
case ']':
continue stepper;
brackets = false;
break;
case '[':
brackets = true;
/* falls through */
case '.':
path.push(step);
step = '';
continue stepper;
break;
}

continue;
}

step += ch;
escape = false;
}

if (error) {
throw new SyntaxError(error + ' at character ' + i + ' in property path ' + propertyPath);
}

// capture the final step
path.push(step);
return path;
Expand Down
2 changes: 1 addition & 1 deletion lodash-deep.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff6a26f

Please sign in to comment.