@obinexusmk2/hypernum 0.1.0 → 0.1.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.
Files changed (140) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +355 -256
  3. package/dist/index.cjs +4 -8
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +4 -7
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.umd.js +2 -2
  9. package/dist/index.umd.js.map +1 -1
  10. package/dist/types/{config → src/config}/config-loader.d.ts +0 -0
  11. package/dist/types/src/config/config-loader.d.ts.map +1 -0
  12. package/dist/types/{config → src/config}/config-parser.d.ts +0 -0
  13. package/dist/types/src/config/config-parser.d.ts.map +1 -0
  14. package/dist/types/{config → src/config}/config-resolver.d.ts +0 -0
  15. package/dist/types/src/config/config-resolver.d.ts.map +1 -0
  16. package/dist/types/{config → src/config}/config-source.d.ts +0 -0
  17. package/dist/types/src/config/config-source.d.ts.map +1 -0
  18. package/dist/types/{config → src/config}/index.d.ts +0 -0
  19. package/dist/types/src/config/index.d.ts.map +1 -0
  20. package/dist/types/{core → src/core}/common.d.ts +0 -0
  21. package/dist/types/src/core/common.d.ts.map +1 -0
  22. package/dist/types/{core → src/core}/config.d.ts +0 -0
  23. package/dist/types/src/core/config.d.ts.map +1 -0
  24. package/dist/types/{core → src/core}/constants.d.ts +0 -0
  25. package/dist/types/src/core/constants.d.ts.map +1 -0
  26. package/dist/types/{core → src/core}/errors.d.ts +0 -0
  27. package/dist/types/src/core/errors.d.ts.map +1 -0
  28. package/dist/types/{core → src/core}/hypernum.d.ts +0 -0
  29. package/dist/types/src/core/hypernum.d.ts.map +1 -0
  30. package/dist/types/{core → src/core}/index.d.ts +0 -0
  31. package/dist/types/src/core/index.d.ts.map +1 -0
  32. package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
  33. package/dist/types/src/index.d.ts.map +1 -0
  34. package/dist/types/{operations → src/operations}/arithmetic.d.ts +0 -0
  35. package/dist/types/src/operations/arithmetic.d.ts.map +1 -0
  36. package/dist/types/{operations → src/operations}/bitwise.d.ts +0 -0
  37. package/dist/types/src/operations/bitwise.d.ts.map +1 -0
  38. package/dist/types/{operations → src/operations}/comparison.d.ts +0 -0
  39. package/dist/types/src/operations/comparison.d.ts.map +1 -0
  40. package/dist/types/{operations → src/operations}/conversion.d.ts +0 -0
  41. package/dist/types/src/operations/conversion.d.ts.map +1 -0
  42. package/dist/types/{operations → src/operations}/factorial.d.ts +0 -0
  43. package/dist/types/src/operations/factorial.d.ts.map +1 -0
  44. package/dist/types/{operations → src/operations}/index.d.ts +0 -0
  45. package/dist/types/src/operations/index.d.ts.map +1 -0
  46. package/dist/types/{operations → src/operations}/power.d.ts +0 -0
  47. package/dist/types/src/operations/power.d.ts.map +1 -0
  48. package/dist/types/{storage → src/storage}/Heap.d.ts +0 -0
  49. package/dist/types/src/storage/Heap.d.ts.map +1 -0
  50. package/dist/types/{storage → src/storage}/index.d.ts +0 -0
  51. package/dist/types/src/storage/index.d.ts.map +1 -0
  52. package/dist/types/{structures → src/structures}/ackermann.d.ts +0 -0
  53. package/dist/types/src/structures/ackermann.d.ts.map +1 -0
  54. package/dist/types/{structures → src/structures}/big-array.d.ts +0 -0
  55. package/dist/types/src/structures/big-array.d.ts.map +1 -0
  56. package/dist/types/{structures → src/structures}/index.d.ts +0 -0
  57. package/dist/types/src/structures/index.d.ts.map +1 -0
  58. package/dist/types/{structures → src/structures}/number-tree.d.ts +0 -0
  59. package/dist/types/src/structures/number-tree.d.ts.map +1 -0
  60. package/dist/types/{structures → src/structures}/power-tower.d.ts +0 -0
  61. package/dist/types/src/structures/power-tower.d.ts.map +1 -0
  62. package/dist/types/{utils → src/utils}/formatting.d.ts +0 -0
  63. package/dist/types/src/utils/formatting.d.ts.map +1 -0
  64. package/dist/types/{utils → src/utils}/index.d.ts +0 -0
  65. package/dist/types/src/utils/index.d.ts.map +1 -0
  66. package/dist/types/{utils → src/utils}/parser.d.ts +0 -0
  67. package/dist/types/src/utils/parser.d.ts.map +1 -0
  68. package/dist/types/{utils → src/utils}/precision.d.ts +0 -0
  69. package/dist/types/src/utils/precision.d.ts.map +1 -0
  70. package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
  71. package/dist/types/src/utils/validation.d.ts.map +1 -0
  72. package/package.json +169 -164
  73. package/rollup.config.js +163 -161
  74. package/src/cli/hypernum.js +272 -0
  75. package/src/config/config-loader.ts +0 -0
  76. package/src/config/config-parser.ts +160 -160
  77. package/src/config/config-resolver.ts +0 -0
  78. package/src/config/config-source.ts +0 -0
  79. package/src/config/index.ts +0 -0
  80. package/src/core/common.ts +184 -184
  81. package/src/core/config.ts +392 -392
  82. package/src/core/constants.ts +101 -101
  83. package/src/core/errors.ts +202 -202
  84. package/src/core/hypernum.ts +240 -240
  85. package/src/core/index.ts +4 -4
  86. package/src/index.ts +179 -182
  87. package/src/operations/arithmetic.ts +332 -332
  88. package/src/operations/bitwise.ts +366 -366
  89. package/src/operations/comparison.ts +271 -271
  90. package/src/operations/conversion.ts +399 -399
  91. package/src/operations/factorial.ts +278 -278
  92. package/src/operations/index.ts +4 -4
  93. package/src/operations/power.ts +315 -315
  94. package/src/storage/Heap.ts +237 -237
  95. package/src/storage/index.ts +0 -0
  96. package/src/structures/ackermann.ts +232 -232
  97. package/src/structures/big-array.ts +305 -305
  98. package/src/structures/index.ts +3 -3
  99. package/src/structures/number-tree.ts +403 -403
  100. package/src/structures/power-tower.ts +277 -277
  101. package/src/types/common.d.ts +356 -356
  102. package/src/types/core.d.ts +160 -160
  103. package/src/types/index.d.ts +1 -1
  104. package/src/utils/formatting.ts +245 -245
  105. package/src/utils/index.ts +4 -4
  106. package/src/utils/parser.ts +244 -244
  107. package/src/utils/precision.ts +216 -216
  108. package/src/utils/validation.ts +182 -182
  109. package/tsconfig.json +83 -83
  110. package/dist/types/config/config-loader.d.ts.map +0 -1
  111. package/dist/types/config/config-parser.d.ts.map +0 -1
  112. package/dist/types/config/config-resolver.d.ts.map +0 -1
  113. package/dist/types/config/config-source.d.ts.map +0 -1
  114. package/dist/types/config/index.d.ts.map +0 -1
  115. package/dist/types/core/common.d.ts.map +0 -1
  116. package/dist/types/core/config.d.ts.map +0 -1
  117. package/dist/types/core/constants.d.ts.map +0 -1
  118. package/dist/types/core/errors.d.ts.map +0 -1
  119. package/dist/types/core/hypernum.d.ts.map +0 -1
  120. package/dist/types/core/index.d.ts.map +0 -1
  121. package/dist/types/index.d.ts.map +0 -1
  122. package/dist/types/operations/arithmetic.d.ts.map +0 -1
  123. package/dist/types/operations/bitwise.d.ts.map +0 -1
  124. package/dist/types/operations/comparison.d.ts.map +0 -1
  125. package/dist/types/operations/conversion.d.ts.map +0 -1
  126. package/dist/types/operations/factorial.d.ts.map +0 -1
  127. package/dist/types/operations/index.d.ts.map +0 -1
  128. package/dist/types/operations/power.d.ts.map +0 -1
  129. package/dist/types/storage/Heap.d.ts.map +0 -1
  130. package/dist/types/storage/index.d.ts.map +0 -1
  131. package/dist/types/structures/ackermann.d.ts.map +0 -1
  132. package/dist/types/structures/big-array.d.ts.map +0 -1
  133. package/dist/types/structures/index.d.ts.map +0 -1
  134. package/dist/types/structures/number-tree.d.ts.map +0 -1
  135. package/dist/types/structures/power-tower.d.ts.map +0 -1
  136. package/dist/types/utils/formatting.d.ts.map +0 -1
  137. package/dist/types/utils/index.d.ts.map +0 -1
  138. package/dist/types/utils/parser.d.ts.map +0 -1
  139. package/dist/types/utils/precision.d.ts.map +0 -1
  140. package/dist/types/utils/validation.d.ts.map +0 -1
