forked from Codility/viz-multiple_value-marketplace
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Updated number formatting library. Added fix for locale format i…
…ssue. (#17) Updated number formatting library. Added fix for locale format issue. (#17) Also: - [ ] Updated dependencies - [ ] Fix build and yarn.lock update - [ ] ignore yarn lock when formatting
- Loading branch information
1 parent
708bbcf
commit 2cf6e53
Showing
12 changed files
with
13,177 additions
and
629 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/multiple_value.js | ||
node_modules/ | ||
node_modules | ||
/yarn.lock |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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,9 @@ | ||
export const LOCALE_NUMBER_FORMATS = { | ||
DEFAULT_EN: '1,234.56', | ||
DEFAULT_EU: '1.234,56', | ||
}; | ||
|
||
export const LOCALE_TAGS = { | ||
EN: 'eu', | ||
DE: 'de', | ||
}; |
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,24 @@ | ||
export const PLOT_CONFIG = { | ||
font_size_main: { | ||
label: 'Font Size', | ||
type: 'string', | ||
section: 'Style', | ||
default: '', | ||
order: 0, | ||
display_size: 'half', | ||
}, | ||
orientation: { | ||
label: 'Orientation', | ||
type: 'string', | ||
section: 'Style', | ||
display: 'select', | ||
values: [ | ||
{Auto: 'auto'}, | ||
{Vertical: 'vertical'}, | ||
{Horizontal: 'horizontal'}, | ||
], | ||
default: 'auto', | ||
order: 0, | ||
display_size: 'half', | ||
}, | ||
}; |
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,196 @@ | ||
import React from 'react'; | ||
import * as d3 from 'd3'; | ||
import _ from 'lodash'; | ||
|
||
export const formatType = valueFormat => { | ||
if (typeof valueFormat != 'string') { | ||
return function (x) { | ||
return x; | ||
}; | ||
} | ||
let format = ''; | ||
switch (valueFormat.charAt(0)) { | ||
case '$': | ||
format += '$'; | ||
break; | ||
case '£': | ||
format += '£'; | ||
break; | ||
case '€': | ||
format += '€'; | ||
break; | ||
} | ||
if (valueFormat.indexOf(',') > -1) { | ||
format += ','; | ||
} | ||
const splitValueFormat = valueFormat.split('.'); | ||
format += '.'; | ||
format += splitValueFormat.length > 1 ? splitValueFormat[1].length : 0; | ||
|
||
switch (valueFormat.slice(-1)) { | ||
case '%': | ||
format += '%'; | ||
break; | ||
case '0': | ||
format += 'f'; | ||
break; | ||
} | ||
return d3.format(format); | ||
}; | ||
|
||
export const handleErrors = (vis, resp, options) => { | ||
function messageFromLimits(min, max, field) { | ||
let message = 'You need ' + min; | ||
if (max) { | ||
message += ' to ' + max; | ||
} | ||
message += ' ' + field; | ||
return message; | ||
} | ||
|
||
if ( | ||
resp.fields.pivots.length < options.min_pivots || | ||
resp.fields.pivots.length > options.max_pivots | ||
) { | ||
let message; | ||
vis.addError({ | ||
group: 'pivot-req', | ||
title: 'Incompatible Pivot Data', | ||
message: messageFromLimits( | ||
options.min_pivots, | ||
options.max_pivots, | ||
'pivots' | ||
), | ||
}); | ||
return false; | ||
} else { | ||
vis.clearErrors('pivot-req'); | ||
} | ||
|
||
if ( | ||
resp.fields.dimensions.length < options.min_dimensions || | ||
resp.fields.dimensions.length > options.max_dimensions | ||
) { | ||
vis.addError({ | ||
group: 'dim-req', | ||
title: 'Incompatible Dimension Data', | ||
message: messageFromLimits( | ||
options.min_dimensions, | ||
options.max_dimensions, | ||
'dimensions' | ||
), | ||
}); | ||
return false; | ||
} else { | ||
vis.clearErrors('dim-req'); | ||
} | ||
|
||
if ( | ||
resp.fields.measure_like.length < options.min_measures || | ||
resp.fields.measure_like.length > options.max_measures | ||
) { | ||
vis.addError({ | ||
group: 'mes-req', | ||
title: 'Incompatible Measure Data', | ||
message: messageFromLimits( | ||
options.min_measures, | ||
options.max_measures, | ||
'measures' | ||
), | ||
}); | ||
return false; | ||
} else { | ||
vis.clearErrors('mes-req'); | ||
} | ||
return true; | ||
}; | ||
|
||
const drillingCallback = event => { | ||
// eslint-disable-line | ||
const ds = event.currentTarget.dataset; | ||
const keys = Object.keys(ds); | ||
let links = []; | ||
_.forEach(keys, key => { | ||
const [k, i] = key.split('-'); | ||
if (!links[i]) { | ||
links[i] = {}; | ||
} | ||
links[i][k] = ds[key]; | ||
}); | ||
LookerCharts.Utils.openDrillMenu({links, event}); | ||
}; | ||
|
||
// Attempt to display in this order: HTML/drill -> rendered -> value | ||
export const displayData = (cell, formattedValue) => { | ||
if (_.isEmpty(cell)) { | ||
return null; | ||
} | ||
let formattedCell; | ||
if (cell.links) { | ||
let dataset = {}; | ||
// Adding data to DOM that we will need when drilling into link which we would not | ||
// otherwise have when ag-grid hits our callback fn. | ||
_.forEach(cell.links, (link, i) => { | ||
dataset[`data-label-${i}`] = link.label; | ||
dataset[`data-url-${i}`] = link.url; | ||
dataset[`data-type-${i}`] = link.type; | ||
}); | ||
// const val = !_.isUndefined(cell.rendered) ? cell.rendered : cell.value; | ||
formattedCell = ( | ||
<a | ||
className="drillable-link" | ||
href="#" | ||
onClick={drillingCallback} | ||
{...dataset} | ||
> | ||
{formattedValue} | ||
</a> | ||
); | ||
} else if (cell.html) { | ||
formattedCell = LookerCharts.Utils.htmlForCell(cell).replace( | ||
'<a ', | ||
'<a className="drillable-link" ' | ||
); | ||
} else { | ||
// formattedCell = LookerCharts.Utils.textForCell(cell); | ||
formattedCell = formattedValue; | ||
} | ||
|
||
return formattedCell; | ||
}; | ||
|
||
// Manipulate HEX colors | ||
const addLight = function (color, amount) { | ||
let cc = parseInt(color, 16) + amount; | ||
let c = cc > 255 ? 255 : cc; | ||
c = c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`; | ||
return c; | ||
}; | ||
|
||
export const lighten = (color, amount) => { | ||
color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color; | ||
amount = parseInt((255 * amount) / 100); | ||
return (color = `#${addLight(color.substring(0, 2), amount)}${addLight( | ||
color.substring(2, 4), | ||
amount | ||
)}${addLight(color.substring(4, 6), amount)}`); | ||
}; | ||
|
||
const subtractLight = function (color, amount) { | ||
let cc = parseInt(color, 16) - amount; | ||
let c = cc < 0 ? 0 : cc; | ||
c = c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`; | ||
return c; | ||
}; | ||
|
||
export const darken = (color, amount) => { | ||
color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color; | ||
amount = parseInt((255 * amount) / 100); | ||
return (color = `#${subtractLight( | ||
color.substring(0, 2), | ||
amount | ||
)}${subtractLight(color.substring(2, 4), amount)}${subtractLight( | ||
color.substring(4, 6), | ||
amount | ||
)}`); | ||
}; |
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,24 @@ | ||
import numfmt from 'numfmt'; | ||
import { | ||
LOCALE_NUMBER_FORMATS, | ||
LOCALE_TAGS, | ||
} from '../constants/locale_formats_tags'; | ||
|
||
export function formatValue(format, value, localeFormat) { | ||
const formatter = numfmt(format); | ||
|
||
return formatter(value, { | ||
locale: getLocaleTagFromNumberFormat(localeFormat), | ||
}); | ||
} | ||
|
||
function getLocaleTagFromNumberFormat(format) { | ||
switch (format) { | ||
case LOCALE_NUMBER_FORMATS.DEFAULT_EN: | ||
return LOCALE_TAGS.EN; | ||
case LOCALE_NUMBER_FORMATS.DEFAULT_EU: | ||
return LOCALE_TAGS.DE; | ||
default: | ||
return LOCALE_TAGS.EN; | ||
} | ||
} |
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
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,10 @@ | ||
import {LOCALE_NUMBER_FORMATS} from '../constants/locale_formats_tags'; | ||
import {formatValue} from '../functions/number_date_format'; | ||
|
||
describe('formatValue', () => { | ||
test('should format value with appropiate format and locale settings', () => { | ||
expect( | ||
formatValue('0.0,k', 22789, LOCALE_NUMBER_FORMATS.DEFAULT_EU) | ||
).toEqual('22,8k'); | ||
}); | ||
}); |
Oops, something went wrong.