Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sanctuary

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanctuary

Refuge from unsafe JavaScript

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
28K
decreased by-23.46%
Maintainers
1
Weekly downloads
 
Created
Source

Sanctuary

Sanctuary is small functional programming library inspired by Haskell and PureScript. Sanctuary makes it possible to write safe code without null checks.

In JavaScript it's trivial to introduce a possible run-time type error:

words[0].toUpperCase()

If words is ['foo', 'bar', 'baz'] this expression will evaluate to 'FOO'. But what if words is []?

Sanctuary is stricter in its types than most JavaScript libraries. Its head function, for example, has type a -> Maybe a which means it never returns null or undefined. This forces one to consider the empty case.

S.head(words) evaluates to a value of type Maybe String. One may derive from it a value of type String by applying S.fromMaybe. The toUpperCase method can then be invoked safely:

// :: String
S.fromMaybe('', S.head(words)).toUpperCase()

Without Sanctuary, one might have written:

// :: String
(words.length > 0 ? words[0] : '').toUpperCase()

Maybe is a functor, so one can use its map method to produce another Maybe:

// :: Maybe String
S.head(words).map(function(word) { return word.toUpperCase(); })

This approach is even cleaner if one uses Ramda:

// :: Maybe String
R.map(R.toUpperCase, S.head(words))

FAQs

Package last updated on 28 Jan 2015

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc