Skip to content

Instantly share code, notes, and snippets.

@misabitencourt
Last active July 10, 2023 17:22
Show Gist options
  • Save misabitencourt/2bd17b33cc2de3e3f4368f7169e34804 to your computer and use it in GitHub Desktop.
Save misabitencourt/2bd17b33cc2de3e3f4368f7169e34804 to your computer and use it in GitHub Desktop.
Javascript simple properties/env parser
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