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