https://github.com/lamansky/plainify
[Node.js] Wraps a value in a plain object, if it isn’t one already.
https://github.com/lamansky/plainify
Last synced: 3 months ago
JSON representation
[Node.js] Wraps a value in a plain object, if it isn’t one already.
- Host: GitHub
- URL: https://github.com/lamansky/plainify
- Owner: lamansky
- License: mit
- Created: 2018-04-14T15:37:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-14T15:39:10.000Z (about 8 years ago)
- Last Synced: 2025-10-24T18:48:05.056Z (8 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# plainify
Wraps a value in a plain object, if it isn’t one already.
Lets you specify a default single key for an options object, for example.
## Installation
Requires [Node.js](https://nodejs.org/) 4.0.0 or above.
```bash
npm i plainify
```
## API
The module exports a single function.
### Parameters
1. `key` (string or symbol): The key under which to file `x` if it’s not already a plain object.
2. `x` (any)
### Return Value
Returns `x` as-is if it’s a plain object. Otherwise, returns a new plain object with one entry, having `key` as the key and `x` as the value.
## Example
Let’s say you have an options object with keys `a`, `b`, and `c`. You can use `plainify` to specify `b` as the default key.
```javascript
const plainify = require('plainify')
function example (options) {
const {a, b, c} = plainify('b', options)
// ...
}
example({a: 1}) // a=1; b and c are undefined
example(2) // b=2; a and c are undefined
example() // a, b, and c are undefined
```
## Related
Inspired by [arrify](https://github.com/sindresorhus/arrify).