-
-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy path_prototype.js
More file actions
35 lines (30 loc) · 903 Bytes
/
_prototype.js
File metadata and controls
35 lines (30 loc) · 903 Bytes
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
Number.prototype.toVariable = function() {
return ((this + 23) % 26 + 10).toString(36)
}
String.prototype.sanitize = function() {
return this
.split(/ |\//)
.filter(string => string)
.join('_')
.replace(/[\W\/]+/g, '')
.toLowerCase()
}
String.prototype.replaceWithVariables = function(match, curlyBrackets = false) {
let i = 0
return this
.replace(match, function() {
const letter = i.toVariable()
i++
return curlyBrackets ? `{{ ${letter} }}` : letter
})
}
Array.prototype.makeCommaSeparatedString = function(useOxfordComma) {
const listStart = this.slice(0, -1).join(', ')
const listEnd = this.slice(-1)
const conjunction = this.length <= 1
? ''
: useOxfordComma && this.length > 2
? '{{ common.delimiters.and_with_oxford_comma }}'
: '{{ common.delimiters.and }}'
return [listStart, listEnd].join(conjunction)
}