Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonepri/env-dot-prop
♻️ Get, set, or delete nested properties of process.env using a dot path
https://github.com/simonepri/env-dot-prop
configs dot-prop env env-dot-prop environment-variables environment-vars nodejs
Last synced: about 1 month ago
JSON representation
♻️ Get, set, or delete nested properties of process.env using a dot path
- Host: GitHub
- URL: https://github.com/simonepri/env-dot-prop
- Owner: simonepri
- License: mit
- Created: 2017-07-21T15:47:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-19T04:16:09.000Z (over 3 years ago)
- Last Synced: 2024-10-22T16:11:03.984Z (about 2 months ago)
- Topics: configs, dot-prop, env, env-dot-prop, environment-variables, environment-vars, nodejs
- Language: JavaScript
- Homepage:
- Size: 109 KB
- Stars: 33
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-nodejs-cn - env-dot-prop - 使用 ```.``` 路径获取、设置或删除 process.env 的嵌套属性 (包 / 其他)
- awesome-nodejs - env-dot-prop - Get, set, or delete nested properties of process.env using a dot path. (Packages / Miscellaneous)
- awesome-nodejs - env-dot-prop - Get, set, or delete nested properties of process.env using a dot path - ★ 11 (Miscellaneous)
- awesome-node - env-dot-prop - Get, set, or delete nested properties of process.env using a dot path. (Packages / Miscellaneous)
- awesome-nodejs-cn - env-dot-prop - 使用点路径获取、设置或删除process.env的嵌套属性. (目录 / 其他)
- awesome-nodejs-cn - env-dot-prop - **star:33** 获取、设置或删除进程的嵌套属性使用点路径的env (包 / 杂项)
README
♻️ Get, set, or delete nested properties of process.env using a dot path
Coded with ❤️ by Simone Primarosa.
## Background
This package aim to let you access to your environment variables as if they were JavaScript object.
See [this guide][12factorsguide] to understand how to use this package to create a [12 Factor compliant][12factors] configuration system for you app.## Install
```
$ npm install --save env-dot-prop
```## Usage
```js
const envDotProp = require('env-dot-prop');// Let's assume process.env contains the following keys
process.env = {
FOO_BAR: 'unicorn',
'FOO_DOT.DOT': 'pony',
'FOO_UND\\_UND': 'whale'
};console.log(process.env);
// => { FOO_BAR: 'unicorn', 'FOO_DOT.DOT': 'pony', 'FOO_UND\_UND': 'whale' }
envDotProp.get('');
// => { foo: { bar: 'unicorn', 'dot.dot': 'pony', und_und: 'whale' } }// getter
envDotProp.get('foo.bar');
// => 'unicorn'envDotProp.get('foo.notDefined.deep');
// => undefinedenvDotProp.get('foo.notDefined.deep', 'default value');
// => 'default value'envDotProp.get('foo.dot\\.dot');
// => 'pony'// setter
envDotProp.set('foo.bar', 'b');
envDotProp.get('foo.bar');
// => 'b'envDotProp.get('');
// => { foo: { bar: 'b', 'dot.dot': 'pony', und_und: 'whale' } }envDotProp.set('foo.baz.e', 'x');
envDotProp.get('foo.baz.e');
// => 'x'
envDotProp.get('foo.baz');
// => { e: 'x' }envDotProp.get('');
// => { foo: { bar: 'b', baz: { e: 'x' }, 'dot.dot': 'pony', und_und: 'whale' } }// has
envDotProp.has('foo.bar');
// => true// deleter
envDotProp.delete('foo.bar');
envDotProp.get('foo');
// => { baz: { e: 'x' }, 'dot.dot': 'pony', und_und: 'whale' }envDotProp.delete('foo.baz.e');
envDotProp.get('foo.baz');
// => undefinedenvDotProp.set('n1', 42, {stringify: false});
envDotProp.get('n1', {parse: false});
// => 42
envDotProp.get('n1', {parse: true});
// => 42envDotProp.set('n2', 42, {stringify: true});
envDotProp.get('n2', {parse: false});
// => '42'
envDotProp.get('n2', {parse: true});
// => 42envDotProp.set('n3', 42);
envDotProp.get('n3');
// => 42envDotProp.set('n4', '42');
envDotProp.get('n4');
// => '42'envDotProp.get('');
// => { n1: '42', n1: 42, n3: 42, n4: '42', foo: { 'dot.dot': 'pony', und_und: 'whale' } }
console.log(process.env);
// => { 'FOO_DOT.DOT': 'pony', 'FOO_UND\_UND': 'whale', N1: '42', N2: 42, N3: 42, N4: '42' }
```## API
## get(path, [defaultValue], [opts]) ⇒
any
Gets the values of environment variables at the path specified.**Kind**: global function
**Returns**:any
- The values of environment variables associated with the path specified.
**Access**: public| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path |string
| | Dot separated path. |
| [defaultValue] |any
| | Default value to return if there is not any environment variable that matches the path provided. |
| [opts] |Object
| | Additional options. |
| [opts.parse] |boolean
|false
| If true the value retrieved is converted to the proper type. |
| [opts.caseSensitive] |boolean
|false
| If true no case conversion will be performed from the dot path provided to the env key search. Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY. |## set(path, value, [opts])
Sets an env key at the path specified. If nested keys are present they will
be deleted.**Kind**: global function
**Access**: public| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path |string
| | Dot separated path. |
| value |string
| | Value to set. |
| [opts] |object
| | Additional options. |
| [opts.stringify] |boolean
|false
| If true the value provided is converted to string. |
| [opts.caseSensitive] |boolean
|false
| If true no case conversion is performed from the dot path provided to the env key search. Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY. |## del(path, [opts])
Deletes an env key at the path specified.
If nested keys are present they will be deleted too.**Kind**: global function
**Access**: public| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path |string
| | A dot separated path. |
| [opts] |object
| | Additional options. |
| [opts.caseSensitive] |boolean
|false
| If true no case conversion is performed from the dot path provided to the env key search. Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY. |## has(path, [opts]) ⇒
boolean
Returns whether an env key exists at the path specified.**Kind**: global function
**Returns**:boolean
- true if exists at least one environment variables with that
path prefix.
**Access**: public| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path |string
| | Dot separated path. |
| [opts] |object
| | Additional options. |
| [opts.caseSensitive] |boolean
|false
| If true no case conversion is performed from the dot path provided to the env key search. Eg: 'tesT.kEy' will look for tesT_kEy environment variable instead of TEST_KEY. |## Authors
- **Simone Primarosa** - *Github* ([@simonepri][github:simonepri]) • *Twitter* ([@simoneprimarosa][twitter:simoneprimarosa])
See also the list of [contributors][contributors] who participated in this project.
## License
This project is licensed under the MIT License - see the [license][license] file for details.
[start]: https://github.com/simonepri/env-dot-prop#start-of-content
[new issue]: https://github.com/simonepri/env-dot-prop/issues/new
[contributors]: https://github.com/simonepri/env-dot-prop/contributors[license]: https://github.com/simonepri/env-dot-prop/tree/master/license
[github:simonepri]: https://github.com/simonepri
[twitter:simoneprimarosa]: http://twitter.com/intent/user?screen_name=simoneprimarosa[12factors]: https://12factor.net/config
[12factorsguide]: https://github.com/simonepri/env-dot-prop/wiki/Create-a-12-factor-compliant-configuration-system