-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
javaHelpers.js
102 lines (85 loc) · 2.38 KB
/
javaHelpers.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// String functions
// For velocity templates to access java functions, to mimick AWS
function javaContains(value) {
return this.includes(value)
}
function javaEquals(anObject) {
return this.toString() === anObject.toString()
}
function javaEqualsIgnoreCase(anotherString) {
return anotherString === null
? false
: this === anotherString ||
this.toLowerCase() === anotherString.toLowerCase()
}
function javaMatches(value) {
return this.match(new RegExp(value, 'm'))
}
function javaReplaceAll(oldValue, newValue) {
return this.replaceAll(new RegExp(oldValue, 'gm'), newValue)
}
function javaReplaceFirst(oldValue, newValue) {
return this.replace(new RegExp(oldValue, 'm'), newValue)
}
// method has 2 function signatures:
// regionMatches(toffset: number, other: string, ooffset: number, len: number): boolean
// regionMatches(ignoreCase: boolean, toffset: number, other: string, ooffset: number, len: number): boolean
function javaRegionMatches(...args) {
let ignoreCase
let toffset
let other
let ooffset
let len
if (args.length === 4) {
;[toffset, other, ooffset, len] = args
ignoreCase = false
} else {
;[ignoreCase, toffset, other, ooffset, len] = args
}
// Note: toffset, ooffset, or len might be near -1>>>1.
if (
ooffset < 0 ||
toffset < 0 ||
toffset > this.length - len ||
ooffset > other.length - len
) {
return false
}
let s1 = this.substring(toffset, toffset + len)
let s2 = other.substring(ooffset, ooffset + len)
if (ignoreCase) {
s1 = s1.toLowerCase()
s2 = s2.toLowerCase()
}
return s1 === s2
}
const {
prototype,
prototype: {
contains,
equals,
equalsIgnoreCase,
matches,
regionMatches,
replaceAll,
replaceFirst,
},
} = String
export default function runInPollutedScope(runScope) {
prototype.contains = javaContains
prototype.equals = javaEquals
prototype.equalsIgnoreCase = javaEqualsIgnoreCase
prototype.matches = javaMatches
prototype.regionMatches = javaRegionMatches
prototype.replaceAll = javaReplaceAll
prototype.replaceFirst = javaReplaceFirst
const result = runScope()
prototype.contains = contains
prototype.equals = equals
prototype.equalsIgnoreCase = equalsIgnoreCase
prototype.matches = matches
prototype.regionMatches = regionMatches
prototype.replaceAll = replaceAll
prototype.replaceFirst = replaceFirst
return result
}