Skip to content

Commit 401bd67

Browse files
committed
update
1 parent bad45ef commit 401bd67

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

example-client.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,91 @@
11
const fnHub = require('./functionHub-library')('https://fno.io/hub/api');
22

33
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}`));
86
}
97

108
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 });
1311
}
1412

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' },
1816
};
1917

2018
async function main() {
2119
console.log('Query without keyword filter:');
2220
let queryResult = await fnHub.doQuery(query);
2321
printFunctions(queryResult);
2422

25-
query['keywords'] = ['total population'];
23+
query.keywords = ['total population'];
2624

2725
console.log('\nQuery with keyword filter:');
2826
printFunctions(await fnHub.doQuery(query));
2927

3028
// This is the one
31-
var totalPopulationFunction = (await fnHub.doQuery(query))[0];
29+
const totalPopulationFunction = (await fnHub.doQuery(query))[0];
3230

3331
console.log('\nFunction parameters info:');
3432
printParameters(totalPopulationFunction);
3533

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,
4038
))[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)}`);
4540

4641
console.log("\nNew query, let's find indent function.");
4742
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'],
5146
};
5247
queryResult = await fnHub.doQuery(indentQuery);
5348
printFunctions(queryResult);
5449

5550
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":',
5752
);
58-
var indentImplementation = (await fnHub.getImplementationsFromFunction(
59-
queryResult[0]
53+
const indentImplementation = (await fnHub.getImplementationsFromFunction(
54+
queryResult[0],
6055
))[0];
6156

6257
console.log(indentImplementation('Hey', 8));
6358

6459
console.log("\nNew query, let's find left-pad function.");
6560
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'],
6964
};
7065
queryResult = await fnHub.doQuery(leftpadQuery);
7166
printFunctions(queryResult);
7267

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+
};
7981

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();
8483
}
8584

8685
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

Comments
 (0)