Releases: vitalics/rslike
@rslike/[email protected]
Patch Changes
-
2513e9b: Adds promise-like API and security fixes
What's new
Add primise-like API for
Option
andResult
Result
usesok
anderr
for callbacks.Example:
const result = new Result((ok, err) => { if (true) ok(1); err("Some Error"); }); result.unwrap(); // 1
Option
usessome
andnone
for callbacks.NOTE
some(null)
andsome(undefined)
will be converted tonone
automatically since it nullable values.Example:
const o = new Option((some, none) => { some(undefined); }); o.isNone(); // true
Result
andOption
now useswithResolves
asPromise
API.Example:
const { ok, result, err } = Result.withResolvers(); ok(3); result.unwrap(); // 3
Security fixes
- 38 - fixes
@babel/traverse
package
- 38 - fixes
@rslike/[email protected]
Patch Changes
- Updated dependencies [2513e9b]
- @rslike/[email protected]
@rslike/[email protected]
Patch Changes
-
df8b72a: ## What's new
Add primise-like API for
Option
andResult
Result
usesok
anderr
for callbacks.Example:
const result = new Result((ok, err) => { if (true) ok(1); err("Some Error"); }); result.unwrap(); // 1
Option
usessome
andnone
for callbacks.NOTE
some(null)
andsome(undefined)
will be converted tonone
automatically since it nullable values.Example:
const o = new Option((some, none) => { some(undefined); }); o.isNone(); // true
Result
andOption
now useswithResolves
asPromise
API.Example:
const { ok, result, err } = Result.withResolvers(); ok(3); result.unwrap(); // 3
@rslike/[email protected]
Major Changes
What's new
See on Wiki: https://github.com/vitalics/rslike/wiki/01-std
std
All changes applies to Option
and Result
objects.
Symbols for Option and Result objects
Implements Symbol.iterator
, Symbol.asyncIterator
, Symbol.search
, Symbol.split
, Symbol.inspect
, Symbol.toPrimitive
, Symbol.toStringTag
for Option
and Result
objects
Example:
// Before
const arr = Some([1, 2, 3]);
// error, for..of only supports with arrays
for (let el of arr) {
}
As a workaround you may unwrap the value:
// workaround
const arr = Some([1, 2, 3]);
// error, for..of now works with arrays
for (let el of arr.unwrap()) {
}
Now you can "just call for..of".
// now it works
const arr = Some([1, 2, 3]);
// error, for..of only supports with arrays
for (let el of arr) {
// works now!
}
Note: This method will only yeild if the Option
is Some
Note: throws UndefinedBehaviorError
for Some(value)
if value
is not implements Symbol.iterator
More about examples with Symbols implementation you can found in wiki
Symbol.hasInstance for Some
, None
, Ok
and Err
functions
Now you can call instanceof
keyword for functions. From now you can skip importing Result
and Option
classes.
Example:
// Before
import { Ok, Err, Result } from "@rslike/std";
const a = Ok(3);
a instanceof Ok; // false
a instanceof Result; // true
// now, less imports
import { Ok, Err } from "@rslike/std";
const b = Ok(3);
b instanceof Ok; // true
b instanceof Err; // false
Err("Some Err") instanceof Err; // true
Advanced Types inferring
Add more complex TS types to understands have value or not.
Example:
// Before
const a = Some(3);
a.isSome(); // TS type: boolean
// now
a.isSome(); // TS type: true
double unwraping in match function for Result<Option<T>, E>
From now you just can use match
function with only 1 unwrapping.
If you're using Async
or Bind
functions - your result will be wraped to Result<Option<_>>
. To unwrap this result you must to use double matching.
Example (before - 67 lines):
import { Bind, match, Err } from "@rslike/std";
function divide(a: number, b: number) {
if (b === 0) Err("Divide to zero");
if (a === 0) Ok(0);
if (Number.isNaN(a) || Number.isNaN(b)) return undefined;
return a / b;
}
const binded = Bind(divide);
const fn1 = binded(1, 1); // Result<Option<number | undefined>, string>
const fn2 = binded(NaN, 1); // Result<Option<undefined>, string>
const res1 = match(
fn1, // or fn2
(res) => {
return match(
res,
(value) => {
console.log("value is:", value);
},
() => {
console.log("value is None");
}
);
},
(err) => {
console.error(err);
}
);
console.log(res1); // value is: 1
console.log(res2); // value is None
Example (now - 27 lines):
import { Bind, match, Err } from "@rslike/std";
function divide(a: number, b: number) {
if (b === 0) Err("Divide to zero");
if (a === 0) Ok(0);
if (Number.isNaN(a) || Number.isNaN(b)) return undefined;
return a / b;
}
const binded = Bind(divide);
const fn1 = binded(1, 1); // Result<Option<number | undefined>, string>
const fn2 = binded(NaN, 1); // Result<Option<undefined>, string>
const res1 = match(
fn1, // or fn2
(value) => {
console.log("value is:", value);
},
(err) => {
if (err) console.error(err);
else console.log("value is None");
}
);
console.log(res1); // value is: 1
console.log(res2); // value is None
@rslike/[email protected]
Major Changes
- 861b1fb: Nothing for dbg package. Sync dependency
@rslike/[email protected]
Major Changes
What's new
cmp
Compare package now register in global scope Symbol.compare
, Symbol.partialEquals
and Symbol.equals
.
for more examples how to work with theese symbols see at wiki
Definitions for built-in objects.
Number
andNumberConstructor
implementsSymbol.compare
,Symbol.partialEquals
andSymbol.equals
String
andStringConstructor
implementsSymbol.compare
,Symbol.partialEquals
andSymbol.equals
Boolean
andBooleanConstructor
implementsSymbol.compare
,Symbol.partialEquals
andSymbol.equals
Date
andDateConstructor
implementsSymbol.compare
,Symbol.partialEquals
andSymbol.equals
utilities function
@rslike/cmp
now export utilities function compare
, partialEquals
and equals
. All this functions call Symbols implementation. (e.g. compare
calls Symbol.compare
implementation)
compare
callsSymbol.compare
implementationpartialEquals
callsSymbol.partialEquals
implementationequals
callsSymbol.equals
implementation
Patch Changes
- Updated dependencies [861b1fb]
- @rslike/[email protected]
@rslike/[email protected]
Patch Changes
- d34b352: make dbg package more reliable for external building
@rslike/[email protected]
Patch Changes
- 9bfb757: make dbg package more reliable for external building
@rslike/[email protected]
Patch Changes
- d34b352: make dbg package more reliable for external building
@rslike/[email protected]
Patch Changes
- 9bfb757: make dbg package more reliable for external building