fix: Add types to memoized functions

This commit is contained in:
Sidharth Vinod
2022-11-20 22:46:12 +05:30
parent 7d3a0577d2
commit 931661ed67

View File

@@ -561,8 +561,16 @@ export const drawSimpleText = function (
return textElem; return textElem;
}; };
export const wrapLabel = memoize( interface WrapLabelConfig {
(label, maxWidth, config) => { fontSize: number;
fontFamily: string;
fontWeight: number;
joinWith: string;
}
export const wrapLabel: (label: string, maxWidth: string, config: WrapLabelConfig) => string =
memoize(
(label: string, maxWidth: string, config: WrapLabelConfig): string => {
if (!label) { if (!label) {
return label; return label;
} }
@@ -601,14 +609,29 @@ export const wrapLabel = memoize(
`${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}` `${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}`
); );
const breakString = memoize( interface BreakStringOutput {
(word, maxWidth, hyphenCharacter = '-', config) => { hyphenatedStrings: string[];
remainingWord: string;
}
const breakString: (
word: string,
maxWidth: number,
hyphenCharacter: string,
config: WrapLabelConfig
) => BreakStringOutput = memoize(
(
word: string,
maxWidth: number,
hyphenCharacter = '-',
config: WrapLabelConfig
): BreakStringOutput => {
config = Object.assign( config = Object.assign(
{ fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 0 }, { fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 0 },
config config
); );
const characters = word.split(''); const characters = word.split('');
const lines = []; const lines: string[] = [];
let currentLine = ''; let currentLine = '';
characters.forEach((character, index) => { characters.forEach((character, index) => {
const nextLine = `${currentLine}${character}`; const nextLine = `${currentLine}${character}`;
@@ -667,6 +690,16 @@ export function calculateTextWidth(
return calculateTextDimensions(text, config).width; return calculateTextDimensions(text, config).width;
} }
interface TextDimensionConfig {
fontSize?: number;
fontWeight?: number;
fontFamily?: string;
}
interface TextDimensions {
width: number;
height: number;
lineHeight?: number;
}
/** /**
* This calculates the dimensions of the given text, font size, font family, font weight, and * This calculates the dimensions of the given text, font size, font family, font weight, and
* margins. * margins.
@@ -676,15 +709,11 @@ export function calculateTextWidth(
* the resulting size * the resulting size
* @returns The dimensions for the given text * @returns The dimensions for the given text
*/ */
export const calculateTextDimensions = memoize( export const calculateTextDimensions: (
function (
text: string, text: string,
config: { config: TextDimensionConfig
fontSize?: number; ) => TextDimensions = memoize(
fontWeight?: number; (text: string, config: TextDimensionConfig): TextDimensions => {
fontFamily?: string;
}
) {
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config); config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
const { fontSize, fontFamily, fontWeight } = config; const { fontSize, fontFamily, fontWeight } = config;
if (!text) { if (!text) {