Skip to content

Commit

Permalink
refactor(dcons): simplify toString() impl, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 16, 2020
1 parent aa74904 commit 2bf8b92
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
8 changes: 1 addition & 7 deletions packages/dcons/src/dcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,7 @@ export class DCons<T>
let res: any = [];
let cell = this.head;
while (cell) {
res.push(
cell.value != null
? String(cell.value)
: cell.value === undefined
? "undefined"
: "null"
);
res.push(String(cell.value));
cell = cell.next;
}
return res.join(", ");
Expand Down
31 changes: 19 additions & 12 deletions packages/dcons/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { compareNumDesc } from "@thi.ng/compare";
import { XsAdd } from "@thi.ng/random";
import { range } from "@thi.ng/transducers";
import * as assert from "assert";
import { DCons, dcons } from "../src/index";
import { DCons, defDCons } from "../src/index";

describe("DCons", () => {
let a: DCons<any>, src: number[];
beforeEach(() => {
src = [1, 2, 3, 4, 5];
a = dcons(src);
a = defDCons(src);
});

it("is instanceof", () => {
Expand All @@ -17,7 +17,7 @@ describe("DCons", () => {

it("has length", () => {
assert.strictEqual(a.length, 5);
a = dcons();
a = defDCons();
assert.strictEqual(a.length, 0);
});

Expand Down Expand Up @@ -46,31 +46,31 @@ describe("DCons", () => {
[3, 5, 1, 4, 2]
);
assert.deepStrictEqual(
[...dcons(range(10)).shuffle(undefined, new XsAdd(0x12345678))],
[...defDCons(range(10)).shuffle(undefined, new XsAdd(0x12345678))],
[3, 0, 7, 8, 5, 2, 9, 1, 6, 4]
);
assert.deepStrictEqual([...dcons().shuffle()], []);
assert.deepStrictEqual([...dcons([1]).shuffle()], [1]);
assert.deepStrictEqual([...defDCons().shuffle()], []);
assert.deepStrictEqual([...defDCons([1]).shuffle()], [1]);
});

it("sort", () => {
assert.deepStrictEqual([...dcons().sort()], []);
assert.deepStrictEqual([...dcons([1]).sort()], [1]);
assert.deepStrictEqual([...dcons([1, -1]).sort()], [-1, 1]);
assert.deepStrictEqual([...defDCons().sort()], []);
assert.deepStrictEqual([...defDCons([1]).sort()], [1]);
assert.deepStrictEqual([...defDCons([1, -1]).sort()], [-1, 1]);
assert.deepStrictEqual(
[...dcons([8, -1, 17, 5, 8, 3, 11]).sort()],
[...defDCons([8, -1, 17, 5, 8, 3, 11]).sort()],
[-1, 3, 5, 8, 8, 11, 17]
);
assert.deepStrictEqual(
[...dcons([8, -1, 17, 5, 8, 3, 11]).sort(compareNumDesc)],
[...defDCons([8, -1, 17, 5, 8, 3, 11]).sort(compareNumDesc)],
[17, 11, 8, 8, 5, 3, -1]
);
});

it("works as stack", () => {
assert.strictEqual(a.push(10).pop(), 10);
assert.strictEqual(a.pop(), 5);
a = dcons();
a = defDCons();
assert.strictEqual(a.pop(), undefined);
});

Expand All @@ -83,4 +83,11 @@ describe("DCons", () => {
assert.strictEqual(a.drop(), 10);
assert.strictEqual(a.drop(), undefined);
});

it("toString", () => {
assert.strictEqual(
defDCons([, null, 0, 1, ["a", "b"], "ab"]).toString(),
"undefined, null, 0, 1, a,b, ab"
);
});
});

0 comments on commit 2bf8b92

Please sign in to comment.