Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: boxxxie/underscore_extended
base: 127d836c65
...
head fork: boxxxie/underscore_extended
compare: cc7e4d7f11
  • 1 commit
  • 6 files changed
  • 0 commit comments
  • 1 contributor
14 .gitignore
View
@@ -0,0 +1,14 @@
+.DS_Store
+node_modules/
+*~
+\#*\#
+/.emacs.desktop
+/.emacs.desktop.lock
+.elc
+auto-save-list
+tramp
+.\#*
+
+# Org-mode
+.org-id-locations
+*_archive
0  README.md
View
No changes.
22 package.json
View
@@ -0,0 +1,22 @@
+{
+ "name": "underscore_extended",
+ "description": "lots of fun functions for use with underscore.js",
+ "author": "Paul Iannazzo <somethingitalian@gmail.com>",
+ "version": "0.0.3",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/boxxxie/underscore_extended.git"
+ },
+ "bugs": "git://github.com/boxxxie/underscore_extended.git/issues",
+ "dependencies": {
+ "underscore":"1.3.x"
+ },
+ "devDependencies": {
+ "mocha" : "1.5.x"
+ },
+ "keywords": [
+ "functional",
+ "fp",
+ "underscore"
+ ]
+}
26 packages.json
View
@@ -0,0 +1,26 @@
+{
+ "name": "underscore_extended",
+ "description": "lots of fun functions for use with underscore.js",
+ "author": "Paul Iannazzo <somethingitalian@gmail.com>",
+ "version": "0.0.3",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/boxxxie/underscore_extended.git"
+ },
+ "bugs": "git://github.com/boxxxie/underscore_extended.git/issues",
+ "dependencies": {
+ "underscore":"*"
+ },
+ "devDependencies": {
+ "mocha":"*",
+ "chai":"*"
+ },
+ "keywords": [
+ "functional",
+ "fp",
+ "underscore"
+ ],
+ "scripts": {
+ "test": "mocha -r expect -R spec"
+ }
+}
160 test.js~ → test/test.js
View
@@ -1,5 +1,9 @@
-var _ = require("./underscore");
-require("./underscore_extended");
+var _ = require("underscore");
+require("../underscore_extended");
+var mocha = require('mocha');
+var chai = require('chai')
+// , chaiModel = require('./helpers/model')
+ , expect = chai.expect;
function _test(fnName){
return function(){
@@ -38,6 +42,7 @@ mapMerge = _test("mapMerge"),
zipMerge = _test("zipMerge"),
partition = _test("partition"),
nest = _test("nest"),
+mapNest = _test("mapNest"),
filter$ = _test("filter$"),
map$ = _test("map$"),
compress = _test("compress"),
@@ -56,6 +61,14 @@ subtract = _test("subtract"),
multiply = _test("multiply"),
divide = _test("divide");
+describe('pairs()',function(){
+ it("should convert {a:'a',b:'b'} into [['a','a'],['b','b']]",function(){
+ expect(
+ _.pairs({a:'a',b:'b'}))
+ .eql([['a','a'],['b','b']]);
+ })
+})
+
pairs({a:'a',b:'b'})
([['a','a'],['b','b']])
("test with a simple object");
@@ -210,12 +223,6 @@ merge([{a:1},{b:2},{c:3}])
({a:1,b:2,c:3})
("all objs in array have have different keys");
-console.log("test map merge")
-console.log(_.mapMerge([
- [{a:1},{b:2},{c:3}],
- [{a:1},{a:2},{a:3}]
- ]))
-
mapMerge([
[{a:1},{b:2},{c:3}],
[{a:1},{a:2},{a:3}]
@@ -258,10 +265,19 @@ nest({},['a','b','c'],'field')
({field:{}})
("nest keys in an empty object");
+//not sure if this should be what happens...
+nest({c:1},['a','b'],'field')
+({c:1,field:{}})
+("nest keys in an empty object, no matches by new field made");
+
nest({d:1,f:2,g:3},['a','b','c'],'field')
({field:{},d:1,f:2,g:3})
("nest keys in an object where none match");
+mapNest([{a:1},{b:1},{c:1}],['a','b'],'field')
+([{field:{a:1}},{field:{b:1}},{c:1,field:{}}])
+('testing on array of simple objects, last case doesnt nest');
+
filter$({a:1,b:2,c:3},function(val){return val == 3;})
({c:3})
("simple obj where filter matches 1 val");
@@ -274,15 +290,15 @@ filter$({},function(val){return val == 4;})
({})
("empty obj");
-map$({a:1,b:2,c:3},function(val,key){return [key,0];})
+map$({a:1,b:2,c:3},function(pair){var key = _.first(pair); var val = _.second(pair);return [key,0];})
({a:0,b:0,c:0})
("transforming all the vals of an object");
-map$({a:1,b:2,c:3},function(val,key){return [val,val];}) //this really shouldn't be done
+map$({a:1,b:2,c:3},function(pair){var key = _.first(pair); var val = _.second(pair);return [val,val];}) //this really shouldn't be done
({1:1,2:2,3:3})
("transforming all the keys of an object");
-map$({},function(val,key){return [val,val];}) //this really shouldn't be done
+map$({},function(pair){var key = _.first(pair); var val = _.second(pair);return [val,val];}) //this really shouldn't be done
({})
("empty object");
@@ -325,6 +341,10 @@ matchTo(["1","2"],[{id:"1",a:2}],'id')
//------------------ extend recursive -------------------
+extend_r({},1)
+(1)
+("primative to object overwrite");
+
extend_r({},{a:1})
({a:1})
("non-recursive extend onto an empty object");
@@ -358,10 +378,12 @@ extend_r(
_rev: "2-27c295b83271e6dcc2dc43df18444177",
date: "Wed Jan 25 2012 22:24:01 GMT-0500 (EST)",
price: {selling_price: 2},
+ arrayTest:[1],
description: "invNeww",
upccode: "01123456"},
{_id: "02bb06f0fed1596decf2024a4c029a59",
_rev: "5-484927d67a1be1454431b4143059c87c",
+ arrayTest:[2,3],
date: "Wed Jan 25 2012 22:25:09 GMT-0500 (EST)",
description: "invNeww",
apply_taxes: {exemption: false, tax1: true, tax2: true, tax3: true},
@@ -369,12 +391,81 @@ extend_r(
({_id: "02bb06f0fed1596decf2024a4c029a59",
_rev: "5-484927d67a1be1454431b4143059c87c",
date: "Wed Jan 25 2012 22:25:09 GMT-0500 (EST)",
+ arrayTest:[2,3],
description: "invNeww",
price: {selling_price: 2},
apply_taxes: {exemption: false, tax1: true, tax2: true, tax3: true},
upccode: "01123456"})
("complex real world object filling in a missing field");
+extend_r([1,2,3],[4,5,6])
+([4,5,6])
+('extending onto arrays')
+
+extend_r([1,2,3],[4,5,6,7])
+([4,5,6,7])
+('extending onto arrays of different sizes')
+
+extend_r([1,2,3,4],[5,6,7])
+([5,6,7,4])
+('extending onto arrays of different sizes')
+
+extend_r([[1],[2]],[[4],[5]])
+([[4],[5]])
+('extending onto nested arrays')
+
+extend_r([[1],[2]],[[4],[5],[6]])
+([[4],[5],[6]])
+('extending onto nested arrays of different sizes')
+
+extend_r([[1],[2,3]],[[4],[5]])
+([[4],[5,3]])
+('extending onto nested arrays of different sizes')
+
+extend_r(
+ {"display":
+ {"description":["","",""],
+ "color":"255,255,25",
+ "is_enabled":false,
+ "screen":0,
+ "position":0
+ },
+ "foodItem":{"price":3.49,
+ "has_modifier":true,
+ "apply_taxes":{"exemption":true,
+ "tax1":true,
+ "tax2":true,
+ "tax3":false
+ },
+ "use_scale":false,
+ "print_to_kitchen":true,
+ "duplicate":false
+ }
+ },
+
+ {display:{color:"#fffff"}}
+)
+(
+ {"display":
+ {"description":["","",""],
+ "color":"#fffff",
+ "is_enabled":false,
+ "screen":0,
+ "position":0},
+ "foodItem":{"price":3.49,
+ "has_modifier":true,
+ "apply_taxes":{"exemption":true,
+ "tax1":true,
+ "tax2":true,
+ "tax3":false},
+ "use_scale":false,
+ "print_to_kitchen":true,
+ "duplicate":false
+ }
+ })
+("real data merging complicated objects with arrays and uneven nested properties")
+
+
//-----------------------fill -------------------------------
fill({},{a:1})
@@ -431,7 +522,7 @@ fill(
//--------------------------- either -------------------------
either(undefined,undefined,null,0,1)
-(0)
+(1)
("simple list of false vals with the last one being the one returend");
either()
@@ -492,6 +583,49 @@ combine({a:1},{a:2},{a:3,b:3})
({a:3,b:3})
("conflict keys overwrite with the last. 3 args");
+combine(
+ {"display":
+ {"description":["","",""],
+ "color":"255,255,25",
+ "is_enabled":false,
+ "screen":0,
+ "position":0
+ },
+ "foodItem":{"price":3.49,
+ "has_modifier":true,
+ "apply_taxes":{"exemption":true,
+ "tax1":true,
+ "tax2":true,
+ "tax3":false
+ },
+ "use_scale":false,
+ "print_to_kitchen":true,
+ "duplicate":false
+ }
+ },
+
+ {display:{color:"#fffff"}}
+)
+(
+ {"display":
+ {"description":["","",""],
+ "color":"#fffff",
+ "is_enabled":false,
+ "screen":0,
+ "position":0},
+ "foodItem":{"price":3.49,
+ "has_modifier":true,
+ "apply_taxes":{"exemption":true,
+ "tax1":true,
+ "tax2":true,
+ "tax3":false},
+ "use_scale":false,
+ "print_to_kitchen":true,
+ "duplicate":false
+ }
+ })
+("real data merging complicated objects with arrays and uneven nested properties")
+
combine({a:{b:1}},{a:{b:2}},{a:{b:3},b:3})
({a:{b:3},b:3})
("recursive merge keys overwrite with the last. 3 args");
@@ -528,4 +662,4 @@ subtract({a:1,b:{b:2}},{b:{b:1}})
({a:1,b:{b:1}})
("subtracting a nested object");
-console.log("tests finished");
+console.log("tests finished");
8 underscore_extended.js
View
@@ -438,6 +438,8 @@ _.mixin({
splitKeys:_.juxtapose(_.selectKeys,_.removeKeys)
});
+//this is really cool. check out clojure's documentation for how the
+//output looks
_.mixin({
//returns a frequencies object based on the list/iterator
frequencies:function(list,iterator){
@@ -451,6 +453,7 @@ _.mixin({
.value();
}
});
+
_.mixin({
curry:function(fn) {
var args = _.chain(arguments).toArray().rest().value();
@@ -470,6 +473,7 @@ _.mixin({
};
}
});
+
_.mixin({
parseBool:function(bool_str){
if(bool_str === 'true'){
@@ -504,6 +508,8 @@ _.mixin({
}
});
+//these are inc and dec functions with guards. useful when you don't
+//want to dec past 0 or some other magic number
_.mixin({
inc:function(num,limit,magnitude){
if(!_.isNumber(num)){
@@ -523,4 +529,4 @@ _.mixin({
}
return num - (magnitude || 1);
}
- });
+ });

No commit comments for this range

Something went wrong with that request. Please try again.