https://github.com/freckle/maybe-js
Package for handling maybe operations
https://github.com/freckle/maybe-js
ghvm-managed
Last synced: 4 months ago
JSON representation
Package for handling maybe operations
- Host: GitHub
- URL: https://github.com/freckle/maybe-js
- Owner: freckle
- License: mit
- Created: 2022-02-04T19:24:01.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2026-01-22T09:55:49.000Z (5 months ago)
- Last Synced: 2026-01-23T00:25:58.785Z (5 months ago)
- Topics: ghvm-managed
- Language: TypeScript
- Homepage:
- Size: 247 KB
- Stars: 2
- Watchers: 7
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# @freckle/maybe
Provides a collection of helper functions for operations on maybe types.
## Motivation
This package seeks to recreate idioms common in strongly typed functional programming languages, with heavy influence from the [Data.Maybe Haskell package][Data.Maybe].
## Usage
Operations are used to refine an input from being possibly `null` or `undefined`:
```js
import {fromJust, fromMaybe, maybe} from '@freckle/maybe'
// Return input from a user, if any:
declare function getUserInput(): ?string
const mUserInput = getUserInput()
// Go from ?string -> string:
const input = fromMaybe(() => 'No input', mUserInput)
// Run a function on ?string with a default:
const capitalized = maybe(() => 'No input to capitalize', capitalize, mUserInput)
// Or produce an error on null | undefined to make future execution more predictable:
const userInput = fromJust(mUserInput, 'No input was given!')
```
Other operations carry the possibly `null` value after applying a function:
```js
import {mmap, mthen} from '@freckle/maybe'
// Return input from a user, if any:
declare function getUserInput(): ?string
// Function that does not handle a null | undefined value:
declare function transform(input: string): string
const mUserInput = getUserInput()
const mTransformed = mmap(transform, mUserInput)
// => null | undefined | transform(mUserInput)
// Alternate form that is more helpful for control flow:
mthen(mTransformed, (transformedUserInput: string) => {
// Process value
})
```
For dealing with Arrays that may contain `null` or `undefined` elements:
```js
import {catMaybes, mapMaybes} from '@freckle/maybe'
const arr = [
null,
'foo',
undefined,
'bar'
]
const out = catMaybes(arr)
console.log(out) // => ['foo', 'bar']
const padString = (input: string) => ` ${input}`
const mapped = mapMaybes(arr, padString)
console.log(mapped) // => [' foo', ' bar']
```
Two operations are tailored to use with React:
```js
import {mEffect, asHTMLAttributeValue} from '@freckle/maybe'
type Props = {
myInput: ?string
}
const MyComponent = (props: Props): React.Node => {
// This prop may be string | undefined | null
const possibleInput = props.myInput
React.useEffect(() => {
mEffect(possibleInput, input =>
// Call a side effect that does not handle a null value:
sideEffect(input)
)
}, [possibleInput])
// Rendering an element with an attribute from a maybe value:
const attrObj = {'my-attribute': asHTMLAttributeValue(possibleInput)}
// If input is not a string,
is rendered;
// otherwise:
return
}
```
[Data.Maybe]: https://hackage.haskell.org/package/base-4.16.0.0/docs/Data-Maybe.html