parent
883f9a6f0b
commit
19b1cc8856
@ -1,5 +1,5 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html>
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
@ -362,6 +362,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li>Vastly improved name handling in Chrome. (<a
|
<li>Vastly improved name handling in Chrome. (<a
|
||||||
href="https://bugzilla.mozilla.org/show_bug.cgi?id=1245652">Missing in Firefox</a>)</li>
|
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>
|
</ul>
|
||||||
|
|
||||||
<h3>Version 4.1 <em>September 21, 2019</em></h3>
|
<h3>Version 4.1 <em>September 21, 2019</em></h3>
|
||||||
|
@ -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 {
|
export class BatchGenerator implements Generator {
|
||||||
private readonly gens: Generator[];
|
private readonly gens: Generator[];
|
||||||
|
|
||||||
@ -120,11 +174,16 @@ export class BatchGenerator implements Generator {
|
|||||||
try {
|
try {
|
||||||
this.gens.push(new Numeral(tok));
|
this.gens.push(new Numeral(tok));
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch {
|
||||||
|
try {
|
||||||
|
this.gens.push(new Character(tok));
|
||||||
|
}
|
||||||
|
catch {
|
||||||
this.gens.push(new Literal(`[${tok}]`));
|
this.gens.push(new Literal(`[${tok}]`));
|
||||||
this.hasInvalid = true;
|
this.hasInvalid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (str) {
|
if (str) {
|
||||||
this.gens.push(new Literal(str));
|
this.gens.push(new Literal(str));
|
||||||
}
|
}
|
||||||
|
@ -113,4 +113,29 @@ describe("BatchGenerator", function() {
|
|||||||
expect(items[0]).to.equal(gen.preview);
|
expect(items[0]).to.equal(gen.preview);
|
||||||
expect(gen.hasInvalid).to.be.true;
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user