Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 142 additions & 2 deletions source/extensions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,145 @@
// NOTE Having issues conflicting with jQuery stuff when setting Object
// prototype settings; instead add into Liquid.Object.extensions and use in
// Array.indexOf
if (!Array.prototype.indexOf) {
Object.defineProperty(Array.prototype, 'indexOf', {
enumerable: false,
value: function(obj) {
for (var i=0; i<this.length; i++) {
if (this[i] == obj) return i;
}

return -1;
}
});
}

// Array.clear
if (!Array.prototype.clear) {
Object.defineProperty(Array.prototype, 'clear', {
enumerable: false,
value: function() {
this.length = 0;
}
});
}

// Array.map
if (!Array.prototype.map) {
Object.defineProperty(Array.prototype, 'map', {
enumerable: false,
value: function(fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw 'Array.map requires first argument to be a function';

var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}

return res;
}
});
}

// Array.first
if (!Array.prototype.first) {
Object.defineProperty(Array.prototype, 'first', {
enumerable: false,
value: function() {
return this[0];
}
});
}

// Array.last
if (!Array.prototype.last) {
Object.defineProperty(Array.prototype, 'last', {
enumerable: false,
value: function() {
return this[this.length - 1];
}
});
}

// Array.flatten
if (!Array.prototype.flatten) {
Object.defineProperty(Array.prototype, 'flatten', {
enumerable: false,
value: function() {
var len = this.length;
var arr = [];
for (var i = 0; i < len; i++) {
// TODO This supposedly isn't safe in multiple frames;
// http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript
// http://stackoverflow.com/questions/4775722/javascript-check-if-object-is-array
if (this[i] instanceof Array) {
arr = arr.concat(this[i]);
} else {
arr.push(this[i]);
}
}

return arr;
}
});
}

// Array.each
if (!Array.prototype.each) {
Object.defineProperty(Array.prototype, 'each', {
enumerable: false,
value: function(fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw 'Array.each requires first argument to be a function';

var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
fun.call(thisp, this[i], i, this);
}

return null;
}
});
}

// Array.include
if (!Array.prototype.include) {
Object.defineProperty(Array.prototype, 'include', {
enumerable: false,
value: function(arg) {
var len = this.length;

return this.indexOf(arg) >= 0;
for (var i = 0; i < len; i++) {
if (arg == this[i]) return true;
}

return false;
}
});
}

// String.capitalize
if (!String.prototype.capitalize) {
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
};
}

// String.strip
if (!String.prototype.strip) {
String.prototype.strip = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}


// NOTE Having issues conflicting with jQuery stuff when setting Object
// prototype settings; instead add into Liquid.Object.extensions and use in
// the particular location; can add into Object.prototype later if we want.
Liquid.extensions = {};
Liquid.extensions.object = {};
Expand Down