Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
linhntaim committed Nov 3, 2023
2 parents f3ffd78 + df06873 commit 0cf38f8
Show file tree
Hide file tree
Showing 15 changed files with 2,297 additions and 849 deletions.
7 changes: 7 additions & 0 deletions .coveralls.yml.example
@@ -0,0 +1,7 @@
service_name: $CI_NAME
service_job_id: $CI_JOB_ID
service_build_url: $CI_BUILD_URL
service_branch: $CI_BRANCH
service_pull_request: $CI_PULL_REQUEST

repo_token: $COVERALLS_REPO_TOKEN
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
@@ -0,0 +1,33 @@
name: build
run-name: ${{ github.actor }} is making a build
on:
push:
branches:
- master
jobs:
test-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
- name: Information
run: |
echo "Node.js"
node -v
echo "npm"
npm -v
- name: Install
run: npm ci
- name: Test
run: |
npm test
mkdir coverage
npm run test:report > coverage/test.lcov
- name: Publish test result to Coveralls
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,6 +18,7 @@ lerna-debug.log*
*.sw?

# Test results
.coveralls.yml
.nyc_output
coverage

Expand Down
3 changes: 3 additions & 0 deletions .npmignore
Expand Up @@ -19,6 +19,7 @@ lerna-debug.log*

# Test results
.nyc_output
.coveralls.yml
coverage

# Common
Expand All @@ -41,6 +42,8 @@ babel.config.*
.nycrc
.nycrc.*
nyc.config.js
.coveralls.yml.example
.travis.yml
.github
/tests
/src
64 changes: 61 additions & 3 deletions README.md
@@ -1,9 +1,9 @@
# dotenv-conversion

[![NPM version](https://img.shields.io/npm/v/dotenv-conversion.svg?style=flat-square)](https://www.npmjs.com/package/dotenv-conversion)
[![Travis (.org)](https://img.shields.io/travis/com/linhntaim/dotenv-conversion?style=flat-square)](https://app.travis-ci.com/github/linhntaim/dotenv-conversion)
[![Coveralls github](https://img.shields.io/coveralls/github/linhntaim/dotenv-conversion?style=flat-square)](https://coveralls.io/github/linhntaim/dotenv-conversion)
[![NPM](https://img.shields.io/npm/l/dotenv-conversion?style=flat-square)](https://github.com/linhntaim/dotenv-conversion/blob/master/LICENSE)
[![Github Actions](https://img.shields.io/github/actions/workflow/status/linhntaim/dotenv-conversion/build.yml?style=flat-square)](https://github.com/linhntaim/dotenv-conversion/actions/workflows/build.yml)
[![Coveralls](https://img.shields.io/coveralls/github/linhntaim/dotenv-conversion?style=flat-square)](https://coveralls.io/github/linhntaim/dotenv-conversion)
[![License](https://img.shields.io/npm/l/dotenv-conversion?style=flat-square)](https://github.com/linhntaim/dotenv-conversion/blob/master/LICENSE)

`dotenv-conversion` adds variable conversion on top of `dotenv`. If you find yourself
needing to convert/transform environment variables to anything more useful than strings,
Expand Down Expand Up @@ -468,6 +468,10 @@ console.log(process.env.VARIABLE_24) // (string) '10'
console.log(process.env.VARIABLE_25) // (string) '-10'
```

***Note:* You can disable the support for binary, octal or hexadecimal number format
by setting the option [`binaryNumber`](#binarynumber),
[`octalNumber`](#octalnumber) or [`hexadecimalNumber`](#hexadecimalnumber) to false.

- **bigint**

Values to be converted to bigint must match the format: `${value}n`;
Expand Down Expand Up @@ -518,6 +522,10 @@ console.log(process.env.VARIABLE_8) // (string) '10n'
console.log(process.env.VARIABLE_9) // (string) '10n'
```

***Note:* You can disable the support for binary, octal or hexadecimal bigint format
by setting the option [`binaryBigInt`](#binarybigint),
[`octalBigInt`](#octalbigint) or [`hexadecimalBigInt`](#hexadecimalbigint) to false.

- **symbol**

Values to be converted to symbol must match the format: `Symbol(${string})`.
Expand Down Expand Up @@ -850,6 +858,10 @@ console.log(process.env.VARIABLE_25) // (string) '10'
console.log(process.env.VARIABLE_26) // (string) '0'
```

***Note:* You can disable the conversion for binary, octal or hexadecimal number format
by setting the option [`binaryNumber`](#binarynumber),
[`octalNumber`](#octalnumber) or [`hexadecimalNumber`](#hexadecimalnumber) to false.

- **bigint**

This method is to convert any value to bigint.
Expand Down Expand Up @@ -953,6 +965,10 @@ console.log(process.env.VARIABLE_27) // (string) '10n'
console.log(process.env.VARIABLE_28) // (string) '0n'
```

***Note:* You can disable the conversion for binary, octal or hexadecimal bigint format
by setting the option [`binaryBigInt`](#binarybigint),
[`octalBigInt`](#octalbigint) or [`hexadecimalBigInt`](#hexadecimalbigint) to false.

- **string**

This method is to keep any value as it is.
Expand Down Expand Up @@ -1740,6 +1756,48 @@ If this option is set to `true`, they won't.

See [this feature](#ignore-processenv).

##### `binaryNumber`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in binary number format
will not be converted to number.

##### `octalNumber`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in octal number format
will not be converted to number.

##### `hexadecimalNumber`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in hexadecimal number format
will not be converted to number.

##### `binaryBigInt`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in binary bigint format
will not be converted to bigint.

##### `octalBigInt`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in octal bigint format
will not be converted to bigint.

##### `hexadecimalBigInt`

*Type:* `boolean`. *Default:* `true`.

If this option is set to `false`, the string in hexadecimal bigint format
will not be converted to bigint.

##### `prevents`

*Type:* `array`. *Default:* `[]`.
Expand Down
121 changes: 98 additions & 23 deletions dist/index.js
Expand Up @@ -10,9 +10,13 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
*/

var NUMBER_REGEX = /^[+-]?(\d+(\.(\d*)?)?|\.\d+)([eE][+-]?\d+)?$/;
var NUM_BOH_REGEX = /^[+-]?0([bB][01]+|[oO][0-8]+|[xX][0-9a-fA-F]+)$/;
var NUM_BIN_REGEX = /^[+-]?0[bB][01]+$/;
var NUM_OCT_REGEX = /^[+-]?0[oO][0-8]+$/;
var NUM_HEX_REGEX = /^[+-]?0[xX][0-9a-fA-F]+$/;
var BIGINT_REGEX = /^[+-]?\d+n$/;
var BIG_BOH_REGEX = /^[+-]?0([bB][01]+|[oO][0-8]+|[xX][0-9a-fA-F]+)n$/;
var BIG_BIN_REGEX = /^[+-]?0[bB][01]+n$/;
var BIG_OCT_REGEX = /^[+-]?0[oO][0-8]+n$/;
var BIG_HEX_REGEX = /^[+-]?0[xX][0-9a-fA-F]+n$/;
var SYMBOL_REGEX = /^Symbol\(.*\)$/;
var ARRAY_REGEX = /^\[.*]$/;
var ARRAY_EMPTY_REGEX = /^\[\s*]$/;
Expand Down Expand Up @@ -71,9 +75,14 @@ function parseNumber(str) {
/**
*
* @param {string} str
* @returns {number}
* @param {boolean} parsed
* @returns {number|string}
*/
function parseBohNumber(str) {
var parsed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (!parsed) {
return str;
}
switch (str[0]) {
case '+':
return Number(str.substring(1));
Expand All @@ -96,9 +105,14 @@ function parseBigInt(str) {
/**
*
* @param {string} str
* @returns {bigint}
* @param {boolean} parsed
* @returns {bigint|string}
*/
function parseBohBigInt(str) {
var parsed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (!parsed) {
return str;
}
switch (str[0]) {
case '+':
return BigInt(str.slice(1, -1));
Expand Down Expand Up @@ -130,34 +144,47 @@ function parseSymbol(str) {
/**
*
* @param {string|*} value
* @param {object} valueTable
* @param {boolean} fromDotEnv
* @param {object} config
* @returns {null|undefined|boolean|number|bigint|string|symbol|array|object}
*/
function restoreValue(value, valueTable, fromDotEnv) {
function restoreValue(value, config) {
if (!(typeof value == 'string' || value instanceof String)) {
return value;
}
if (fromDotEnv) {
if (config.fromDotEnv) {
value = unescapeValue(value);
}
var trimmed = value.trim();
// defined values
var valueTable = config._cache.valueTables.forAutoForced;
if (trimmed in valueTable) {
return valueTable[trimmed];
}
// Number
if (NUMBER_REGEX.test(trimmed)) {
return parseNumber(trimmed);
}
if (NUM_BOH_REGEX.test(trimmed)) {
return parseBohNumber(trimmed);
if (NUM_BIN_REGEX.test(trimmed)) {
return parseBohNumber(trimmed, config.binaryNumber);
}
if (NUM_OCT_REGEX.test(trimmed)) {
return parseBohNumber(trimmed, config.octalNumber);
}
if (NUM_HEX_REGEX.test(trimmed)) {
return parseBohNumber(trimmed, config.hexadecimalNumber);
}
// BigInt
if (BIGINT_REGEX.test(trimmed)) {
return parseBigInt(trimmed);
}
if (BIG_BOH_REGEX.test(trimmed)) {
return parseBohBigInt(trimmed);
if (BIG_BIN_REGEX.test(trimmed)) {
return parseBohBigInt(trimmed, config.binaryBigInt);
}
if (BIG_OCT_REGEX.test(trimmed)) {
return parseBohBigInt(trimmed, config.octalBigInt);
}
if (BIG_HEX_REGEX.test(trimmed)) {
return parseBohBigInt(trimmed, config.hexadecimalBigInt);
}
// Symbol
if (SYMBOL_REGEX.test(trimmed)) {
Expand Down Expand Up @@ -222,11 +249,17 @@ function defaultConfig() {
parsed: {},
fromDotEnv: true,
ignoreProcessEnv: false,
binaryNumber: true,
octalNumber: true,
hexadecimalNumber: true,
binaryBigInt: true,
octalBigInt: true,
hexadecimalBigInt: true,
prevents: [],
specs: {},
methods: {
auto: function auto(value, name, config) {
value = restoreValue(value, config._cache.valueTables.forAutoForced, config.fromDotEnv);
value = restoreValue(value, config);
if (typeof value === 'string') {
var lTrimmed = value.replace(/^\s+/, '');
var findPossibleMethod = function findPossibleMethod(methods) {
Expand Down Expand Up @@ -261,13 +294,13 @@ function defaultConfig() {
if (NUMBER_REGEX.test(value)) {
return parseNumber(value) !== 0;
}
if (NUM_BOH_REGEX.test(value)) {
if (NUM_BIN_REGEX.test(value) || NUM_OCT_REGEX.test(value) || NUM_HEX_REGEX.test(value)) {
return parseBohNumber(value) !== 0;
}
if (BIGINT_REGEX.test(value)) {
return parseBigInt(value) !== 0n;
}
if (BIG_BOH_REGEX.test(value)) {
if (BIG_BIN_REGEX.test(value) || BIG_OCT_REGEX.test(value) || BIG_HEX_REGEX.test(value)) {
return parseBohBigInt(value) !== 0n;
}
return true;
Expand All @@ -284,14 +317,26 @@ function defaultConfig() {
if (NUMBER_REGEX.test(value)) {
return parseNumber(value);
}
if (NUM_BOH_REGEX.test(value)) {
return parseBohNumber(value);
if (NUM_BIN_REGEX.test(value)) {
return parseBohNumber(value, config.binaryNumber);
}
if (NUM_OCT_REGEX.test(value)) {
return parseBohNumber(value, config.octalNumber);
}
if (NUM_HEX_REGEX.test(value)) {
return parseBohNumber(value, config.hexadecimalNumber);
}
if (BIGINT_REGEX.test(value)) {
return parseNumber(value.slice(0, -1));
}
if (BIG_BOH_REGEX.test(value)) {
return parseBohNumber(value.slice(0, -1));
if (BIG_BIN_REGEX.test(value)) {
return parseBohNumber(value.slice(0, -1), config.binaryNumber);
}
if (BIG_OCT_REGEX.test(value)) {
return parseBohNumber(value.slice(0, -1), config.octalNumber);
}
if (BIG_HEX_REGEX.test(value)) {
return parseBohNumber(value.slice(0, -1), config.hexadecimalNumber);
}
return function (number) {
return Number.isNaN(number) ? 0 : safeZero(number);
Expand All @@ -309,14 +354,26 @@ function defaultConfig() {
if (NUMBER_REGEX.test(value)) {
return numberAsBigInt(parseNumber(value));
}
if (NUM_BOH_REGEX.test(value)) {
return numberAsBigInt(parseBohNumber(value));
if (NUM_BIN_REGEX.test(value)) {
return parseBohBigInt("".concat(value, "n"), config.binaryBigInt);
}
if (NUM_OCT_REGEX.test(value)) {
return parseBohBigInt("".concat(value, "n"), config.octalBigInt);
}
if (NUM_HEX_REGEX.test(value)) {
return parseBohBigInt("".concat(value, "n"), config.hexadecimalBigInt);
}
if (BIGINT_REGEX.test(value)) {
return parseBigInt(value);
}
if (BIG_BOH_REGEX.test(value)) {
return parseBohBigInt(value);
if (BIG_BIN_REGEX.test(value)) {
return parseBohBigInt(value, config.binaryBigInt);
}
if (BIG_OCT_REGEX.test(value)) {
return parseBohBigInt(value, config.octalBigInt);
}
if (BIG_HEX_REGEX.test(value)) {
return parseBohBigInt(value, config.hexadecimalBigInt);
}
return function (number) {
return Number.isNaN(number) ? 0n : numberAsBigInt(safeZero(number));
Expand Down Expand Up @@ -376,6 +433,24 @@ function mergeConfig(config) {
if ('ignoreProcessEnv' in config) {
mergingConfig.ignoreProcessEnv = config.ignoreProcessEnv;
}
if ('binaryNumber' in config) {
mergingConfig.binaryNumber = config.binaryNumber;
}
if ('octalNumber' in config) {
mergingConfig.octalNumber = config.octalNumber;
}
if ('hexadecimalNumber' in config) {
mergingConfig.hexadecimalNumber = config.hexadecimalNumber;
}
if ('binaryBigInt' in config) {
mergingConfig.binaryBigInt = config.binaryBigInt;
}
if ('octalBigInt' in config) {
mergingConfig.octalBigInt = config.octalBigInt;
}
if ('hexadecimalBigInt' in config) {
mergingConfig.hexadecimalBigInt = config.hexadecimalBigInt;
}
if ('prevents' in config) {
mergingConfig.prevents = config.prevents;
}
Expand Down

0 comments on commit 0cf38f8

Please sign in to comment.