String.prototype.localeCompare() broken?

Trying to use localeCompare to check if two strings are considered the same with different sensitivities, it seems that the implementation in DOpus is... somewhat broken; it seems it always just returns a===b, instead of actually checking.
I tried switching the usage option between search and sort, and the sensitivity between accent and base.
Running

var a = "a";
var b = "aAäÄ";
for(var i = 0; i<=3; i++){
	log(a + " vs " + b[i] + ", search, accent: " + a.localeCompare(b[i], undefined, { sensitivity: "accent", usage: "search" }));
	log(a + " vs " + b[i] + ", sort, accent: " + a.localeCompare(b[i], undefined, { sensitivity: "accent", usage: "sort" }));
	log(a + " vs " + b[i] + ", search, base: " + a.localeCompare(b[i], undefined, { sensitivity: "base", usage: "search" }));
	log(a + " vs " + b[i] + ", sort, base: " + a.localeCompare(b[i], undefined, { sensitivity: "base", usage: "sort" }));
}

, I always get non-zero unless the chars are actually the same, no matter the sensitivity.
I compared it to the same code run in Chrome, which produces the expected values:
2021-01-22 17-18-13

JScript and JavaScript are not quite the same. It's basically an old version of JavaScript, so it's missing some of the newer things. This is probably one of them.

(Removed the bug-report tag because even if it was a bug, we didn't make JScript and have no control over what its string object implements. JScript is part of Windows.)

That said, we have our own code for doing something similar, so we might add a helper function for it. Not sure when, but it's been added to the to-do list.

I thought that might be the case, but because the method ran without otherwise complaining, I thought it must've been the same/should've worked :sweat_smile:
Not so...

Neato! Looking forward to it :slight_smile:

In the meantime, I'll be very glad I actually only need case insensitive compare (accent sensitivity was just an academic interest), which I'll fudge with .toLowerCase()ing the candidates, as it looks like the JScript .localeCompare() is so much less capable/flexible than the JavaScript version.
(Well, to be fair, it does do a (system default) locale-based compare. The JavaScript version can just do so much more, including choosing which locale to refer to, including no specific locale at all)

And documentation on JScript is weirdly sparse O.o
Microsoft only seems to provide indirect API information about its implementation where every page is littered with notices that

This API supports the product infrastructure and is not intended to be used directly from your code.

What ever that means...
Others are broken (JScript Language ReferenceObjects -> String Object just displays the Table Of Contents again)
Others are unwieldy (no search, no glossary, every page is an ad for their editor. Still the best I found.)

It's weird that the best documentation I came across was an aside on a website of some company who made a VBscript editor. What the hell.

Yeah, JScript documentation is almost non-existent, and search engines always return results for JavaScript instead, which is often useful but also sometimes misleading. MS have abandoned JScript now as well, apparently thinking that PowerShell has replaced the need for it (it hasn't; they're two very different things).

indeed!

I've been programming a lot JScript lately (new project incoming) and had the same problem with documentation.
What I've found is JScript is simply a reverse-engineered ECMAScript v3 according to multiple sources. And I can confirm that it is mostly ES3 indeed. Funnily JScript has JSON.parse & JSON.stringify, but other than that it's just ES3, which came out 1999. It lacks quite a lot of nice features of say ES5, but many missing methods, like .keys(), .trim(), .toISOString(), etc. can be easily added.
Here's something from my own source code; I had put the comment before finding out JScript is ES3.

	String.prototype.trim = function () {
		return this.replace(/^\s+|\s+$/g, ''); // not even trim() JScript??
	}

EDIT: Here's one quite an authoritative source for my info; Resig is the creator of jQuery:
https://johnresig.com/blog/versions-of-javascript/

2 Likes