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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// TODO: https://github.com/MozillaSecurity/octo/issues/7
if (!String.fromCodePoint) {
String.fromCodePoint = function fromCodePoint () {
var chars = []
var point, offset, units, i
for (i = 0; i < arguments.length; ++i) {
point = arguments[i]
offset = point - 0x10000
units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point]
chars.push(String.fromCharCode.apply(null, units))
}
return chars.join('')
Object.defineProperty(String.prototype, 'fromCodePoint', function () {
let chars = []
let point, offset, units, i
for (i = 0; i < arguments.length; ++i) {
point = arguments[i]
offset = point - 0x10000
units = point > 0xFFFF
? [
0xD800 + (offset >> 10),
0xDC00 + (offset & 0x3FF)]
: [point]
chars.push(String.fromCharCode.apply(null, units))
}
}
return chars.join('')
})
if (!String.prototype.endsWith) {
String.prototype.endsWith = function (str) { return (this.match(str + '$') === str) }
}
Object.defineProperty(String.prototype, 'endsWith', function (str) {
return this.match(str + '$') === str
})
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (str) {
return (this.match('^' + str) === str)
Object.defineProperty(String.prototype, 'startsWith', function (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) {
String.prototype.trim = function () {
return (this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, ''))
}
}
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
}
}
Object.defineProperty(Array.prototype, 'map', function (fn, array) {
let result = []
Array.forEach(array, function (element) {
result.push(fn(element))
})
return result
})