Add Character batches

Closes #118
This commit is contained in:
Nils Maier 2019-10-02 04:37:19 +02:00
parent 883f9a6f0b
commit 19b1cc8856
3 changed files with 89 additions and 4 deletions

View File

@ -1,5 +1,5 @@
<!doctype html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
@ -362,6 +362,7 @@
<ul>
<li>Vastly improved name handling in Chrome. (<a
href="https://bugzilla.mozilla.org/show_bug.cgi?id=1245652">Missing in Firefox</a>)</li>
<li>Support <code>[a:z]</code> character batches</li>
</ul>
<h3>Version 4.1 <em>September 21, 2019</em></h3>

View File

@ -93,6 +93,60 @@ class Numeral implements Generator {
}
}
class Character implements Generator {
public readonly start: number;
public readonly stop: number;
public readonly step: number;
public readonly length: number;
public readonly preview: string;
constructor(str: string) {
const rawpieces = str.split(":").map(e => e.trim());
const pieces = rawpieces.map((e, i) => {
if (i === 2) {
return reallyParseInt(e);
}
if (e.length > 1) {
throw new Error("Malformed Character sequence");
}
return e.charCodeAt(0);
});
if (pieces.length < 2) {
throw new Error("Invalid input");
}
const [start, stop, step] = pieces;
if (step === 0) {
throw new Error("Invalid step");
}
this.step = !step ? 1 : step;
const dir = this.step > 0;
if (dir && start > stop) {
throw new Error("Invalid sequence");
}
else if (!dir && start < stop) {
throw new Error("Invalid sequence");
}
this.start = start;
this.stop = stop;
this.length = Math.floor(
(this.stop - this.start + (dir ? 1 : -1)) / this.step);
this.preview = this[Symbol.iterator]().next().value as string;
Object.freeze(this);
}
*[Symbol.iterator]() {
const {start, stop, step} = this;
const dir = step > 0;
for (let i = start; (dir ? i <= stop : i >= stop); i += step) {
yield String.fromCharCode(i);
}
}
}
export class BatchGenerator implements Generator {
private readonly gens: Generator[];
@ -120,11 +174,16 @@ export class BatchGenerator implements Generator {
try {
this.gens.push(new Numeral(tok));
}
catch (ex) {
catch {
try {
this.gens.push(new Character(tok));
}
catch {
this.gens.push(new Literal(`[${tok}]`));
this.hasInvalid = true;
}
}
}
if (str) {
this.gens.push(new Literal(str));
}

View File

@ -113,4 +113,29 @@ describe("BatchGenerator", function() {
expect(items[0]).to.equal(gen.preview);
expect(gen.hasInvalid).to.be.true;
});
it("characters", function() {
const gen = new BatchGenerator("abc[a:c].lol[1].b");
const items = Array.from(gen);
expect(items).to.deep.equal([
"abca.lol[1].b",
"abcb.lol[1].b",
"abcc.lol[1].b",
]);
expect(items.length).to.equal(gen.length);
expect(items[0]).to.equal(gen.preview);
});
it("characters two", function() {
const gen = new BatchGenerator("abc[D:G].lol[1].b");
const items = Array.from(gen);
expect(items).to.deep.equal([
"abcD.lol[1].b",
"abcE.lol[1].b",
"abcF.lol[1].b",
"abcG.lol[1].b",
]);
expect(items.length).to.equal(gen.length);
expect(items[0]).to.equal(gen.preview);
});
});