forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Math markdown helper (elastic#157)
* chore: replace get_mathjs_scope with pivot_object_array more generic helper function that can be used elsewhere in the code (with tests) * feat: add math helper to handlebars and wrap the handlebars instance so the helper is only added once
- Loading branch information
Showing
7 changed files
with
69 additions
and
17 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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import expect from 'expect.js'; | ||
import { pivotObjectArray } from '../pivot_object_array'; | ||
|
||
describe('pivotObjectArray', () => { | ||
let rows; | ||
|
||
beforeEach(() => { | ||
rows = [ | ||
{ make: 'honda', model: 'civic', price: '10000' }, | ||
{ make: 'toyota', model: 'corolla', price: '12000' }, | ||
{ make: 'tesla', model: 'model 3', price: '35000' }, | ||
]; | ||
}); | ||
|
||
it('converts array of objects', () => { | ||
const data = pivotObjectArray(rows); | ||
|
||
expect(data).to.be.an('object'); | ||
expect(data).to.have.property('make'); | ||
expect(data).to.have.property('model'); | ||
expect(data).to.have.property('price'); | ||
|
||
expect(data.make).to.eql(['honda', 'toyota', 'tesla']); | ||
expect(data.model).to.eql(['civic', 'corolla', 'model 3']); | ||
expect(data.price).to.eql(['10000', '12000', '35000']); | ||
}); | ||
|
||
it('uses passed in column list', () => { | ||
const data = pivotObjectArray(rows, ['price']); | ||
|
||
expect(data).to.be.an('object'); | ||
expect(data).to.eql({ price: ['10000', '12000', '35000'] }); | ||
}); | ||
|
||
it('adds missing columns with undefined values', () => { | ||
const data = pivotObjectArray(rows, ['price', 'missing']); | ||
|
||
expect(data).to.be.an('object'); | ||
expect(data).to.eql({ | ||
price: ['10000', '12000', '35000'], | ||
missing: [undefined, undefined, undefined], | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Hbars from 'handlebars/dist/handlebars'; | ||
import { math } from './math.js'; | ||
import { pivotObjectArray } from './pivot_object_array.js'; | ||
|
||
Hbars.registerHelper('math', (rows, expression, precision) => { | ||
if (!Array.isArray(rows)) return 'MATH ERROR: first argument must be an array'; | ||
const value = math.eval(expression, pivotObjectArray(rows)); | ||
return (precision) ? value.toFixed(precision) : value; | ||
}); | ||
|
||
export const Handlebars = Hbars; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { map, zipObject } from 'lodash'; | ||
|
||
export function pivotObjectArray(rows, columns) { | ||
const columnNames = columns || Object.keys(rows[0]); | ||
const columnValues = map(columnNames, (name) => map(rows, name)); | ||
return zipObject(columnNames, columnValues); | ||
} |
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