|
1 | 1 | const fnHub = require('./functionHub-library')('https://fno.io/hub/api');
|
2 | 2 |
|
3 | 3 | function printFunctions(functions) {
|
4 |
| - console.log( |
5 |
| - 'This query found ' + functions.length + ' possible functions:' |
6 |
| - ); |
7 |
| - functions.forEach(func => console.log('\t' + func.func.name)); |
| 4 | + console.log(`This query found ${functions.length} possible functions:`); |
| 5 | + functions.forEach((func) => console.log(`\t${func.func.name}`)); |
8 | 6 | }
|
9 | 7 |
|
10 | 8 | function printParameters(func) {
|
11 |
| - const {expects, returns} = func.func; |
12 |
| - console.log({expects, returns}); |
| 9 | + const { expects, returns } = func.func; |
| 10 | + console.log({ expects, returns }); |
13 | 11 | }
|
14 | 12 |
|
15 |
| -var query = { |
16 |
| - expects: [{type: 'float'}, {type: 'float'}], |
17 |
| - returns: {type: 'float'} |
| 13 | +const query = { |
| 14 | + expects: [{ type: 'float' }, { type: 'float' }], |
| 15 | + returns: { type: 'float' }, |
18 | 16 | };
|
19 | 17 |
|
20 | 18 | async function main() {
|
21 | 19 | console.log('Query without keyword filter:');
|
22 | 20 | let queryResult = await fnHub.doQuery(query);
|
23 | 21 | printFunctions(queryResult);
|
24 | 22 |
|
25 |
| - query['keywords'] = ['total population']; |
| 23 | + query.keywords = ['total population']; |
26 | 24 |
|
27 | 25 | console.log('\nQuery with keyword filter:');
|
28 | 26 | printFunctions(await fnHub.doQuery(query));
|
29 | 27 |
|
30 | 28 | // This is the one
|
31 |
| - var totalPopulationFunction = (await fnHub.doQuery(query))[0]; |
| 29 | + const totalPopulationFunction = (await fnHub.doQuery(query))[0]; |
32 | 30 |
|
33 | 31 | console.log('\nFunction parameters info:');
|
34 | 32 | printParameters(totalPopulationFunction);
|
35 | 33 |
|
36 |
| - var populationDensity = 363.6; |
37 |
| - var totalArea = 30528.0; |
38 |
| - var totalPopulationImplementation = (await fnHub.getImplementationsFromFunction( |
39 |
| - totalPopulationFunction |
| 34 | + const populationDensity = 363.6; |
| 35 | + const totalArea = 30528.0; |
| 36 | + const totalPopulationImplementation = (await fnHub.getImplementationsFromFunction( |
| 37 | + totalPopulationFunction, |
40 | 38 | ))[0];
|
41 |
| - console.log( |
42 |
| - '\nThe total population of Belgium is: ' + |
43 |
| - totalPopulationImplementation(populationDensity, totalArea) |
44 |
| - ); |
| 39 | + console.log(`\nThe total population of Belgium is: ${totalPopulationImplementation(populationDensity, totalArea)}`); |
45 | 40 |
|
46 | 41 | console.log("\nNew query, let's find indent function.");
|
47 | 42 | const indentQuery = {
|
48 |
| - expects: [{type: 'string'}, {type: 'integer'}], |
49 |
| - returns: {type: 'string'}, |
50 |
| - keywords: ['indent'] |
| 43 | + expects: [{ type: 'string' }, { type: 'integer' }], |
| 44 | + returns: { type: 'string' }, |
| 45 | + keywords: ['indent'], |
51 | 46 | };
|
52 | 47 | queryResult = await fnHub.doQuery(indentQuery);
|
53 | 48 | printFunctions(queryResult);
|
54 | 49 |
|
55 | 50 | console.log(
|
56 |
| - 'Let\'s call its implementation on the string "Hey" with the additional parameter "8":' |
| 51 | + 'Let\'s call its implementation on the string "Hey" with the additional parameter "8":', |
57 | 52 | );
|
58 |
| - var indentImplementation = (await fnHub.getImplementationsFromFunction( |
59 |
| - queryResult[0] |
| 53 | + const indentImplementation = (await fnHub.getImplementationsFromFunction( |
| 54 | + queryResult[0], |
60 | 55 | ))[0];
|
61 | 56 |
|
62 | 57 | console.log(indentImplementation('Hey', 8));
|
63 | 58 |
|
64 | 59 | console.log("\nNew query, let's find left-pad function.");
|
65 | 60 | const leftpadQuery = {
|
66 |
| - expects: [{type: 'string'}, {type: 'integer'}], |
67 |
| - returns: {type: 'string'}, |
68 |
| - keywords: ['left-pad'] |
| 61 | + expects: [{ type: 'string' }, { type: 'integer' }], |
| 62 | + returns: { type: 'string' }, |
| 63 | + keywords: ['left-pad'], |
69 | 64 | };
|
70 | 65 | queryResult = await fnHub.doQuery(leftpadQuery);
|
71 | 66 | printFunctions(queryResult);
|
72 | 67 |
|
73 |
| - console.log( |
74 |
| - 'Let\'s call its implementation on the string "Hey" with the additional parameter "5":' |
75 |
| - ); |
76 |
| - var leftpadImplementations = await fnHub.getImplementationsFromFunction( |
77 |
| - queryResult[0] |
78 |
| - ); |
| 68 | + console.log('Let\'s call its implementation on the string "Hey" with the additional parameter "5":'); |
| 69 | + let leftpadImplementations = []; |
| 70 | + const start = async () => { |
| 71 | + await asyncForEach(queryResult, async (q) => { |
| 72 | + const impls = await fnHub.getImplementationsFromFunction(q); |
| 73 | + leftpadImplementations = leftpadImplementations.concat(impls); |
| 74 | + }); |
| 75 | + |
| 76 | + console.log(`Found ${leftpadImplementations.length} implementations:`); |
| 77 | + leftpadImplementations.forEach((imp, i) => { |
| 78 | + console.log('\tOutput of implementation', i, ':', imp('Hey', 5)); |
| 79 | + }); |
| 80 | + }; |
79 | 81 |
|
80 |
| - console.log(`Found ${leftpadImplementations.length} implementations:`); |
81 |
| - leftpadImplementations.forEach((imp, i) => { |
82 |
| - console.log('\tOutput of implementation', i, ':', imp('Hey', 5)); |
83 |
| - }); |
| 82 | + start(); |
84 | 83 | }
|
85 | 84 |
|
86 | 85 | main().catch(console.error);
|
| 86 | + |
| 87 | +async function asyncForEach(array, callback) { |
| 88 | + for (let index = 0; index < array.length; index++) { |
| 89 | + await callback(array[index], index, array); |
| 90 | + } |
| 91 | +} |
0 commit comments