# stdopt

> Wrap and validate optional values

Latest version **13.0.0** (published 2020-05-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install stdopt
pnpm add stdopt
yarn add stdopt
bun add stdopt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 13.0.0 |
| Published | 2020-05-08 |
| First published | 2019-11-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 29.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Brecht Savelkoul |
| Maintainers | savelbr |
| Keywords | options, optional, option, wrap, unwrap, value, or, default, fallback, some, array, boolean, hash, list, number, object, string, monad |

## Links

- npm: https://www.npmjs.com/package/stdopt
- Repository: https://github.com/brechtcs/stdopt
- Homepage: https://github.com/brechtcs/stdopt#readme
- Issues: https://github.com/brechtcs/stdopt/issues
- npm.io page: https://npm.io/package/stdopt

## Dependencies (4)

- [verror](https://npm.io/package/verror.md) ^1.10.0
- [stdprop](https://npm.io/package/stdprop.md) ^1.0.1
- [inherits](https://npm.io/package/inherits.md) ^2.0.4
- [is-arrayish](https://npm.io/package/is-arrayish.md) ^0.3.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 13.0.0 (latest) — 2020-05-08
- 12.0.0 — 2020-05-04
- 11.1.0 — 2020-05-03
- 11.0.0 — 2020-04-30
- 10.2.1 — 2020-04-17
- 10.2.0 — 2020-04-16
- 10.1.0 — 2020-03-07
- 10.0.0 — 2020-03-06
- 9.4.0 — 2020-02-11
- 9.3.1 — 2020-02-10
- 9.3.0 — 2020-02-10
- 9.2.0 — 2020-02-10
- 9.1.0 — 2020-02-10
- 9.0.1 — 2020-02-10
- 9.0.0 — 2020-02-06
- … 23 more at https://npm.io/package/stdopt/versions

## README

# stdopt

Wrap and validate optional values

## Usage

This package provides wrappers to check inputs and provide fallbacks. There is a general `some` wrapper to check if a value is defined or not, as well as a handful of others for various primitives. The chained syntax is more verbose than the standard JS way (i.e. `var str = opts.str || 'default'`), but it is more solid and predictable.

```js
var { some } = require('stdopt')
some('some data').value() // => 'some data'
some(null).or('other data').value() // => 'other data'
some(null).or(undefined).value() // => throws error

// string primitive
var { string } = require('stdopt')
string('some text').value() // => 'some text'
string(true).or('other text').value() // => 'other text'
string(5).value() // => '5'

// number primitive
var { number } = require('stdopt')
number(5).value() // => 5
number(true).or(7).value() // => 7
number('11').value() // => 11

// boolean primitive
var { boolean } = require('stdopt')
boolean(false).value() // => false
boolean(null).or(true).value() // => true
boolean('True').value() // => true
boolean('fAlSe').value() // => false

// hash primitive
var { hash } = require('stdopt')
hash({stuff: true}).value() // => {stuff: true}
hash(true).or({other: false}).value() // => {other: false}
hash([1, 2, 3]).value() // => throws error

// list primitive
var { list } = require('stdopt')
list([1, 2, 3]).value() // => [1, 2, 3]
list('one').value() // => ['one']
list(null).or([4, 5, 6]).value() // => [4, 5, 6]
list({0: 'stuff', length: 1}).value() // => ['stuff']
```

## Custom

You can also create your own custom primitives,  by using the `Opt` class and defining a custom static `parse` method.

```js
var Opt = require('stdopt')

function lowercase (value) {
  if (!(this instanceof lowercase)) {
    return new lowercase(value)
  }
  Opt.call(this, value)
}

lowercase.parse = function (value) {
  if (typeof value === 'string' && value.toLowerCase() === value) {
    return value.toLowerCase()
  }
}

lowercase('oh.').value() // => 'oh.'
lowercase('AHA!').or('oh.').value() // => 'oh.'
lowercase('AHA!').value() // => throws error
```

## License

Apache-2.0

---
_Source: https://npm.io/package/stdopt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
