Update prototypes to use defineProperty

This commit is contained in:
Christoph Diehl 2017-04-27 01:30:33 +03:00
parent 0302a84fa6
commit 85dc6b9bd8
1 changed files with 44 additions and 56 deletions

View File

@ -3,64 +3,52 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// TODO: https://github.com/MozillaSecurity/octo/issues/7 Object.defineProperty(String.prototype, 'fromCodePoint', function () {
let chars = []
if (!String.fromCodePoint) { let point, offset, units, i
String.fromCodePoint = function fromCodePoint () { for (i = 0; i < arguments.length; ++i) {
var chars = [] point = arguments[i]
var point, offset, units, i offset = point - 0x10000
for (i = 0; i < arguments.length; ++i) { units = point > 0xFFFF
point = arguments[i] ? [
offset = point - 0x10000 0xD800 + (offset >> 10),
units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point] 0xDC00 + (offset & 0x3FF)]
chars.push(String.fromCharCode.apply(null, units)) : [point]
} chars.push(String.fromCharCode.apply(null, units))
return chars.join('')
} }
} return chars.join('')
})
if (!String.prototype.endsWith) { Object.defineProperty(String.prototype, 'endsWith', function (str) {
String.prototype.endsWith = function (str) { return (this.match(str + '$') === str) } return this.match(str + '$') === str
} })
if (!String.prototype.startsWith) { Object.defineProperty(String.prototype, 'startsWith', function (str) {
String.prototype.startsWith = function (str) { return this.match('^' + str) === str
return (this.match('^' + str) === str) })
Object.defineProperty(String.prototype, 'trim', function () {
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')
})
Object.defineProperty(String.prototype, 'insert', function (data, idx) {
return this.slice(0, idx) + data + this.slice(idx, this.length)
})
Object.defineProperty(Array.prototype, 'has', function (v) {
return this.indexOf(v) !== -1
})
Object.defineProperty(Array.prototype, 'forEach', function (array, fn) {
for (let i = 0; i < array.length; i++) {
fn(array[i])
} }
} })
if (!String.prototype.trim) { Object.defineProperty(Array.prototype, 'map', function (fn, array) {
String.prototype.trim = function () { let result = []
return (this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')) Array.forEach(array, function (element) {
} result.push(fn(element))
} })
return result
if (!String.prototype.insert) { })
String.prototype.insert = function (data, idx) {
return this.slice(0, idx) + data + this.slice(idx, this.length)
}
}
if (!Array.prototype.has) {
Array.prototype.has = function (v) {
return this.indexOf(v) !== -1
}
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (array, fn) {
for (var i = 0; i < array.length; i++) {
fn(array[i])
}
}
}
if (!Array.prototype.map) {
Array.prototype.map = function (fn, array) {
var result = []
Array.forEach(array, function (element) {
result.push(fn(element))
})
return result
}
}