env-dot-prop
Advanced tools
Comparing version 1.0.2 to 1.1.0
204
index.js
@@ -7,6 +7,33 @@ 'use strict'; | ||
/** | ||
* Replace a character in the string provided taking care of the escaped chars. | ||
* @private | ||
* @param {string} str A string. | ||
* @param {from} from A character. | ||
* @param {to} to A character. | ||
* @return {string} A new string with the character replaced. | ||
*/ | ||
function transform(str, from, to) { | ||
let out = ''; | ||
const escaped = '\\' + to; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === to) { | ||
out += escaped; | ||
} else if (str[i] === from) { | ||
out += to; | ||
} else if (str[i] === '\\' && i + 1 < str.length && str[i + 1] === from) { | ||
out += from; | ||
i++; | ||
} else { | ||
out += str[i]; | ||
} | ||
} | ||
return out; | ||
} | ||
/** | ||
* Converts a dot-path to an underscore-path. | ||
* @private | ||
* @param {string} path String separated by dots. | ||
* @return {string} String separated by underscores. | ||
* @param {string} path A string separated by dots. | ||
* @return {string} A new string separated by underscores. | ||
*/ | ||
@@ -20,4 +47,4 @@ function toUnderscore(path) { | ||
* @private | ||
* @param {string} env String separated by underscores. | ||
* @return {string} String separated by dots. | ||
* @param {string} env A string separated by underscores. | ||
* @return {string} A new string separated by dots. | ||
*/ | ||
@@ -29,11 +56,69 @@ function toDot(env) { | ||
/** | ||
* Returns the values of env keys at the path specified. | ||
* Return a list of environment variables that matches the path provided. | ||
* @private | ||
* @param {string} path A string separated by dots. | ||
* @param {Object} [opts] Additional options. | ||
* @return {string[]} An array of environment variables. | ||
*/ | ||
function keys(path, opts) { | ||
let env = toUnderscore(path); | ||
if (!opts || !opts.caseSensitive) { | ||
env = env.toUpperCase(); | ||
return Object.keys(process.env).filter(key => | ||
key.toUpperCase().startsWith(env) | ||
); | ||
} | ||
return Object.keys(process.env).filter(key => key.startsWith(env)); | ||
} | ||
function parse(str, opts) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
let ret; | ||
if (opts && opts.parse) { | ||
try { | ||
ret = circularJSON.parse(str); | ||
} catch (error) { | ||
ret = String(str); | ||
} | ||
} else { | ||
ret = String(str); | ||
} | ||
return ret; | ||
} | ||
function stringify(val, opts) { | ||
if (typeof val === 'string') { | ||
return val; | ||
} | ||
let ret; | ||
if (opts && opts.stringify) { | ||
try { | ||
ret = circularJSON.stringify(val); | ||
} catch (error) { | ||
ret = String(val); | ||
} | ||
} else { | ||
ret = String(val); | ||
} | ||
return ret; | ||
} | ||
/** | ||
* Gets the values of environment variables at the path specified. | ||
* @public | ||
* @param {string} path Dot separated path. | ||
* @param {string} [defaultValue] Default value to return if there aren't keys in | ||
* the path provided. | ||
* @param {object} [opts] Additional options. | ||
* @param {boolean} [opts.parse] If true the value returned is parsed using circular-json. | ||
* @return {string} The value at the path specified. | ||
* @param {boolean} [opts.caseSensitive] 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. | ||
* @param {any} [defaultValue=undefined] Default value to return if there is | ||
* not any environment variable that matches the path provided. | ||
* @param {Object} [opts] Additional options. | ||
* @param {boolean} [opts.parse=false] If true the value returned is parsed | ||
* using circular-json. | ||
* @param {boolean} [opts.caseSensitive=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. | ||
* @return {any} The values of environment variables associated with the path specified. | ||
*/ | ||
@@ -57,3 +142,3 @@ function get(path, defaultValue, opts) { | ||
const val = parse(process.env[key], opts); | ||
const val = process.env[key]; | ||
if (dotp === '') { | ||
@@ -86,4 +171,6 @@ obj = val; | ||
* @param {object} [opts] Additional options. | ||
* @param {boolean} [opts.stringify] If true the value passed is stringified using circular-json. | ||
* @param {boolean} [opts.caseSensitive] 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. | ||
* @param {boolean} [opts.stringify=false] If true the value passed is stringified using circular-json. | ||
* @param {boolean} [opts.caseSensitive=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. | ||
*/ | ||
@@ -101,14 +188,13 @@ function set(path, value, opts) { | ||
/** | ||
* Deletes an env key at the path specified. If nested keys are present they will | ||
* be deleted too. | ||
* Deletes an env key at the path specified. | ||
* If nested keys are present they will be deleted too. | ||
* @public | ||
* @param {string} path Dot separated path. | ||
* @param {string} path A dot separated path. | ||
* @param {object} [opts] Additional options. | ||
* @param {boolean} [opts.caseSensitive] 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. | ||
* @param {boolean} [opts.caseSensitive=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. | ||
*/ | ||
function del(path, opts) { | ||
return keys(path, opts) | ||
.forEach(key => { | ||
delete process.env[key]; | ||
}); | ||
keys(path, opts).forEach(key => delete process.env[key]); | ||
} | ||
@@ -121,4 +207,7 @@ | ||
* @param {object} [opts] Additional options. | ||
* @return {boolean} true if at least an env key with that path exists. | ||
* @param {boolean} [opts.caseSensitive] 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. | ||
* @param {boolean} [opts.caseSensitive=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. | ||
* @return {boolean} true if exists at least one environment variables with that | ||
* path prefix. | ||
*/ | ||
@@ -129,69 +218,2 @@ function has(path, opts) { | ||
function transform(str, from, to) { | ||
let out = ''; | ||
const escaped = '\\' + to; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === to) { | ||
out += escaped; | ||
} else if (str[i] === from) { | ||
out += to; | ||
} else if (str[i] === '\\' && i + 1 < str.length && str[i + 1] === from) { | ||
out += from; | ||
i++; | ||
} else { | ||
out += str[i]; | ||
} | ||
} | ||
return out; | ||
} | ||
function keys(path, opts) { | ||
let env = toUnderscore(path); | ||
if (!opts || !opts.caseSensitive) { | ||
env = env.toUpperCase(); | ||
return Object.keys(process.env) | ||
.filter(key => key.toUpperCase().startsWith(env)); | ||
} | ||
return Object.keys(process.env) | ||
.filter(key => key.startsWith(env)); | ||
} | ||
function parse(str, opts) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
let ret; | ||
if (opts && opts.parse) { | ||
try { | ||
ret = circularJSON.parse(str); | ||
} catch (err) { | ||
ret = String(str); | ||
} | ||
} else { | ||
ret = String(str); | ||
} | ||
return ret; | ||
} | ||
function stringify(val, opts) { | ||
if (typeof val === 'string') { | ||
return val; | ||
} | ||
let ret; | ||
if (opts && opts.stringify) { | ||
try { | ||
ret = circularJSON.stringify(val); | ||
} catch (err) { | ||
ret = String(val); | ||
} | ||
} else { | ||
ret = String(val); | ||
} | ||
return ret; | ||
} | ||
module.exports = { | ||
@@ -198,0 +220,0 @@ get: get, |
{ | ||
"version": "1.1.0", | ||
"name": "env-dot-prop", | ||
"version": "1.0.2", | ||
"description": "Get, set, or delete nested properties of process.env using a dot path", | ||
"main": "index.js", | ||
"dependencies": { | ||
"circular-json": "^0.4.0", | ||
"dot-prop": "^4.2.0" | ||
"license": "MIT", | ||
"homepage": "https://github.com/simonepri/env-dot-prop#readme", | ||
"repository": "github:simonepri/env-dot-prop", | ||
"bugs": { | ||
"url": "https://github.com/simonepri/env-dot-prop/issues", | ||
"email": "simonepri@outlook.com" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.23.0", | ||
"codecov": "^3.0.0", | ||
"nyc": "^11.3.0", | ||
"xo": "^0.19.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava", | ||
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/simonepri/env-dot-prop.git" | ||
}, | ||
"author": { | ||
"name": "Simone Primarosa", | ||
"email": "simone.pri@hotmail.it" | ||
}, | ||
"author": "Simone Primarosa <simonepri@outlook.com> (https://simoneprimarosa.com)", | ||
"contributors": [ | ||
"Simone Primarosa <simonepri@outlook.com> (https://simoneprimarosa.com)" | ||
], | ||
"keywords": [ | ||
@@ -44,27 +32,44 @@ "env-dot-prop", | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/simonepri/env-dot-prop/issues" | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"homepage": "https://github.com/simonepri/env-dot-prop#readme", | ||
"scripts": { | ||
"test": "xo &&nyc ava", | ||
"release": "np", | ||
"update": "npm-check -u" | ||
}, | ||
"dependencies": { | ||
"circular-json": "^0.5.9", | ||
"dot-prop": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"np": "*", | ||
"npm-check": "*", | ||
"nyc": "*", | ||
"xo": "*" | ||
}, | ||
"ava": { | ||
"verbose": true | ||
}, | ||
"nyc": { | ||
"reporter": [ | ||
"lcovonly", | ||
"text" | ||
] | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"space": true, | ||
"overrides": [ | ||
{ | ||
"files": "index.js", | ||
"esnext": false | ||
}, | ||
{ | ||
"files": "test.js", | ||
"rules": { | ||
"camelcase": [ | ||
"error", | ||
{ | ||
"properties": "never" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
"rules": { | ||
"object-shorthand": "off", | ||
"prefer-rest-params": "off", | ||
"camelcase": "off", | ||
"prefer-destructuring": "off" | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17281
196
242
5
1
6
1
+ Addedcircular-json@0.5.9(transitive)
- Removedcircular-json@0.4.0(transitive)
Updatedcircular-json@^0.5.9