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

true-myth

Package Overview
Dependencies
Maintainers
2
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

true-myth

A library for safe functional programming in JavaScript, with first-class support for TypeScript

  • 8.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
302K
increased by20.47%
Maintainers
2
Weekly downloads
 
Created

What is true-myth?

The true-myth npm package provides a set of utilities for working with types that represent the presence or absence of a value (Option) and the success or failure of an operation (Result). It is inspired by functional programming concepts and aims to make error handling and value management more robust and expressive.

What are true-myth's main functionalities?

Option

The Option type represents a value that may or may not be present. It can be either Some (containing a value) or None (containing no value). The match method allows for pattern matching on the Option type.

const { Option } = require('true-myth');

const someValue = Option.of(42);
const noneValue = Option.none();

someValue.match({
  Some: (value) => console.log(`Got a value: ${value}`),
  None: () => console.log('Got nothing')
});

noneValue.match({
  Some: (value) => console.log(`Got a value: ${value}`),
  None: () => console.log('Got nothing')
});

Result

The Result type represents the outcome of an operation that can either succeed (Ok) or fail (Err). The match method allows for pattern matching on the Result type.

const { Result } = require('true-myth');

const successResult = Result.ok('Success!');
const failureResult = Result.err('Failure!');

successResult.match({
  Ok: (value) => console.log(`Operation succeeded with value: ${value}`),
  Err: (error) => console.log(`Operation failed with error: ${error}`)
});

failureResult.match({
  Ok: (value) => console.log(`Operation succeeded with value: ${value}`),
  Err: (error) => console.log(`Operation failed with error: ${error}`)
});

Chaining and Transformations

Both Option and Result types support chaining and transformations using methods like map. This allows for functional-style transformations of the contained values.

const { Option, Result } = require('true-myth');

const someValue = Option.of(42);
const transformedValue = someValue.map(value => value * 2);

transformedValue.match({
  Some: (value) => console.log(`Transformed value: ${value}`),
  None: () => console.log('Got nothing')
});

const successResult = Result.ok(42);
const transformedResult = successResult.map(value => value * 2);

transformedResult.match({
  Ok: (value) => console.log(`Transformed result: ${value}`),
  Err: (error) => console.log(`Operation failed with error: ${error}`)
});

Other packages similar to true-myth

Keywords

FAQs

Package last updated on 22 Aug 2024

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