Skip to content

Commit

Permalink
Make deepPluck and deepHas work with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
beaugunderson committed Dec 27, 2014
1 parent 1051ad8 commit fd2ac65
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gulp.task("test-browser", ["minify"], function(callback){
}, callback);
});

gulp.task("test-node", function(){
gulp.task("test-node", ["minify"], function(){
return gulp.src('test/*')
.pipe(jasmine());
});
Expand All @@ -22,4 +22,4 @@ gulp.task("minify", function(){
.pipe(uglify())
.pipe(rename("lodash-deep.min.js"))
.pipe(gulp.dest("./"));
});
});
7 changes: 4 additions & 3 deletions lodash-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
var properties = getProperties(propertyPath);
for(var i = 0; i < properties.length; i++){
var property = properties[i];
if(_.isObject(collection) && property in collection){
if(_.has(collection, property) ||
_.isObject(collection) && property in collection){
collection = collection[property];
}
else{
Expand All @@ -50,7 +51,7 @@
var properties = getProperties(propertyPath);
for(var i = 0; i < properties.length; i++){
var property = properties[i];
if(_.isObject(collection) && collection.hasOwnProperty(property)){
if(_.has(collection, property)){
collection = collection[property];
}
else{
Expand Down Expand Up @@ -268,4 +269,4 @@
}

return mixins;
}));
}));
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.

14 changes: 12 additions & 2 deletions test/lodashDeepSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals: require, beforeEach, describe, it, module, inject, expect */
/* globals: require, beforeEach, describe, it, module, inject, expect, spyOn */
describe('lodash-deep mixins', function(){
'use strict';
var isNode, _, object, array, objectWithFunc, thisArg;
Expand Down Expand Up @@ -115,12 +115,15 @@ describe('lodash-deep mixins', function(){
});

describe('deepHas(collection, propertyPath)', function(){
it('should return whether a value exist in an object tree, as own value, for the specified property path.', function(){
it('should return whether a value exists in an object tree, as own value, for the specified property path.', function(){
expect(_.deepHas(object, 'level1')).toBe(true);
expect(_.deepHas(object, 'level1.value')).toBe(true);
expect(_.deepHas(object, 'level1.level2')).toBe(true);
expect(_.deepHas(object, 'level1.level2.value')).toBe(true);

expect(_.deepHas(object, 'level1.value.length')).toBe(true);
expect(_.deepHas(object, 'level1.level2.value.length')).toBe(true);

expect(_.deepHas(object, 'level1.level2.level3.0')).toBe(false);
expect(_.deepHas(object, 'level1.level2.level3.0.value')).toBe(false);
expect(_.deepHas(object, ['level1', 'level2', 'level3', 0, 'value'])).toBe(false);
Expand Down Expand Up @@ -190,6 +193,13 @@ describe('lodash-deep mixins', function(){
expect(_.deepPluck(array, 'level1.level2.level3.0.value')).toEqual(['value 3_1', 'value 3_2', 'value 3_3', undefined, undefined]);
expect(_.deepPluck(array, ['level1', 'level2', 'level3', 0, 'value'])).toEqual(['value 3_1', 'value 3_2', 'value 3_3', undefined, undefined]);
});

it('should do a deep pluck on non-object properties', function(){
expect(_.deepPluck(array, 'level1.value.length')).toEqual([9, 9, 9, undefined, undefined]);
expect(_.deepPluck(array, 'level1.level2.value.length')).toEqual([9, 9, 9, undefined, undefined]);
expect(_.deepPluck(array, 'level1.level2.level3.0.value.length')).toEqual([9, 9, 9, undefined, undefined]);
expect(_.deepPluck(array, ['level1', 'level2', 'level3', 0, 'value', 'length'])).toEqual([9, 9, 9, undefined, undefined]);
});
});

describe('deepCall(collection, propertyPath, thisArg, *arg)', function(){
Expand Down

0 comments on commit fd2ac65

Please sign in to comment.