Stick with require for overwriting String|Array prototypes

This commit is contained in:
Christoph Diehl 2018-03-20 00:55:16 +01:00
parent 94f85030e4
commit 3c297bdea2
No known key found for this signature in database
GPG Key ID: 799CE5B68FEF404A
2 changed files with 14 additions and 10 deletions

View File

@ -22,6 +22,10 @@ class utils {
static get script () { static get script () {
return require('./script') return require('./script')
} }
static get prototypes () {
return require('./prototypes')
}
} }
module.exports = utils module.exports = utils

View File

@ -3,7 +3,7 @@
* 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/. */
Object.defineProperty(global.String.prototype, 'fromCodePoint', function () { Object.defineProperty(String.prototype, 'fromCodePoint', function () {
let chars = [] let chars = []
let point, offset, units, i let point, offset, units, i
for (i = 0; i < arguments.length; ++i) { for (i = 0; i < arguments.length; ++i) {
@ -19,33 +19,33 @@ Object.defineProperty(global.String.prototype, 'fromCodePoint', function () {
return chars.join('') return chars.join('')
}) })
Object.defineProperty(global.String.prototype, 'endsWith', function (str) { Object.defineProperty(String.prototype, 'endsWith', function (str) {
return this.match(str + '$') === str return this.match(str + '$') === str
}) })
Object.defineProperty(global.String.prototype, 'startsWith', function (str) { Object.defineProperty(String.prototype, 'startsWith', function (str) {
return this.match('^' + str) === str return this.match('^' + str) === str
}) })
Object.defineProperty(global.String.prototype, 'trim', function () { Object.defineProperty(String.prototype, 'trim', function () {
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '') return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')
}) })
Object.defineProperty(global.String.prototype, 'insert', function (data, idx) { Object.defineProperty(String.prototype, 'insert', function (data, idx) {
return this.slice(0, idx) + data + this.slice(idx, this.length) return this.slice(0, idx) + data + this.slice(idx, this.length)
}) })
Object.defineProperty(global.Array.prototype, 'has', function (v) { Object.defineProperty(Array.prototype, 'has', function (v) {
return this.indexOf(v) !== -1 return this.indexOf(v) !== -1
}) })
Object.defineProperty(global.Array.prototype, 'forEach', function (array, fn) { Object.defineProperty(Array.prototype, 'forEach', function (array, fn) {
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
fn(array[i]) fn(array[i])
} }
}) })
Object.defineProperty(global.Array.prototype, 'map', function (fn, array) { Object.defineProperty(Array.prototype, 'map', function (fn, array) {
let result = [] let result = []
Array.forEach(array, function (element) { Array.forEach(array, function (element) {
result.push(fn(element)) result.push(fn(element))
@ -53,7 +53,7 @@ Object.defineProperty(global.Array.prototype, 'map', function (fn, array) {
return result return result
}) })
Object.defineProperty(global.Array.prototype, 'extend', { Object.defineProperty(Array.prototype, 'extend', {
value: function (obj) { value: function (obj) {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
obj.forEach(function (v) { obj.forEach(function (v) {
@ -67,7 +67,7 @@ Object.defineProperty(global.Array.prototype, 'extend', {
} }
}) })
Object.defineProperty(global.Object, 'isObject', { Object.defineProperty(Object, 'isObject', {
value: function (obj) { value: function (obj) {
return (obj !== null && typeof obj === 'object' && return (obj !== null && typeof obj === 'object' &&
Object.prototype.toString.call(obj) === '[object Object]') Object.prototype.toString.call(obj) === '[object Object]')