-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(marshal)!: compare strings by codepoint
- Loading branch information
Showing
4 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
import { compareRank } from '../src/rankOrder.js'; | ||
|
||
test('unicode code point order', t => { | ||
// Test case from | ||
// https://icu-project.org/docs/papers/utf16_code_point_order.html | ||
const str0 = '\u{ff61}'; | ||
const str3 = '\u{d800}\u{dc02}'; | ||
|
||
// str1 and str2 become impossible examples once we prohibit | ||
// non - well - formed strings. | ||
// See https://github.com/endojs/endo/pull/2002 | ||
const str1 = '\u{d800}X'; | ||
const str2 = '\u{d800}\u{ff61}'; | ||
|
||
// harden to ensure it is not sorted in place, just for sanity | ||
const strs = harden([str0, str1, str2, str3]); | ||
|
||
/** | ||
* @param {string} left | ||
* @param {string} right | ||
* @returns {import('../src/types.js').RankComparison} | ||
*/ | ||
const nativeComp = (left, right) => | ||
// eslint-disable-next-line no-nested-ternary | ||
left < right ? -1 : left > right ? 1 : 0; | ||
|
||
const nativeSorted = strs.toSorted(nativeComp); | ||
|
||
t.deepEqual(nativeSorted, [str1, str3, str2, str0]); | ||
|
||
const rankSorted = strs.toSorted(compareRank); | ||
|
||
t.deepEqual(rankSorted, [str1, str2, str0, str3]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// modeled on test-string-rank-order.js | ||
|
||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
import { compareKeys } from '../src/keys/compareKeys.js'; | ||
|
||
test('unicode code point order', t => { | ||
// Test case from | ||
// https://icu-project.org/docs/papers/utf16_code_point_order.html | ||
const str0 = '\u{ff61}'; | ||
const str3 = '\u{d800}\u{dc02}'; | ||
|
||
// str1 and str2 become impossible examples once we prohibit | ||
// non - well - formed strings. | ||
// See https://github.com/endojs/endo/pull/2002 | ||
const str1 = '\u{d800}X'; | ||
const str2 = '\u{d800}\u{ff61}'; | ||
|
||
// harden to ensure it is not sorted in place, just for sanity | ||
const strs = harden([str0, str1, str2, str3]); | ||
|
||
/** | ||
* @param {string} left | ||
* @param {string} right | ||
* @returns {import('../src/types.js').KeyComparison} | ||
*/ | ||
const nativeComp = (left, right) => | ||
// eslint-disable-next-line no-nested-ternary | ||
left < right ? -1 : left > right ? 1 : 0; | ||
|
||
const nativeSorted = strs.toSorted(nativeComp); | ||
|
||
t.deepEqual(nativeSorted, [str1, str3, str2, str0]); | ||
|
||
const keySorted = strs.toSorted(compareKeys); | ||
|
||
t.deepEqual(keySorted, [str1, str2, str0, str3]); | ||
}); |