underscore-keypath
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -179,4 +179,43 @@ (function(){ | ||
return null; | ||
}, | ||
sortByKeyPath : function(array, keyPath){ | ||
if(typeof keyPath !== "string"){ | ||
return _.sortBy(array, keyPath); | ||
}else{ | ||
return _.sortBy(array, function(it){ | ||
return _.valueForKeyPath(it, keyPath); | ||
}); | ||
} | ||
}, | ||
groupByKeyPath : function(array, keyPath){ | ||
if(typeof keyPath !== "string"){ | ||
return _.groupBy(array, keyPath); | ||
}else{ | ||
return _.groupBy(array, function(it){ | ||
return _.valueForKeyPath(it, keyPath); | ||
}); | ||
} | ||
}, | ||
indexByKeyPath : function(array, keyPath){ | ||
if(typeof keyPath !== "string"){ | ||
return _.indexBy(array, keyPath); | ||
}else{ | ||
return _.indexBy(array, function(it){ | ||
return _.valueForKeyPath(it, keyPath); | ||
}); | ||
} | ||
}, | ||
countByKeyPath : function(array, keyPath){ | ||
if(typeof keyPath !== "string"){ | ||
return _.countBy(array, keyPath); | ||
}else{ | ||
return _.countBy(array, function(it){ | ||
return _.valueForKeyPath(it, keyPath); | ||
}); | ||
} | ||
} | ||
}; | ||
@@ -183,0 +222,0 @@ |
@@ -1,2 +0,2 @@ | ||
(function(){var r=true;try{module;exports}catch(e){r=false}if(r){_=require("underscore")}else{try{_}catch(e){throw new Error("underscore.js is not found!")}}function t(r){r=r==null?"":String(r);return r.charAt(0).toUpperCase()+r.slice(1)}function n(r,e){if(e==""){return r}var n=r["get"+t(e)];if(n===undefined){n=r["is"+t(e)]}if(n===undefined){n=r[e]}if(typeof n==="function"){n=n.call(r)}return n}function i(r,e,n){var i=r["set"+t(e)];if(i){i.call(r,n)}else{r[e]=n}}function u(r,e){var t=true;var n,i;for(n in e){i=e[n];if(_(r).valueForKeyPath(n)!=i){t=false;break}}return t}var a={valueForKeyPath:function(r,e,t){if(typeof e!="string"){throw new Error("keypath must be a string")}if(r===null||r===undefined){return t}var i=r;var u=e.split(".");var a=u.shift();var f=undefined;while(i!==null&&i!==undefined&&typeof a==="string"){f=i;i=n(i,a);if(typeof i==="function"){i=i.call(f)}a=u.shift()}return i||t},setValueForKeyPath:function(r,e,t){if(typeof e!="string"){throw new Error("keypath must be a string")}var n=e.split(".");var u=n.pop();var a=_(r).valueForKeyPath(n.join("."));if(a!==null&&a!==undefined){i(a,u,t)}},pluckByKeyPath:function(r,e){var t=[];var n,i;for(n in r){i=r[n];t[n]=_(i).valueForKeyPath(e)}return t},whereByKeyPath:function(r,e){"use strict";var t=[];var n,i;var a,f;for(n in r){i=r[n];if(u(i,e)){t.push(i)}}return t},findWhereByKeyPath:function(r,e){"use strict";var t=[];var n,i;var a,f;for(n in r){i=r[n];if(u(i,e)){return i}}return null}};var f={getValueForKeyPath:a.valueForKeyPath};_.mixin(a);_.mixin(f);if(r){module.exports=_}})(); | ||
(function(){var e=true;try{module;exports}catch(r){e=false}if(e){_=require("underscore")}else{try{_}catch(r){throw new Error("underscore.js is not found!")}}function t(e){e=e==null?"":String(e);return e.charAt(0).toUpperCase()+e.slice(1)}function n(e,r){if(r=="@first"){return _.first(e)}else if(r=="@last"){return _.last(e)}else if(r=="@max"){return _.max(e)}else if(r=="@min"){return _.min(e)}else if(r=="@size"){return _.size(e)}}function i(e,r){if(r==""){return e}else if(r.indexOf("@")===0){return n(e,r)}var i=e["get"+t(r)];if(i===undefined){i=e["is"+t(r)]}if(i===undefined){i=e[r]}if(typeof i==="function"){i=i.call(e)}return i}function u(e,r,n){var i=e["set"+t(r)];if(i){i.call(e,n)}else{e[r]=n}}function f(e,r){var t=true;var n,i;for(n in r){i=r[n];if(_(e).valueForKeyPath(n)!=i){t=false;break}}return t}var o={valueForKeyPath:function(e,r,t){if(typeof r!="string"){throw new Error("keypath must be a string")}if(e===null||e===undefined){return t}var n=e;var u=r.split(".");var f=u.shift();var o=undefined;while(n!==null&&n!==undefined&&typeof f==="string"){o=n;n=i(n,f);if(typeof n==="function"){n=n.call(o)}f=u.shift()}return n||t},setValueForKeyPath:function(e,r,t){if(typeof r!="string"){throw new Error("keypath must be a string")}var n=r.split(".");var i=n.pop();var f=_(e).valueForKeyPath(n.join("."));if(f!==null&&f!==undefined){u(f,i,t)}},pluckByKeyPath:function(e,r){var t=[];var n,i;for(n in e){i=e[n];t[n]=_(i).valueForKeyPath(r)}return t},whereByKeyPath:function(e,r){"use strict";var t=[];var n,i;var u,o;for(n in e){i=e[n];if(f(i,r)){t.push(i)}}return t},findWhereByKeyPath:function(e,r){"use strict";var t=[];var n,i;var u,o;for(n in e){i=e[n];if(f(i,r)){return i}}return null},sortByKeyPath:function(e,r){if(typeof r!=="string"){return _.sortBy(e,r)}else{return _.sortBy(e,function(e){return _.valueForKeyPath(e,r)})}},groupByKeyPath:function(e,r){if(typeof r!=="string"){return _.groupBy(e,r)}else{return _.groupBy(e,function(e){return _.valueForKeyPath(e,r)})}},indexByKeyPath:function(e,r){if(typeof r!=="string"){return _.indexBy(e,r)}else{return _.indexBy(e,function(e){return _.valueForKeyPath(e,r)})}},countByKeyPath:function(e,r){if(typeof r!=="string"){return _.countBy(e,r)}else{return _.countBy(e,function(e){return _.valueForKeyPath(e,r)})}}};var a={getValueForKeyPath:o.valueForKeyPath};_.mixin(o);_.mixin(a);if(e){module.exports=_}})(); | ||
//# sourceMappingURL=lib/underscore-keypath.min.js.map |
{ | ||
"name" : "underscore-keypath", | ||
"version" : "0.0.3", | ||
"version" : "0.0.4", | ||
@@ -6,0 +6,0 @@ "author" : "Jeeeyul Lee <jeeeyul@gmail.com>", |
@@ -5,4 +5,6 @@ # underscore-keypath | ||
key-path mechanism extensions for underscore | ||
key-path mechanism extensions for underscore (mixin). | ||
**underscore-keypath** let you access JavaScript objects and arrays with keypath easily. | ||
```bash | ||
@@ -33,8 +35,39 @@ $ npm install underscore-keypath | ||
name : "Cool!" | ||
} | ||
}, | ||
scores : [55, 27, 100, 33] | ||
}; | ||
_(foo).valueForKeyPath("bar.name"); // --> "Cool!" | ||
_(foo).valueForKeyPath("bar.name"); // --> "Cool!" | ||
_(foo).setValueForKeyPath("bar.name", "BAR"); // --> sets foo.bar.name as "BAR" | ||
_(foo).valueForKeyPath("scores.@max"); // --> 100 | ||
``` | ||
see [API](https://github.com/jeeeyul/underscore-keypath/wiki/API) Document | ||
## more? | ||
```javascript | ||
var list = [{ | ||
name : "foo", | ||
info : { | ||
favoriteColor : "red", | ||
age : 20 | ||
} | ||
},{ | ||
name : "bar", | ||
info : { | ||
favoriteColor : "green", | ||
age : 17 | ||
} | ||
},{ | ||
name : "zar", | ||
info : { | ||
favoriteColor : "red", | ||
age : 34 | ||
} | ||
}]; | ||
_(list).pluckByKeyPath("info.age"); // --> [20, 17, 34] | ||
_(list).sortByKeyPath("info.age"); // --> [{name:"bar", ..}, {name:"foo", ..}, {name:"zar", ..}] | ||
_(list).groupByKeyPath("info.favoriteColor"); // --> {red:2, green:1} | ||
``` | ||
See [API Document](https://github.com/jeeeyul/underscore-keypath/wiki) |
var _ = require("../lib/underscore-keypath"); | ||
var assert = require("assert"); | ||
var should = require("should"); | ||
var Person = require("./fixture").Person; | ||
var arr = [ | ||
{ | ||
"entry" : { | ||
"name" : "a", | ||
"tag" : "foo", | ||
"age" : 1, | ||
"data" : [1, 3, 5, 7, 9] | ||
var fixture = [ | ||
new Person("foo", 1, { | ||
company : { | ||
name : "AAA", | ||
since : 1995 | ||
} | ||
}, | ||
{ | ||
"entry" : { | ||
"name" : "b", | ||
"age" : 2 | ||
}), | ||
new Person("bar", 2, { | ||
company : { | ||
name : "BBB", | ||
since : 1990 | ||
} | ||
}, | ||
{ | ||
"entry" : { | ||
"name" : "c", | ||
"tag" : "foo", | ||
"age" : 3 | ||
}), | ||
new Person("zar", 3, { | ||
company : { | ||
name : "AAA", | ||
since : 1998 | ||
} | ||
} | ||
}) | ||
]; | ||
describe("pluckByKeyPath", function(){ | ||
it("pluckByKeyPath should have to pluck by keypath mechanism", function(){ | ||
var actual = _(arr).pluckByKeyPath("entry.name"); | ||
JSON.stringify(actual) | ||
.should.be.equal(JSON.stringify(["a", "b", "c"])); | ||
describe("collection", function(){ | ||
describe("pluckByKeyPath", function(){ | ||
it("pluckByKeyPath should have to pluck by keypath mechanism", function(){ | ||
_(fixture).pluckByKeyPath("name") | ||
.should.be.an.Array | ||
.and.containEql("foo") | ||
.and.containEql("bar") | ||
.and.containEql("zar") | ||
.and.have.lengthOf(3); | ||
}); | ||
}); | ||
}); | ||
describe("whereByKeyPath", function(){ | ||
it("whereByKeyPath should work as _.where with keypath", function(){ | ||
var actual = _(arr).whereByKeyPath({ | ||
"entry.tag" : "foo" | ||
describe("whereByKeyPath", function(){ | ||
it("whereByKeyPath should act as where with key-value mechanism", function(){ | ||
_(fixture).whereByKeyPath({ | ||
"options.company.name" : "AAA" | ||
}).should.be.an.Array | ||
.and.containEql(fixture[0]) | ||
.and.containEql(fixture[2]) | ||
.and.have.lengthOf(2); | ||
}); | ||
actual.should.have.lengthOf(2); | ||
}); | ||
it("findWhereByKeyPath should work as _.findWhere with keypath", function(){ | ||
_(arr).findWhereByKeyPath({ | ||
"entry.tag" : "foo" | ||
}).entry.name.should.be.exactly("a"); | ||
_(arr).findWhereByKeyPath({ | ||
"entry.name" : "c" | ||
}).entry.name.should.be.exactly("c"); | ||
it("findWhere should act as _.findWhere with key-value mechanism", function(){ | ||
_(fixture).findWhereByKeyPath({ | ||
"age" : 1 | ||
}).should.be.an.Object | ||
.and.be.exactly(fixture[0]); | ||
}); | ||
}); | ||
}); | ||
describe("@property", function(){ | ||
it("@first", function(){ | ||
_(fixture).valueForKeyPath("@first.name") | ||
.should.be.exactly("foo"); | ||
}); | ||
it("@max", function(){ | ||
_.chain(fixture) | ||
.pluckByKeyPath("age") | ||
.valueForKeyPath("@max") | ||
.value() | ||
.should.be.an.Number | ||
.and.be.exactly(3); | ||
}); | ||
it("@min", function(){ | ||
_.chain(fixture) | ||
.pluckByKeyPath("age") | ||
.valueForKeyPath("@min") | ||
.value() | ||
.should.be.exactly(1); | ||
}); | ||
describe("@property", function(){ | ||
it("@first should have to return first element", function(){ | ||
_(arr).valueForKeyPath("@first.entry.name").should.be.exactly("a"); | ||
}); | ||
it("@size", function(){ | ||
_(fixture).valueForKeyPath("@size") | ||
.should.be.exactly(3); | ||
}); | ||
it("@last should have to return last element", function(){ | ||
_(arr).valueForKeyPath("@last.entry.name").should.be.exactly("c"); | ||
it("@last", function(){ | ||
_(fixture).valueForKeyPath("@last.name") | ||
.should.be.exactly("zar"); | ||
}); | ||
}); | ||
it("@max should have to return max value of list", function(){ | ||
_.chain(arr) | ||
.pluckByKeyPath("entry.age") | ||
.max() | ||
.value().should.be.exactly(3); | ||
describe("grouping and indexing", function(){ | ||
it("sortByKeyPath", function(){ | ||
var sorted = _(fixture).sortByKeyPath("options.company.since"); | ||
_(sorted).pluckByKeyPath("name").join(",") | ||
.should.be.exactly("bar,foo,zar"); | ||
}); | ||
_(arr).valueForKeyPath("@first.entry.data.@max").should.be.exactly(9); | ||
}); | ||
it("countByKeyPath", function(){ | ||
var countMap = _(fixture).countByKeyPath("options.company.name"); | ||
it("@min should have to return min value of list", function(){ | ||
_(arr).valueForKeyPath("@first.entry.data.@min").should.be.exactly(1); | ||
}); | ||
countMap["AAA"].should.be.exactly(2); | ||
countMap["BBB"].should.be.exactly(1); | ||
}); | ||
it("@size should have to return size of list", function(){ | ||
_(arr).valueForKeyPath("@size").should.be.exactly(3); | ||
it("indexByKeyPath", function(){ | ||
var index = _(fixture).indexByKeyPath("options.company.name"); | ||
index["BBB"].should.be.exactly(fixture[1]); | ||
}); | ||
it("groupByKeyPath", function(){ | ||
var group = _(fixture).groupByKeyPath("options.company.name"); | ||
group["AAA"].should.be.an.Array | ||
.and.have.lengthOf(2); | ||
group["BBB"].should.be.an.Array | ||
.and.have.lengthOf(1); | ||
}); | ||
}); | ||
}); | ||
var _ = require("../lib/underscore-keypath"); | ||
var assert = require("assert"); | ||
var should = require("should"); | ||
var Person = require("./fixture.js").Person; | ||
var foo = { | ||
"_version" : "0.0.1", | ||
"getVersion" : function(){ | ||
return this._version; | ||
}, | ||
"setVersion" : function(newVersion){ | ||
this._version = newVersion; | ||
}, | ||
var fixture = { | ||
foo : new Person("foo", 1), | ||
bar : new Person("bar", 2) | ||
}; | ||
"bar" : { | ||
"_name" : "bar", | ||
"setName" : function(newName){ | ||
return this._name = newName; | ||
}, | ||
"getName" : function(){ | ||
return this._name; | ||
} | ||
} | ||
} | ||
describe("setValueForKeyPath", function(){ | ||
describe("set plain property", function(){ | ||
it("plain property must be updated", function(){ | ||
_(fixture).setValueForKeyPath("foo.alias", "FOO"); | ||
fixture.foo.alias.should.be.exactly("FOO"); | ||
}); | ||
}); | ||
describe("set plain property", function(){ | ||
it("plain property must be updated", function(){ | ||
_(foo).setValueForKeyPath("aProp", 1024); | ||
assert.equal(foo.aProp, 1024); | ||
_(foo).setValueForKeyPath("bar.bProp", 1111); | ||
assert.equal(foo.bar.bProp, 1111); | ||
describe("set property by setter", function(){ | ||
it("what if there is setter for given keypath, it must be called", function(){ | ||
_(fixture).setValueForKeyPath("bar.age", 99); | ||
fixture.bar._age.should.be.exactly(99); | ||
}); | ||
}); | ||
}); | ||
describe("set property by setter", function(){ | ||
it("what if there is setter for given keypath, it must be called", function(){ | ||
_(foo).setValueForKeyPath("version", "9.9.9"); | ||
assert.equal(foo._version, "9.9.9"); | ||
_(foo).setValueForKeyPath("bar.name", "nodejs"); | ||
assert.equal(foo.bar._name, "nodejs"); | ||
}); | ||
}); |
var _ = require("../lib/underscore-keypath"); | ||
var assert = require("assert"); | ||
var Person = require("./fixture").Person; | ||
var foo = { | ||
"_version" : "0.0.1", | ||
"_valid" : true, | ||
"getVersion" : function(){ | ||
return this._version; | ||
}, | ||
"isValid" : function(){ | ||
return this._valid; | ||
}, | ||
var fixture = { | ||
foo : new Person("aaa", 1), | ||
bar : new Person("bar", 2) | ||
}; | ||
"name" : "foo", | ||
"bar" : { | ||
"name" : "bar", | ||
"fnProp" : function(){ | ||
return { | ||
name : "fnProp" | ||
} | ||
} | ||
} | ||
} | ||
fixture.foo.data = "sample data"; | ||
fixture.count = function(){ | ||
return 2; | ||
}; | ||
@@ -28,6 +18,3 @@ describe("valueForKeyPath", function(){ | ||
it("what if keypath is empty, result must be current context", function(){ | ||
assert.equal( | ||
_(foo).valueForKeyPath(""), | ||
foo | ||
); | ||
_(fixture).valueForKeyPath("").should.be.exactly(fixture); | ||
}); | ||
@@ -38,3 +25,4 @@ }); | ||
it("should return fallback value when there is no value for give keypath", function(){ | ||
assert.equal(_(foo).valueForKeyPath("bar.nonexistproperty", "fallback"), "fallback"); | ||
_(fixture).valueForKeyPath("zar.whatever", "fallback") | ||
.should.be.exactly("fallback"); | ||
}); | ||
@@ -45,6 +33,3 @@ }); | ||
it("what if property is function, it's result have to be returned.", function(){ | ||
assert.equal( | ||
_(foo).valueForKeyPath("bar.fnProp.name"), | ||
"fnProp" | ||
); | ||
_(fixture).valueForKeyPath("count").should.be.exactly(2); | ||
}); | ||
@@ -55,11 +40,7 @@ }); | ||
it("what if there is a getter for given key, it must be call", function(){ | ||
assert.equal( | ||
_(foo).valueForKeyPath("version"), | ||
"0.0.1" | ||
); | ||
_(fixture).valueForKeyPath("foo.name") | ||
.should.be.exactly(fixture.foo._name); | ||
assert.equal( | ||
_(foo).valueForKeyPath("valid"), | ||
true | ||
); | ||
_(fixture).valueForKeyPath("foo.male") | ||
.should.be.exactly(fixture.foo._male); | ||
}); | ||
@@ -70,10 +51,15 @@ }); | ||
it("valueForKeyPath returns value of given keypath", function(){ | ||
_(foo).valueForKeyPath("bar.name").should.be.exactly("bar"); | ||
}); | ||
_(fixture).valueForKeyPath("foo.data") | ||
.should.be.exactly("sample data"); | ||
}); | ||
}); | ||
describe("alias", function(){ | ||
it("getValueForKeyPath must be act same as valueForKeyPath", function(){ | ||
_(foo).getValueForKeyPath("bar.name").should.be.exactly("bar"); | ||
_(fixture).getValueForKeyPath("foo.data") | ||
.should.be.exactly("sample data"); | ||
}); | ||
}); | ||
}); | ||
Sorry, the diff of this file is not supported yet
18778
11
401
72