as-container
Advanced tools
Comparing version 0.6.1 to 0.7.0
/// <reference types="@as-pect/assembly/types/as-pect" /> |
@@ -5,3 +5,3 @@ import { Box } from "../box"; | ||
@operator("==") | ||
eq(other: this): bool { | ||
eqA(_other: this | null): bool { | ||
return true; | ||
@@ -12,3 +12,3 @@ } | ||
@operator("==") | ||
eq(other: this): bool { | ||
eqB(_other: this | null): bool { | ||
return false; | ||
@@ -23,3 +23,3 @@ } | ||
const box = Box.from<i32>(1); | ||
const box2 = Box.new(1); | ||
const box2 = Box.new<i32>(1); | ||
expect(box).toStrictEqual(box2); | ||
@@ -67,7 +67,12 @@ }); | ||
expect(new A() == new B()).toBe(true); | ||
expect(changetype<B>(new A()) == new B()).toBe(false); | ||
expect(new B() == new B()).toBe(false); | ||
// eqA | ||
expect(Box.from(new A()) == Box.from(new A())).toBe(true); | ||
// eqA | ||
expect(Box.from<A>(new A()) == Box.from<A>(new B())).toBe(true); | ||
expect(Box.from(new B()) == Box.from(new B())).toBe(false); | ||
expect(Box.from<A>(new B()) == Box.from<A>(new B())).toBe(false); | ||
// eqB | ||
expect(Box.from<B>(new B()) == Box.from(new B())).toBe(false); | ||
// eqA | ||
expect(Box.from<A>(new B()) == Box.from<A>(new B())).toBe(true); | ||
}); | ||
@@ -81,4 +86,4 @@ | ||
const box3 = Box.from("box"); | ||
const box4 = Box.from("box"); | ||
const box3 = Box.from<String>("box"); | ||
const box4 = Box.from<String>("box"); | ||
expect(box3 != box4).toStrictEqual(false); | ||
@@ -160,6 +165,6 @@ | ||
it("Box<i32>", () => { | ||
let box = Box.from(2); | ||
let box2 = Box.from(1); | ||
let box3 = Box.from(0); | ||
let box4 = Box.from(-1); | ||
let box = Box.from<i32>(2); | ||
let box2 = Box.from<i32>(1); | ||
let box3 = Box.from<i32>(0); | ||
let box4 = Box.from<i32>(-1); | ||
@@ -339,4 +344,4 @@ expect(box == box2).toStrictEqual(false); | ||
it("Box<string>", () => { | ||
const box = Box.from("2"); | ||
const box2 = Box.from("1"); | ||
const box = Box.from<String>("2"); | ||
const box2 = Box.from<String>("1"); | ||
@@ -343,0 +348,0 @@ expect(box == box2).toStrictEqual(false); |
@@ -0,0 +0,0 @@ import { Option } from "../../primitive/option"; |
@@ -0,0 +0,0 @@ import { Result } from "../../primitive/result"; |
@@ -0,0 +0,0 @@ import { Box } from "../../box"; |
@@ -0,0 +0,0 @@ import { Option } from "../../reference"; |
@@ -0,0 +0,0 @@ import { Result } from "../../reference"; |
@@ -0,0 +0,0 @@ import { Box } from "../../box"; |
@@ -63,4 +63,3 @@ /** | ||
@operator("==") | ||
eq(other: this | null): bool { | ||
if (other === null) return false; | ||
eq(other: this): bool { | ||
return this._val == other._val; | ||
@@ -71,4 +70,3 @@ } | ||
@operator("!=") | ||
notEq(other: this | null): bool { | ||
if (other === null) return false; | ||
notEq(other: this): bool { | ||
return this._val != other._val; | ||
@@ -75,0 +73,0 @@ } |
@@ -0,0 +0,0 @@ export * from "./optionable"; |
@@ -0,0 +0,0 @@ import { MapFn, RecoveryFn } from "./shared"; |
export * from "./option"; | ||
export * from "./result"; | ||
export * from "./util"; |
@@ -0,0 +0,0 @@ import { MapFn, RecoveryFn } from "../shared"; |
@@ -160,3 +160,7 @@ import { Option } from "./option"; | ||
expect(message: string): O { | ||
assert(this.isOk, message); | ||
if (isString<E>(this._err)) { | ||
assert(this.isOk, message + "; " + this._err.toString()); | ||
} else { | ||
assert(this.isOk, message); | ||
} | ||
return this._ok; | ||
@@ -163,0 +167,0 @@ } |
@@ -0,0 +0,0 @@ import { Box } from "../box"; |
export * from "./option"; | ||
export * from "./result"; | ||
export * from "./util"; |
@@ -27,3 +27,7 @@ import { Optionable } from "../optionable"; | ||
get isSome(): bool { | ||
return this._val !== null; | ||
if (this._val) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
@@ -33,3 +37,7 @@ | ||
get isNone(): bool { | ||
return this._val === null; | ||
if (this._val) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
@@ -49,3 +57,5 @@ | ||
expect(msg: string): T { | ||
assert(this._val !== null, msg); | ||
if (this.isNone) { | ||
assert(false, msg); | ||
} | ||
return this._val as T; | ||
@@ -56,3 +66,3 @@ } | ||
unwrapOr(def: T): T { | ||
if (this._val !== null) { | ||
if (this.isSome) { | ||
return this._val as T; | ||
@@ -65,3 +75,3 @@ } | ||
unwrapOrElse(fn: RecoveryFn<T>): T { | ||
if (this._val !== null) { | ||
if (this.isSome) { | ||
return this._val as T; | ||
@@ -73,3 +83,3 @@ } | ||
map<U>(fn: MapFn<T, U>): Option<U> { | ||
if (this._val === null) { | ||
if (this.isNone) { | ||
return Option.None<U>(); | ||
@@ -81,3 +91,3 @@ } | ||
mapOr<U>(def: U, fn: MapFn<T, U>): U { | ||
if (this._val === null) { | ||
if (this.isNone) { | ||
return def; | ||
@@ -89,3 +99,3 @@ } | ||
mapOrElse<U>(defFn: RecoveryFn<U>, fn: MapFn<T, U>): U { | ||
if (this._val === null) { | ||
if (this.isNone) { | ||
return defFn(); | ||
@@ -97,3 +107,3 @@ } | ||
flatMap<U>(fn: FlatMapFn<T, U>): Option<U> { | ||
if (this._val === null) { | ||
if (this.isNone) { | ||
return Option.None<U>(); | ||
@@ -117,3 +127,3 @@ } | ||
or(def: Option<T>): Option<T> { | ||
if (this._val !== null) { | ||
if (this.isSome) { | ||
return Option.Some<T>(this._val as T); | ||
@@ -120,0 +130,0 @@ } |
@@ -49,3 +49,3 @@ import { Option } from "./option"; | ||
ok(): Option<O> { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return Option.Some(this._ok as O); | ||
@@ -57,3 +57,3 @@ } | ||
err(): Option<E> { | ||
if (this._err !== null) { | ||
if (this.isErr) { | ||
return Option.Some(this._err as E); | ||
@@ -65,3 +65,3 @@ } | ||
map<U>(op: MapFn<O, U>): Result<U, E> { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return Result.Ok<U, E>(op(this._ok as O)); | ||
@@ -73,3 +73,3 @@ } | ||
mapOr<U>(def: U, fn: MapFn<O, U>): U { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return fn(this._ok as O); | ||
@@ -81,3 +81,3 @@ } | ||
mapOrElse<U>(defFn: RecoveryWithErrorFn<E, U>, fn: MapFn<O, U>): U { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return fn(this._ok as O); | ||
@@ -89,3 +89,3 @@ } | ||
mapErr<F>(op: MapFn<E, F>): Result<O, F> { | ||
if (this._err !== null) { | ||
if (this.isErr) { | ||
return Result.Err<O, F>(op(this._err as E)); | ||
@@ -97,3 +97,3 @@ } | ||
and<U>(res: Result<U, E>): Result<U, E> { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return res; | ||
@@ -105,3 +105,3 @@ } | ||
andThen<U>(op: FlatMapOkFn<O, U, E>): Result<U, E> { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return op(this._ok as O); | ||
@@ -118,3 +118,3 @@ } | ||
or<F>(res: Result<O, F>): Result<O, F> { | ||
if (this._err !== null) { | ||
if (this.isErr) { | ||
return res; | ||
@@ -144,3 +144,3 @@ } | ||
unwrapOr(optb: O): O { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return this._ok as O; | ||
@@ -152,3 +152,3 @@ } | ||
unwrapOrElse(op: RecoveryWithErrorFn<E, O>): O { | ||
if (this._ok !== null) { | ||
if (this.isOk) { | ||
return this._ok as O; | ||
@@ -161,3 +161,7 @@ } | ||
expect(message: string): O { | ||
assert(this._ok !== null, message); | ||
if (this.isErr && isString<E>(this._err as E)) { | ||
assert(this.isOk, message + "; " + (this._err as string).toString()); | ||
} else { | ||
assert(this.isOk, message); | ||
} | ||
return this._ok as O; | ||
@@ -168,3 +172,3 @@ } | ||
expectErr(message: string): E { | ||
assert(this._err !== null, message); | ||
assert(this.isErr, message); | ||
return this._err as E; | ||
@@ -171,0 +175,0 @@ } |
@@ -0,0 +0,0 @@ import { Box } from "../box"; |
@@ -0,0 +0,0 @@ import { MapFn, RecoveryWithErrorFn } from "./shared"; |
export type RecoveryFn<T> = () => T; | ||
export type MapFn<T, U> = (v: T) => U; | ||
export type RecoveryWithErrorFn<E, O> = (e: E) => O; |
@@ -0,0 +0,0 @@ // @ts-ignore |
export * from "./assembly"; |
{ | ||
"name": "as-container", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "assemblyscript version of Rust Option<T> and Result<T, E> and Box<T> etc.", | ||
@@ -8,2 +8,3 @@ "author": "yjhmelody <yjh465402634@gmail.com>", | ||
"main": "index.ts", | ||
"type": "module", | ||
"keywords": [ | ||
@@ -27,19 +28,17 @@ "assemblyscript", | ||
"changelog": "conventional-changelog -i CHANGELOG.md -s" | ||
}, | ||
"devDependencies": { | ||
"@as-pect/cli": "^6.2", | ||
"@typescript-eslint/eslint-plugin": "^4.28", | ||
"@typescript-eslint/parser": "^4.28", | ||
"assemblyscript": "^0.19.9", | ||
"eslint": "^7.28", | ||
"typescript": "^4.3" | ||
"@as-pect/cli": "^8.0", | ||
"@typescript-eslint/eslint-plugin": "^5.46", | ||
"@typescript-eslint/parser": "^5.46", | ||
"assemblyscript": "^0.25", | ||
"eslint": "^8.29", | ||
"typescript": "^4.9" | ||
}, | ||
"files": [ | ||
"assembly", | ||
"LICENCE", | ||
"index.ts", | ||
"assembly/", | ||
"README.md", | ||
"package.json" | ||
"README.md" | ||
] | ||
} |
@@ -56,3 +56,3 @@ # as-container | ||
`as-container` offers two versions of Result/Option. They provide the same API, but different implementations | ||
`as-container` offers two versions of Result/Option. They provide the same API, but different implementations. | ||
@@ -59,0 +59,0 @@ The default version can handle any type including primitive type. But because the primitive types need one more byte to record state, it may take more overhead. |
2043
Yes
69439
26