-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.js
58 lines (42 loc) · 1.34 KB
/
prototype.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
57
58
var stringSimilarity = require('string-similarity')
// String
String.prototype.emojiID = function() {
return this.replace(/<|>/gi, '').split(':')[2]
}
// Array
Array.prototype.random = function() {
return this[Math.floor(Math.random() * this.length)]
}
Array.prototype.search = function(text) {
var matches = stringSimilarity.findBestMatch(text, this)
var getSimilar = []
for (var i in matches.ratings) {
if (matches.ratings[i].rating > 0.2) {
getSimilar.push({
rating: matches.ratings[i].rating,
element: matches.ratings[i].target
})
}
}
getSimilar.sort((a, b) => b.rating - a.rating)
return getSimilar.splice(0, 1)
}
Array.prototype.chunkArray = function(size) {
var arrayLength = this.length
var tempArray = []
for (let index = 0; index < arrayLength; index += size) {
let myChunk = this.slice(index, index + size)
// Do something if you want with the group
tempArray.push(myChunk)
}
return tempArray
}
// Format Numbers
Number.prototype.formatIt = function(){
if(this === 0) return 0
var reg = /(^[+-]?\d+)(\d{3})/
var n = (this.toString())
while (reg.test(n)) n = n.replace(reg, '$1' + ',' + '$2')
return n
}
String.prototype.formatIt = function(){ return Number(this).formatIt() }