Browse files

add Array#sliceNonUnique and experimental TableDecorator

  • Loading branch information...
1 parent 430b5da commit 8673325258b547d0b02ade5e837e864a45a64a2d kangax committed Jun 23, 2008
Showing with 47 additions and 0 deletions.
  1. +23 −0 array.extensions.js
  2. +24 −0 experimental/table_decorator.js
View
23 array.extensions.js
@@ -35,4 +35,27 @@ Array.prototype.clone = function() {
Array.prototype.isUnique = function(value){
var idx = this.indexOf(value);
return this.indexOf(value, idx + 1) == -1;
+};
+
+/**
+ * Array#sliceNonUnique -> Array
+ *
+ * var arr = [1,1,2,3,3,4,1,2,2,5,5,5];
+ * arr.sliceRepeating();
+ *
+ * // produces
+ * [[1, 1, 1], [2, 2, 2], [3, 3], [5, 5, 5]]
+ *
+ **/
+Array.prototype.sliceNonUnique = function() {
+ var result = [], clone = this.sort();
+ for (var i=0, l=clone.length; i<l; i++) {
+ if (clone[i] === clone[i+1]) {
+ var temp = [];
+ while (clone[i] === clone[i+1]) { temp.push(clone[i]); i++; }
+ if (clone[i] === clone[i-1]) { temp.push(clone[i]); }
+ result.push(temp);
+ }
+ }
+ return result;
};
View
24 experimental/table_decorator.js
@@ -0,0 +1,24 @@
+var TableDecorator = Class.create({
+ initialize: function(element) {
+ this.element = $(element);
+ },
+ toElement: function() {
+ return this.element;
+ },
+ getHeadRows: function() {
+ return $A(this.element.tHead.rows);
+ },
+ getBodyRows: function() {
+ return $A(this.element.tBodies[0].rows);
+ },
+ getHeadCells: function(index) {
+ return this.getHeadRows().map(function(row) {
+ return $A(row.cells)[index];
+ })
+ },
+ getBodyCells: function(index) {
+ return this.getBodyRows().map(function(row) {
+ return $A(row.cells)[index];
+ })
+ }
+})

0 comments on commit 8673325

Please sign in to comment.