Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created July 6, 2022 17:06
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 aleclarson/b447b7f05fe14eda49b1cbb6f8dab959 to your computer and use it in GitHub Desktop.
Save aleclarson/b447b7f05fe14eda49b1cbb6f8dab959 to your computer and use it in GitHub Desktop.
const MagicString = require('magic-string');
const assert = require('assert');
const kleur = require('kleur');
function test() {
const s1 = new MagicString('abcde');
const s2 = s1.clone();
const desiredResult = 'abxcde';
// Sanity check without remove call.
s2.prependRight(2, 'x');
assert(s2.toString() == desiredResult, `Sanity check failed!`);
// Prove that current methods are not
// sufficient for the desired behavior.
s1.remove(1, 3);
assert(s1.toString() == 'ade');
// Restore the removed range with appendLeft.
s1.appendLeft(1, s1.original.slice(1, 3));
assert(s1.toString() == 'abcde');
// But now prependRight behaves differently when
// compared with the sanity check.
s1.prependRight(2, 'x');
assert(s1.toString() == desiredResult, `${s1} !== ${desiredResult}`);
}
try {
test();
console.log(kleur.green('All assertions passed!'));
} catch (e) {
console.error(kleur.red(e.message));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment