-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolyfill.js
56 lines (49 loc) · 1.48 KB
/
polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// eslint-disable-next-line no-extend-native
String.prototype.reverse = function () {
return this.split('').reverse().join('')
}
// eslint-disable-next-line no-extend-native
String.prototype.prepend = function (value) {
return `${value}${this}`
}
// eslint-disable-next-line no-extend-native
String.prototype.replaceWith = function (replaces) {
let replaced = this
let regex
for (const key in replaces) {
if (!replaces.hasOwnProperty(key)) {
continue
}
regex = new RegExp(key, 'g')
replaced = replaced.replace(regex, replaces[key])
}
return replaced
}
// eslint-disable-next-line no-extend-native
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1)
}
// eslint-disable-next-line no-extend-native
String.prototype.unCapitalize = function () {
return this.charAt(0).toLowerCase() + this.slice(1)
}
// eslint-disable-next-line no-extend-native
String.prototype.toCamelCase = function (first = false) {
const camelCase = this.replace(/-([a-z])/g, (group) => group[1].toUpperCase())
if (!first) {
return camelCase
}
return camelCase.capitalize()
}
// eslint-disable-next-line no-extend-native
String.prototype.toDashCase = function (first = false) {
return this.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`)
}
// eslint-disable-next-line no-extend-native
Number.prototype.pad = function (size) {
let padded = String(this)
while (padded.length < (size || 2)) {
padded = `0${padded}`
}
return padded
}