From 3c297bdea23d26eebe3c8db8946ca146af43221f Mon Sep 17 00:00:00 2001 From: Christoph Diehl <1614333+posidron@users.noreply.github.com> Date: Tue, 20 Mar 2018 00:55:16 +0100 Subject: [PATCH] Stick with require for overwriting String|Array prototypes --- lib/utils/index.js | 4 ++++ lib/utils/prototypes.js | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index be0f16c..c579338 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -22,6 +22,10 @@ class utils { static get script () { return require('./script') } + + static get prototypes () { + return require('./prototypes') + } } module.exports = utils diff --git a/lib/utils/prototypes.js b/lib/utils/prototypes.js index 62e6aec..e50d95c 100644 --- a/lib/utils/prototypes.js +++ b/lib/utils/prototypes.js @@ -3,7 +3,7 @@ * 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/. */ -Object.defineProperty(global.String.prototype, 'fromCodePoint', function () { +Object.defineProperty(String.prototype, 'fromCodePoint', function () { let chars = [] let point, offset, units, i for (i = 0; i < arguments.length; ++i) { @@ -19,33 +19,33 @@ Object.defineProperty(global.String.prototype, 'fromCodePoint', function () { return chars.join('') }) -Object.defineProperty(global.String.prototype, 'endsWith', function (str) { +Object.defineProperty(String.prototype, 'endsWith', function (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 }) -Object.defineProperty(global.String.prototype, 'trim', function () { +Object.defineProperty(String.prototype, 'trim', function () { 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) }) -Object.defineProperty(global.Array.prototype, 'has', function (v) { +Object.defineProperty(Array.prototype, 'has', function (v) { 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++) { fn(array[i]) } }) -Object.defineProperty(global.Array.prototype, 'map', function (fn, array) { +Object.defineProperty(Array.prototype, 'map', function (fn, array) { let result = [] Array.forEach(array, function (element) { result.push(fn(element)) @@ -53,7 +53,7 @@ Object.defineProperty(global.Array.prototype, 'map', function (fn, array) { return result }) -Object.defineProperty(global.Array.prototype, 'extend', { +Object.defineProperty(Array.prototype, 'extend', { value: function (obj) { if (Array.isArray(obj)) { 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) { return (obj !== null && typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]')