Skip to content

Commit 4bcdf78

Browse files
authored
Regular Expressions: i might be stupid (#499)
* Regular Expressions: i might be stupid * ok ait turns out you can spread iterators into arrays * regex converter over each item
1 parent 1c98b84 commit 4bcdf78

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

static/extensions/DogeisCut/dogeiscutRegularExpressions.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,7 @@
207207
this.regex.lastIndex = Cast.toNumber(newLastIndex)
208208
}
209209

210-
exec(string) {
211-
// returns an array or null. need arrays ext
212-
// also this array aparently has additional properties.. i love js
213-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
214-
let baseArray = this.regex.exec(string)
210+
static convertEvilArrayIHateToObject(baseArray) {
215211
if (baseArray) {
216212
let newObj = Object.create(null);
217213
newObj.array = vm.jwArray.Type.toArray([...baseArray])
@@ -229,7 +225,15 @@
229225
}
230226
return vm.dogeiscutObject.Type.toObject(newObj)
231227
}
232-
return "" //returning an empty object felt weird
228+
return ""
229+
}
230+
231+
exec(string) {
232+
// returns an array or null. need arrays ext
233+
// also this array aparently has additional properties.. i love js
234+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
235+
let baseArray = this.regex.exec(string)
236+
return RegularExpressionType.convertEvilArrayIHateToObject(baseArray)
233237
}
234238
test(string) {
235239
return Cast.toBoolean(this.regex.test(string))
@@ -553,14 +557,15 @@
553557
}
554558

555559
matchAll({ REGEX, STRING }) {
560+
// ok so turns out match all returns an iterator and we kinda sorta dont have an extension to handle that so im turning it into an array
556561
REGEX = RegularExpressionType.toRegularExpression(REGEX)
557562
STRING = Cast.toString(STRING)
558563
try {
559-
return vm.jwArray.Type.toArray(STRING.matchAll(REGEX.regex))
564+
return vm.jwArray.Type.toArray([...STRING.matchAll(REGEX.regex)].map((x) => RegularExpressionType.convertEvilArrayIHateToObject(x)))
560565
} catch {
561566
return ""
562567
}
563-
}
568+
}
564569

565570
sourceOf({ REGEX }) {
566571
REGEX = RegularExpressionType.toRegularExpression(REGEX)

0 commit comments

Comments
 (0)