@evidence-dev/component-utilities
Advanced tools
Comparing version 0.0.0-ac3d47d3 to 0.0.0-ae012465
@@ -1,6 +0,4 @@ | ||
# License | ||
MIT License | ||
Copyright \(c\) 2021 Evidence | ||
Copyright \(c\) 2023 Evidence | ||
@@ -7,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files \(the "Software"\), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
{ | ||
"name": "@evidence-dev/component-utilities", | ||
"version": "0.0.0-ac3d47d3", | ||
"version": "0.0.0-ae012465", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,3 @@ import ssf from 'ssf'; | ||
import { standardizeDateString } from './dateParsing'; | ||
import { inferValueType } from './inferColumnTypes'; | ||
@@ -49,2 +50,29 @@ const AXIS_FORMATTING_CONTEXT = 'axis'; | ||
/** | ||
* Returns an Evidence format object to be used in the applyFormatting function | ||
* @param {string} formatString string containing an Excel-style format code, or a format name matching a built-in or custom format | ||
* @param {string} valueType optional - a string representing the data type within the column that will be formatted ('number', 'date', 'boolean', or 'string) | ||
* @returns a format object based on the formatString matching a built-in or custom format name, or a new custom format object containing an Excel-style format code | ||
*/ | ||
export function getFormatObjectFromString(formatString, valueType = undefined) { | ||
let potentialFormatTag = formatString; | ||
let customFormats = getCustomFormats(); | ||
let matchingFormat = [...BUILT_IN_FORMATS, ...customFormats].find( | ||
(format) => format.formatTag?.toLowerCase() === potentialFormatTag?.toLowerCase() | ||
); | ||
let newFormat = {}; | ||
if (matchingFormat) { | ||
return matchingFormat; | ||
} else { | ||
newFormat = { | ||
formatTag: 'custom', | ||
formatCode: potentialFormatTag | ||
}; | ||
if (valueType) { | ||
newFormat.valueType = valueType; | ||
} | ||
return newFormat; | ||
} | ||
} | ||
export const formatValue = (value, columnFormat = undefined, columnUnitSummary = undefined) => { | ||
@@ -140,2 +168,3 @@ try { | ||
} | ||
let result = undefined; | ||
@@ -205,1 +234,14 @@ if (columnFormat) { | ||
} | ||
/** | ||
* Formats a value to whichever format is passed in | ||
* @param {*} value the value to be formatted | ||
* @param {string} format string containing an Excel-style format code, or a format name matching a built-in or custom format | ||
* @returns a formatted value | ||
*/ | ||
export function fmt(value, format) { | ||
let formatObj = getFormatObjectFromString(format); | ||
let valueType = inferValueType(value); | ||
formatObj.valueType = valueType; | ||
return formatValue(value, formatObj); | ||
} |
@@ -17,3 +17,3 @@ // To-do, replace with import from db-commons | ||
const inferValueType = function (columnValue) { | ||
export const inferValueType = function (columnValue) { | ||
if (typeof columnValue === 'number') { | ||
@@ -20,0 +20,0 @@ return EvidenceType.NUMBER; |
176854
33
6508