Skip to content

Commit

Permalink
Merge pull request #47 from aegenet/feat-env-to-obj
Browse files Browse the repository at this point in the history
Feat `envToObject`
  • Loading branch information
aegenet committed Aug 3, 2024
2 parents 1267d29 + a06d2a6 commit 247ec0e
Show file tree
Hide file tree
Showing 24 changed files with 525 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .build/yawt.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"links": [],
"publish": true
},
{
"name": "belt-env-to-obj",
"links": [],
"publish": true
},
{
"name": "belt-array-async-filter",
"links": [],
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ packages/belt-benchmark/coverage/
packages/belt-benchmark/.nyc_output/
packages/belt-benchmark/.vscode/
packages/belt-benchmark/yarn-error.log
packages/belt-benchmark/temp/
packages/belt-benchmark/temp/
*.timestamp-*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
| [@aegenet/belt-array-string-join](./packages/belt-array-string-join/README.md) | `stringJoin()` `stringConcat()` => Array.join() is slow, this tool helps you to join() your string array faster |
| [@aegenet/belt-array-to-obj](./packages/belt-array-to-obj/README.md) | Array to Object |
| [@aegenet/belt-argv-to-obj](./packages/belt-argv-to-obj/README.md) | Convert an array of command line arguments (argv) to an object |
| [@aegenet/belt-env-to-obj](./packages/belt-env-to-obj/README.md) | Combine multiple environment variables into one JS object |
| [@aegenet/belt-base64](./packages/belt-base64/README.md) | Base64 (`toBase64`, `fromBase64`) |
| [@aegenet/belt-benchmark](./packages/belt-benchmark/README.md) | Benchmark your functions |
| [@aegenet/belt-binary-search](./packages/belt-binary-search/README.md) | Binary search |
Expand Down
4 changes: 4 additions & 0 deletions belt.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
"name": "Argv To Object",
"path": "./packages/belt-argv-to-obj"
},
{
"name": "Env To Object",
"path": "./packages/belt-env-to-obj"
},
{
"name": "Hook",
"path": "./packages/belt-hook"
Expand Down
18 changes: 16 additions & 2 deletions packages/belt-argv-to-obj/src/argv-to-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe('argvToObject', () => {
});

it('equals syntax', () => {
const argv = ['--name=John', '--age=25'];
const argv = ['--name=John', '--age=25', '--level=0'];
const result = argvToObject(argv);
const expected = { name: 'John', age: 25 };
const expected = { name: 'John', age: 25, level: 0 };
expect(result).toEqual(expected);
});

Expand Down Expand Up @@ -50,4 +50,18 @@ describe('argvToObject', () => {
const expected = { verbose: true, debug: false };
expect(result).toEqual(expected);
});

it('should not ignore empty value (quotes)', () => {
const argv = ["--something=''"];
const result = argvToObject(argv);
const expected = { something: '' };
expect(result).toEqual(expected);
});

it('should not ignore empty value (dbl quotes)', () => {
const argv = ['--something=""'];
const result = argvToObject(argv);
const expected = { something: '' };
expect(result).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion packages/belt-argv-to-obj/src/argv-to-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function argvToObject<O extends object = Record<string, string | boolean
value = valueMatch[1];
}
} else if (value) {
value = valueMatch[2] || valueMatch[3];
value = valueMatch[2] || valueMatch[3] || '';
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/belt-env-to-obj/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
shamefully-hoist=true
registry=https://registry.npmjs.org/
_always-auth= true
4 changes: 4 additions & 0 deletions packages/belt-env-to-obj/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { prettierConfigurator: configurator } = require('@aegenet/yawt');

module.exports = configurator();
21 changes: 21 additions & 0 deletions packages/belt-env-to-obj/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Alexandre Genet (https://github.com/aegenet)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
91 changes: 91 additions & 0 deletions packages/belt-env-to-obj/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[![npm version](https://img.shields.io/npm/v/@aegenet/belt-env-to-obj.svg)](https://www.npmjs.com/package/@aegenet/belt-env-to-obj)
<br>

# @aegenet/belt-env-to-obj

Combine multiple environment variables into one JS object.

## 💾 Installation

```shell
yarn add @aegenet/belt-env-to-obj@^1.6.0
# or
npm i @aegenet/belt-env-to-obj@^1.6.0
```

## 📝 Usage

### Common

```typescript
import { env } from 'node:process';
import { envToObject } from '@aegenet/belt-env-to-obj';

// { NAME: 'John', AGE: '25', IS_OK: 'true' }
const result = envToObject(env);
//=> { NAME: 'John', AGE: 25, IS_OK: true }
```

### Properties type

We automatically convert the value to `number` or `boolean` if possible.

| Type | Env value | Result Type | Result value |
| --- | --- | --- | --- |
| string | `true` | boolean | `true` |
| string | `false` | boolean | `false` |
| string | `25` | number | `25` |
| string | `Something` | string | `Something` |
| string | `'25'` | string | `25` |
| string | `'true'` | string | `true` |
| string | `"25"` | string | `"25"` |
| string | `"true"` | string | `"true"` |


### Nested object

```ts
// Default delimiter is '__' (double underscore)
const config = envToObject(
{
BELT__CONTACT__NAME: 'John',
BELT__CONTACT__AGE: '25',
BELT__CONTACT__IS_OK: 'true',
},
{
convertKey: key => key.toLowerCase(),
}
);
// config => { belt: { contact: { name: 'John', age: 25, is_ok: true } } }
```

```ts
// With a custom delimiter '.'
const config = envToObject(
{
'BELT.CONTACT.NAME': 'John',
'BELT.CONTACT.AGE': '25',
},
{
convertKey: key => key.toLowerCase(),
nestedDelimiter: '.',
}
);
// config => { belt: { contact: { name: 'John', age: 25 } } }
```

```ts
// With a custom delimiter RegExp
const config = envToObject(
{
'BELT.CONTACT_NAME': 'John',
'BELT.CONTACT@AGE': '25',
},
{
convertKey: key => key.toLowerCase(),
// Every non-alphanumeric character
nestedDelimiter: /[^a-zA-Z0-9]/,
}
);
// config => { belt: { contact: { name: 'John', age: 25 } } }
```
4 changes: 4 additions & 0 deletions packages/belt-env-to-obj/eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { eslintConfigurator: configurator } = require('@aegenet/yawt');

module.exports = configurator();
14 changes: 14 additions & 0 deletions packages/belt-env-to-obj/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions packages/belt-env-to-obj/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@aegenet/belt-env-to-obj",
"description": "Combine multiple environment variables into one JS object",
"keywords": ["argv"],
"version": "0.0.0-dev",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"browser": "dist/index.mjs",
"exports": {
"node": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/bundle.d.ts"
},
"default": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/bundle.d.ts"
}
},
"typings": "./dist/bundle.d.ts",
"types": "./dist/bundle.d.ts",
"author": "Alexandre Genet",
"license": "MIT",
"files": [
"dist"
],
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"repository": "https://github.com/aegenet/belt/tree/master/packages/belt-env-to-obj",
"dependencies": {},
"peerDependencies": {},
"devDependencies": {},
"pre-commit": [
"lint"
],
"scripts": {
"clean": "node ./../../node_modules/rimraf/dist/esm/bin.mjs ./dist ./coverage ./.nyc_output ./node_modules",
"lint": "node ./../../node_modules/eslint/bin/eslint ./src/**/*.{js,ts,tsx}",
"lint:fix": "node ./../../node_modules/eslint/bin/eslint ./src/**/*.{js,ts,tsx} --fix",
"test:vitest:browser": "vitest run --environment jsdom",
"test:vitest:watch": "vitest",
"test:vitest": "vitest run",
"build": "npm run tsc && npm run build:vite && npm run build:dts",
"tsc": "node ./../../node_modules/typescript/bin/tsc --noEmit",
"build:dts": "node ./../../node_modules/rollup/dist/bin/rollup -c rollup.config.dts.mjs",
"build:vite": "node ./../../node_modules/vite/bin/vite build"
}
}
10 changes: 10 additions & 0 deletions packages/belt-env-to-obj/rollup.config.dts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { rollupDTSConfigurator } from '@aegenet/yawt';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

export default rollupDTSConfigurator({
cwd: dirname(fileURLToPath(import.meta.url)),
libName: '@aegenet/belt-env-to-obj',
entryPoint: 'src/index.ts',
nodeExternal: true,
});
Loading

0 comments on commit 247ec0e

Please sign in to comment.