-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,91 @@ | ||
const fnHub = require('./functionHub-library')('https://fno.io/hub/api'); | ||
|
||
function printFunctions(functions) { | ||
console.log( | ||
'This query found ' + functions.length + ' possible functions:' | ||
); | ||
functions.forEach(func => console.log('\t' + func.func.name)); | ||
console.log(`This query found ${functions.length} possible functions:`); | ||
functions.forEach((func) => console.log(`\t${func.func.name}`)); | ||
} | ||
|
||
function printParameters(func) { | ||
const {expects, returns} = func.func; | ||
console.log({expects, returns}); | ||
const { expects, returns } = func.func; | ||
console.log({ expects, returns }); | ||
} | ||
|
||
var query = { | ||
expects: [{type: 'float'}, {type: 'float'}], | ||
returns: {type: 'float'} | ||
const query = { | ||
expects: [{ type: 'float' }, { type: 'float' }], | ||
returns: { type: 'float' }, | ||
}; | ||
|
||
async function main() { | ||
console.log('Query without keyword filter:'); | ||
let queryResult = await fnHub.doQuery(query); | ||
printFunctions(queryResult); | ||
|
||
query['keywords'] = ['total population']; | ||
query.keywords = ['total population']; | ||
|
||
console.log('\nQuery with keyword filter:'); | ||
printFunctions(await fnHub.doQuery(query)); | ||
|
||
// This is the one | ||
var totalPopulationFunction = (await fnHub.doQuery(query))[0]; | ||
const totalPopulationFunction = (await fnHub.doQuery(query))[0]; | ||
|
||
console.log('\nFunction parameters info:'); | ||
printParameters(totalPopulationFunction); | ||
|
||
var populationDensity = 363.6; | ||
var totalArea = 30528.0; | ||
var totalPopulationImplementation = (await fnHub.getImplementationsFromFunction( | ||
totalPopulationFunction | ||
const populationDensity = 363.6; | ||
const totalArea = 30528.0; | ||
const totalPopulationImplementation = (await fnHub.getImplementationsFromFunction( | ||
totalPopulationFunction, | ||
))[0]; | ||
console.log( | ||
'\nThe total population of Belgium is: ' + | ||
totalPopulationImplementation(populationDensity, totalArea) | ||
); | ||
console.log(`\nThe total population of Belgium is: ${totalPopulationImplementation(populationDensity, totalArea)}`); | ||
|
||
console.log("\nNew query, let's find indent function."); | ||
const indentQuery = { | ||
expects: [{type: 'string'}, {type: 'integer'}], | ||
returns: {type: 'string'}, | ||
keywords: ['indent'] | ||
expects: [{ type: 'string' }, { type: 'integer' }], | ||
returns: { type: 'string' }, | ||
keywords: ['indent'], | ||
}; | ||
queryResult = await fnHub.doQuery(indentQuery); | ||
printFunctions(queryResult); | ||
|
||
console.log( | ||
'Let\'s call its implementation on the string "Hey" with the additional parameter "8":' | ||
'Let\'s call its implementation on the string "Hey" with the additional parameter "8":', | ||
); | ||
var indentImplementation = (await fnHub.getImplementationsFromFunction( | ||
queryResult[0] | ||
const indentImplementation = (await fnHub.getImplementationsFromFunction( | ||
queryResult[0], | ||
))[0]; | ||
|
||
console.log(indentImplementation('Hey', 8)); | ||
|
||
console.log("\nNew query, let's find left-pad function."); | ||
const leftpadQuery = { | ||
expects: [{type: 'string'}, {type: 'integer'}], | ||
returns: {type: 'string'}, | ||
keywords: ['left-pad'] | ||
expects: [{ type: 'string' }, { type: 'integer' }], | ||
returns: { type: 'string' }, | ||
keywords: ['left-pad'], | ||
}; | ||
queryResult = await fnHub.doQuery(leftpadQuery); | ||
printFunctions(queryResult); | ||
|
||
console.log( | ||
'Let\'s call its implementation on the string "Hey" with the additional parameter "5":' | ||
); | ||
var leftpadImplementations = await fnHub.getImplementationsFromFunction( | ||
queryResult[0] | ||
); | ||
console.log('Let\'s call its implementation on the string "Hey" with the additional parameter "5":'); | ||
let leftpadImplementations = []; | ||
const start = async () => { | ||
await asyncForEach(queryResult, async (q) => { | ||
const impls = await fnHub.getImplementationsFromFunction(q); | ||
leftpadImplementations = leftpadImplementations.concat(impls); | ||
}); | ||
|
||
console.log(`Found ${leftpadImplementations.length} implementations:`); | ||
leftpadImplementations.forEach((imp, i) => { | ||
console.log('\tOutput of implementation', i, ':', imp('Hey', 5)); | ||
}); | ||
}; | ||
|
||
console.log(`Found ${leftpadImplementations.length} implementations:`); | ||
leftpadImplementations.forEach((imp, i) => { | ||
console.log('\tOutput of implementation', i, ':', imp('Hey', 5)); | ||
}); | ||
start(); | ||
} | ||
|
||
main().catch(console.error); | ||
|
||
async function asyncForEach(array, callback) { | ||
for (let index = 0; index < array.length; index++) { | ||
await callback(array[index], index, array); | ||
} | ||
} |