optionkit 0.0.0-stage → 0.0.1

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 optionkit contributors
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 CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # optionkit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `optionkit` resolves typed Node.js configuration from defaults, environment
4
+ variables, and long CLI flags.
5
+
6
+ ```js
7
+ import { loadConfig } from "optionkit";
8
+
9
+ const config = loadConfig({
10
+ defaults: {
11
+ port: 3000,
12
+ debug: false,
13
+ logLevel: "info",
14
+ },
15
+ prefix: "MY_APP_",
16
+ });
17
+ ```
18
+
19
+ With `MY_APP_PORT=4000` and `node app.js --port=5000 --debug`, the result is:
20
+
21
+ ```js
22
+ {
23
+ port: 5000,
24
+ debug: true,
25
+ logLevel: "info",
26
+ }
27
+ ```
28
+
29
+ Precedence is `defaults < environment < CLI`. Camel-case keys map to
30
+ upper-snake-case environment names (`logLevel` becomes `MY_APP_LOG_LEVEL`) and
31
+ kebab-case CLI flags (`--log-level`). Boolean flags accept `--debug` and
32
+ `--no-debug`.
33
+
34
+ ## Install
35
+
36
+ ```sh
37
+ bun add optionkit
38
+ ```
39
+
40
+ ## License
41
+
42
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export type ConfigValue = string | number | boolean;
2
+
3
+ export interface LoadConfigOptions<T extends Record<string, ConfigValue>> {
4
+ defaults: T;
5
+ env?: Readonly<Record<string, string | undefined>>;
6
+ argv?: readonly string[];
7
+ prefix?: string;
8
+ }
9
+
10
+ /**
11
+ * Resolves configuration using defaults, environment variables, and CLI flags,
12
+ * in increasing order of precedence.
13
+ */
14
+ export declare function loadConfig<T extends Record<string, ConfigValue>>(
15
+ options: LoadConfigOptions<T>,
16
+ ): T;
package/index.js ADDED
@@ -0,0 +1,105 @@
1
+ function toConfigKey(flag) {
2
+ return flag.replace(/-([a-zA-Z0-9])/g, (_, character) => character.toUpperCase());
3
+ }
4
+
5
+ function toEnvironmentKey(key) {
6
+ return key
7
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
8
+ .replace(/-/g, "_")
9
+ .toUpperCase();
10
+ }
11
+
12
+ function parseBoolean(value, key, source) {
13
+ const normalized = String(value).toLowerCase();
14
+
15
+ if (["1", "true", "yes", "on"].includes(normalized)) return true;
16
+ if (["0", "false", "no", "off"].includes(normalized)) return false;
17
+
18
+ throw new TypeError(`${source} value for ${key} must be a boolean`);
19
+ }
20
+
21
+ function coerce(value, sample, key, source) {
22
+ if (typeof sample === "string") return String(value);
23
+ if (typeof sample === "boolean") return parseBoolean(value, key, source);
24
+
25
+ if (typeof sample === "number") {
26
+ const parsed = Number(value);
27
+ if (Number.isFinite(parsed)) return parsed;
28
+ throw new TypeError(`${source} value for ${key} must be a finite number`);
29
+ }
30
+
31
+ throw new TypeError(`default value for ${key} must be a string, number, or boolean`);
32
+ }
33
+
34
+ function parseArguments(argv) {
35
+ const values = new Map();
36
+
37
+ for (let index = 0; index < argv.length; index += 1) {
38
+ const token = argv[index];
39
+ if (token === "--") break;
40
+ if (!token.startsWith("--")) continue;
41
+
42
+ const body = token.slice(2);
43
+ if (body.startsWith("no-") && !body.includes("=")) {
44
+ values.set(toConfigKey(body.slice(3)), false);
45
+ continue;
46
+ }
47
+
48
+ const equalsAt = body.indexOf("=");
49
+ if (equalsAt !== -1) {
50
+ values.set(toConfigKey(body.slice(0, equalsAt)), body.slice(equalsAt + 1));
51
+ continue;
52
+ }
53
+
54
+ const key = toConfigKey(body);
55
+ const next = argv[index + 1];
56
+ if (next !== undefined && !next.startsWith("--")) {
57
+ values.set(key, next);
58
+ index += 1;
59
+ } else {
60
+ values.set(key, true);
61
+ }
62
+ }
63
+
64
+ return values;
65
+ }
66
+
67
+ /**
68
+ * Resolves a typed configuration object. Later sources override earlier ones:
69
+ * defaults, then environment variables, then command-line flags.
70
+ */
71
+ export function loadConfig({
72
+ defaults,
73
+ env = process.env,
74
+ argv = process.argv.slice(2),
75
+ prefix = "",
76
+ }) {
77
+ if (defaults === null || typeof defaults !== "object" || Array.isArray(defaults)) {
78
+ throw new TypeError("defaults must be an object");
79
+ }
80
+
81
+ const resolved = { ...defaults };
82
+
83
+ for (const [key, sample] of Object.entries(defaults)) {
84
+ coerce(sample, sample, key, "default");
85
+ const environmentKey = `${prefix}${toEnvironmentKey(key)}`;
86
+ const environmentValue = env[environmentKey];
87
+
88
+ if (environmentValue !== undefined) {
89
+ resolved[key] = coerce(environmentValue, sample, key, "environment");
90
+ }
91
+ }
92
+
93
+ for (const [key, value] of parseArguments(argv)) {
94
+ if (!Object.hasOwn(defaults, key)) continue;
95
+
96
+ const sample = defaults[key];
97
+ if (value === true && typeof sample !== "boolean") {
98
+ throw new TypeError(`CLI flag --${key} requires a value`);
99
+ }
100
+
101
+ resolved[key] = coerce(value, sample, key, "CLI");
102
+ }
103
+
104
+ return resolved;
105
+ }
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "optionkit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Resolve typed Node.js configuration from defaults, environment variables, and CLI flags.",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "types": "./index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "import": "./index.js",
12
+ "default": "./index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts"
18
+ ],
19
+ "scripts": {
20
+ "test": "bun test"
21
+ },
22
+ "keywords": [
23
+ "args",
24
+ "cli",
25
+ "config",
26
+ "environment"
27
+ ],
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/regbo/optionkit.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/regbo/optionkit/issues"
35
+ },
36
+ "homepage": "https://github.com/regbo/optionkit#readme",
37
+ "engines": {
38
+ "node": ">=18"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }