• Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    TypeScript
  • License
    BSD 3-Clause "New...
  • Created over 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!)

rustic

rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!).

The repo has 100% test code coverage, so you can be sure it will never fail! Even more in your production codeβ„’. (The not fail part is obviously a lie. Test your code.)



Installation

Just install as any other package:

$ npm i rustic

Usage

Result

  1. Let's suppose we you have a fallible function that'll return an error for random number lower than 5:
import { Result, Err, Ok, isOk } from 'rustic';

function fallible(): Result<number, string> {
	const zeroToTen: number = Math.random() * 10;

	// Using Err and Ok helper as a shorthand to produce Result
	if (zeroToTen < 5) {
		return Err("Lower than 5");
	} else {
		return Ok(zeroToTen);
	}
}

const res = fallible();

// Using isOk helper will do this for You, but You can also
// access the `__kind` field and compare it with `ResultKind` enum directly
if (isOk(res)) {
	// Typescript infers res.data's type as `number`
	console.log('Successful num sq:', res.data * res.data);	// Successful num sq: <number>
} else {
	console.log('Error:', res.data);	// 'Error: Lower than 5'
}
  1. Suppose you want to map the Result of a fallible function:
import { Result, equip, ResultEquipped } from 'rustic';

function fallible(): Result<number, string> { ... }

const res: Result<number, string> = fallible();

// Call `equip` with the Result of fallible function
const equipped: ResultEquipped<number, string> = equip(res);

// Use as You would Rust's Result
const squared: number = equipped.map(n => n * n).expect('Squared n');

// Using unwrap can cause a panic: `panicked at 'Squared n: "<err message>"'`

// You can access the underlying Result<number, string> using the `.inner` getter:
// `equipped.inner`;

console.log('Squared', squared);

Option

  1. Option follows the same methods as Result does:
import { Option } from 'rustic';

function returnsOption(): Option<number> { ... }

const res: Option<number> = returnsOption();

if (res == null) {
	console.log('Returned null');
} else {
	// Typescript can infer for sure, that the res is of type `number`.
	console.log('Returned num:', res * 2);
}
  1. Call equip with the optional variable to gain access to the full functionality:
import { Option, equip, OptionEquipped } from 'rustic';

function returnsOption(): Option<number> { ... }

const res: OptionEquipped<number> = equip(returnsOption());

const squared = res.map(n => n * n);

// Unwrap can lead to panics. You can still access the underlying Option<number>
// by using `.inner`: `squared.inner`
console.log('Sqared num:', squared.unwrap());

Wrappers

catchResult

import { catchResult } from 'rustic';

function throwsError(): void { throw new Error('1234') }

function doesNotThrowError(): number { return 5 }

const res1 = catchResult(throwsError);	// Err('Error: 1234')
const res2 = catchResult(doesNotThrowError);	// Ok(5)

parseJson

import { parseJson, Result } from 'rustic';

const parsed1: Result<number, string> = parseJson('5');	// Ok(5)

const parsed2: Result<number, string> = parseJson('{');	// Err('...')

More Repositories

1

dino

Chrome's t-rex based bootsector game (512 bytes) written in 16-bit x86 assembly (now with 8086 support!)
Assembly
104
star
2

pocket-operator-simulator

Simulation of teenage engineering's pocket operator PO-20
JavaScript
100
star
3

clubi

A group-oriented social media platform written in Laravel and Vue
PHP
31
star
4

luxya

Programming language with a tree-walking interpreter written in RustΒ©β„’.
Rust
29
star
5

scaffolding

scaffolding is a very pointless esolang. Don't try it.
C
16
star
6

abak

An unnecessarily minimal backlight controller
Assembly
7
star
7

tie

Package franeklubi/tie provides a Processing-like API for simple and fun drawing, game making, data and algorithm visualization, and generally - art :)
Go
7
star
8

ablm

Online metronome inspired by Ableton!
JavaScript
6
star
9

SmartList

SmartList provides an easy way to manage slices of vars of any type.
Go
3
star
10

tie-examples

This repo contains example code to use with franeklubi/tie.
Go
3
star
11

ledgend

ledgend is a Go library aiming to provide a simple api for creating animations You can use to display on a LED strip.
Go
3
star
12

ledserv

Server for ledgend; Designed for sending data to franeklubi/ledgend-esp8266
Go
2
star
13

ledgend-esp8266

franeklubi/ledserv client for esp8266
C++
2
star
14

linear-regression-processing

Processing implementation of linear regression
Processing
1
star
15

vbak

An utterly minimal backlight controller
C
1
star
16

SimpleVector

My simple vector lib for Golang
Go
1
star
17

scaf-visualizer

Visualizer for franeklubi/scaffolding
C
1
star
18

wrdt

A simple CLI tool that helps to learn new words efficiently
Python
1
star