Skip to content

Commit

Permalink
feat(spec): change some constructor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 5, 2023
1 parent 6ca9ce2 commit a37d1f6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 66 deletions.
8 changes: 4 additions & 4 deletions operators/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { type Option } from "../spec.ts";
* import { unwrap } from "https://deno.land/x/optio/operators/extract.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(unwrap(Some.of(0)), 0);
* assertEquals(unwrap(Some(0)), 0);
* ```
* @throws {Error} if the {@link option} is `None`.
* @example
Expand All @@ -38,7 +38,7 @@ export function unwrap<T>(option: Option<T>): T {
* import { unwrapOr } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(unwrapOr(Some.of(0), 1), 0);
* assertEquals(unwrapOr(Some(0), 1), 0);
* assertEquals(unwrapOr(None, 1), 1);
* ```
*/
Expand All @@ -56,7 +56,7 @@ export function unwrapOr<T>(option: Option<T>, defaultValue: T): T {
* import { unwrapOrElse } from "https://deno.land/x/optio/operators/extract.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(unwrapOrElse(Some.of(0), () => 2 ** 3), 0);
* assertEquals(unwrapOrElse(Some(0), () => 2 ** 3), 0);
* assertEquals(unwrapOrElse(None, () => 2 ** 3), 8);
* ```
*/
Expand All @@ -74,7 +74,7 @@ export function unwrapOrElse<T>(option: Option<T>, fn: () => T): T {
* import { expect } from "https://deno.land/x/optio/operators/extract.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const option = Some.of(0);
* const option = Some(0);
* declare const message: string;
*
* assertEquals(expect(option, message), 0);
Expand Down
10 changes: 5 additions & 5 deletions operators/extract_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

describe("unwrap", () => {
it("should return some value", () => {
assertEquals(unwrap(Some.of(0)), 0);
assertEquals(unwrap(Some(0)), 0);
});

it("should throw error if None", () => {
Expand All @@ -24,7 +24,7 @@ describe("unwrap", () => {

describe("unwrapOr", () => {
it("should some value if some", () => {
assertEquals(unwrapOr(Some.of(0), 1), 0);
assertEquals(unwrapOr(Some(0), 1), 0);
});

it("should default value if None", () => {
Expand All @@ -35,7 +35,7 @@ describe("unwrapOr", () => {
describe("unwrapOrElse", () => {
it("should some value if some", () => {
const fn = spy(() => 1);
assertEquals(unwrapOrElse(Some.of(0), fn), 0);
assertEquals(unwrapOrElse(Some(0), fn), 0);
assertSpyCalls(fn, 0);
});

Expand All @@ -48,7 +48,7 @@ describe("unwrapOrElse", () => {

describe("expect", () => {
it("should return some value", () => {
assertEquals(expect(Some.of(0), ""), 0);
assertEquals(expect(Some(0), ""), 0);
});

it("should throw error if None", () => {
Expand All @@ -67,7 +67,7 @@ describe("match", () => {
const s = spy(() => 1);
const n = spy(() => 2);

assertEquals(match(Some.of(0), { Some: s, None: n }), 1);
assertEquals(match(Some(0), { Some: s, None: n }), 1);
assertSpyCalls(s, 1);
assertSpyCalls(n, 0);
assertSpyCallArgs(s, 0, [0]);
Expand Down
14 changes: 7 additions & 7 deletions operators/logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { None, type Option, Some } from "../spec.ts";
* import { None, or, Some } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const x = Some.of(2);
* assertEquals(or(x, None), Some.of(2));
* const x = Some(2);
* assertEquals(or(x, None), Some(2));
*
* const y = None;
* assertEquals(or(y, Some.of(100)), Some.of(100));
* assertEquals(or(y, Some(100)), Some(100));
* ```
*/
export function or<T>(option: Option<T>, obtb: Option<T>): Option<T> {
Expand All @@ -32,8 +32,8 @@ export function or<T>(option: Option<T>, obtb: Option<T>): Option<T> {
* import { None, Some } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(orElse(Some.of(0), () => Some.of(1)), Some.of(0));
* assertEquals(orElse(None, () => Some.of(1)), Some.of(1));
* assertEquals(orElse(Some(0), () => Some(1)), Some(0));
* assertEquals(orElse(None, () => Some(1)), Some(1));
* ```
*/
export function orElse<T>(option: Option<T>, fn: () => Option<T>): Option<T> {
Expand Down Expand Up @@ -75,7 +75,7 @@ export function and<T>(option: Option<unknown>, optb: Option<T>): Option<T> {
*
* declare const square: (value: number) => number;
*
* assertEquals(andThen(Some.of(3), square), Some.of(9));
* assertEquals(andThen(Some(3), square), Some(9));
* assertEquals(andThen(None, square), None);
* ```
*/
Expand All @@ -85,7 +85,7 @@ export function andThen<T, U>(
): Option<U> {
if (isNone(option)) return option;

return Some.of(fn(option.get));
return Some(fn(option.get));
}

/** Returns `Some` if exactly one of {@link option}, {@link optb} is `Some`, otherwise returns `None`.
Expand Down
26 changes: 13 additions & 13 deletions operators/logical_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import {

describe("or", () => {
it("should return option if it is Some", () => {
const option = Some.of(0);
assert(or(option, Some.of(0)) === option);
const option = Some(0);
assert(or(option, Some(0)) === option);
assert(or(option, None) === option);
});

it("should return optb if it is None", () => {
const optb = Some.of(0);
const optb = Some(0);
assert(or(None, optb) === optb);
assert(or(None, None) === None);
});
});

describe("and", () => {
it("should return Some if both of arg is some otherwise None", () => {
const some = Some.of(0);
const someb = Some.of(1);
const some = Some(0);
const someb = Some(1);

assert(and(some, None) === None);
assert(and(some, someb) === someb);
Expand All @@ -40,20 +40,20 @@ describe("and", () => {

describe("xor", () => {
it("should return Some if one of option or optb is Some, otherwise None", () => {
const option = Some.of(0);
const optb = Some.of(0);
const option = Some(0);
const optb = Some(0);

assert(xor(option, None) === option);
assert(xor(None, optb) === optb);
assert(xor(Some.of(0), Some.of(1)) === None);
assert(xor(Some(0), Some(1)) === None);
assert(xor(None, None) === None);
});
});

describe("andThen", () => {
it("should return Some and call fn if option is Some", () => {
const fn = spy((v: number) => v ** 3);
assertEquals(andThen(Some.of(2), fn), Some.of(8));
assertEquals(andThen(Some(2), fn), Some(8));
assertSpyCalls(fn, 1);
assertSpyCallArgs(fn, 0, [2]);
});
Expand All @@ -67,14 +67,14 @@ describe("andThen", () => {

describe("orElse", () => {
it("should return Some and call fn if option is Some", () => {
const fn = spy(() => Some.of(1));
assertEquals(orElse(Some.of(0), fn), Some.of(0));
const fn = spy(() => Some(1));
assertEquals(orElse(Some(0), fn), Some(0));
assertSpyCalls(fn, 0);
});

it("should return None if option is None", () => {
const fn = spy(() => Some.of(1));
assertEquals(orElse(None, fn), Some.of(1));
const fn = spy(() => Some(1));
assertEquals(orElse(None, fn), Some(1));
assertSpyCalls(fn, 1);
});
});
2 changes: 1 addition & 1 deletion operators/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { type None, type Option, OptionType, type Some } from "../spec.ts";
* import { isSome, Option, Some } from "https://deno.land/x/optio/mod.ts";
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<number> = Some.of(2);
* const option: Option<number> = Some(2);
* assert(isSome(option));
* ```
*/
Expand Down
4 changes: 2 additions & 2 deletions operators/query_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { assert, assertFalse, describe, it } from "../_dev_deps.ts";

describe("isSome", () => {
it("should return true if it is Some", () => {
assert(isSome(Some.of(0)));
assert(isSome(Some(0)));
});

it("should return false if it is None", () => {
Expand All @@ -20,6 +20,6 @@ describe("isNone", () => {
});

it("should return false if it is Some", () => {
assertFalse(isNone(Some.of(0)));
assertFalse(isNone(Some(0)));
});
});
24 changes: 12 additions & 12 deletions operators/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { None, type Option, Some } from "../spec.ts";
* import { map, Option, Some } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<string> = Some.of("Hello, World!");
* const option: Option<string> = Some("Hello, World!");
* const optionLen = map(option, (v) => v.length);
*
* assertEquals(optionLen, Some.of(13));
* assertEquals(optionLen, Some(13));
* ```
*/
export function map<T, U>(
option: Option<T>,
fn: (value: T) => U,
): Option<U> {
if (isSome(option)) return Some.of(fn(option.get));
if (isSome(option)) return Some(fn(option.get));

return option;
}
Expand All @@ -33,7 +33,7 @@ export function map<T, U>(
* import { mapOr, Option, Some } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<string> = Some.of("Hello");
* const option: Option<string> = Some("Hello");
* assertEquals(mapOr(option, 0, ({ length }) => length), 5);
* ```
*/
Expand All @@ -54,7 +54,7 @@ export function mapOr<T, U>(
* import { mapOrElse, Option, Some } from "https://deno.land/x/optio/mod.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<string> = Some.of("Hello");
* const option: Option<string> = Some("Hello");
* assertEquals(mapOrElse(option, () => 2 ** 3, ({ length }) => length), 5);
* ```
*/
Expand Down Expand Up @@ -99,8 +99,8 @@ export function filter<T, U extends T = T>(
*
* declare const isEven: (value: number) => boolean;
*
* assertEquals(filter(Some.of(0), isEven), Some.of(0));
* assertEquals(filter(Some.of(1), isEven), None);
* assertEquals(filter(Some(0), isEven), Some(0));
* assertEquals(filter(Some(1), isEven), None);
* assertEquals(filter(None, isEven), None);
* ```
*/
Expand All @@ -125,8 +125,8 @@ export function filter<T>(
* import { flat } from "https://deno.land/x/optio/operators/transform.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<Option<number>> = Some.of(Some.of(0));
* assertEquals(flat(option), Some.of(0));
* const option: Option<Option<number>> = Some(Some(0));
* assertEquals(flat(option), Some(0));
* ```
*/
export function flat<T>(option: Option<Option<T>>): Option<T> {
Expand All @@ -146,8 +146,8 @@ export function flat<T>(option: Option<Option<T>>): Option<T> {
* import { zip } from "https://deno.land/x/optio/operators/transform.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(zip(Some.of(0), Some.of(1)), Some.of<[0, 1]>([0, 1]));
* assertEquals(zip(Some.of(0), None), None);
* assertEquals(zip(Some(0), Some(1)), Some<[0, 1]>([0, 1]));
* assertEquals(zip(Some(0), None), None);
* ```
*/
export function zip<T, U>(
Expand All @@ -157,5 +157,5 @@ export function zip<T, U>(
if (isNone(option) || isNone(other)) return None;

const tuple: [T, U] = [option.get, other.get];
return Some.of(tuple);
return Some(tuple);
}
24 changes: 12 additions & 12 deletions operators/transform_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import {
describe("map", () => {
it("should call mapper if it is some", () => {
const INPUT = "Hello, World!";
const option: Option<string> = Some.of(INPUT);
const option: Option<string> = Some(INPUT);
const fn = spy((v: string) => v.length);
const optionLen = map(option, fn);

assertEquals(optionLen, Some.of(13));
assertEquals(optionLen, Some(13));
assertSpyCalls(fn, 1);
assertSpyCallArgs(fn, 0, [INPUT]);
});
Expand All @@ -40,7 +40,7 @@ describe("map", () => {
describe("mapOr", () => {
it("should call mapper if it is some", () => {
const INPUT = "Hello, World!";
const option: Option<string> = Some.of(INPUT);
const option: Option<string> = Some(INPUT);
const fn = spy((v: string) => v.length);
const optionLen = mapOr(option, 0, fn);

Expand All @@ -62,7 +62,7 @@ describe("mapOr", () => {
describe("mapOrElse", () => {
it("should call mapper if it is some", () => {
const INPUT = "Hello, World!";
const option: Option<string> = Some.of(INPUT);
const option: Option<string> = Some(INPUT);
const fn = spy((v: string) => v.length);
const defaultFn = spy(() => 0);
const optionLen = mapOrElse(option, defaultFn, fn);
Expand Down Expand Up @@ -94,22 +94,22 @@ describe("filter", () => {

it("should return Some if Some and predicate is true", () => {
const predicate = spy(() => true);
const some = Some.of(0);
const some = Some(0);
assert(filter(some, predicate) === some);
assertSpyCalls(predicate, 1);
assertSpyCallArgs(predicate, 0, [0]);
});

it("should return None if Some and predicate is false", () => {
const predicate = spy(() => false);
const some = Some.of(0);
const some = Some(0);
assert(filter(some, predicate) === None);
assertSpyCalls(predicate, 1);
assertSpyCallArgs(predicate, 0, [0]);
});

it("should infer narrowing", () => {
const option: Option<string | number> = Some.of(0);
const option: Option<string | number> = Some(0);

const opt = filter(option, isString);
assertType<IsExact<typeof opt, Option<string>>>(true);
Expand All @@ -118,8 +118,8 @@ describe("filter", () => {

describe("flat", () => {
it("should return unwrapped option", () => {
const option: Option<Option<number>> = Some.of(Some.of(0));
assertEquals(flat(option), Some.of(0));
const option: Option<Option<number>> = Some(Some(0));
assertEquals(flat(option), Some(0));
});

it("should return None if None", () => {
Expand All @@ -129,12 +129,12 @@ describe("flat", () => {

describe("zip", () => {
it("should return Some of tuple if option and other option is Some", () => {
assertEquals(zip(Some.of(0), Some.of(1)), Some.of<[0, 1]>([0, 1]));
assertEquals(zip(Some(0), Some(1)), Some<[0, 1]>([0, 1]));
});

it("should return None if option or other is None", () => {
assertEquals(zip(Some.of(0), None), None);
assertEquals(zip(None, Some.of(0)), None);
assertEquals(zip(Some(0), None), None);
assertEquals(zip(None, Some(0)), None);
assertEquals(zip(None, None), None);
});
});
Loading

0 comments on commit a37d1f6

Please sign in to comment.