package/rollup.config.js CHANGED
@@ -1,162 +1,164 @@
1
- import typescript from 'rollup-plugin-typescript2';
2
- import resolve from '@rollup/plugin-node-resolve';
3
- import commonjs from '@rollup/plugin-commonjs';
4
- import terser from '@rollup/plugin-terser';
5
- import dts from 'rollup-plugin-dts';
6
- import alias from '@rollup/plugin-alias';
7
- import path from 'path';
8
- import { readFileSync } from 'fs';
9
- import { fileURLToPath } from 'url';
10
-
11
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
- const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
13
-
14
- const banner = `/**
15
- * ${pkg.name} v${pkg.version}
16
- * Precison at Scale
17
- *${pkg.description}
18
- * @license ISC
19
- */`;
20
-
21
- // External dependencies
22
- const external = [
23
- ...Object.keys(pkg.dependencies || {}),
24
- ...Object.keys(pkg.peerDependencies || {}),
25
- 'tslib'
26
- ];
27
-
28
- // Alias configurations for hypernum specific paths
29
- const srcAliases = [
30
- { find: '@', replacement: path.resolve(__dirname, 'src') },
31
- { find: '@core', replacement: path.resolve(__dirname, 'src/core') },
32
- { find: '@operations', replacement: path.resolve(__dirname, 'src/operations') },
33
- { find: '@structures', replacement: path.resolve(__dirname, 'src/structures') },
34
- {find: '@storage', replacement: path.resolve(__dirname, 'src/storage') },
35
- { find: '@utils', replacement: path.resolve(__dirname, 'src/utils') },
36
- { find: '@types', replacement: path.resolve(__dirname, 'src/types') }
37
- ];
38
-
39
- // TypeScript configuration optimized for BigInt operations
40
- const typeScriptConfig = {
41
- tsconfig: './tsconfig.json',
42
- clean: true,
43
- useTsconfigDeclarationDir: true,
44
- tsconfigOverride: {
45
- compilerOptions: {
46
- declaration: true,
47
- declarationDir: './dist/types',
48
- sourceMap: true,
49
- module: 'esnext',
50
- moduleResolution: 'node',
51
- target: 'es2020', // Ensures BigInt compatibility
52
- lib: ['es2020', 'dom'],
53
- allowSyntheticDefaultImports: true
54
- },
55
- exclude: ['**/__tests__/**', '**/*.test.ts', 'src/**/*.spec.ts']
56
- }
57
- };
58
-
59
- // Base plugins configuration
60
- const basePlugins = [
61
- alias({ entries: srcAliases }),
62
- resolve({
63
- browser: true,
64
- preferBuiltins: true,
65
- mainFields: ['module', 'browser', 'main'],
66
- extensions: ['.ts', '.js']
67
- }),
68
- commonjs({
69
- include: /node_modules/,
70
- requireReturnsDefault: 'auto'
71
- }),
72
- typescript(typeScriptConfig)
73
- ];
74
-
75
- // Base output configuration
76
- const baseOutput = {
77
- banner,
78
- sourcemap: true,
79
- exports: 'named'
80
- };
81
-
82
- // Main builds configuration
83
- const mainBuilds = [
84
- // UMD build
85
- {
86
- input: 'src/index.ts',
87
- output: {
88
- ...baseOutput,
89
- file: 'dist/index.umd.js',
90
- format: 'umd',
91
- name: 'Hypernum',
92
- globals: {
93
- tslib: 'tslib'
94
- }
95
- },
96
- external,
97
- plugins: [
98
- ...basePlugins,
99
- terser({
100
- output: {
101
- comments: (node, comment) =>
102
- comment.type === 'comment2' && /@license/i.test(comment.value)
103
- }
104
- })
105
- ]
106
- },
107
- // ESM build
108
- {
109
- input: 'src/index.ts',
110
- output: {
111
- ...baseOutput,
112
- file: 'dist/index.js',
113
- format: 'esm'
114
- },
115
- external,
116
- plugins: basePlugins
117
- },
118
- // CommonJS build
119
- {
120
- input: 'src/index.ts',
121
- output: {
122
- ...baseOutput,
123
- file: 'dist/index.cjs',
124
- format: 'cjs'
125
- },
126
- external,
127
- plugins: basePlugins
128
- }
129
- ];
130
-
131
- // Types build configuration
132
- const typesBuild = {
133
- input: 'src/index.ts',
134
- output: {
135
- file: 'dist/index.d.ts',
136
- format: 'es'
137
- },
138
- external: [
139
- ...external,
140
- /\.css$/,
141
- /@types\/.*/,
142
- /@core\/.*/,
143
- /@operations\/.*/,
144
- /@structures\/.*/,
145
- /@utils\/.*/,
146
- /@\/.*/
147
- ],
148
- plugins: [
149
- alias({
150
- entries: srcAliases.map(entry => ({
151
- ...entry,
152
- replacement: entry.replacement.replace('/src/', '/dist/types/')
153
- }))
154
- }),
155
- dts()
156
- ]
157
- };
158
-
159
- export default [
160
- ...mainBuilds,
161
- typesBuild
1
+ import typescript from 'rollup-plugin-typescript2';
2
+ import resolve from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import terser from '@rollup/plugin-terser';
5
+ import dts from 'rollup-plugin-dts';
6
+ import alias from '@rollup/plugin-alias';
7
+ import json from '@rollup/plugin-json';
8
+ import path from 'path';
9
+ import { readFileSync } from 'fs';
10
+ import { fileURLToPath } from 'url';
11
+
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
+ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
14
+
15
+ const banner = `/**
16
+ * ${pkg.name} v${pkg.version}
17
+ * Precison at Scale
18
+ *${pkg.description}
19
+ * @license ISC
20
+ */`;
21
+
22
+ // External dependencies
23
+ const external = [
24
+ ...Object.keys(pkg.dependencies || {}),
25
+ ...Object.keys(pkg.peerDependencies || {}),
26
+ 'tslib'
27
+ ];
28
+
29
+ // Alias configurations for hypernum specific paths
30
+ const srcAliases = [
31
+ { find: '@', replacement: path.resolve(__dirname, 'src') },
32
+ { find: '@core', replacement: path.resolve(__dirname, 'src/core') },
33
+ { find: '@operations', replacement: path.resolve(__dirname, 'src/operations') },
34
+ { find: '@structures', replacement: path.resolve(__dirname, 'src/structures') },
35
+ {find: '@storage', replacement: path.resolve(__dirname, 'src/storage') },
36
+ { find: '@utils', replacement: path.resolve(__dirname, 'src/utils') },
37
+ { find: '@types', replacement: path.resolve(__dirname, 'src/types') }
38
+ ];
39
+
40
+ // TypeScript configuration optimized for BigInt operations
41
+ const typeScriptConfig = {
42
+ tsconfig: './tsconfig.json',
43
+ clean: true,
44
+ useTsconfigDeclarationDir: true,
45
+ tsconfigOverride: {
46
+ compilerOptions: {
47
+ declaration: true,
48
+ declarationDir: './dist/types',
49
+ sourceMap: true,
50
+ module: 'esnext',
51
+ moduleResolution: 'node',
52
+ target: 'es2020', // Ensures BigInt compatibility
53
+ lib: ['es2020', 'dom'],
54
+ allowSyntheticDefaultImports: true
55
+ },
56
+ exclude: ['**/__tests__/**', '**/*.test.ts', 'src/**/*.spec.ts']
57
+ }
58
+ };
59
+
60
+ // Base plugins configuration
61
+ const basePlugins = [
62
+ alias({ entries: srcAliases }),
63
+ json({ compact: true }),
64
+ resolve({
65
+ browser: true,
66
+ preferBuiltins: true,
67
+ mainFields: ['module', 'browser', 'main'],
68
+ extensions: ['.ts', '.js']
69
+ }),
70
+ commonjs({
71
+ include: /node_modules/,
72
+ requireReturnsDefault: 'auto'
73
+ }),
74
+ typescript(typeScriptConfig)
75
+ ];
76
+
77
+ // Base output configuration
78
+ const baseOutput = {
79
+ banner,
80
+ sourcemap: true,
81
+ exports: 'named'
82
+ };
83
+
84
+ // Main builds configuration
85
+ const mainBuilds = [
86
+ // UMD build
87
+ {
88
+ input: 'src/index.ts',
89
+ output: {
90
+ ...baseOutput,
91
+ file: 'dist/index.umd.js',
92
+ format: 'umd',
93
+ name: 'Hypernum',
94
+ globals: {
95
+ tslib: 'tslib'
96
+ }
97
+ },
98
+ external,
99
+ plugins: [
100
+ ...basePlugins,
101
+ terser({
102
+ output: {
103
+ comments: (node, comment) =>
104
+ comment.type === 'comment2' && /@license/i.test(comment.value)
105
+ }
106
+ })
107
+ ]
108
+ },
109
+ // ESM build
110
+ {
111
+ input: 'src/index.ts',
112
+ output: {
113
+ ...baseOutput,
114
+ file: 'dist/index.js',
115
+ format: 'esm'
116
+ },
117
+ external,
118
+ plugins: basePlugins
119
+ },
120
+ // CommonJS build
121
+ {
122
+ input: 'src/index.ts',
123
+ output: {
124
+ ...baseOutput,
125
+ file: 'dist/index.cjs',
126
+ format: 'cjs'
127
+ },
128
+ external,
129
+ plugins: basePlugins
130
+ }
131
+ ];
132
+
133
+ // Types build configuration
134
+ const typesBuild = {
135
+ input: 'src/index.ts',
136
+ output: {
137
+ file: 'dist/index.d.ts',
138
+ format: 'es'
139
+ },
140
+ external: [
141
+ ...external,
142
+ /\.css$/,
143
+ /@types\/.*/,
144
+ /@core\/.*/,
145
+ /@operations\/.*/,
146
+ /@structures\/.*/,
147
+ /@utils\/.*/,
148
+ /@\/.*/
149
+ ],
150
+ plugins: [
151
+ alias({
152
+ entries: srcAliases.map(entry => ({
153
+ ...entry,
154
+ replacement: entry.replacement.replace('/src/', '/dist/types/')
155
+ }))
156
+ }),
157
+ dts()
158
+ ]
159
+ };
160
+
161
+ export default [
162
+ ...mainBuilds,
163
+ typesBuild
162
164
  ];
@@ -0,0 +1,272 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import process from 'node:process';
6
+ import { fileURLToPath } from 'node:url';
7
+
8
+ const DEFAULT_CONFIG = {
9
+ precision: 10,
10
+ roundingMode: 'HALF_EVEN',
11
+ checkOverflow: true,
12
+ maxSteps: 1000,
13
+ debug: false
14
+ };
15
+
16
+ const DEFAULT_CONFIG_NAMES = [
17
+ 'hypernum.config.json',
18
+ '.hypernumrc',
19
+ '.hypernumrc.json'
20
+ ];
21
+
22
+ const COMMANDS = {
23
+ // Arithmetic (2 args)
24
+ add: { args: 2, fn: 'add' },
25
+ subtract: { args: 2, fn: 'subtract' },
26
+ multiply: { args: 2, fn: 'multiply' },
27
+ divide: { args: 2, fn: 'divide' },
28
+ mod: { args: 2, fn: 'remainder' },
29
+ gcd: { args: 2, fn: 'gcd' },
30
+ lcm: { args: 2, fn: 'lcm' },
31
+ // Arithmetic (1 arg)
32
+ abs: { args: 1, fn: 'abs' },
33
+ sign: { args: 1, fn: 'sign' },
34
+ // Power
35
+ power: { args: 2, fn: 'power' },
36
+ sqrt: { args: 1, fn: 'sqrt' },
37
+ nthRoot: { args: 2, fn: 'nthRoot' },
38
+ // Factorial
39
+ factorial: { args: 1, fn: 'factorial' },
40
+ binomial: { args: 2, fn: 'binomial' },
41
+ primorial: { args: 1, fn: 'primorial' },
42
+ // Conversion
43
+ toBinary: { args: 1, fn: 'toBinary' },
44
+ toHex: { args: 1, fn: 'toHexadecimal' },
45
+ toOctal: { args: 1, fn: 'toOctal' },
46
+ toRoman: { args: 1, fn: 'toRoman' },
47
+ fromRoman: { args: 1, fn: 'fromRoman', stringArg: true },
48
+ toScientific: { args: 1, fn: 'toScientific' },
49
+ toBase: { args: 2, fn: 'toBase', secondArgIsInt: true },
50
+ // Comparison
51
+ compare: { args: 2, fn: 'compare' },
52
+ equals: { args: 2, fn: 'equals' },
53
+ lessThan: { args: 2, fn: 'lessThan' },
54
+ greaterThan: { args: 2, fn: 'greaterThan' },
55
+ };
56
+
57
+ const USAGE = `hypernum - High-precision mathematics CLI
58
+
59
+ Usage:
60
+ hypernum <command> [args...] [options]
61
+
62
+ Arithmetic:
63
+ add <a> <b> Add two numbers
64
+ subtract <a> <b> Subtract b from a
65
+ multiply <a> <b> Multiply two numbers
66
+ divide <a> <b> Divide a by b
67
+ mod <a> <b> Remainder of a / b
68
+ gcd <a> <b> Greatest common divisor
69
+ lcm <a> <b> Least common multiple
70
+ abs <n> Absolute value
71
+ sign <n> Sign (-1, 0, or 1)
72
+
73
+ Power:
74
+ power <base> <exp> Exponentiation
75
+ sqrt <n> Integer square root
76
+ nthRoot <n> <k> k-th root of n
77
+
78
+ Factorial:
79
+ factorial <n> Factorial (n!)
80
+ binomial <n> <k> Binomial coefficient C(n, k)
81
+ primorial <n> Primorial (n#)
82
+
83
+ Conversion:
84
+ toBinary <n> Convert to binary
85
+ toOctal <n> Convert to octal
86
+ toHex <n> Convert to hexadecimal
87
+ toRoman <n> Convert to Roman numeral
88
+ fromRoman <str> Convert from Roman numeral
89
+ toScientific <n> Convert to scientific notation
90
+ toBase <n> <base> Convert to arbitrary base
91
+
92
+ Comparison:
93
+ compare <a> <b> Compare (-1, 0, 1)
94
+ equals <a> <b> Test equality (true/false)
95
+ lessThan <a> <b> Test a < b (true/false)
96
+ greaterThan <a> <b> Test a > b (true/false)
97
+
98
+ Config:
99
+ --init [path] Create a config file (default: ./hypernum.config.json)
100
+ --config [path] Show resolved config
101
+
102
+ Options:
103
+ --precision <n> Set decimal precision
104
+ --no-overflow Disable overflow checking
105
+ --help, -h Show this help
106
+ --version, -v Show version
107
+
108
+ Examples:
109
+ hypernum add 123456789012345678901234567890 987654321098765432109876543210
110
+ hypernum factorial 100
111
+ hypernum toRoman 2024
112
+ hypernum toBase 255 16
113
+ `;
114
+
115
+ let _lib = null;
116
+
117
+ async function loadLibrary() {
118
+ if (_lib) return _lib;
119
+ try {
120
+ _lib = await import('@obinexusmk2/hypernum');
121
+ } catch {
122
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
123
+ _lib = await import(path.resolve(__dirname, '../../dist/index.js'));
124
+ }
125
+ return _lib;
126
+ }
127
+
128
+ function findConfigPath() {
129
+ for (const name of DEFAULT_CONFIG_NAMES) {
130
+ const candidate = path.resolve(process.cwd(), name);
131
+ if (fs.existsSync(candidate)) {
132
+ return candidate;
133
+ }
134
+ }
135
+ return null;
136
+ }
137
+
138
+ function readConfig(filePath) {
139
+ try {
140
+ const raw = fs.readFileSync(filePath, 'utf8');
141
+ return JSON.parse(raw);
142
+ } catch (error) {
143
+ console.error(`Unable to read config: ${filePath}`);
144
+ console.error(error instanceof Error ? error.message : String(error));
145
+ process.exitCode = 1;
146
+ return null;
147
+ }
148
+ }
149
+
150
+ function resolveConfig() {
151
+ const configPath = findConfigPath();
152
+ if (!configPath) return { ...DEFAULT_CONFIG };
153
+ const fileConfig = readConfig(configPath);
154
+ return { ...DEFAULT_CONFIG, ...(fileConfig || {}) };
155
+ }
156
+
157
+ function writeConfig(filePath) {
158
+ const targetPath = path.resolve(process.cwd(), filePath || 'hypernum.config.json');
159
+
160
+ if (fs.existsSync(targetPath)) {
161
+ console.error(`Config already exists: ${targetPath}`);
162
+ process.exitCode = 1;
163
+ return;
164
+ }
165
+
166
+ fs.writeFileSync(targetPath, `${JSON.stringify(DEFAULT_CONFIG, null, 2)}\n`, 'utf8');
167
+ console.log(`Created Hypernum config at ${targetPath}`);
168
+ }
169
+
170
+ function printConfig(explicitPath) {
171
+ const resolvedPath = explicitPath
172
+ ? path.resolve(process.cwd(), explicitPath)
173
+ : findConfigPath();
174
+
175
+ if (!resolvedPath) {
176
+ console.log(JSON.stringify(DEFAULT_CONFIG, null, 2));
177
+ return;
178
+ }
179
+
180
+ const parsed = readConfig(resolvedPath);
181
+ if (!parsed) return;
182
+
183
+ console.log(JSON.stringify({ ...DEFAULT_CONFIG, ...parsed }, null, 2));
184
+ }
185
+
186
+ async function main() {
187
+ const args = process.argv.slice(2);
188
+
189
+ if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
190
+ console.log(USAGE);
191
+ return;
192
+ }
193
+
194
+ if (args.includes('--version') || args.includes('-v')) {
195
+ const lib = await loadLibrary();
196
+ console.log(lib.VERSION);
197
+ return;
198
+ }
199
+
200
+ if (args[0] === '--init') {
201
+ writeConfig(args[1]);
202
+ return;
203
+ }
204
+
205
+ if (args[0] === '--config') {
206
+ printConfig(args[1]);
207
+ return;
208
+ }
209
+
210
+ const commandName = args[0];
211
+ const cmd = COMMANDS[commandName];
212
+
213
+ if (!cmd) {
214
+ console.error(`Unknown command: ${commandName}\n`);
215
+ console.log(USAGE);
216
+ process.exitCode = 1;
217
+ return;
218
+ }
219
+
220
+ // Parse positional args and options
221
+ const positionalArgs = [];
222
+ // Default: disable overflow checking for CLI (BigInt handles arbitrary precision)
223
+ const options = { checkOverflow: false };
224
+ for (let i = 1; i < args.length; i++) {
225
+ if (args[i] === '--precision' && args[i + 1]) {
226
+ options.precision = parseInt(args[++i], 10);
227
+ } else if (args[i] === '--overflow') {
228
+ options.checkOverflow = true;
229
+ } else if (args[i] === '--no-overflow') {
230
+ options.checkOverflow = false;
231
+ } else if (!args[i].startsWith('--')) {
232
+ positionalArgs.push(args[i]);
233
+ }
234
+ }
235
+
236
+ if (positionalArgs.length < cmd.args) {
237
+ console.error(`Command '${commandName}' requires ${cmd.args} argument(s), got ${positionalArgs.length}`);
238
+ process.exitCode = 1;
239
+ return;
240
+ }
241
+
242
+ const lib = await loadLibrary();
243
+ const fn = lib[cmd.fn];
244
+
245
+ if (typeof fn !== 'function') {
246
+ console.error(`Function '${cmd.fn}' not found in library`);
247
+ process.exitCode = 1;
248
+ return;
249
+ }
250
+
251
+ // Only pass explicitly provided CLI options to library functions.
252
+ // Config file defaults (precision: 10, etc.) don't map to operation defaults.
253
+ try {
254
+ let result;
255
+ if (cmd.args === 1) {
256
+ result = cmd.stringArg ? fn(positionalArgs[0]) : fn(positionalArgs[0], options);
257
+ } else if (cmd.secondArgIsInt) {
258
+ result = fn(positionalArgs[0], parseInt(positionalArgs[1], 10), options);
259
+ } else {
260
+ result = fn(positionalArgs[0], positionalArgs[1], options);
261
+ }
262
+ console.log(String(result));
263
+ } catch (err) {
264
+ console.error(`Error: ${err.message || err}`);
265
+ process.exitCode = 1;
266
+ }
267
+ }
268
+
269
+ main().catch((err) => {
270
+ console.error('hypernum: fatal error:', err.message || err);
271
+ process.exitCode = 1;
272
+ });
File without changes