envdeep 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pemrouz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # envdeep
2
+
3
+ Parse environment variables into a nested object.
4
+
5
+ ```sh
6
+ npm i envdeep
7
+ ```
8
+
9
+ ```js
10
+ import env from 'envdeep'
11
+
12
+ // FERO_RETRIES_MAX=5 FERO_HOST=localhost
13
+ env()
14
+ // → { fero: { retries: { max: '5' }, host: 'localhost' } }
15
+
16
+ env({ FOO_BAR: '1' }) // any object works as the source
17
+ // → { foo: { bar: '1' } }
18
+ ```
19
+
20
+ ## Rules
21
+
22
+ - Keys are lowercased and split on `_`: `FOO_BAR_BAZ` → `foo.bar.baz`.
23
+ - Values are left as **strings**. Coercion (`'5'` → `5`) is the caller's job.
24
+ - A scalar and a nested object on the same path: the object wins, in either
25
+ order. `FOO=5` next to `FOO_BAZ=6` → `{ foo: { baz: '6' } }`.
26
+ - Empty segments are ignored: `_FOO`, `FOO__BAR`, `FOO_` behave like `FOO`,
27
+ `FOO_BAR`, `FOO`.
28
+ - `undefined` values are skipped.
29
+
30
+ Zero dependencies. ESM only. Ships TypeScript types.
31
+
32
+ ## License
33
+
34
+ MIT
@@ -0,0 +1,7 @@
1
+ /** What `process.env` looks like: string values, possibly undefined. */
2
+ export type Source = Record<string, string | undefined>;
3
+ /** The parsed tree: strings at the leaves, nested objects everywhere else. */
4
+ export type Parsed = {
5
+ [key: string]: string | Parsed;
6
+ };
7
+ export default function envdeep(env?: Source): Parsed;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ // envdeep — parse environment variables into a nested object.
2
+ //
3
+ // FOO_BAR=1 FOO_BAZ=2 → { foo: { bar: '1', baz: '2' } }
4
+ //
5
+ // Keys are lowercased and split on '_'. Values are left as strings — coercion
6
+ // is the caller's job (copts does it). When a scalar and a nested object land
7
+ // on the same path (FOO=5 next to FOO_BAZ=6), the object wins, in either
8
+ // order. Empty segments (_FOO, FOO__BAR, FOO_) are ignored.
9
+ const isObject = (v) => typeof v === 'object' && v !== null;
10
+ function parse(o, [key, value]) {
11
+ if (value === undefined)
12
+ return o;
13
+ const path = key.toLowerCase().split('_').filter(Boolean);
14
+ const last = path.pop();
15
+ if (last === undefined)
16
+ return o;
17
+ let n = o;
18
+ for (const seg of path) {
19
+ const next = n[seg];
20
+ n = isObject(next) ? next : (n[seg] = {});
21
+ }
22
+ // a nested object already built here (FOO_BAZ before FOO) outranks the scalar
23
+ if (!isObject(n[last]))
24
+ n[last] = value;
25
+ return o;
26
+ }
27
+ export default function envdeep(env = process.env) {
28
+ return Object.entries(env).reduce(parse, {});
29
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "envdeep",
3
+ "version": "1.0.0",
4
+ "description": "Parse environment variables into a nested object: FOO_BAR=1 → { foo: { bar: '1' } }",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.build.json",
20
+ "typecheck": "tsc -p tsconfig.json",
21
+ "test": "node --experimental-strip-types --no-warnings --test src/*.test.ts",
22
+ "prepublishOnly": "npm run build && npm run typecheck && npm test"
23
+ },
24
+ "keywords": [
25
+ "env",
26
+ "environment",
27
+ "variables",
28
+ "config",
29
+ "nested",
30
+ "parse"
31
+ ],
32
+ "author": "pemrouz (https://github.com/pemrouz)",
33
+ "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/pemrouz/envdeep.git"
37
+ },
38
+ "homepage": "https://github.com/pemrouz/envdeep#readme",
39
+ "bugs": {
40
+ "url": "https://github.com/pemrouz/envdeep/issues"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^26.4.1",
47
+ "typescript": "^5.9.3"
48
+ }
49
+ }