carto/build/ecma-5.js

121 lines
3.0 KiB
JavaScript
Raw Normal View History

2010-05-18 10:57:11 +08:00
2010-05-27 01:37:53 +08:00
// ecma-5.js
//
2010-05-18 10:57:11 +08:00
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
// -- tlrobinson Tom Robinson
// dantman Daniel Friesen
//
// Array
//
if (!Array.isArray) {
Array.isArray = function(obj) {
2010-05-27 01:37:53 +08:00
return Object.prototype.toString.call(obj) === "[object Array]" ||
(obj instanceof Array);
2010-05-18 10:57:11 +08:00
};
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(block, thisObject) {
var len = this.length >>> 0;
for (var i = 0; i < len; i++) {
if (i in this) {
block.call(thisObject, this[i], i, this);
}
}
};
}
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
var res = new Array(len);
var thisp = arguments[1];
2010-05-27 01:37:53 +08:00
2010-05-18 10:57:11 +08:00
for (var i = 0; i < len; i++) {
2010-05-27 01:37:53 +08:00
if (i in this) {
2010-05-18 10:57:11 +08:00
res[i] = fun.call(thisp, this[i], i, this);
2010-05-27 01:37:53 +08:00
}
2010-05-18 10:57:11 +08:00
}
return res;
};
}
if (!Array.prototype.filter) {
Array.prototype.filter = function (block /*, thisp */) {
var values = [];
var thisp = arguments[1];
2010-05-27 01:37:53 +08:00
for (var i = 0; i < this.length; i++) {
if (block.call(thisp, this[i])) {
2010-05-18 10:57:11 +08:00
values.push(this[i]);
2010-05-27 01:37:53 +08:00
}
}
2010-05-18 10:57:11 +08:00
return values;
};
}
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fun /*, initial*/) {
var len = this.length >>> 0;
2010-05-27 01:37:53 +08:00
var i = 0;
2010-05-18 10:57:11 +08:00
// no value to return if no initial value and an empty array
2010-05-27 01:37:53 +08:00
if (len === 0 && arguments.length === 1) throw new TypeError();
2010-05-18 10:57:11 +08:00
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
// if array contains no values, no initial value to return
2010-05-27 01:37:53 +08:00
if (++i >= len) throw new TypeError();
2010-05-18 10:57:11 +08:00
} while (true);
}
for (; i < len; i++) {
2010-05-27 01:37:53 +08:00
if (i in this) {
2010-05-18 10:57:11 +08:00
rv = fun.call(null, rv, this[i], i, this);
2010-05-27 01:37:53 +08:00
}
2010-05-18 10:57:11 +08:00
}
return rv;
};
}
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (value /*, fromIndex */ ) {
var length = this.length;
var i = arguments[1] || 0;
2010-05-27 01:37:53 +08:00
if (!length) return -1;
if (i >= length) return -1;
if (i < 0) i += length;
2010-05-18 10:57:11 +08:00
for (; i < length; i++) {
2010-05-27 01:37:53 +08:00
if (!Object.prototype.hasOwnProperty.call(this, i)) { continue }
if (value === this[i]) return i;
2010-05-18 10:57:11 +08:00
}
return -1;
};
}
//
// Object
//
if (!Object.keys) {
Object.keys = function (object) {
var keys = [];
for (var name in object) {
if (Object.prototype.hasOwnProperty.call(object, name)) {
keys.push(name);
}
}
return keys;
};
}
//
// String
//
if (!String.prototype.trim) {
String.prototype.trim = function () {
2010-05-27 01:37:53 +08:00
return String(this).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
2010-05-18 10:57:11 +08:00
};
}