stigmergy 1.2.6 → 1.2.8
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/README.md +32 -17
- package/STIGMERGY.md +16 -7
- package/docs/MULTI_USER_WIKI_COLLABORATION_SYSTEM.md +523 -0
- package/docs/PROMPT_BASED_SKILLS_SYSTEM_DESIGN.md +458 -0
- package/docs/SKILL_IMPLEMENTATION_CONSTRAINTS_AND_ALIGNMENT.md +423 -0
- package/docs/TECHNICAL_FEASIBILITY_ANALYSIS.md +308 -0
- package/examples/multilingual-hook-demo.js +125 -0
- package/package.json +14 -17
- package/scripts/dependency-analyzer.js +101 -0
- package/scripts/generate-cli-docs.js +64 -0
- package/scripts/postuninstall.js +46 -0
- package/scripts/preuninstall.js +75 -0
- package/scripts/run-layered-tests.js +3 -3
- package/src/adapters/claude/install_claude_integration.js +17 -17
- package/src/adapters/codebuddy/install_codebuddy_integration.js +13 -13
- package/src/adapters/codex/install_codex_integration.js +27 -27
- package/src/adapters/copilot/install_copilot_integration.js +46 -46
- package/src/adapters/gemini/install_gemini_integration.js +10 -10
- package/src/adapters/iflow/install_iflow_integration.js +7 -7
- package/src/adapters/qoder/install_qoder_integration.js +12 -12
- package/src/adapters/qwen/install_qwen_integration.js +17 -17
- package/src/auth.js +173 -173
- package/src/auth_command.js +208 -208
- package/src/calculator.js +313 -313
- package/src/cli/router.js +151 -7
- package/src/core/cache_cleaner.js +767 -767
- package/src/core/cli_help_analyzer.js +680 -680
- package/src/core/cli_parameter_handler.js +132 -132
- package/src/core/cli_tools.js +89 -89
- package/src/core/coordination/index.js +16 -16
- package/src/core/coordination/nodejs/AdapterManager.js +102 -102
- package/src/core/coordination/nodejs/CLCommunication.js +132 -132
- package/src/core/coordination/nodejs/CLIIntegrationManager.js +272 -272
- package/src/core/coordination/nodejs/HealthChecker.js +76 -76
- package/src/core/coordination/nodejs/HookDeploymentManager.js +463 -274
- package/src/core/coordination/nodejs/StatisticsCollector.js +71 -71
- package/src/core/coordination/nodejs/index.js +90 -90
- package/src/core/coordination/nodejs/utils/Logger.js +29 -29
- package/src/core/enhanced_installer.js +479 -479
- package/src/core/enhanced_uninstaller.js +638 -638
- package/src/core/error_handler.js +406 -406
- package/src/core/installer.js +32 -32
- package/src/core/memory_manager.js +83 -83
- package/src/core/multilingual/language-pattern-manager.js +172 -0
- package/src/core/rest_client.js +160 -160
- package/src/core/smart_router.js +261 -249
- package/src/core/upgrade_manager.js +48 -20
- package/src/data_encryption.js +143 -143
- package/src/data_structures.js +440 -440
- package/src/deploy.js +55 -55
- package/src/index.js +30 -30
- package/src/test/cli-availability-checker.js +194 -194
- package/src/test/test-environment.js +289 -289
- package/src/utils/helpers.js +35 -35
- package/src/utils.js +921 -921
- package/src/weatherProcessor.js +228 -228
- package/test/multilingual/hook-deployment.test.js +91 -0
- package/test/multilingual/language-pattern-manager.test.js +140 -0
- package/test/multilingual/system-test.js +85 -0
package/src/calculator.js
CHANGED
|
@@ -1,313 +1,313 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calculator class for performing basic arithmetic operations
|
|
3
|
-
*/
|
|
4
|
-
class Calculator {
|
|
5
|
-
/**
|
|
6
|
-
* Create a new Calculator
|
|
7
|
-
*/
|
|
8
|
-
constructor() {
|
|
9
|
-
this.result = 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Add two numbers
|
|
14
|
-
* @param {number} a - First number
|
|
15
|
-
* @param {number} b - Second number
|
|
16
|
-
* @returns {number} Sum of a and b
|
|
17
|
-
*/
|
|
18
|
-
add(a, b) {
|
|
19
|
-
return a + b;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Subtract two numbers
|
|
24
|
-
* @param {number} a - First number
|
|
25
|
-
* @param {number} b - Second number
|
|
26
|
-
* @returns {number} Difference of a and b
|
|
27
|
-
*/
|
|
28
|
-
subtract(a, b) {
|
|
29
|
-
return a - b;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Multiply two numbers
|
|
34
|
-
* @param {number} a - First number
|
|
35
|
-
* @param {number} b - Second number
|
|
36
|
-
* @returns {number} Product of a and b
|
|
37
|
-
*/
|
|
38
|
-
multiply(a, b) {
|
|
39
|
-
return a * b;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Divide two numbers
|
|
44
|
-
* @param {number} a - Dividend
|
|
45
|
-
* @param {number} b - Divisor
|
|
46
|
-
* @returns {number} Quotient of a and b
|
|
47
|
-
* @throws {Error} If dividing by zero
|
|
48
|
-
*/
|
|
49
|
-
divide(a, b) {
|
|
50
|
-
if (b === 0) {
|
|
51
|
-
throw new Error('Cannot divide by zero');
|
|
52
|
-
}
|
|
53
|
-
return a / b;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Calculate the power of a number
|
|
58
|
-
* @param {number} base - Base number
|
|
59
|
-
* @param {number} exponent - Exponent
|
|
60
|
-
* @returns {number} Base raised to the power of exponent
|
|
61
|
-
*/
|
|
62
|
-
power(base, exponent) {
|
|
63
|
-
return Math.pow(base, exponent);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Calculate the square root of a number
|
|
68
|
-
* @param {number} a - Number to calculate square root for
|
|
69
|
-
* @returns {number} Square root of a
|
|
70
|
-
* @throws {Error} If calculating square root of negative number
|
|
71
|
-
*/
|
|
72
|
-
sqrt(a) {
|
|
73
|
-
if (a < 0) {
|
|
74
|
-
throw new Error('Cannot calculate square root of negative number');
|
|
75
|
-
}
|
|
76
|
-
return Math.sqrt(a);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Calculate the factorial of a number
|
|
81
|
-
* @param {number} n - Number to calculate factorial for
|
|
82
|
-
* @returns {number} Factorial of n
|
|
83
|
-
* @throws {Error} If calculating factorial of negative number
|
|
84
|
-
*/
|
|
85
|
-
factorial(n) {
|
|
86
|
-
if (n < 0) {
|
|
87
|
-
throw new Error('Cannot calculate factorial of negative number');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (n === 0 || n === 1) {
|
|
91
|
-
return 1;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
let result = 1;
|
|
95
|
-
for (let i = 2; i <= n; i++) {
|
|
96
|
-
result *= i;
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Calculate percentage
|
|
103
|
-
* @param {number} part - Part value
|
|
104
|
-
* @param {number} whole - Whole value
|
|
105
|
-
* @returns {number} Percentage of part in relation to whole
|
|
106
|
-
*/
|
|
107
|
-
percentage(part, whole) {
|
|
108
|
-
if (whole === 0) {
|
|
109
|
-
throw new Error('Cannot calculate percentage with zero as whole');
|
|
110
|
-
}
|
|
111
|
-
return (part / whole) * 100;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Find the maximum of two numbers
|
|
116
|
-
* @param {number} a - First number
|
|
117
|
-
* @param {number} b - Second number
|
|
118
|
-
* @returns {number} The larger of the two numbers
|
|
119
|
-
*/
|
|
120
|
-
max(a, b) {
|
|
121
|
-
return Math.max(a, b);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Find the minimum of two numbers
|
|
126
|
-
* @param {number} a - First number
|
|
127
|
-
* @param {number} b - Second number
|
|
128
|
-
* @returns {number} The smaller of the two numbers
|
|
129
|
-
*/
|
|
130
|
-
min(a, b) {
|
|
131
|
-
return Math.min(a, b);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Calculate the nth Fibonacci number
|
|
136
|
-
* @param {number} n - Position in the Fibonacci sequence (0-indexed)
|
|
137
|
-
* @returns {number} The nth Fibonacci number
|
|
138
|
-
* @throws {Error} If n is negative or not an integer
|
|
139
|
-
*/
|
|
140
|
-
fibonacci(n) {
|
|
141
|
-
if (n < 0) {
|
|
142
|
-
throw new Error('Cannot calculate Fibonacci of negative number');
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!Number.isInteger(n)) {
|
|
146
|
-
throw new Error('Fibonacci input must be an integer');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (n === 0) return 0;
|
|
150
|
-
if (n === 1) return 1;
|
|
151
|
-
|
|
152
|
-
let prev = 0;
|
|
153
|
-
let curr = 1;
|
|
154
|
-
|
|
155
|
-
for (let i = 2; i <= n; i++) {
|
|
156
|
-
const next = prev + curr;
|
|
157
|
-
prev = curr;
|
|
158
|
-
curr = next;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return curr;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Generate a Fibonacci sequence up to n terms
|
|
166
|
-
* @param {number} n - Number of terms to generate
|
|
167
|
-
* @returns {Array<number>} Array containing the first n Fibonacci numbers
|
|
168
|
-
* @throws {Error} If n is negative or not an integer
|
|
169
|
-
*/
|
|
170
|
-
fibonacciSequence(n) {
|
|
171
|
-
if (n < 0) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
'Cannot generate Fibonacci sequence with negative length',
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (!Number.isInteger(n)) {
|
|
178
|
-
throw new Error('Fibonacci sequence length must be an integer');
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (n === 0) return [];
|
|
182
|
-
if (n === 1) return [0];
|
|
183
|
-
if (n === 2) return [0, 1];
|
|
184
|
-
|
|
185
|
-
const sequence = [0, 1];
|
|
186
|
-
|
|
187
|
-
for (let i = 2; i < n; i++) {
|
|
188
|
-
sequence[i] = sequence[i - 1] + sequence[i - 2];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return sequence;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Calculate the circumference of a circle
|
|
196
|
-
* @param {number} radius - The radius of the circle
|
|
197
|
-
* @returns {number} The circumference of the circle
|
|
198
|
-
* @throws {Error} If radius is negative
|
|
199
|
-
*/
|
|
200
|
-
circleCircumference(radius) {
|
|
201
|
-
if (radius < 0) {
|
|
202
|
-
throw new Error('Radius cannot be negative');
|
|
203
|
-
}
|
|
204
|
-
return 2 * Math.PI * radius;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Chain calculations with a running result
|
|
209
|
-
* @param {number} initialValue - Starting value for calculations
|
|
210
|
-
* @returns {CalculationChain} Object for chaining calculations
|
|
211
|
-
*/
|
|
212
|
-
chain(initialValue = 0) {
|
|
213
|
-
return new CalculationChain(initialValue);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Helper class for chaining calculations
|
|
219
|
-
*/
|
|
220
|
-
class CalculationChain {
|
|
221
|
-
/**
|
|
222
|
-
* Create a new calculation chain
|
|
223
|
-
* @param {number} initialValue - Starting value
|
|
224
|
-
*/
|
|
225
|
-
constructor(initialValue) {
|
|
226
|
-
this.result = initialValue;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Add a number to the current result
|
|
231
|
-
* @param {number} value - Number to add
|
|
232
|
-
* @returns {CalculationChain} This chain instance
|
|
233
|
-
*/
|
|
234
|
-
add(value) {
|
|
235
|
-
this.result += value;
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Subtract a number from the current result
|
|
241
|
-
* @param {number} value - Number to subtract
|
|
242
|
-
* @returns {CalculationChain} This chain instance
|
|
243
|
-
*/
|
|
244
|
-
subtract(value) {
|
|
245
|
-
this.result -= value;
|
|
246
|
-
return this;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Multiply the current result by a number
|
|
251
|
-
* @param {number} value - Number to multiply by
|
|
252
|
-
* @returns {CalculationChain} This chain instance
|
|
253
|
-
*/
|
|
254
|
-
multiply(value) {
|
|
255
|
-
this.result *= value;
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Divide the current result by a number
|
|
261
|
-
* @param {number} value - Number to divide by
|
|
262
|
-
* @returns {CalculationChain} This chain instance
|
|
263
|
-
* @throws {Error} If dividing by zero
|
|
264
|
-
*/
|
|
265
|
-
divide(value) {
|
|
266
|
-
if (value === 0) {
|
|
267
|
-
throw new Error('Cannot divide by zero');
|
|
268
|
-
}
|
|
269
|
-
this.result /= value;
|
|
270
|
-
return this;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Raise the current result to a power
|
|
275
|
-
* @param {number} exponent - Exponent
|
|
276
|
-
* @returns {CalculationChain} This chain instance
|
|
277
|
-
*/
|
|
278
|
-
power(exponent) {
|
|
279
|
-
this.result = Math.pow(this.result, exponent);
|
|
280
|
-
return this;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Calculate the square root of the current result
|
|
285
|
-
* @returns {CalculationChain} This chain instance
|
|
286
|
-
* @throws {Error} If calculating square root of negative number
|
|
287
|
-
*/
|
|
288
|
-
sqrt() {
|
|
289
|
-
if (this.result < 0) {
|
|
290
|
-
throw new Error('Cannot calculate square root of negative number');
|
|
291
|
-
}
|
|
292
|
-
this.result = Math.sqrt(this.result);
|
|
293
|
-
return this;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Get the final result of the chained calculations
|
|
298
|
-
* @returns {number} Final result
|
|
299
|
-
*/
|
|
300
|
-
equals() {
|
|
301
|
-
return this.result;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Get the current result without ending the chain
|
|
306
|
-
* @returns {number} Current result
|
|
307
|
-
*/
|
|
308
|
-
value() {
|
|
309
|
-
return this.result;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
module.exports = Calculator;
|
|
1
|
+
/**
|
|
2
|
+
* Calculator class for performing basic arithmetic operations
|
|
3
|
+
*/
|
|
4
|
+
class Calculator {
|
|
5
|
+
/**
|
|
6
|
+
* Create a new Calculator
|
|
7
|
+
*/
|
|
8
|
+
constructor() {
|
|
9
|
+
this.result = 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Add two numbers
|
|
14
|
+
* @param {number} a - First number
|
|
15
|
+
* @param {number} b - Second number
|
|
16
|
+
* @returns {number} Sum of a and b
|
|
17
|
+
*/
|
|
18
|
+
add(a, b) {
|
|
19
|
+
return a + b;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Subtract two numbers
|
|
24
|
+
* @param {number} a - First number
|
|
25
|
+
* @param {number} b - Second number
|
|
26
|
+
* @returns {number} Difference of a and b
|
|
27
|
+
*/
|
|
28
|
+
subtract(a, b) {
|
|
29
|
+
return a - b;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Multiply two numbers
|
|
34
|
+
* @param {number} a - First number
|
|
35
|
+
* @param {number} b - Second number
|
|
36
|
+
* @returns {number} Product of a and b
|
|
37
|
+
*/
|
|
38
|
+
multiply(a, b) {
|
|
39
|
+
return a * b;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Divide two numbers
|
|
44
|
+
* @param {number} a - Dividend
|
|
45
|
+
* @param {number} b - Divisor
|
|
46
|
+
* @returns {number} Quotient of a and b
|
|
47
|
+
* @throws {Error} If dividing by zero
|
|
48
|
+
*/
|
|
49
|
+
divide(a, b) {
|
|
50
|
+
if (b === 0) {
|
|
51
|
+
throw new Error('Cannot divide by zero');
|
|
52
|
+
}
|
|
53
|
+
return a / b;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Calculate the power of a number
|
|
58
|
+
* @param {number} base - Base number
|
|
59
|
+
* @param {number} exponent - Exponent
|
|
60
|
+
* @returns {number} Base raised to the power of exponent
|
|
61
|
+
*/
|
|
62
|
+
power(base, exponent) {
|
|
63
|
+
return Math.pow(base, exponent);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Calculate the square root of a number
|
|
68
|
+
* @param {number} a - Number to calculate square root for
|
|
69
|
+
* @returns {number} Square root of a
|
|
70
|
+
* @throws {Error} If calculating square root of negative number
|
|
71
|
+
*/
|
|
72
|
+
sqrt(a) {
|
|
73
|
+
if (a < 0) {
|
|
74
|
+
throw new Error('Cannot calculate square root of negative number');
|
|
75
|
+
}
|
|
76
|
+
return Math.sqrt(a);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Calculate the factorial of a number
|
|
81
|
+
* @param {number} n - Number to calculate factorial for
|
|
82
|
+
* @returns {number} Factorial of n
|
|
83
|
+
* @throws {Error} If calculating factorial of negative number
|
|
84
|
+
*/
|
|
85
|
+
factorial(n) {
|
|
86
|
+
if (n < 0) {
|
|
87
|
+
throw new Error('Cannot calculate factorial of negative number');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (n === 0 || n === 1) {
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let result = 1;
|
|
95
|
+
for (let i = 2; i <= n; i++) {
|
|
96
|
+
result *= i;
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Calculate percentage
|
|
103
|
+
* @param {number} part - Part value
|
|
104
|
+
* @param {number} whole - Whole value
|
|
105
|
+
* @returns {number} Percentage of part in relation to whole
|
|
106
|
+
*/
|
|
107
|
+
percentage(part, whole) {
|
|
108
|
+
if (whole === 0) {
|
|
109
|
+
throw new Error('Cannot calculate percentage with zero as whole');
|
|
110
|
+
}
|
|
111
|
+
return (part / whole) * 100;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Find the maximum of two numbers
|
|
116
|
+
* @param {number} a - First number
|
|
117
|
+
* @param {number} b - Second number
|
|
118
|
+
* @returns {number} The larger of the two numbers
|
|
119
|
+
*/
|
|
120
|
+
max(a, b) {
|
|
121
|
+
return Math.max(a, b);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Find the minimum of two numbers
|
|
126
|
+
* @param {number} a - First number
|
|
127
|
+
* @param {number} b - Second number
|
|
128
|
+
* @returns {number} The smaller of the two numbers
|
|
129
|
+
*/
|
|
130
|
+
min(a, b) {
|
|
131
|
+
return Math.min(a, b);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Calculate the nth Fibonacci number
|
|
136
|
+
* @param {number} n - Position in the Fibonacci sequence (0-indexed)
|
|
137
|
+
* @returns {number} The nth Fibonacci number
|
|
138
|
+
* @throws {Error} If n is negative or not an integer
|
|
139
|
+
*/
|
|
140
|
+
fibonacci(n) {
|
|
141
|
+
if (n < 0) {
|
|
142
|
+
throw new Error('Cannot calculate Fibonacci of negative number');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!Number.isInteger(n)) {
|
|
146
|
+
throw new Error('Fibonacci input must be an integer');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (n === 0) return 0;
|
|
150
|
+
if (n === 1) return 1;
|
|
151
|
+
|
|
152
|
+
let prev = 0;
|
|
153
|
+
let curr = 1;
|
|
154
|
+
|
|
155
|
+
for (let i = 2; i <= n; i++) {
|
|
156
|
+
const next = prev + curr;
|
|
157
|
+
prev = curr;
|
|
158
|
+
curr = next;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return curr;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Generate a Fibonacci sequence up to n terms
|
|
166
|
+
* @param {number} n - Number of terms to generate
|
|
167
|
+
* @returns {Array<number>} Array containing the first n Fibonacci numbers
|
|
168
|
+
* @throws {Error} If n is negative or not an integer
|
|
169
|
+
*/
|
|
170
|
+
fibonacciSequence(n) {
|
|
171
|
+
if (n < 0) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
'Cannot generate Fibonacci sequence with negative length',
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!Number.isInteger(n)) {
|
|
178
|
+
throw new Error('Fibonacci sequence length must be an integer');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (n === 0) return [];
|
|
182
|
+
if (n === 1) return [0];
|
|
183
|
+
if (n === 2) return [0, 1];
|
|
184
|
+
|
|
185
|
+
const sequence = [0, 1];
|
|
186
|
+
|
|
187
|
+
for (let i = 2; i < n; i++) {
|
|
188
|
+
sequence[i] = sequence[i - 1] + sequence[i - 2];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return sequence;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Calculate the circumference of a circle
|
|
196
|
+
* @param {number} radius - The radius of the circle
|
|
197
|
+
* @returns {number} The circumference of the circle
|
|
198
|
+
* @throws {Error} If radius is negative
|
|
199
|
+
*/
|
|
200
|
+
circleCircumference(radius) {
|
|
201
|
+
if (radius < 0) {
|
|
202
|
+
throw new Error('Radius cannot be negative');
|
|
203
|
+
}
|
|
204
|
+
return 2 * Math.PI * radius;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Chain calculations with a running result
|
|
209
|
+
* @param {number} initialValue - Starting value for calculations
|
|
210
|
+
* @returns {CalculationChain} Object for chaining calculations
|
|
211
|
+
*/
|
|
212
|
+
chain(initialValue = 0) {
|
|
213
|
+
return new CalculationChain(initialValue);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Helper class for chaining calculations
|
|
219
|
+
*/
|
|
220
|
+
class CalculationChain {
|
|
221
|
+
/**
|
|
222
|
+
* Create a new calculation chain
|
|
223
|
+
* @param {number} initialValue - Starting value
|
|
224
|
+
*/
|
|
225
|
+
constructor(initialValue) {
|
|
226
|
+
this.result = initialValue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Add a number to the current result
|
|
231
|
+
* @param {number} value - Number to add
|
|
232
|
+
* @returns {CalculationChain} This chain instance
|
|
233
|
+
*/
|
|
234
|
+
add(value) {
|
|
235
|
+
this.result += value;
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Subtract a number from the current result
|
|
241
|
+
* @param {number} value - Number to subtract
|
|
242
|
+
* @returns {CalculationChain} This chain instance
|
|
243
|
+
*/
|
|
244
|
+
subtract(value) {
|
|
245
|
+
this.result -= value;
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Multiply the current result by a number
|
|
251
|
+
* @param {number} value - Number to multiply by
|
|
252
|
+
* @returns {CalculationChain} This chain instance
|
|
253
|
+
*/
|
|
254
|
+
multiply(value) {
|
|
255
|
+
this.result *= value;
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Divide the current result by a number
|
|
261
|
+
* @param {number} value - Number to divide by
|
|
262
|
+
* @returns {CalculationChain} This chain instance
|
|
263
|
+
* @throws {Error} If dividing by zero
|
|
264
|
+
*/
|
|
265
|
+
divide(value) {
|
|
266
|
+
if (value === 0) {
|
|
267
|
+
throw new Error('Cannot divide by zero');
|
|
268
|
+
}
|
|
269
|
+
this.result /= value;
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Raise the current result to a power
|
|
275
|
+
* @param {number} exponent - Exponent
|
|
276
|
+
* @returns {CalculationChain} This chain instance
|
|
277
|
+
*/
|
|
278
|
+
power(exponent) {
|
|
279
|
+
this.result = Math.pow(this.result, exponent);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Calculate the square root of the current result
|
|
285
|
+
* @returns {CalculationChain} This chain instance
|
|
286
|
+
* @throws {Error} If calculating square root of negative number
|
|
287
|
+
*/
|
|
288
|
+
sqrt() {
|
|
289
|
+
if (this.result < 0) {
|
|
290
|
+
throw new Error('Cannot calculate square root of negative number');
|
|
291
|
+
}
|
|
292
|
+
this.result = Math.sqrt(this.result);
|
|
293
|
+
return this;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Get the final result of the chained calculations
|
|
298
|
+
* @returns {number} Final result
|
|
299
|
+
*/
|
|
300
|
+
equals() {
|
|
301
|
+
return this.result;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Get the current result without ending the chain
|
|
306
|
+
* @returns {number} Current result
|
|
307
|
+
*/
|
|
308
|
+
value() {
|
|
309
|
+
return this.result;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
module.exports = Calculator;
|