Skip to content

Commit

Permalink
Merge f4ecc1a into 472db2e
Browse files Browse the repository at this point in the history
  • Loading branch information
albinekb committed Feb 15, 2018
2 parents 472db2e + f4ecc1a commit c85090f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
22 changes: 4 additions & 18 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs')
const path = require('path')
const parseKeyValue = require('parse-key-value-pair')

/*
* Parses a string or buffer into an object
Expand All @@ -14,24 +15,9 @@ function parse (src) {
// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
// matching "KEY' and 'VAL' in 'KEY=VAL'
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
if (keyValueArr != null) {
const key = keyValueArr[1]

// default undefined or missing values to empty string
let value = keyValueArr[2] || ''

// 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')
}

// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

obj[key] = value
const pair = parseKeyValue(line, { ignoreMalformed: true })
if (pair) {
obj[pair[0]] = pair[1]
}
})

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"standard": "8.4.0",
"standard-markdown": "2.2.0"
},
"dependencies": {},
"dependencies": {
"parse-key-value-pair": "^1.1.0"
},
"engines": {
"node": ">=4.6.0"
}
Expand Down

0 comments on commit c85090f

Please sign in to comment.