Last active
July 10, 2023 17:22
-
-
Save misabitencourt/2bd17b33cc2de3e3f4368f7169e34804 to your computer and use it in GitHub Desktop.
Javascript simple properties/env parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const content = ` | |
PROPERTY=value | |
Property2=value2 | |
`; | |
const config = (content + '').split('\n').map(line => { | |
line = line.trim(); | |
if (line.indexOf('#') === 0) { | |
return ''; | |
} | |
return line; | |
}).reduce((acc, line) => { | |
if (!line) { | |
return acc; | |
} | |
const dividerIndex = line.indexOf('='); | |
if (dividerIndex < 2) { | |
return acc; | |
} | |
acc[line.substring(0, dividerIndex).trim()] = line.substring(dividerIndex + 1, line.length).trim(); | |
return acc; | |
}, {}); | |
console.log(config); // { PROPERTY: "value", Property2: "value2" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment