Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 6 files changed
  • 0 comments
  • 1 contributor
14  .gitignore
... ...
@@ -0,0 +1,14 @@
  1
+.DS_Store
  2
+node_modules/
  3
+*~
  4
+\#*\#
  5
+/.emacs.desktop
  6
+/.emacs.desktop.lock
  7
+.elc
  8
+auto-save-list
  9
+tramp
  10
+.\#*
  11
+
  12
+# Org-mode
  13
+.org-id-locations
  14
+*_archive
0  README.md
Source Rendered
No changes.
22  package.json
... ...
@@ -0,0 +1,22 @@
  1
+{
  2
+  "name": "underscore_extended",
  3
+  "description": "lots of fun functions for use with underscore.js",
  4
+  "author": "Paul Iannazzo <somethingitalian@gmail.com>",
  5
+  "version": "0.0.3",
  6
+  "repository": {
  7
+    "type": "git",
  8
+    "url": "git://github.com/boxxxie/underscore_extended.git"
  9
+  },
  10
+  "bugs": "git://github.com/boxxxie/underscore_extended.git/issues",
  11
+  "dependencies": {
  12
+    "underscore":"1.3.x"
  13
+  },
  14
+  "devDependencies": {
  15
+    "mocha"    :  "1.5.x"
  16
+  },
  17
+  "keywords": [
  18
+    "functional",
  19
+    "fp",
  20
+    "underscore"
  21
+  ]
  22
+}
26  packages.json
... ...
@@ -0,0 +1,26 @@
  1
+{
  2
+  "name": "underscore_extended",
  3
+  "description": "lots of fun functions for use with underscore.js",
  4
+  "author": "Paul Iannazzo <somethingitalian@gmail.com>",
  5
+  "version": "0.0.3",
  6
+  "repository": {
  7
+    "type": "git",
  8
+    "url": "git://github.com/boxxxie/underscore_extended.git"
  9
+  },
  10
+  "bugs": "git://github.com/boxxxie/underscore_extended.git/issues",
  11
+  "dependencies": {
  12
+    "underscore":"*"
  13
+  },
  14
+  "devDependencies": {
  15
+    "mocha":"*",
  16
+    "chai":"*"
  17
+  },
  18
+  "keywords": [
  19
+    "functional",
  20
+    "fp",
  21
+    "underscore"
  22
+  ],
  23
+  "scripts": {
  24
+    "test": "mocha -r expect -R spec"
  25
+  }
  26
+}
160  test.js~ → test/test.js
... ...
@@ -1,5 +1,9 @@
1  
-var _ = require("./underscore");
2  
-require("./underscore_extended");
  1
+var _ = require("underscore");
  2
+require("../underscore_extended");
  3
+var mocha = require('mocha');
  4
+var chai = require('chai')
  5
+//  , chaiModel = require('./helpers/model')
  6
+  , expect = chai.expect;
3 7
 
