Comparing version 0.1.0 to 0.2.0
@@ -1,33 +0,2 @@ | ||
type DotEnvValue = { | ||
/** | ||
* The key of the environment variable. | ||
*/ | ||
key: string; | ||
/** | ||
* The value of the environment variable. | ||
* Multiline values are supported. | ||
*/ | ||
value: string; | ||
/** | ||
* `true` if the key is a duplicate of another key. | ||
* Its value will still be parsed. | ||
*/ | ||
duplicate: boolean; | ||
}; | ||
type Options = { | ||
/** | ||
* 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 parseDotEnv(text: string, options?: Options): DotEnvValue[]; | ||
export {}; | ||
export { parse } from "./parse"; | ||
export { serialize } from "./serialize"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseDotEnv = void 0; | ||
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 parseDotEnv(text, options) { | ||
const { regexEnvKey = RE_VALID_ENV_KEY } = options || {}; | ||
const lines = text.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 key = k.replace(RE_EXPORTS, ""); | ||
if (!regexEnvKey.test(key)) | ||
continue; | ||
let value = v.trim(); | ||
if (!value) | ||
continue; | ||
if (value.startsWith('"') && !RE_QUOTE_WITH_COMMENT.test(value)) { | ||
let multiline_value = value.replace(RE_BOUNDING_QUOTES, ""); | ||
while (i < lines.length - 1 && !lines[i].endsWith('"')) { | ||
i += 1; | ||
multiline_value += "\n" + removeComment(lines[i]).replace(/"$/, ""); | ||
} | ||
value = multiline_value; | ||
} | ||
else { | ||
value = removeComment(value); | ||
} | ||
values.add({ key, value, duplicate: keys.has(key) }); | ||
keys.add(key); | ||
} | ||
return Array.from(values); | ||
} | ||
exports.parseDotEnv = parseDotEnv; | ||
function removeComment(text) { | ||
const chars = text.split(""); | ||
let value = ""; | ||
let start = chars[0] === '"' ? 1 : 0; | ||
let break_char = chars[0] === '"' ? '"' : "#"; | ||
for (let i = start; i < chars.length; i++) { | ||
const char = chars[i]; | ||
if (char === break_char) | ||
break; | ||
value += char; | ||
} | ||
return value.trim(); | ||
} | ||
exports.serialize = exports.parse = void 0; | ||
var parse_1 = require("./parse"); | ||
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_1.parse; } }); | ||
var serialize_1 = require("./serialize"); | ||
Object.defineProperty(exports, "serialize", { enumerable: true, get: function () { return serialize_1.serialize; } }); |
{ | ||
"name": "didone", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Minimalist dotenv-like parser for the browser", |
# didone | ||
> Minimalist dotenv-like parser for the browser | ||
> Minimalist dotenv-like parser for the browser. | ||
> Pronounced "dee-doh-neh". | ||
This zero-dependency library is a minimalist dotenv-like parser for the browser. Given a string, it returns an array of objects with the following properties: | ||
This zero-dependency library is a minimalist dotenv-like parser. Given a string, it returns an array of objects with the following properties: | ||
@@ -13,3 +14,3 @@ - `key`: The key of the variable. | ||
DB_HOST=localhost | ||
DB_PORT="5432" # Quoted values | ||
DB_PORT="5432" | ||
DB_USER=myuser | ||
@@ -54,6 +55,8 @@ DB_USER=myuser2 # Duplicate keys are parsed but marked as duplicates | ||
### `parse` | ||
```js | ||
import { parseDotEnv } from "didone"; | ||
import { parse } from "didone"; | ||
const values = parseDotEnv( | ||
const values = parse( | ||
` | ||
@@ -67,10 +70,10 @@ DB_HOST=localhost | ||
### Options | ||
#### Options | ||
The default RegEx 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. | ||
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 RegEx via the `regexEnvKey` option. | ||
You can customize the `RegExp` via the `regexEnvKey` option. | ||
```js | ||
parseDotEnv("...", { | ||
parse("...", { | ||
regexEnvKey: /^[a-zA-Z][a-zA-Z0-9_.-]*$/, | ||
@@ -80,2 +83,28 @@ }); | ||
### `serialize` | ||
```js | ||
import { serialize } from "didone"; | ||
const text = serialize([ | ||
{ | ||
duplicate: false, | ||
key: "FOO", | ||
value: "bar", | ||
}, | ||
]); | ||
console.log(text); // "FOO=bar" | ||
``` | ||
#### Options | ||
Set `removeDuplicates` to `true` to remove duplicate keys. | ||
```js | ||
serialize(values, { | ||
removeDuplicates: true, | ||
}); | ||
``` | ||
## Changelog | ||
@@ -82,0 +111,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8042
11
119
113