{"size": 2838, "ext": "js", "max_stars_repo_path": "exercises/practice/acronym/acronym.spec.js", "max_stars_repo_name": "exercism-bot/wasm", "max_stars_repo_stars_event_min_datetime": "2022-01-27T00:37:55.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-08T19:27:27.000Z", "max_issues_repo_path": "exercises/practice/acronym/acronym.spec.js", "max_issues_repo_name": "exercism-bot/wasm", "max_issues_repo_issues_event_min_datetime": "2022-01-26T23:08:34.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-15T12:55:28.000Z", "max_forks_repo_path": "exercises/practice/acronym/acronym.spec.js", "max_forks_repo_name": "exercism-bot/wasm", "max_forks_repo_forks_event_min_datetime": "2022-01-26T23:08:54.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-15T12:06:26.000Z"}
{"max_stars_count": 8, "max_issues_count": 20, "max_forks_count": 8, "avg_line_length": 27.2884615385, "max_line_length": 84, "alphanum_fraction": 0.6550387597}
import { compileWat, WasmRunner } from "@exercism/wasm-lib";

let wasmModule;
let currentInstance;

beforeAll(async () => {
  try {
    const watPath = new URL("./acronym.wat", import.meta.url);
    const { buffer } = await compileWat(watPath);
    wasmModule = await WebAssembly.compile(buffer);
  } catch (err) {
    console.log(`Error compiling *.wat: ${err}`);
    process.exit(1);
  }
});

function parse(input) {
  const inputBufferOffset = 64;
  const inputBufferCapacity = 128;

  const inputLengthEncoded = new TextEncoder().encode(input).length;
  if (inputLengthEncoded > inputBufferCapacity) {
    throw new Error(
      `String is too large for buffer of size ${inputBufferCapacity} bytes`
    );
  }

  currentInstance.set_mem_as_utf8(inputBufferOffset, inputLengthEncoded, input);

  // Pass offset and length to WebAssembly function
  const [outputOffset, outputLength] = currentInstance.exports.parse(
    inputBufferOffset,
    input.length
  );

  // Decode JS string from returned offset and length
  return currentInstance.get_mem_as_utf8(outputOffset, outputLength);
}

describe("Acronyms are produced from", () => {
  beforeEach(async () => {
    currentInstance = null;
    if (!wasmModule) {
      return Promise.reject();
    }
    try {
      currentInstance = await new WasmRunner(wasmModule);
      return Promise.resolve();
    } catch (err) {
      console.log(`Error instantiating WebAssembly module: ${err}`);
      return Promise.reject();
    }
  });

  // basic
  test("title cased phrases", () => {
    expect(parse("Portable Network Graphics")).toEqual("PNG");
  });

  // lowercase words
  xtest("other title cased phrases", () => {
    expect(parse("Ruby on Rails")).toEqual("ROR");
  });

  // punctuation
  xtest("phrases with punctuation", () => {
    expect(parse("First In, First Out")).toEqual("FIFO");
  });

  // all caps word
  xtest("phrases with all uppercase words", () => {
    expect(parse("GNU Image Manipulation Program")).toEqual("GIMP");
  });

  // punctuation without whitespace
  xtest("phrases with punctuation without whitespace", () => {
    expect(parse("Complementary metal-oxide semiconductor")).toEqual("CMOS");
  });

  // very long abbreviation
  xtest("long phrases", () => {
    expect(
      parse(
        "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
      )
    ).toEqual("ROTFLSHTMDCOALM");
  });

  // consecutive delimiters
  xtest("phrases with consecutive delimiters", () => {
    expect(parse("Something - I made up from thin air")).toEqual("SIMUFTA");
  });

  // apostrophes
  xtest("phrases with apostrophes", () => {
    expect(parse("Halley's Comet")).toEqual("HC");
  });

  // underscore emphasis
  xtest("phrases with underscore emphasis", () => {
    expect(parse("The Road _Not_ Taken")).toEqual("TRNT");
  });
});
