🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

@koober/std

Package Overview
Dependencies
Maintainers
5
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@koober/std

Standard library

1.0.0-rc.54
latest
Source
npm
Version published
Weekly downloads
167
-1.76%
Maintainers
5
Weekly downloads
 
Created
Source

Koober Standard Library (@koober/std)

NPM Version License

Standard library

About the project

std is a library for typed functional programming in TypeScript. It focuses on strong typing, purity, simplicity and restricted set of functionalities. Advanced functional programming patterns, immutability should not be achieved if it degrades significantly type safety and simplicity.

Balance
Loose typing□□□□□□■Strong typing
Impure, Mutable□□□□□■□Pure, Immutable
Simple Functional Programming□□■□□□□Advanced Functional Programming
Lean□□■□□□□Complete

Installation

npm install @koober/std

Usage

Enforce STD to write better code

VanillaJSSTDExplanation
null, undefinedOptionStop spending time choosing between undefined and null. Based on Microsoft standard, undefined (== Option.None) is preferred.
throw new Error()return Result.Error(new Error())Error throwing / Promise rejection is a mechanism that should only be used to stop the execution of a program. When a computation represents an expected failure (ex: parsing, data fetching), Result should be used.
PromiseTask.AsyncTask.Async is like a Promise but lazily evaluated. It has numerous advantages (composable, etc). See Article
N/ATime, DurationTagged types that makes the unit of time explicit (milliseconds). Some libraries could use seconds or minutes implicitly which is confusing
setTimeout(fn, ms)Task.andThen(Time.delay(ms), fn)setTimeout is impure, create a task that will run after Time.delay.
Date.nowTime.nowDate.now is impure, use Time.now that is a Task.Sync.
console.debugConsole.debugconsole.debug is impure, use Console.debug that is a Task.Sync.
Math.randomRandom.numberMath.random is impure, use Random.number that is a Task.Sync.
UUID, ...Task.SyncMore impure function, wrap them in a Task.Sync()
N/AIntA tagged type that narrows number to only the safe integer values
[].map, [].filter, ...Array.map, Array.filter, ...Array module contains all immutable operations on arrays.

Example

import { Result, runTask, Console } from '@koober/std';

function parseNumber(expr: string) {
  const parsed = Number(expr);

  // - Return a immutable Result object
  // - Avoid throwing error because impure
  // - Avoid using NaN because the error case is implicit in the typing
  return Number.isNaN(parsed) ? Result.Ok(parsed) : Result.Error('NotANumber');
}

export function main() {
  const parsed = parseNumber('1.1'); // Result.Ok(1.1)
  const computed = Result.map(parsed, (amount) => amount + 2); // Result.Ok(3.1)

  // Lazy operation that will display in console the computed result when evaluated
  return Console.debug(computed);
}

// runTask is impure and should be put at the edge of the program
runTask(main()); // prints { _type: 'Result/Ok', value: 3.1 }

License

MIT © Julien Polo julien.polo@koober.com

Keywords

fp

FAQs

Package last updated on 01 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts