Skip to content

Instantly share code, notes, and snippets.

@tomasreichmann
Created October 17, 2018 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasreichmann/9dc5538d386a5553cd07179925abcc5e to your computer and use it in GitHub Desktop.
Save tomasreichmann/9dc5538d386a5553cd07179925abcc5e to your computer and use it in GitHub Desktop.
({ babel: false })
const range = require('lodash/range');
const attributeItems = {
year: [
'2018', '2017'
],
quarter: [
'Q1', 'Q2', 'Q3', 'Q4'
],
country: [
'CZ', 'SK'
],
manufacturer: [
'BMW', 'VW', 'Škoda'
],
measureGroup: [ 'MEASURE_1', 'MEASURE_2' ]
};
const resultSpec = {
dimensions: [
{
itemIdentifiers: ['country', 'manufacturer']
},
{
itemIdentifiers: ['year', 'quarter', 'measureGroup']
}
]
};
const result = resultSpec.dimensions.reduce((result, dimension) => {
const dimensionItems = dimension.itemIdentifiers.reverse().map(itemIdentifier => attributeItems[itemIdentifier]);
const headers = dimensionItems.reduce((headerItems, dimensionItem, dimensionItemIndex)=>{
if (headerItems.length === 0) {
return [dimensionItem];
}
const previousValueCount = headerItems[0].length;
const currentValueCount = dimensionItem.length;
const multipliedHeaderItems = headerItems.map(headerItemValues => {
return range(currentValueCount).reduce((itemValues) => {
return [...itemValues, ...headerItemValues];
}, []);
});
const multipliedCurrentValues = range(previousValueCount * currentValueCount).map((itemValueIndex) => {
return dimensionItem[Math.floor(itemValueIndex / previousValueCount)]
}, []);
return [multipliedCurrentValues, ...multipliedHeaderItems];
}, [] )
return {
...result,
headerItems: [
...result.headerItems,
headers
]
};
}, { data: [], headerItems: [] });
console.log('rows attributes', result.headerItems[0].length);
console.log(result.headerItems[0]);
console.log('column attributes', result.headerItems[1].length);
console.log(result.headerItems[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment