Comparing version 0.2.0 to 0.3.0
import type { DotEnvValue } from "./types"; | ||
type ParseOptions = { | ||
/** | ||
* Customize the RegEx used to assert valid env keys. | ||
* By default, keys must be alphanumeric and must not start with a number. | ||
* However, keys can start with or contain underscores, dashes, and periods. | ||
* @default /^[a-zA-Z_.-][a-zA-Z0-9_.-]*$/ | ||
* @example | ||
* "FOO" // valid | ||
* "FOO_BAR" // valid | ||
* "FOO-BAR" // valid | ||
* "FOO.BAR" // valid | ||
* "42" // invalid | ||
*/ | ||
regexEnvKey?: RegExp; | ||
}; | ||
export declare function parse(text: string, options?: ParseOptions): DotEnvValue[]; | ||
export {}; | ||
export declare function parse(text: string): DotEnvValue[]; |
@@ -5,19 +5,22 @@ "use strict"; | ||
const strip_comment_1 = require("./strip-comment"); | ||
const RE_VALID_ENV_KEY = /^[a-zA-Z_.-][a-zA-Z0-9_.-]*$/; | ||
const RE_EXPORTS = /^export\s+/; | ||
const RE_BOUNDING_QUOTES = /^"|"$/g; | ||
const RE_QUOTE_WITH_COMMENT = /(".*?")(\s*#\s*.*)?/; | ||
function parse(text, options) { | ||
const { regexEnvKey = RE_VALID_ENV_KEY } = options || {}; | ||
const lines = text.split("\n"); | ||
function parse(text) { | ||
const lines = text.trim().split("\n"); | ||
const keys = new Set(); | ||
const values = new Set(); | ||
for (let i = 0; i < lines.length; i++) { | ||
const [k, v = ""] = lines[i].trim().split("="); | ||
const line = lines[i].trim(); | ||
if (line.startsWith("#")) | ||
continue; | ||
if (!line.includes("=")) | ||
continue; | ||
const [k, v = ""] = line.split("="); | ||
const key = k.replace(RE_EXPORTS, ""); | ||
if (!regexEnvKey.test(key)) | ||
if (!key.trim()) | ||
continue; | ||
if (/\s+/g.test(key)) | ||
continue; | ||
let value = v.trim(); | ||
if (!value) | ||
continue; | ||
if (value.startsWith('"') && !RE_QUOTE_WITH_COMMENT.test(value)) { | ||
@@ -24,0 +27,0 @@ let multiline_value = value.replace(RE_BOUNDING_QUOTES, ""); |
@@ -5,6 +5,6 @@ "use strict"; | ||
function serialize(values, options) { | ||
const { removeDuplicates = false } = options || {}; | ||
const removeDuplicates = (options === null || options === void 0 ? void 0 : options.removeDuplicates) === true; | ||
let text = ""; | ||
for (const { key, value, duplicate } of values) { | ||
if (removeDuplicates === true && duplicate) | ||
if (duplicate && removeDuplicates) | ||
continue; | ||
@@ -11,0 +11,0 @@ text += `${key}=${/(#|\n)/.test(value) ? `"${value}"` : value}\n`; |
{ | ||
"name": "didone", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Minimalist dotenv-like parser for the browser", |
# didone | ||
> Minimalist dotenv-like parser for the browser. | ||
> Pronounced "dee-doh-neh". | ||
This zero-dependency library is a minimalist dotenv-like parser. Given a string, it returns an array of objects with the following properties: | ||
This zero-dependency library is a minimalist dotenv-like parser. It intentionally does not perform validation and is simply designed to extract key-value pairs from a given string. The `parse` function returns an array of objects with the following properties: | ||
- `key`: The key of the variable. | ||
- `value`: The value of the variable. Multi-line values are supported. | ||
- `value`: The value of the variable. Multiline values are supported. | ||
- `duplicate`: Whether the key is duplicated or not. | ||
@@ -46,2 +47,4 @@ | ||
If the provided text does not contain any key-value pairs, an empty array is returned. | ||
## Install | ||
@@ -69,14 +72,2 @@ | ||
#### Options | ||
The default `RegExp` used to validate env keys is `/^[a-zA-Z_.-][a-zA-Z0-9_.-]*$/`. This means that keys must be alphanumeric but must not start with a number. However, they can start with or contain underscores, dashes, and periods. | ||
You can customize the `RegExp` via the `regexEnvKey` option. | ||
```js | ||
parse("...", { | ||
regexEnvKey: /^[a-zA-Z][a-zA-Z0-9_.-]*$/, | ||
}); | ||
``` | ||
### `serialize` | ||
@@ -83,0 +74,0 @@ |
7415
106
104