Skip to content

Commit

Permalink
✨ Add toString methods to all primitive data types
Browse files Browse the repository at this point in the history
The `toString` method is very useful for debugging purposes. Hence, I
added it to all the primitive data types.
  • Loading branch information
aaditmshah committed Apr 2, 2024
1 parent 596858d commit 271a7bd
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/bool.ts
Expand Up @@ -11,6 +11,10 @@ export class Bool implements TotalOrder<Bool> {
return new Bool(value);
}

public toString(this: Bool): string {
return `Bool(${this.value})`;
}

public isSame(this: Bool, that: Bool): boolean {
return this.value === that.value;
}
Expand Down Expand Up @@ -59,6 +63,10 @@ export class Any extends Bool implements Semigroup<Any> {
return new Any(value);
}

public override toString(this: Any): string {
return `Any(${this.value})`;
}

public append(this: Any, that: Any): Any {
return new Any(this.value || that.value);
}
Expand All @@ -69,6 +77,10 @@ export class All extends Bool implements Semigroup<All> {
return new All(value);
}

public override toString(this: All): string {
return `All(${this.value})`;
}

public append(this: All, that: All): All {
return new All(this.value && that.value);
}
Expand Down
4 changes: 4 additions & 0 deletions src/datetime.ts
Expand Up @@ -10,6 +10,10 @@ export class DateTime implements TotalOrder<DateTime> {
return new DateTime(value);
}

public toString(this: DateTime): string {
return `DateTime(${this.value.getTime()})`;
}

public isSame(this: DateTime, that: DateTime): boolean {
return Object.is(this.value.getTime(), that.value.getTime());
}
Expand Down
4 changes: 4 additions & 0 deletions src/double.ts
Expand Up @@ -10,6 +10,10 @@ export class Double implements TotalOrder<Double> {
return new Double(value);
}

public toString(this: Double): string {
return `Double(${this.value})`;
}

public isSame(this: Double, that: Double): boolean {
return Object.is(this.value, that.value);
}
Expand Down
12 changes: 12 additions & 0 deletions src/integer.ts
Expand Up @@ -11,6 +11,10 @@ export class Integer implements TotalOrder<Integer> {
return new Integer(value);
}

public toString(this: Integer): string {
return `Integer(${this.value})`;
}

public isSame(this: Integer, that: Integer): boolean {
return this.value === that.value;
}
Expand Down Expand Up @@ -59,6 +63,10 @@ export class Sum extends Integer implements Semigroup<Sum> {
return new Sum(value);
}

public override toString(this: Sum): string {
return `Sum(${this.value})`;
}

public append(this: Sum, that: Sum): Sum {
return new Sum(this.value + that.value);
}
Expand All @@ -69,6 +77,10 @@ export class Product extends Integer implements Semigroup<Product> {
return new Product(value);
}

public override toString(this: Product): string {
return `Product(${this.value})`;
}

public append(this: Product, that: Product): Product {
return new Product(this.value * that.value);
}
Expand Down
4 changes: 4 additions & 0 deletions src/text.ts
Expand Up @@ -11,6 +11,10 @@ export class Text implements Semigroup<Text>, TotalOrder<Text> {
return new Text(value);
}

public toString(this: Text): string {
return `Text(${JSON.stringify(this.value)})`;
}

public append(this: Text, that: Text): Text {
return new Text(`${this.value}${that.value}`);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/bool.test.ts
@@ -0,0 +1,48 @@
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";

import type { All, Any, Bool } from "../src/bool.js";

import { all, any, bool } from "./arbitraries.js";

const boolToStringDefinition = (m: Bool): void => {
expect(m.toString()).toStrictEqual(`Bool(${m.value})`);
};

const anyToStringDefinition = (m: Any): void => {
expect(m.toString()).toStrictEqual(`Any(${m.value})`);
};

const allToStringDefinition = (m: All): void => {
expect(m.toString()).toStrictEqual(`All(${m.value})`);
};

describe("Bool", () => {
describe("toString", () => {
it("should convert Bool to a string", () => {
expect.assertions(100);

fc.assert(fc.property(bool, boolToStringDefinition));
});
});
});

describe("Any", () => {
describe("toString", () => {
it("should convert Any to a string", () => {
expect.assertions(100);

fc.assert(fc.property(any, anyToStringDefinition));
});
});
});

describe("All", () => {
describe("toString", () => {
it("should convert All to a string", () => {
expect.assertions(100);

fc.assert(fc.property(all, allToStringDefinition));
});
});
});
20 changes: 20 additions & 0 deletions tests/datetime.test.ts
@@ -0,0 +1,20 @@
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";

import type { DateTime } from "../src/datetime.js";

import { datetime } from "./arbitraries.js";

const toStringDefinition = (m: DateTime): void => {
expect(m.toString()).toStrictEqual(`DateTime(${m.value.getTime()})`);
};

describe("DateTime", () => {
describe("toString", () => {
it("should convert DateTime to a string", () => {
expect.assertions(100);

fc.assert(fc.property(datetime, toStringDefinition));
});
});
});
20 changes: 20 additions & 0 deletions tests/double.test.ts
@@ -0,0 +1,20 @@
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";

import type { Double } from "../src/double.js";

import { double } from "./arbitraries.js";

const toStringDefinition = (m: Double): void => {
expect(m.toString()).toStrictEqual(`Double(${m.value})`);
};

describe("Double", () => {
describe("toString", () => {
it("should convert Double to a string", () => {
expect.assertions(100);

fc.assert(fc.property(double, toStringDefinition));
});
});
});
48 changes: 48 additions & 0 deletions tests/integer.test.ts
@@ -0,0 +1,48 @@
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";

import type { Integer, Product, Sum } from "../src/integer.js";

import { integer, product, sum } from "./arbitraries.js";

const integerToStringDefinition = (m: Integer): void => {
expect(m.toString()).toStrictEqual(`Integer(${m.value})`);
};

const sumToStringDefinition = (m: Sum): void => {
expect(m.toString()).toStrictEqual(`Sum(${m.value})`);
};

const productToStringDefinition = (m: Product): void => {
expect(m.toString()).toStrictEqual(`Product(${m.value})`);
};

describe("Integer", () => {
describe("toString", () => {
it("should convert Integer to a string", () => {
expect.assertions(100);

fc.assert(fc.property(integer, integerToStringDefinition));
});
});
});

describe("Sum", () => {
describe("toString", () => {
it("should convert Sum to a string", () => {
expect.assertions(100);

fc.assert(fc.property(sum, sumToStringDefinition));
});
});
});

describe("Product", () => {
describe("toString", () => {
it("should convert Product to a string", () => {
expect.assertions(100);

fc.assert(fc.property(product, productToStringDefinition));
});
});
});
20 changes: 20 additions & 0 deletions tests/text.test.ts
@@ -0,0 +1,20 @@
import { describe, expect, it } from "@jest/globals";
import fc from "fast-check";

import type { Text } from "../src/text.js";

import { text } from "./arbitraries.js";

const toStringDefinition = (m: Text): void => {
expect(m.toString()).toStrictEqual(`Text(${JSON.stringify(m.value)})`);
};

describe("Text", () => {
describe("toString", () => {
it("should convert Text to a string", () => {
expect.assertions(100);

fc.assert(fc.property(text, toStringDefinition));
});
});
});

0 comments on commit 271a7bd

Please sign in to comment.