Skip to content

Instantly share code, notes, and snippets.

@intrnl
Created September 27, 2023 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrnl/a9b51b4a495dc77df383f6ecfa890e9a to your computer and use it in GitHub Desktop.
Save intrnl/a9b51b4a495dc77df383f6ecfa890e9a to your computer and use it in GitHub Desktop.
const FOLLOWING = 4;
const CONTAINED_BY = 16;
const PRECEDING = 2;
const CONTAINS = 8;
export const collateNode = (a: Node, b: Node) => {
const position = a.compareDocumentPosition(b);
if (position & (FOLLOWING | CONTAINED_BY)) {
return -1;
}
if (position & (PRECEDING | CONTAINS)) {
return 1;
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment