Skip to content

Commit 69ad404

Browse files
committed
✨ Add Async Versions
1 parent 8252e4b commit 69ad404

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ You can get this package by [nest.land](https://nest.land/package/fibonacci) or
1515

1616
## Usage
1717

18-
This package exposes two Functions,
19-
[fibonacci](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L1)
20-
and
21-
[fibonacciSequence](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L15).
18+
This package exposes four functions,
19+
[fibonacci](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L6),
20+
[fibonacciSequence](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L15),
21+
[fibonacciSync](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L24) and
22+
[fibonacciSync](https://github.com/UltiRequiem/fibonacci-deno/blob/main/mod.ts#L43).
2223

2324
```typescript
2425
import fibonacci, {
2526
fibonacciSequence,
2627
} from "https://deno.land/x/fibonacci/mod.ts";
2728

28-
console.log(fibonacci(9)); // 34
29-
console.log(fibonacciSequence(4)); // [ 0, 1, 1, 2]
29+
console.log(await fibonacci(9)); // 34
30+
console.log(await fibonacciSequence(4)); // [ 0, 1, 1, 2]
3031
```
3132

3233
### CLI Tool

cli.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { parse } from "https://deno.land/std/flags/mod.ts";
1+
import { parse } from "https://deno.land/std@0.109.0/flags/mod.ts";
22

33
import fibonacci, { fibonacciSequence } from "./mod.ts";
44

5-
function Exec() {
5+
async function main() {
66
const { number, sequence, help } = parse(Deno.args);
77

88
if (!number && !sequence || help) {
@@ -20,14 +20,15 @@ For more help check https://github.com/UltiRequiem/fibonacci-deno`;
2020
}
2121

2222
if (number) {
23-
console.log(fibonacci(number));
23+
console.log(await fibonacci(number));
2424
}
2525

2626
if (sequence) {
27-
console.table(fibonacciSequence(sequence));
27+
const numbers = await fibonacciSequence(sequence);
28+
numbers.forEach((num) => console.log(num));
2829
}
2930
}
3031

3132
if (import.meta.main) {
32-
Exec();
33+
main();
3334
}

egg.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"entry": "./mod.ts",
55
"description": "🦕 Fibonacci Number and Sequence Utilities",
66
"homepage": "https://github.com/UltiRequiem/fibonacci-deno",
7-
"version": "1.1.0",
7+
"version": "1.2.0",
88
"releaseType": "patch",
99
"unstable": false,
1010
"unlisted": false,

mod.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,48 @@
33
* @param n The Nth Fibonacci Number you want to get
44
* @returns The Nth Fibonacci Number
55
*/
6-
export default function fibonacci(n: number): number {
6+
export default function fibonacci(n: number): Promise<number> {
7+
return Promise.resolve(fibonacciSync(n));
8+
}
9+
10+
/**
11+
* Return an Array with the first N numbers of the Fibonacci Sequence
12+
* @param length The length of the array you want to get
13+
* @returns The Array with the first N numbers of the Fibonacci Sequence
14+
*/
15+
export function fibonacciSequence(length: number): Promise<number[]> {
16+
return Promise.resolve(fibonacciSequenceSync(length));
17+
}
18+
19+
/**
20+
* Returns the Nth Fibonacci Number
21+
* @param n The Nth Fibonacci Number you want to get
22+
* @returns The Nth Fibonacci Number
23+
*/
24+
export function fibonacciSync(n: number): number {
725
if (n < 0) {
826
throw new Error(`Expected a number bigger than zero, but got ${n}.`);
927
}
1028

11-
if (fibonacci.cache[n] === undefined) {
12-
fibonacci.cache[n] = fibonacci(n - 1) + fibonacci(n - 2);
29+
if (fibonacciSync.cache[n] === undefined) {
30+
fibonacciSync.cache[n] = fibonacciSync(n - 1) + fibonacciSync(n - 2);
1331
}
1432

15-
return fibonacci.cache[n];
33+
return fibonacciSync.cache[n];
1634
}
1735

18-
fibonacci.cache = [0, 1, 1];
36+
fibonacciSync.cache = [0, 1, 1];
1937

2038
/**
2139
* Return an Array with the first N numbers of the Fibonacci Sequence
2240
* @param length The length of the array you want to get
2341
* @returns The Array with the first N numbers of the Fibonacci Sequence
2442
*/
25-
export function fibonacciSequence(length: number): number[] {
43+
export function fibonacciSequenceSync(length: number): number[] {
2644
const sequence = [];
2745

2846
for (let i = 0; i < length; i++) {
29-
sequence[i] = fibonacci(i);
47+
sequence[i] = fibonacciSync(i);
3048
}
3149

3250
return sequence;

mod_test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { assertEquals } from "./test_deps.ts";
22
import fibonacci, { fibonacciSequence } from "./mod.ts";
33

4-
Deno.test("Test Fibonacci", () => {
5-
assertEquals(fibonacci(4), 3);
4+
Deno.test("Test Fibonacci", async() => {
5+
assertEquals(await fibonacci(4), 3);
66
});
77

8-
Deno.test("Test Fibonacci Sequence", () => {
9-
assertEquals(fibonacciSequence(3), [0, 1, 1]);
8+
Deno.test("Test Fibonacci Sequence",async () => {
9+
assertEquals(await fibonacciSequence(3), [0, 1, 1]);
1010
});

0 commit comments

Comments
 (0)