A small and simple table driven module for NodeJS
- NodeJS >= v7.6
npm install table-driven
const tabledriven = require('table-driven')
const add = function (a, b) {
return a + b
}
const result = tabledriven([
[ 1, 2 ],
[ 3, 4 ],
[ 5, 6 ],
], add)
console.log(result) // [ 3, 7, 11 ]
If give third argument as true then return a promise object and pass to then
or catch
as method chain.
const tabledriven = require('table-driven')
const pow = function (a) {
return a ** a
}
tabledriven([ 1, 2, 3 ], add, true).then((result) => {
console.log(result) // [ 1, 4, 27 ]
})
Also use in the testing framework. Example is;
const assert = require('assert')
const tabledriven = require('table-driven')
describe('Test tabledriven as sync', () => {
describe('Each one argument', () => {
const markup = function (a) {
return '<' + a + '>'
}
it('should return markuped string', () => {
const arr = tabledriven(
[
1,
2
]
, markup)
assert.deepEqual(arr, [ '<1>', '<2>' ])
})
})
describe('Each two arguments', () => {
const add = function (a, b) {
return a + b
}
it('should return result of addition of args', () => {
const arr = tabledriven(
[
[ 1, 2 ],
[ 2, 2 ]
]
, add)
assert.deepEqual(arr, [ 3, 4 ])
})
})
})