Use global namespace for overwriting String|Array prototypes

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

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(String.prototype, 'fromCodePoint', function () { Object.defineProperty(global.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(String.prototype, 'fromCodePoint', function () {
return chars.join('') return chars.join('')
}) })
Object.defineProperty(String.prototype, 'endsWith', function (str) { Object.defineProperty(global.String.prototype, 'endsWith', function (str) {
return this.match(str + '$') === str return this.match(str + '$') === str
}) })
Object.defineProperty(String.prototype, 'startsWith', function (str) { Object.defineProperty(global.String.prototype, 'startsWith', function (str) {
return this.match('^' + str) === str return this.match('^' + str) === str
}) })
Object.defineProperty(String.prototype, 'trim', function () { Object.defineProperty(global.String.prototype, 'trim', function () {
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '') return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')
}) })
Object.defineProperty(String.prototype, 'insert', function (data, idx) { Object.defineProperty(global.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(Array.prototype, 'has', function (v) { Object.defineProperty(global.Array.prototype, 'has', function (v) {
return this.indexOf(v) !== -1 return this.indexOf(v) !== -1
}) })
Object.defineProperty(Array.prototype, 'forEach', function (array, fn) { Object.defineProperty(global.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(Array.prototype, 'map', function (fn, array) { Object.defineProperty(global.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(Array.prototype, 'map', function (fn, array) {
return result return result
}) })
Object.defineProperty(Array.prototype, 'extend', { Object.defineProperty(global.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(Array.prototype, 'extend', {
} }
}) })
Object.defineProperty(Object, 'isObject', { Object.defineProperty(global.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]')

View file

@ -1,6 +1,6 @@
{ {
"name": "@mozillasecurity/octo", "name": "@mozillasecurity/octo",
"version": "1.0.2", "version": "1.0.3",
"description": "", "description": "",
"keywords": [ "keywords": [
"fuzzing", "fuzzing",
@ -34,9 +34,11 @@
"tests/**", "tests/**",
"dist/" "dist/"
], ],
"envs": [ "envs": {
"browser" "browser": true,
] "node": true,
"es6": true
}
}, },
"devDependencies": { "devDependencies": {
"cross-env": "^5.1.3", "cross-env": "^5.1.3",
@ -46,7 +48,6 @@
"grunt-standard": "*", "grunt-standard": "*",
"husky": "^0.14.3", "husky": "^0.14.3",
"jshint": "^2.9.5", "jshint": "^2.9.5",
"jshint-loader": "^0.8.4",
"karma": "*", "karma": "*",
"karma-chrome-launcher": "*", "karma-chrome-launcher": "*",
"karma-coverage": "*", "karma-coverage": "*",