Dtbase gets an array of objects, do something with them and return result.
- dtbase({ methodName, fn, source, options })
- methodName - name of the method (string).
- fn - a function that will compare an object with a condition
- source - array of objects(array)
- options - some options for methods(object)
- select - compare with a condition and return selection of items
- All methods return an array of objects. [object, object2 ...]
- async function
- arguments:
- item - object
- return:
- object -if it meets a conditions
- null - if not.
- an array of objects
- some options for methods
Expamle
const dtbase = require('@romchik38/dtbase');
//@romchik38/dtbase@2.0.0
const age = async item => {
try {
const age = parseInt(item['age'], 10);
if (typeof age === 'number' && age > 21) return item;
else return null;
} catch (err) {
throw new Error(err);
}
};
const arr = [
{"name": "ser", "lastname": "rom", "age": "20"},
{"name": "dim", "lastname": "fan", "age": "25"},
{"name": "zin", "lastname": "opr", "age": "18"}
];
const result = dtbase({
methodName: 'select',
fn: age,
source: arr,
options: {},
});
result.then(data => {
console.log({ data });
}).catch(err => {
console.log('error from catch', err);
});