as-container
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,2 +0,2 @@ | ||
import { Box } from "../box"; | ||
import { Box, False, True } from "../box"; | ||
@@ -12,3 +12,3 @@ describe("Box", () => { | ||
const box = Box.from("233"); | ||
expect(box.unwrap()).toBe("233"); | ||
expect(box.unwrap()).toStrictEqual("233"); | ||
}); | ||
@@ -18,3 +18,3 @@ | ||
const box = Box.from(1); | ||
expect(box.map<i32>((n) => n * 2).unwrap()).toBe(2); | ||
expect(box.map<i32>((n) => n * 2).unwrap()).toStrictEqual(2); | ||
}); | ||
@@ -25,8 +25,8 @@ | ||
const box2 = Box.from(1); | ||
expect(box == box2).toBe(true); | ||
expect(box.eq(box2)).toBe(true); | ||
expect(box == box2).toStrictEqual(True); | ||
expect(box.eq(box2)).toStrictEqual(True); | ||
const box3 = Box.from("box"); | ||
const box4 = Box.from("box"); | ||
expect(box3 == box4).toBe(true); | ||
expect((box3 == box4).unwrap()).toStrictEqual(true); | ||
@@ -36,3 +36,3 @@ const s = "box"; | ||
const box6 = Box.from(s); | ||
expect(box5 == box6).toBe(true); | ||
expect(box5 == box6).toStrictEqual(True); | ||
@@ -42,3 +42,3 @@ const p1 = new Person(); | ||
expect(p1 == p2).toBe(false); | ||
expect(Box.from(p1) != Box.from(p2)).toStrictEqual(False); | ||
}); | ||
@@ -49,8 +49,8 @@ | ||
const box2 = Box.from(2); | ||
expect(box != box2).toBe(true); | ||
expect(box.notEq(box2)).toBe(true); | ||
expect(box != box2).toStrictEqual(True); | ||
expect(box.notEq(box2)).toStrictEqual(True); | ||
const box3 = Box.from("box"); | ||
const box4 = Box.from("box"); | ||
expect(box3 != box4).toBe(false); | ||
expect(box3 != box4).toStrictEqual(False); | ||
@@ -60,3 +60,3 @@ const s = "box"; | ||
const box6 = Box.from(s); | ||
expect(box5 != box6).toBe(false); | ||
expect(box5 != box6).toStrictEqual(False); | ||
@@ -66,6 +66,56 @@ const p1 = new Person(); | ||
expect(p1 != p2).toBe(true); | ||
expect(Box.from(p1) != Box.from(p2)).toStrictEqual(True); | ||
}); | ||
it("Box<i32>", () => { | ||
const box = Box.from(2); | ||
const box2 = Box.from(1); | ||
const box3 = Box.from(0); | ||
const box4 = Box.from(-1); | ||
expect(box == box2).toStrictEqual(False); | ||
expect(box != box2).toStrictEqual(True); | ||
expect(box > box2).toStrictEqual(True); | ||
expect(box >= box2).toStrictEqual(True); | ||
expect(box < box2).toStrictEqual(False); | ||
expect(box <= box2).toStrictEqual(False); | ||
expect(box >> box2).toStrictEqual(Box.from(1)); | ||
expect(box >>> box2).toStrictEqual(Box.from(1)); | ||
expect(box << box2).toStrictEqual(Box.from(4)); | ||
expect(box & box2).toStrictEqual(Box.from(0)); | ||
expect(box | box2).toStrictEqual(Box.from(3)); | ||
expect(box ^ box2).toStrictEqual(Box.from(3)); | ||
expect(box + box2).toStrictEqual(Box.from(3)); | ||
expect(box - box2).toStrictEqual(Box.from(1)); | ||
expect(box * box2).toStrictEqual(Box.from(2)); | ||
expect(box / box2).toStrictEqual(Box.from(2)); | ||
expect(box ** box2).toStrictEqual(Box.from(2)); | ||
expect(box % box2).toStrictEqual(Box.from(0)); | ||
expect(!box).toStrictEqual(False); | ||
expect(!box3).toStrictEqual(True); | ||
expect(~box).toStrictEqual(Box.from(~2)); | ||
expect(+box4).toStrictEqual(Box.from(1)); | ||
expect(-box4).toStrictEqual(Box.from(1)); | ||
}); | ||
it("Box<string>", () => { | ||
const box = Box.from("2"); | ||
const box2 = Box.from("1"); | ||
expect(box == box2).toStrictEqual(False); | ||
expect(box != box2).toStrictEqual(True); | ||
expect(box > box2).toStrictEqual(True); | ||
expect(box >= box2).toStrictEqual(True); | ||
expect(box < box2).toStrictEqual(False); | ||
expect(box <= box2).toStrictEqual(False); | ||
expect(box + box2).toStrictEqual(Box.from("21")); | ||
}); | ||
}); | ||
class Person {} |
@@ -1,8 +0,7 @@ | ||
export interface Boxable<T> { | ||
unwrap(): T; | ||
map<U>(fn: (arg: T) => U): Box<U>; | ||
} | ||
export class Box<T> implements Boxable<T> { | ||
/** | ||
* Box is used to wrap primitive type such u32 which cannot be null. | ||
* All operators of box have been overloaded to box version | ||
*/ | ||
export class Box<T> { | ||
constructor(private readonly val: T) {} | ||
@@ -32,5 +31,5 @@ | ||
@operator("==") | ||
eq(other: Box<T> | null): bool { | ||
if (other === null) return false; | ||
return this.val == other.val; | ||
eq(other: Box<T> | null): Box<bool> { | ||
if (other === null) return False; | ||
return Box.from(this.val == other.val); | ||
} | ||
@@ -40,5 +39,129 @@ | ||
@operator("!=") | ||
notEq(other: Box<T> | null): bool { | ||
return !this.eq(other); | ||
notEq(other: Box<T> | null): Box<bool> { | ||
if (other === null) return False; | ||
return Box.from(this.val != other.val); | ||
} | ||
@inline | ||
@operator(">") | ||
gt(other: Box<T>): Box<bool> { | ||
return Box.from(this.val > other.val); | ||
} | ||
@inline | ||
@operator(">=") | ||
ge(other: Box<T>): Box<bool> { | ||
return Box.from(this.val >= other.val); | ||
} | ||
@inline | ||
@operator("<") | ||
lt(other: Box<T>): Box<bool> { | ||
return Box.from(this.val < other.val); | ||
} | ||
@inline | ||
@operator("<=") | ||
le(other: Box<T>): Box<bool> { | ||
return Box.from(this.val <= other.val); | ||
} | ||
@inline | ||
@operator(">>") | ||
arithmeticRightShift(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val >> other.val); | ||
} | ||
@inline | ||
@operator(">>>") | ||
logicalRightShift(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val >>> other.val); | ||
} | ||
@inline | ||
@operator("<<") | ||
leftShift(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val << other.val); | ||
} | ||
@inline | ||
@operator("&") | ||
and(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val & other.val); | ||
} | ||
@inline | ||
@operator("|") | ||
or(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val | other.val); | ||
} | ||
@inline | ||
@operator("^") | ||
xor(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val ^ other.val); | ||
} | ||
@inline | ||
@operator("+") | ||
add(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val + other.val); | ||
} | ||
@inline | ||
@operator("-") | ||
sub(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val - other.val); | ||
} | ||
@inline | ||
@operator("*") | ||
multi(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val * other.val); | ||
} | ||
@inline | ||
@operator("/") | ||
divide(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val / other.val); | ||
} | ||
@inline | ||
@operator("**") | ||
exp(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val ** other.val); | ||
} | ||
@inline | ||
@operator("%") | ||
remainder(other: Box<T>): Box<T> { | ||
return Box.from<T>(this.val % other.val); | ||
} | ||
@inline | ||
@operator.prefix("!") | ||
not(): Box<bool> { | ||
return Box.from(!this.val); | ||
} | ||
@inline | ||
@operator.prefix("~") | ||
bitNot(): Box<T> { | ||
return Box.from<T>(~this.val); | ||
} | ||
@inline | ||
@operator.prefix("+") | ||
plus(): Box<T> { | ||
return Box.from<T>(+this.val); | ||
} | ||
@inline | ||
@operator.prefix("-") | ||
negate(): Box<T> { | ||
return Box.from<T>(-this.val); | ||
} | ||
} | ||
export const True = new Box<bool>(true); | ||
export const False = new Box<bool>(false); |
@@ -119,3 +119,3 @@ import { Option } from "./option"; | ||
/** | ||
* return true if options are both None, or the inner value is equall by `==`. | ||
* return true if options are both None, or the inner value is equal by `==`. | ||
* @param other Option | ||
@@ -127,3 +127,3 @@ * @returns | ||
/** | ||
* return false if options are both None, or the inner value is equall by `==`. | ||
* return true if options are different option types, or the inner value is not equal by `!=`. | ||
* @param other Option | ||
@@ -130,0 +130,0 @@ * @returns |
import { Option } from "./option"; | ||
import { Resultable } from "./resultable"; | ||
import { FlatMapErrFn, FlatMapOkFn, MapFn, RecoveryWithErrorFn } from "./shared"; | ||
import { | ||
FlatMapErrFn, | ||
FlatMapOkFn, | ||
MapFn, | ||
RecoveryWithErrorFn, | ||
} from "./shared"; | ||
@@ -155,4 +160,12 @@ /** | ||
notEq(other: Result<O, E>): bool { | ||
return !this.eq(other); | ||
if (this._ok !== null && other._ok !== null) { | ||
return this._ok != other._ok; | ||
} | ||
if (this._err !== null && other._err !== null) { | ||
return this._err != other._err; | ||
} | ||
return true; | ||
} | ||
} |
@@ -121,3 +121,3 @@ import { Option } from "./option"; | ||
/** | ||
* return false if results are both Ok or Err and the value is equal by `==`. | ||
* return true if results are different result types, or the value is not equal by `!=`. | ||
* @param other Option | ||
@@ -124,0 +124,0 @@ * @returns |
{ | ||
"name": "as-container", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "assemblyscript version of Rust Option<T> and Result<T, E> etc.", | ||
@@ -5,0 +5,0 @@ "author": "yjhmelody <yjh465402634@gmail.com>", |
@@ -7,3 +7,3 @@ # as-container | ||
as-container provides some utils such as Option and Result inspired by Rust for other people to use. | ||
as-container provides some utils such as `Option` and `Result` inspired by Rust for other people to use. | ||
@@ -10,0 +10,0 @@ ## Example |
47974
22
1273