Code Monkey home page Code Monkey logo

nanoption's Introduction

nanoption

Tiny (220 bytes), zero dependency wrapper for nullable values.

TL;DR

// With nanoption
import { Option } from 'nanoption'

function getJwtToken(executionContext) {
  const jwtToken = Option.of(executionContext)
    .map(context => context.switchToHttp()) // context can have no http mod (e.g. WebSocket)
    .map(httpMod => httpMod.getRequest())   // context can have no http request
    .map(request => request.headers)        // request can have no headers
    .map(headers => headers.authorizarion)  // headers can have no auth header
    .map(jwtAuth => jwtAuth.split(' ')[1])  // auth header can contain only one part
    .getOrElse('')

  return jwtToken
}

// Without nanoption
function getJwtToken(executionContext) {
  const httpMod = executionContext.switchToHttp()
  if (!httpMod) return ''  // context can have no http mod (e.g. WebSocket)

  const request = httpMod.getRequest()
  if (!request) return ''  // context can have no http request

  const headers = request.headers
  if (!headers) return ''  // request can have no headers

  const jwtAuth = headers.authorizarion
  if (!jwtAuth) return ''  // headers can have no auth header

  const jwtToken = wtAuth.split(' ')[1]
  if (!jwtToken) return '' // auth header can contain only one part
  
  return jwtAuth
}

Installation

yarn add nanoption

Or if you prefer npm:

npm i nanoption

Motivation

It JS in some cases we can get an error Cannot do something of undefined/null. This library explicitly works with nullable values and provide clear interface for it.

Also, library support TypeSciript as first-class citizen and do some checks in complie time. For example:

import { Option } from 'nanoption'

// #map
Option.of(2).map(_ => _ * 2) // Some(4)
Option.of().map(() => 2)     // None (known at compile time)

// #get
Option.of(3).get()           // 3
Option.of().get()            // COMPILE ERROR! Can't call get() on None

Of course, you can use JavaScript without TypeScript, but it's not as fun.

Now Optional Chaining Proposal only on stage 1, but very useful. This library give ECMAScript-version independent alternative for it.

API

import { Option } from 'nanoption'

// Create an option
Option.of(3)                                // Some(3)
Option.of('abc')                            // Some('abc')
Option.of(null)                             // None
Option.of(undefined)                        // None

// #map
Option.of(2).map(_ => _ * 2)                // Some(4)
Option.of().map(() => 2)                    // None

// #flatMap
Option.of(3).flatMap(_ => Option.of(_ * 2)) // Some(6)
Option.of().flatMap(() => Option.of(2))     // None

// #get
Option.of(3).get()                          // 3
Option.of().get()                           // throw Exception

// #getOrElse
Option.of(1).getOrElse(2)                   // 1
Option.of(null).getOrElse(2)                // 2

// #isEmpty
Option.of(2).isEmpty()                      // false
Option.of().isEmpty()                       // true

// #nonEmpty
Option.of(2).nonEmpty()                     // true
Option.of().nonEmpty()                      // false

// #toString
Option.of(2).toString()                     // "Some(2)"
Option.of().toString()                      // "None"

Alternatives

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.