dotenv-haphap
Advanced tools
Comparing version 3.0.1 to 3.1.0
63
main.js
@@ -21,7 +21,6 @@ 'use strict'; | ||
/* | ||
* (stolen from dotenv) | ||
* Parses a string or buffer into an object | ||
* @param {(string|Buffer)} src - source to be parsed | ||
* @returns {Object} keys and values from src | ||
*/ | ||
*/ | ||
function parse(src) { | ||
@@ -34,23 +33,53 @@ const obj = {}; | ||
.split('\n') | ||
.forEach(function(line) { | ||
.reduce( | ||
({ lines, currentMultiLine }, line) => { | ||
if (currentMultiLine === null) { | ||
const firtsEqualsSign = line.indexOf('='); | ||
const startOfMultiline = line.indexOf('=`'); | ||
if (startOfMultiline === -1 || firtsEqualsSign !== startOfMultiline) { | ||
return { lines: lines.concat(line), currentMultiLine: null }; | ||
} else { | ||
return { lines, currentMultiLine: line }; | ||
} | ||
} else { | ||
const isLastPieceOfMultiline = line.match(/`$/); | ||
const multilineSoFar = currentMultiLine + '\n' + line; | ||
if (isLastPieceOfMultiline) { | ||
return { lines: lines.concat(multilineSoFar), currentMultiLine: null }; | ||
} else { | ||
return { lines, currentMultiLine: multilineSoFar }; | ||
} | ||
} | ||
}, | ||
{ lines: [], currentMultiLine: null } | ||
) | ||
.lines.forEach(function(line) { | ||
// matching "KEY' and 'VAL' in 'KEY=VAL' | ||
const keyValueArr = line.match(/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/); | ||
const keyValueArr = line.match(/^\s*([\w.-]+)\s*=\s*([\s\S]*)?\s*/); | ||
// matched? | ||
if (keyValueArr != null) { | ||
const key = keyValueArr[1]; | ||
if (keyValueArr === null) { | ||
return; | ||
} | ||
// default undefined or missing values to empty string | ||
let value = keyValueArr[2] || ''; | ||
const key = keyValueArr[1]; | ||
// default undefined or missing values to empty string | ||
let val = keyValueArr[2] || ''; | ||
const end = val.length - 1; | ||
const isDoubleQuoted = val[0] === '"' && val[end] === '"'; | ||
const isSingleQuoted = val[0] === "'" && val[end] === "'"; | ||
const isBackTickQuoted = val[0] === '`' && val[end] === '`'; | ||
// expand newlines in quoted values | ||
const len = value ? value.length : 0; | ||
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') { | ||
value = value.replace(/\\n/gm, '\n'); | ||
if (isSingleQuoted || isDoubleQuoted || isBackTickQuoted) { | ||
val = val.substring(1, end); | ||
// if double quoted, expand newlines | ||
if (isDoubleQuoted) { | ||
val = val.replace(/\\n/g, '\n'); | ||
} | ||
} else { | ||
// remove surrounding whitespace | ||
val = val.trim(); | ||
} | ||
// remove any surrounding quotes and extra spaces | ||
value = value.replace(/(^['"]|['"]$)/g, '').trim(); | ||
obj[key] = value; | ||
} | ||
obj[key] = val; | ||
}); | ||
@@ -57,0 +86,0 @@ |
{ | ||
"name": "dotenv-haphap", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "dotenv with multiple dotenv file support", | ||
"main": "main.js", | ||
"scripts": { | ||
"test": "echo 'haphap'" | ||
"test": "tape tests/*.spec.js", | ||
"tdd": "tape-watcher tests/*.spec.js" | ||
}, | ||
@@ -28,3 +29,7 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/mkls/dotenv-dot-env#readme" | ||
"homepage": "https://github.com/mkls/dotenv-dot-env#readme", | ||
"devDependencies": { | ||
"tape": "^4.10.2", | ||
"tape-watcher": "0.0.7" | ||
} | ||
} |
9489
6
175
2