Repository: apple/pkl
File: pkl-doc/src/main/resources/org/pkl/doc/scripts/search-worker.js (JavaScript)

Code before:

  if (length > 1) {
    let class1 = toCharClass(characters[length - 1]);
    let class2;
    let wordStart = -1;
    for (let idx = length - 1; idx >= 1; idx -= 1) {
      class2 = class1;
      class1 = toCharClass(characters[idx - 1]);
      const diff = class1 - class2;
      // transitions other than uppercase -> other
      if (diff !== 0 && diff !== 3) wordStart = idx;
      result[idx] = wordStart;
      // uppercase -> other
      if (diff === 3) wordStart = idx - 1;
    }
  }

  // first character is always a word start
  result[0] = 0;

  return result;
}

const regexIsUppercase = /\p{Lu}/u

const regexIsNumericCharacter = /\p{N}/u

// Partitions characters into uppercase, digit, dot, and other.
function toCharClass(ch) {
  return regexIsUppercase.test(ch) ? 3 : regexIsNumericCharacter.test(ch) ? 2 : ch === '.' ? 1 : 0;
}

// Tests if `queryCps` matches any of `member`'s names.
// If so, records information about the match in `member`.
// Preconditions:
// queryCps.length > 0
function isMatch(queryCps, member) {
  const queryLength = queryCps.length;
  let nameIdx = 0;

  for (const name of member.names) {
    const nameCps = name.normalizedCps;
    const nameLength = nameCps.length;
    const wordStarts = name.wordStarts;
    const maxStartIdx = nameLength - queryLength;

    for (let startIdx = 0; startIdx <= maxStartIdx; startIdx += 1) {
      const matchLength = computeMatchFrom(queryCps, nameCps, wordStarts, startIdx);
      if (matchLength > 0) {
        member.matchNameIdx = nameIdx;
        member.matchStartIdx = startIdx;

Review comment (suggestion):
Declaring these outside of `toCharClass` was causing problems because `function toCharClass` gets hoisted to the top of the file, while these consts do not. So, the consts aren't actually available to the body of `function toCharClass`.

JS is silly sometimes..

Code after:
  const result = length <= 128 ? new Int8Array(length) : new Int16Array(length);

  if (length > 1) {
    let class1 = toCharClass(characters[length - 1]);
    let class2;
    let wordStart = -1;
    for (let idx = length - 1; idx >= 1; idx -= 1) {
      class2 = class1;
      class1 = toCharClass(characters[idx - 1]);
      const diff = class1 - class2;
      // transitions other than uppercase -> other
      if (diff !== 0 && diff !== 3) wordStart = idx;
      result[idx] = wordStart;
      // uppercase -> other
      if (diff === 3) wordStart = idx - 1;
    }
  }

  // first character is always a word start
  result[0] = 0;

  return result;
}


// Partitions characters into uppercase, digit, dot, and other.
function toCharClass(ch) {
  const regexIsUppercase = /\p{Lu}/u
  const regexIsNumericCharacter = /\p{N}/u
  return regexIsUppercase.test(ch) ? 3 : regexIsNumericCharacter.test(ch) ? 2 : ch === '.' ? 1 : 0;
}

// Tests if `queryCps` matches any of `member`'s names.
// If so, records information about the match in `member`.
// Preconditions:
// queryCps.length > 0
function isMatch(queryCps, member) {
  const queryLength = queryCps.length;
  let nameIdx = 0;

  for (const name of member.names) {
    const nameCps = name.normalizedCps;
    const nameLength = nameCps.length;
    const wordStarts = name.wordStarts;
    const maxStartIdx = nameLength - queryLength;

    for (let startIdx = 0; startIdx <= maxStartIdx; startIdx += 1) {
      const matchLength = computeMatchFrom(queryCps, nameCps, wordStarts, startIdx);
      if (matchLength > 0) {
        member.matchNameIdx = nameIdx;
        member.matchStartIdx = startIdx;