octo-deno/lib/utils/prototypes.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

/* eslint no-extend-native: ["error", { "exceptions": ["String", "Array"] }] */
2017-04-22 22:49:49 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
const utils = require('../utils')
class prototypes extends utils {
static enable () {
if (!String.prototype.hasOwnProperty('insert')) {
Object.defineProperty(String.prototype, 'insert', {
value: function (data, i) {
return this.slice(0, i) + data + this.slice(i, this.length)
}
})
}
2017-04-22 22:49:49 +00:00
if (!Array.prototype.hasOwnProperty('has')) {
Object.defineProperty(Array.prototype, 'has', {
value: function (v) {
return this.indexOf(v) !== -1
}
})
}
if (!Array.prototype.hasOwnProperty('extend')) {
Object.defineProperty(Array.prototype, 'extend', {
value: function (obj) {
if (Array.isArray(obj)) {
obj.forEach(function (v) {
if (typeof v !== 'undefined') {
this.push(v)
}
}, this)
} else {
this.push(obj)
}
}
})
}
2017-06-08 21:56:46 +00:00
if (!Object.hasOwnProperty('isObject')) {
Object.defineProperty(Object, 'isObject', {
value: function (obj) {
return (obj !== null && typeof obj === 'object' &&
Object.prototype.toString.call(obj) === '[object Object]')
2017-06-13 17:19:09 +00:00
}
})
2017-06-13 17:19:09 +00:00
}
}
}
2017-06-13 17:19:09 +00:00
module.exports = prototypes