2017-04-26 22:01:48 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(String.prototype, 'fromCodePoint', function () {
|
2017-04-26 22:30:33 +00:00
|
|
|
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))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-26 22:30:33 +00:00
|
|
|
return chars.join('')
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(String.prototype, 'endsWith', function (str) {
|
2017-04-26 22:30:33 +00:00
|
|
|
return this.match(str + '$') === str
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(String.prototype, 'startsWith', function (str) {
|
2017-04-26 22:30:33 +00:00
|
|
|
return this.match('^' + str) === str
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(String.prototype, 'trim', function () {
|
2017-04-26 22:30:33 +00:00
|
|
|
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(String.prototype, 'insert', function (data, idx) {
|
2017-04-26 22:30:33 +00:00
|
|
|
return this.slice(0, idx) + data + this.slice(idx, this.length)
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(Array.prototype, 'has', function (v) {
|
2017-04-26 22:30:33 +00:00
|
|
|
return this.indexOf(v) !== -1
|
|
|
|
})
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(Array.prototype, 'forEach', function (array, fn) {
|
2017-04-26 22:30:33 +00:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
fn(array[i])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-26 22:30:33 +00:00
|
|
|
})
|
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(Array.prototype, 'map', function (fn, array) {
|
2017-04-26 22:30:33 +00:00
|
|
|
let result = []
|
|
|
|
Array.forEach(array, function (element) {
|
|
|
|
result.push(fn(element))
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
})
|
2017-06-08 21:56:46 +00:00
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(Array.prototype, 'extend', {
|
2017-06-13 17:19:09 +00:00
|
|
|
value: function (obj) {
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
obj.forEach(function (v) {
|
|
|
|
if (typeof v !== 'undefined') {
|
|
|
|
this.push(v)
|
|
|
|
}
|
|
|
|
}, this)
|
|
|
|
} else {
|
|
|
|
this.push(obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-03-19 23:55:16 +00:00
|
|
|
Object.defineProperty(Object, 'isObject', {
|
2017-06-08 21:56:46 +00:00
|
|
|
value: function (obj) {
|
2017-07-06 14:18:45 +00:00
|
|
|
return (obj !== null && typeof obj === 'object' &&
|
2017-06-08 22:15:31 +00:00
|
|
|
Object.prototype.toString.call(obj) === '[object Object]')
|
2017-06-08 21:56:46 +00:00
|
|
|
}
|
|
|
|
})
|