All Projects → franeklubi → rustic

franeklubi / rustic

Licence: BSD-3-Clause License
rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!)

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rustic

bound
Data-binding made easy
Stars: ✭ 21 (-70.42%)
Mutual labels:  typescript-library
constant-time-js
Constant-time JavaScript functions
Stars: ✭ 43 (-39.44%)
Mutual labels:  typescript-library
safe-touch
⛓ Runtime optional chaining for JS
Stars: ✭ 71 (+0%)
Mutual labels:  typescript-library
ColorTranslator
A JavaScript library, written in TypeScript, to convert among different color models
Stars: ✭ 34 (-52.11%)
Mutual labels:  typescript-library
moment-recur-ts
Conversion of the moment-recur library into TypeScript.
Stars: ✭ 17 (-76.06%)
Mutual labels:  typescript-library
TypeScript-Library-Checklist
Your pre-launch checklist.
Stars: ✭ 19 (-73.24%)
Mutual labels:  typescript-library
ooop
OOP has never been sooo professionally over engineered before.
Stars: ✭ 20 (-71.83%)
Mutual labels:  typescript-library
fejl
Error-making utility for Node apps.
Stars: ✭ 30 (-57.75%)
Mutual labels:  typescript-library
material-color-utilities
Color libraries for Material You
Stars: ✭ 605 (+752.11%)
Mutual labels:  typescript-library
react-picture-annotation
A simple annotation component.
Stars: ✭ 53 (-25.35%)
Mutual labels:  typescript-library
TypeScript.NET-Core
A JavaScript-Friendly .NET Based TypeScript Library
Stars: ✭ 30 (-57.75%)
Mutual labels:  typescript-library
kraftfahrstrasse
Typescript implementation of WAMP protocol.
Stars: ✭ 20 (-71.83%)
Mutual labels:  typescript-library
jsonrpc-ts
A very flexible library for building JSON-RPC 2.0 endpoints
Stars: ✭ 19 (-73.24%)
Mutual labels:  typescript-library
orkid-node
Reliable and modern Redis Streams based task queue for Node.js 🤖
Stars: ✭ 61 (-14.08%)
Mutual labels:  typescript-library
tzientist
Scientist-like library for Node.js in TypeScript
Stars: ✭ 37 (-47.89%)
Mutual labels:  typescript-library
typijs
The Angular CMS Framework for building fully-featured SPA sites powered by NodeJS and MongoDB with TypeScript
Stars: ✭ 141 (+98.59%)
Mutual labels:  typescript-library
react-loading-icons
A TypeScript-React edition of Sam Herbert's amazing SVG Loaders.
Stars: ✭ 32 (-54.93%)
Mutual labels:  typescript-library
stan
🔨 Collection of front-end engineering tools
Stars: ✭ 19 (-73.24%)
Mutual labels:  typescript-library
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-80.28%)
Mutual labels:  typescript-library
sqlweb
SqlWeb is an extension of JsStore which allows to use sql query for performing database operation in IndexedDB.
Stars: ✭ 38 (-46.48%)
Mutual labels:  typescript-library

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('...')
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].