
regex parity
sakurayang opened this issue ยท 2 comments
Minecraft Version
1.20.1
KubeJS Version
2001.6.5-build.16
Rhino Version
2001.2.3-build.10
Architectury Version
9.2.14
Forge/Fabric Version
fabric 0.16.10
Describe your issue
here is my code
PlayerEvents.decorateChat(event => {
const regex = /^(\[).*?([xyz]:(-?\d*\.*\d*), ){2}[xyz]:(-?\d*\.*\d*).*?\]$/gim;
let msg = event.message.trim().toLowerCase()
if (regex.test(msg) || msg.includes('xaero-waypoint')) {
let props = { name: '', x: 0, y: 0, z: 0, dim: '', xearo_dim: '' };
if (regex.test(msg)) {
for (const ele of msg.replace('[', '').replace(']', '').split(',')) {
console.log(ele) // not work here
props[ele.trim().split(':')[0]] = ele.trim().split(':').slice(1).join(':')
}
props.dim = props.dim !== '' ? props.dim : event.player.block.dimension.toString();
props.name = props.name !== '' ? props.name : event.username;
props.xearo_dim = `Internal-dim%${props.dim.replace(':', '$')}-waypoints`;
} else {
let parse = msg.split(':')
props.name = parse[1]
props.x = parse[3]
props.y = parse[4]
props.z = parse[5]
props.xearo_dim = parse[parse.length - 1]
props.dim = parse[parse.length - 1].replace('internal-', '').replace('-waypoints', '').replace('$', ':').replace('dim%', '')
}
console.printObject(props)
//event.setMessage('some text here')
}
})
so, in normal, if I input [x:-16, y:23, z:419, dim:minecraft:overworld, name:cave]
in game, result should be {name: cave, x: -16, y: 23, z: 419, dim: minecraft:overworld, xearo_dim: Internal-dim%minecraft$overworld-waypoints}
and console should output:
[Server thread/INFO]: j.js#20: x:-16
[Server thread/INFO]: j.js#20: y:23
[Server thread/INFO]: j.js#20: z:419
[Server thread/INFO]: j.js#20: dim:minecraft:overworld
[Server thread/INFO]: j.js#20: name:cave
[Server thread/INFO]: j.js#35: === dev.latvian.mods.rhino.NativeObject ===
[Server thread/INFO]: j.js#35: = toString() =
[Server thread/INFO]: j.js#35: > {name: cave, x: -16, y: 23, z: 419, dim: minecraft:overworld, xearo_dim: Internal-dim%minecraft$overworld-waypoints}
[Server thread/INFO]: j.js#35: = hashCode() =
[Server thread/INFO]: j.js#35: > 2d1660ab
[Server thread/INFO]: j.js#35:
[Server thread/INFO]: j.js#35: === dev.latvian.mods.rhino.NativeObject ===
[Server thread/INFO]: j.js#35: = Parent class =
[Server thread/INFO]: j.js#35: > dev.latvian.mods.rhino.IdScriptableObject
[Server thread/INFO]: j.js#35: = Variables and Functions =
[Server thread/INFO]: j.js#35: > val className: String
[Server thread/INFO]: j.js#35: > function clear(): void
[Server thread/INFO]: j.js#35: > function containsKey(Object): boolean
[Server thread/INFO]: j.js#35: > function containsValue(Object): boolean
[Server thread/INFO]: j.js#35: > function createDataObject(Supplier, Context): Object
[Server thread/INFO]: j.js#35: > function createDataObjectList(Supplier, Context): List
[Server thread/INFO]: j.js#35: > function entrySet(): Set
[Server thread/INFO]: j.js#35: > function execIdCall(IdFunctionObject, Context, Scriptable, Scriptable, Object;): Object
[Server thread/INFO]: j.js#35: > function get(Object): Object
[Server thread/INFO]: j.js#35: > function isDataObjectList(): boolean
[Server thread/INFO]: j.js#35: > function keySet(): Set
[Server thread/INFO]: j.js#35: > function put(Object, Object): Object
[Server thread/INFO]: j.js#35: > function putAll(Map): void
[Server thread/INFO]: j.js#35: > function remove(Object): Object
[Server thread/INFO]: j.js#35: > function toString(): String
[Server thread/INFO]: j.js#35: > function values(): Collection
but actually, it only show text of console.printObject(props)
unless I put the for loop outside inner if condition.
and second bug here, if i put for loop inside the inner condition, the output will become
[Server thread/INFO]: example.js#5: Hello, World! (Loaded server scripts)
[Server thread/INFO]: Loaded script server_scripts:example.js in 0.0 s
[Server thread/INFO]: Loaded script server_scripts:j.js in 0.001 s
[Server thread/INFO]: Loaded 2/2 KubeJS server scripts in 0.004 s with 0 errors and 0 warnings
[Render thread/INFO]: [CHAT] Done! To reload recipes, tags, loot tables and other datapack things, run '/reload'
[Server thread/INFO]: j.js#35: === dev.latvian.mods.rhino.NativeObject ===
[Server thread/INFO]: j.js#35: = toString() =
[Server thread/INFO]: j.js#35: > {name: -16, y, x: 419, dim, y: minecraft, z: overworld, name, dim: ???sd], xearo_dim: cave]}
[Server thread/INFO]: j.js#35: = hashCode() =
[Server thread/INFO]: j.js#35: > 2d1660ab
[Server thread/INFO]: j.js#35:
[Server thread/INFO]: j.js#35: === dev.latvian.mods.rhino.NativeObject ===
[Server thread/INFO]: j.js#35: = Parent class =
[Server thread/INFO]: j.js#35: > dev.latvian.mods.rhino.IdScriptableObject
[Server thread/INFO]: j.js#35: = Variables and Functions =
[Server thread/INFO]: j.js#35: > val className: String
[Server thread/INFO]: j.js#35: > function clear(): void
[Server thread/INFO]: j.js#35: > function containsKey(Object): boolean
[Server thread/INFO]: j.js#35: > function containsValue(Object): boolean
[Server thread/INFO]: j.js#35: > function createDataObject(Supplier, Context): Object
[Server thread/INFO]: j.js#35: > function createDataObjectList(Supplier, Context): List
[Server thread/INFO]: j.js#35: > function entrySet(): Set
[Server thread/INFO]: j.js#35: > function execIdCall(IdFunctionObject, Context, Scriptable, Scriptable, Object;): Object
[Server thread/INFO]: j.js#35: > function get(Object): Object
[Server thread/INFO]: j.js#35: > function isDataObjectList(): boolean
[Server thread/INFO]: j.js#35: > function keySet(): Set
[Server thread/INFO]: j.js#35: > function put(Object, Object): Object
[Server thread/INFO]: j.js#35: > function putAll(Map): void
[Server thread/INFO]: j.js#35: > function remove(Object): Object
[Server thread/INFO]: j.js#35: > function toString(): String
[Server thread/INFO]: j.js#35: > function values(): Collection
same code with for loop, but output become: {name: -16, y, x: 419, dim, y: minecraft, z: overworld, name, dim: cave], xearo_dim: cave]}
Crash report/logs
No response
This is a lot of code, can you try to reduce it down to a minimal example of the bug?
EDIT: close, it's javascript feature: ->https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
the minimal code here, seems like the value of regex.test()
change?
const regex = /a/gim;
let msg = 'a'
if (regex.test(msg)) {
if (regex.test(msg)) {
console.log('1')
} else {
console.log('2')
}
}
... and console
[Server thread/INFO]: j.js#64: 2
if i check the value....
const regex = /a/gim;
let msg = 'a'
+ console.log(regex.test(msg))
if (regex.test(msg)) {
if (regex.test(msg)) {
console.log('1')
} else {
console.log('2')
}
}
[Server thread/INFO]: Loaded script server_scripts:example.js in 0.0 s
[Server thread/INFO]: j.js#60: true
[Server thread/INFO]: Loaded script server_scripts:j.js in 0.0 s
if i check the value in different position ....
const regex = /a/gim;
let msg = 'a'
- console.log(regex.test(msg))
if (regex.test(msg)) {
+ console.log(regex.test(msg))
if (regex.test(msg)) {
console.log('1')
} else {
console.log('2')
}
}
[Server thread/INFO]: j.js#62: false
[Server thread/INFO]: j.js#64: 1
[Server thread/INFO]: Loaded script server_scripts:j.js in 0.002 s
if i use a variable...
const regex = /a/gim;
let msg = 'a'
+ let test = regex.test(msg);
- if (test) {
- if (test) {
+ if (regex.test(msg)) {
+ if (regex.test(msg)) {
console.log('1')
} else {
console.log('2')
}
}
console:
[Server thread/INFO]: j.js#63: 1
one more thing....
const regex = /a/gim;
let msg = 'a'
for (let i = 0; i < 10; i++) {
console.log(regex.test(msg))
}
[Server thread/INFO]: j.js#63: true
[Server thread/INFO]: j.js#63: false
[Server thread/INFO]: j.js#63: true
[Server thread/INFO]: j.js#63: false
[Server thread/INFO]: j.js#63: true
[Server thread/INFO]: j.js#63: false
[Server thread/INFO]: j.js#63: true
[Server thread/INFO]: j.js#63: false
[Server thread/INFO]: j.js#63: true
[Server thread/INFO]: j.js#63: false