Last active
July 30, 2024 19:06
-
-
Save metruzanca/dc7c377a393c61c974ddd55a911ccaf5 to your computer and use it in GitHub Desktop.
Didn't end up using this in the end, didn't wanna delete it outright.
This file contains hidden or 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
import { truncateMiddle } from '.'; | |
describe('utils', () => { | |
// ...pretty exaustive for a string utility, I know... | |
describe('truncateMiddle', () => { | |
const testStrings = [ | |
'0x3172635bc846d0c68afce1738d048520dcfafed2d55d1866a5ed5a40d5ea66c7', // 66 chars | |
'0000000000000000000000000000000000000000000000', // 46 chars | |
'00000000000000000000000000000000000000000000000', // 47 chars | |
]; | |
it("doesn't truncate if too short", () => { | |
const tooShort = 'not long enough to be truncated'; | |
expect(truncateMiddle(tooShort, 36)).toEqual(tooShort); | |
}); | |
it('trucates correctly with even maxLength', () => { | |
const maxLength = 36; | |
const expectedResults = [ | |
'0x3172635bc846d0c6…6a5ed5a40d5ea66c7', // 36 chars | |
'000000000000000000…00000000000000000', | |
'000000000000000000…00000000000000000', | |
]; | |
for (let i = 0; i < testStrings.length; i++) { | |
const result = truncateMiddle(testStrings[i], maxLength); | |
expect(result).toEqual(expectedResults[i]); | |
expect(result).toHaveLength(maxLength); | |
} | |
}); | |
it('trucates correctly with odd maxLength', () => { | |
const maxLength = 35; | |
const expectedResults = [ | |
'0x3172635bc846d0c…6a5ed5a40d5ea66c7', // 35 chars | |
'00000000000000000…00000000000000000', | |
'00000000000000000…00000000000000000', | |
]; | |
for (let i = 0; i < testStrings.length; i++) { | |
const result = truncateMiddle(testStrings[i], maxLength); | |
expect(result).toEqual(expectedResults[i]); | |
expect(result).toHaveLength(maxLength); | |
} | |
}); | |
it('truncates correctly with an even scoopText', () => { | |
const scoopText = '<>'; | |
let maxLength = 35; | |
let expectedResults = [ | |
'0x3172635bc846d0c<>a5ed5a40d5ea66c7', | |
'00000000000000000<>0000000000000000', | |
'00000000000000000<>0000000000000000', | |
]; | |
for (let i = 0; i < testStrings.length; i++) { | |
const result = truncateMiddle(testStrings[i], maxLength, { scoopText }); | |
expect(result).toEqual(expectedResults[i]); | |
expect(result).toHaveLength(maxLength); | |
} | |
maxLength = 36; | |
expectedResults = [ | |
'0x3172635bc846d0c<>6a5ed5a40d5ea66c7', | |
'00000000000000000<>00000000000000000', | |
'00000000000000000<>00000000000000000', | |
]; | |
for (let i = 0; i < testStrings.length; i++) { | |
const result = truncateMiddle(testStrings[i], maxLength, { scoopText }); | |
expect(result).toEqual(expectedResults[i]); | |
expect(result).toHaveLength(maxLength); | |
} | |
}); | |
}); | |
}); |
This file contains hidden or 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
type TruncateOptions = { | |
/** truncation text, defaults to ellipsis character "…" */ | |
scoopText?: string; | |
}; | |
/** | |
* Applies a middle truncation, with consisten text max length | |
* @param text string to be truncated | |
* @param finalLength final max length of the string | |
*/ | |
export function truncateMiddle(text: string, finalLength: number, options: TruncateOptions = {}) { | |
const opt = {scoopText: '…', ...options}; | |
if (text.length <= finalLength) { | |
return text; | |
} | |
const scoopLength = opt.scoopText.length; | |
const halfLength = Math.floor((finalLength - scoopLength) / 2); | |
const remainder = (finalLength - scoopLength) % 2; | |
const start = text.slice(0, halfLength + remainder); | |
const end = text.slice(text.length - halfLength); | |
return start + opt.scoopText + end; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment