truthenv 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 Lino Brendler
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,80 @@
1
+ # truthenv
2
+
3
+ Tiny zero-dependency helper for safely reading boolean environment flags.
4
+
5
+ ```js
6
+ import envFlag from 'truthenv';
7
+
8
+ if (envFlag('FEATURE_AI_SEARCH')) {
9
+ console.log('enabled');
10
+ }
11
+ ```
12
+
13
+ Why? Because this common JavaScript pattern is buggy:
14
+
15
+ ```js
16
+ if (process.env.FEATURE_AI_SEARCH) {
17
+ // This also runs when FEATURE_AI_SEARCH="false"
18
+ }
19
+ ```
20
+
21
+ `truthenv` understands the values developers actually write in `.env`, CI and hosting dashboards:
22
+
23
+ | true | false |
24
+ | --- | --- |
25
+ | `true` | `false` |
26
+ | `1` | `0` |
27
+ | `yes` | `no` |
28
+ | `on` | `off` |
29
+ | `enabled` | `disabled` |
30
+ | `active` | `inactive` |
31
+ | `ok` | `nope` |
32
+
33
+ ## Install
34
+
35
+ ```bash
36
+ npm install truthenv
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```js
42
+ import { envFlag, toFlag } from 'truthenv';
43
+
44
+ const debug = envFlag('DEBUG');
45
+ const billingEnabled = envFlag('BILLING_ENABLED', { default: true });
46
+ const strictFeature = envFlag('FEATURE_X', { strict: true });
47
+
48
+ console.log(toFlag('off')); // false
49
+ console.log(toFlag('YES')); // true
50
+ ```
51
+
52
+ CommonJS also works:
53
+
54
+ ```js
55
+ const envFlag = require('truthenv');
56
+
57
+ if (envFlag('DEBUG')) {
58
+ console.log('debug mode');
59
+ }
60
+ ```
61
+
62
+ ## API
63
+
64
+ ### `envFlag(name, options?)`
65
+
66
+ Reads a value from `process.env` and returns a real boolean.
67
+
68
+ Options:
69
+
70
+ - `default`: value returned when the env var is missing or unknown. Default: `false`.
71
+ - `strict`: when `true`, unknown values throw a `TypeError`.
72
+ - `env`: custom env object for tests, edge runtimes or non-Node environments.
73
+
74
+ ### `toFlag(value, fallback?)`
75
+
76
+ Converts a raw value to a boolean without reading `process.env`.
77
+
78
+ ## License
79
+
80
+ MIT
package/index.cjs ADDED
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const TRUE_VALUES = new Set(['1', 'true', 't', 'yes', 'y', 'on', 'enabled', 'enable', 'active', 'ok']);
4
+ const FALSE_VALUES = new Set(['0', 'false', 'f', 'no', 'n', 'off', 'disabled', 'disable', 'inactive', 'nope', '']);
5
+
6
+ function toFlag(value, fallback = false) {
7
+ if (value === undefined || value === null) return fallback;
8
+ if (typeof value === 'boolean') return value;
9
+ if (typeof value === 'number') {
10
+ if (Number.isNaN(value)) return fallback;
11
+ return value !== 0;
12
+ }
13
+
14
+ const normalized = String(value).trim().toLowerCase();
15
+ if (TRUE_VALUES.has(normalized)) return true;
16
+ if (FALSE_VALUES.has(normalized)) return false;
17
+
18
+ return fallback;
19
+ }
20
+
21
+ function envFlag(name, options = {}) {
22
+ const env = options.env ?? (typeof process !== 'undefined' ? process.env : {});
23
+ const fallback = options.default ?? false;
24
+ const value = env?.[name];
25
+
26
+ if (options.strict && value !== undefined && value !== null) {
27
+ const normalized = String(value).trim().toLowerCase();
28
+ if (!TRUE_VALUES.has(normalized) && !FALSE_VALUES.has(normalized)) {
29
+ throw new TypeError(
30
+ `Invalid boolean env flag ${name}=${JSON.stringify(value)}. ` +
31
+ 'Use true/false, 1/0, yes/no, on/off, or enabled/disabled.'
32
+ );
33
+ }
34
+ }
35
+
36
+ return toFlag(value, fallback);
37
+ }
38
+
39
+ module.exports = envFlag;
40
+ module.exports.envFlag = envFlag;
41
+ module.exports.toFlag = toFlag;
42
+ module.exports.default = envFlag;
package/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export type EnvFlagOptions = {
2
+ /** Custom env object. Defaults to process.env when available. */
3
+ env?: Record<string, string | boolean | number | null | undefined>;
4
+ /** Returned when the variable is missing or unrecognized. Defaults to false. */
5
+ default?: boolean;
6
+ /** Throw on unrecognized values instead of returning the default. */
7
+ strict?: boolean;
8
+ };
9
+
10
+ export function toFlag(value: unknown, fallback?: boolean): boolean;
11
+ export function envFlag(name: string, options?: EnvFlagOptions): boolean;
12
+ export default envFlag;
package/index.js ADDED
@@ -0,0 +1,37 @@
1
+ const TRUE_VALUES = new Set(['1', 'true', 't', 'yes', 'y', 'on', 'enabled', 'enable', 'active', 'ok']);
2
+ const FALSE_VALUES = new Set(['0', 'false', 'f', 'no', 'n', 'off', 'disabled', 'disable', 'inactive', 'nope', '']);
3
+
4
+ export function toFlag(value, fallback = false) {
5
+ if (value === undefined || value === null) return fallback;
6
+ if (typeof value === 'boolean') return value;
7
+ if (typeof value === 'number') {
8
+ if (Number.isNaN(value)) return fallback;
9
+ return value !== 0;
10
+ }
11
+
12
+ const normalized = String(value).trim().toLowerCase();
13
+ if (TRUE_VALUES.has(normalized)) return true;
14
+ if (FALSE_VALUES.has(normalized)) return false;
15
+
16
+ return fallback;
17
+ }
18
+
19
+ export function envFlag(name, options = {}) {
20
+ const env = options.env ?? (typeof process !== 'undefined' ? process.env : {});
21
+ const fallback = options.default ?? false;
22
+ const value = env?.[name];
23
+
24
+ if (options.strict && value !== undefined && value !== null) {
25
+ const normalized = String(value).trim().toLowerCase();
26
+ if (!TRUE_VALUES.has(normalized) && !FALSE_VALUES.has(normalized)) {
27
+ throw new TypeError(
28
+ `Invalid boolean env flag ${name}=${JSON.stringify(value)}. ` +
29
+ 'Use true/false, 1/0, yes/no, on/off, or enabled/disabled.'
30
+ );
31
+ }
32
+ }
33
+
34
+ return toFlag(value, fallback);
35
+ }
36
+
37
+ export default envFlag;
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "truthenv",
3
+ "version": "1.0.0",
4
+ "description": "Tiny zero-dependency helper for safely reading boolean environment flags.",
5
+ "type": "module",
6
+ "main": "./index.cjs",
7
+ "module": "./index.js",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "import": "./index.js",
13
+ "require": "./index.cjs"
14
+ }
15
+ },
16
+ "sideEffects": false,
17
+ "files": [
18
+ "index.js",
19
+ "index.cjs",
20
+ "index.d.ts",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "keywords": [
25
+ "env",
26
+ "environment",
27
+ "boolean",
28
+ "flag",
29
+ "dotenv",
30
+ "config",
31
+ "zero-dependency"
32
+ ],
33
+ "author": "Lino Brendler",
34
+ "license": "MIT",
35
+ "engines": {
36
+ "node": ">=14"
37
+ },
38
+ "scripts": {
39
+ "test": "node test.mjs"
40
+ }
41
+ }