Fix test titles POC

by Csaba Hellinger

JavaScript

import nlp from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';              // → "Isn't null"

 
console.log( nlp("should return 3").terms().out('tags') );
console.log('---')

// helper – "return" → "returns", "fly" → "flies", …
const to3sg = (verb) => {
  const conj = nlp(verb).verbs().conjugate()[0];
  return conj?.PresentTense || verb;
};

export function fixTitle(title) {
  const doc = nlp(title);
  doc.contractions().expand();                      // shouldn't → should not

  // replace every "should [not] VERB" chunk
  doc.replace('should (#Negative)? #Verb', (m) => {
    const neg      = m.has('#Negative');

    // real verb = first verb after "should" (skip the modal itself)
    const verbTok  = m.match('#Verb').not('should').first();
    if (!verbTok.found) return m.out('text');       // nothing to fix

    const verb     = verbTok.out('root');           // base form
    const repl     = neg
      ? verb === 'be'
        ? "isn't"                                   // special-case be
        : `doesn't ${verb}`
      : to3sg(verb);

    return repl;
  }, false);   // false ⇒ delete the matched words, don’t leave them behind

  const fixed = doc.text().trim();
  console.log(title + '\n' + fixed + '\n\n');
}

/* quick sanity-check ------------------------------------ */
fixTitle('should return 3');
fixTitle("shouldn't throw");
fixTitle("should not render anything");
fixTitle('should return 3 and shouldn’t throw');
fixTitle('should call function and should not change store');
fixTitle('should support foo if the user is logged in');
fixTitle("shouldn't be null");
fixTitle('should have token');