Skip to content

Commit

Permalink
fix(kcodeblock): fix maxHeight and getSizeFromString util (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo authored Dec 30, 2024
1 parent 7685a6b commit 974a707
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
26 changes: 20 additions & 6 deletions src/composables/useUtilities.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,46 +183,60 @@ describe('Client-side sorting (deprecated in favor of server-side sorting)', ()
})

describe('getSizeFromString(): ', () => {
it('handles numbers', () => {
it('handles integers', () => {
const sizeStr = '500'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}px`)
})

it('handles floats', () => {
const sizeStr = '500.500'
const result = getSizeFromString(sizeStr)

expect(result).equal('500.5px')
})

it('handles auto', () => {
const sizeStr = 'auto'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}`)
expect(result).equal(sizeStr)
})

it('handles percentages', () => {
const sizeStr = '500%'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}`)
expect(result).equal(sizeStr)
})

it('handles vh', () => {
const sizeStr = '500vh'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}`)
expect(result).equal(sizeStr)
})

it('handles vw', () => {
const sizeStr = '500vw'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}`)
expect(result).equal(sizeStr)
})

it('handles px', () => {
const sizeStr = '500px'
const result = getSizeFromString(sizeStr)

expect(result).equal(`${sizeStr}`)
expect(result).equal(sizeStr)
})

it('handles invalid values', () => {
const sizeStr = 'foo'
const result = getSizeFromString(sizeStr)

expect(result).equal(sizeStr)
})
})

Expand Down
12 changes: 3 additions & 9 deletions src/composables/useUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,13 @@ export default function useUtilities() {
}

/**
* Convert a given string to a height with units. If no units are provided, append `px`.
* Convert a given string to a height with units. If pure number, append `px`.
* @param sizeStr A string that can be used for the height of an element.
* @returns A string to be used for the height of an element.
*/
const getSizeFromString = (sizeStr: string): string => {
return sizeStr === 'auto' ||
sizeStr.endsWith('%') ||
sizeStr.endsWith('vw') ||
sizeStr.endsWith('vh') ||
sizeStr.endsWith('px') ||
sizeStr.includes('calc(')
? sizeStr
: sizeStr + 'px'
const numeric = Number(sizeStr)
return !Number.isNaN(numeric) ? `${numeric}px` : sizeStr
}

/**
Expand Down

0 comments on commit 974a707

Please sign in to comment.