4 8
 function _test(fnName){
5 9
     return function(){
@@ -38,6 +42,7 @@ mapMerge = _test("mapMerge"),
38 42
 zipMerge = _test("zipMerge"),
39 43
 partition = _test("partition"),
40 44
 nest = _test("nest"),
  45
+mapNest = _test("mapNest"),
41 46
 filter$ = _test("filter$"),
42 47
 map$ = _test("map$"),
43 48
 compress = _test("compress"),
@@ -56,6 +61,14 @@ subtract = _test("subtract"),
56 61
 multiply = _test("multiply"),
57 62
 divide = _test("divide");
58 63
 
  64
+describe('pairs()',function(){
  65
+  it("should convert {a:'a',b:'b'} into [['a','a'],['b','b']]",function(){
  66
+    expect(
  67
+      _.pairs({a:'a',b:'b'}))
  68
+      .eql([['a','a'],['b','b']]);
  69
+  })
  70
+})
  71
+
59 72
 pairs({a:'a',b:'b'})
60 73
 ([['a','a'],['b','b']])
61 74
 ("test with a simple object");
@@ -210,12 +223,6 @@ merge([{a:1},{b:2},{c:3}])
210 223
 ({a:1,b:2,c:3})
211 224
 ("all objs in array have have different keys");
212 225
 
213  
-console.log("test map merge")
214  
-console.log(_.mapMerge([
215  
-	     [{a:1},{b:2},{c:3}],
216  
-	     [{a:1},{a:2},{a:3}]
217  
-	 ]))
218  
-
219 226
 mapMerge([
220 227
 	     [{a:1},{b:2},{c:3}],
221 228
 	     [{a:1},{a:2},{a:3}]
@@ -258,10 +265,19 @@ nest({},['a','b','c'],'field')
258 265
 ({field:{}})
259 266
 ("nest keys in an empty object");
260 267
 
  268
+//not sure if this should be what happens...
  269
+nest({c:1},['a','b'],'field')
  270
+({c:1,field:{}})
  271
+("nest keys in an empty object, no matches by new field made");
  272
+
261 273
 nest({d:1,f:2,g:3},['a','b','c'],'field')
262 274
 ({field:{},d:1,f:2,g:3})
263 275
 ("nest keys in an object where none match");
264 276
 
  277
+mapNest([{a:1},{b:1},{c:1}],['a','b'],'field')
  278
+([{field:{a:1}},{field:{b:1}},{c:1,field:{}}])
  279
+('testing on array of simple objects, last case doesnt nest');
  280
+
265 281
 filter$({a:1,b:2,c:3},function(val){return val == 3;})
266 282
 ({c:3})
267 283
 ("simple obj where filter matches 1 val");
@@ -274,15 +290,15 @@ filter$({},function(val){return val == 4;})
274 290
 ({})
275 291
 ("empty obj");
276 292
 
277  
-map$({a:1,b:2,c:3},function(val,key){return [key,0];})
  293
+map$({a:1,b:2,c:3},function(pair){var key = _.first(pair); var val = _.second(pair);return [key,0];})
278 294
 ({a:0,b:0,c:0})
279 295
 ("transforming all the vals of an object");
280 296
 
281  
-map$({a:1,b:2,c:3},function(val,key){return [val,val];}) //this really shouldn't be done
  297
+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
282 298
 ({1:1,2:2,3:3})
283 299
 ("transforming all the keys of an object");
284 300
 
285  
-map$({},function(val,key){return [val,val];}) //this really shouldn't be done
  301
+map$({},function(pair){var key = _.first(pair); var val = _.second(pair);return [val,val];}) //this really shouldn't be done
286 302
 ({})
287 303
 ("empty object");
288 304
 
@@ -325,6 +341,10 @@ matchTo(["1","2"],[{id:"1",a:2}],'id')
325 341
 
326 342
 //------------------ extend recursive -------------------
327 343
 
  344
+extend_r({},1)
  345
+(1)
  346
+("primative to object overwrite");
  347
+
328 348
 extend_r({},{a:1})
329 349
 ({a:1})
330 350
 ("non-recursive extend onto an empty object");
@@ -358,10 +378,12 @@ extend_r(
358 378
      _rev: "2-27c295b83271e6dcc2dc43df18444177",
359 379
      date: "Wed Jan 25 2012 22:24:01 GMT-0500 (EST)",
360 380
      price: {selling_price: 2},
  381
+     arrayTest:[1],
361 382
      description: "invNeww",
362 383
      upccode: "01123456"},
363 384
     {_id: "02bb06f0fed1596decf2024a4c029a59",
364 385
      _rev: "5-484927d67a1be1454431b4143059c87c",
  386
+     arrayTest:[2,3],
365 387
      date: "Wed Jan 25 2012 22:25:09 GMT-0500 (EST)",
366 388
      description: "invNeww",
367 389
      apply_taxes: {exemption: false, tax1: true, tax2: true, tax3: true},
@@ -369,12 +391,81 @@ extend_r(
369 391
 ({_id: "02bb06f0fed1596decf2024a4c029a59",
370 392
   _rev: "5-484927d67a1be1454431b4143059c87c",
371 393
   date: "Wed Jan 25 2012 22:25:09 GMT-0500 (EST)",
  394
+  arrayTest:[2,3],
372 395
   description: "invNeww",
373 396
   price: {selling_price: 2},
374 397
   apply_taxes: {exemption: false, tax1: true, tax2: true, tax3: true},
375 398
   upccode: "01123456"})
376 399
 ("complex real world object filling in a missing field");
377 400
 
  401
+extend_r([1,2,3],[4,5,6])
  402
+([4,5,6])
  403
+('extending onto arrays')
  404
+
  405
+extend_r([1,2,3],[4,5,6,7])
  406
+([4,5,6,7])
  407
+('extending onto arrays of different sizes')
  408
+
  409
+extend_r([1,2,3,4],[5,6,7])
  410
+([5,6,7,4])
  411
+('extending onto arrays of different sizes')
  412
+
  413
+extend_r([[1],[2]],[[4],[5]])
  414
+([[4],[5]])
  415
+('extending onto nested arrays')
  416
+
  417
+extend_r([[1],[2]],[[4],[5],[6]])
  418
+([[4],[5],[6]])
  419
+('extending onto nested arrays of different sizes')
  420
+
  421
+extend_r([[1],[2,3]],[[4],[5]])
  422
+([[4],[5,3]])
  423
+('extending onto nested arrays of different sizes')
  424
+
  425
+extend_r(
  426
+    {"display":
  427
+     {"description":["","",""],
  428
+      "color":"255,255,25",
  429
+      "is_enabled":false,
  430
+      "screen":0,
  431
+      "position":0
  432
+     },
  433
+     "foodItem":{"price":3.49,
  434
+		 "has_modifier":true,
  435
+		 "apply_taxes":{"exemption":true,
  436
+				"tax1":true,
  437
+				"tax2":true,
  438
+				"tax3":false
  439
+			       },
  440
+		 "use_scale":false,
  441
+		 "print_to_kitchen":true,
  442
+		 "duplicate":false
  443
+		}
  444
+    },
  445
+
  446
+    {display:{color:"#fffff"}}
  447
+)
  448
+(
  449
+    {"display":
  450
+     {"description":["","",""],
  451
+      "color":"#fffff",
  452
+      "is_enabled":false,
  453
+      "screen":0,
  454
+      "position":0},
  455
+     "foodItem":{"price":3.49,
  456
+		 "has_modifier":true,
  457
+		 "apply_taxes":{"exemption":true,
  458
+				"tax1":true,
  459
+				"tax2":true,
  460
+				"tax3":false},
  461
+		 "use_scale":false,
  462
+		 "print_to_kitchen":true,
  463
+		 "duplicate":false
  464
+     }
  465
+    })
  466
+("real data merging complicated objects with arrays and uneven nested properties")
  467
+
  468
+
378 469
 //-----------------------fill -------------------------------
379 470
 
380 471
 fill({},{a:1})
@@ -431,7 +522,7 @@ fill(
431 522
 //--------------------------- either -------------------------
432 523
 
433 524
 either(undefined,undefined,null,0,1)
434  
-(0)
  525
+(1)
435 526
 ("simple list of false vals with the last one being the one returend");
436 527
 
437 528
 either()
@@ -492,6 +583,49 @@ combine({a:1},{a:2},{a:3,b:3})
492 583
 ({a:3,b:3})
493 584
 ("conflict keys overwrite with the last. 3 args");
494 585
 
  586
+combine(
  587
+    {"display":
  588
+     {"description":["","",""],
  589
+      "color":"255,255,25",
  590
+      "is_enabled":false,
  591
+      "screen":0,
  592
+      "position":0
  593
+     },
  594
+     "foodItem":{"price":3.49,
  595
+		 "has_modifier":true,
  596
+		 "apply_taxes":{"exemption":true,
  597
+				"tax1":true,
  598
+				"tax2":true,
  599
+				"tax3":false
  600
+			       },
  601
+		 "use_scale":false,
  602
+		 "print_to_kitchen":true,
  603
+		 "duplicate":false
  604
+		}
  605
+    },
  606
+
  607
+    {display:{color:"#fffff"}}
  608
+)
  609
+(
  610
+    {"display":
  611
+     {"description":["","",""],
  612
+      "color":"#fffff",
  613
+      "is_enabled":false,
  614
+      "screen":0,
  615
+      "position":0},
  616
+     "foodItem":{"price":3.49,
  617
+		 "has_modifier":true,
  618
+		 "apply_taxes":{"exemption":true,
  619
+				"tax1":true,
  620
+				"tax2":true,
  621
+				"tax3":false},
  622
+		 "use_scale":false,
  623
+		 "print_to_kitchen":true,
  624
+		 "duplicate":false
  625
+     }
  626
+    })
  627
+("real data merging complicated objects with arrays and uneven nested properties")
  628
+
495 629
 combine({a:{b:1}},{a:{b:2}},{a:{b:3},b:3})
496 630
 ({a:{b:3},b:3})
497 631
 ("recursive merge keys overwrite with the last. 3 args");
@@ -528,4 +662,4 @@ subtract({a:1,b:{b:2}},{b:{b:1}})
528 662
 ({a:1,b:{b:1}})
529 663
 ("subtracting a nested object");
530 664
 
531  
-console.log("tests finished");
  665
+console.log("tests finished");
8  underscore_extended.js
@@ -438,6 +438,8 @@ _.mixin({
438 438
 	    splitKeys:_.juxtapose(_.selectKeys,_.removeKeys)
439 439
 	});
440 440
 
  441
+//this is really cool. check out clojure's documentation for how the
  442
+//output looks
441 443
 _.mixin({
442 444
 	    //returns a frequencies object based on the list/iterator
443 445
 	    frequencies:function(list,iterator){
@@ -451,6 +453,7 @@ _.mixin({
451 453
 		    .value();
452 454
 	    }
453 455
 	});
  456
+
454 457
 _.mixin({
455 458
 	    curry:function(fn) {
456 459
 		var args = _.chain(arguments).toArray().rest().value();
@@ -470,6 +473,7 @@ _.mixin({
470 473
 		};
471 474
 	    }
472 475
 	});
  476
+
473 477
 _.mixin({
474 478
 	    parseBool:function(bool_str){
475 479
 		if(bool_str === 'true'){
@@ -504,6 +508,8 @@ _.mixin({
504 508
 	    }
505 509
 	});
506 510
 
  511
+//these are inc and dec functions with guards. useful when you don't
  512
+//want to dec past 0 or some other magic number
507 513
 _.mixin({
508 514
 	    inc:function(num,limit,magnitude){
509 515
 		if(!_.isNumber(num)){
@@ -523,4 +529,4 @@ _.mixin({
523 529
 		}
524 530
 		return num - (magnitude || 1);
525 531
 	    }
526  
-	});
  532
+	});

No commit comments for this range

Something went wrong with that request. Please try again.