-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
291 additions
and
60 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
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
45 changes: 45 additions & 0 deletions
45
web-components/src/components/inputs/new/EditableInput/EditableInput.utils.test.ts
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,45 @@ | ||
import { sanitizeValue } from './EditableInput.utils'; | ||
|
||
describe('sanitizeValue', () => { | ||
it('should return "0." if value starts with "."', () => { | ||
expect(sanitizeValue('.')).toBe('0.'); | ||
}); | ||
|
||
it('should return "0." if value is "0.a"', () => { | ||
expect(sanitizeValue('0.a')).toBe('0.'); | ||
}); | ||
|
||
it('should return "0." if value is "0."', () => { | ||
expect(sanitizeValue('0.')).toBe('0.'); | ||
}); | ||
|
||
it('should return "0.1" if value is "0.1"', () => { | ||
expect(sanitizeValue('0.1')).toBe('0.1'); | ||
}); | ||
|
||
it('should strip leading zeros', () => { | ||
expect(sanitizeValue('00123')).toBe('123'); | ||
}); | ||
|
||
it('should strip non-digit characters', () => { | ||
expect(sanitizeValue('123abc')).toBe('123'); | ||
}); | ||
|
||
it('should strip multiple dots', () => { | ||
expect(sanitizeValue('123.45.67')).toBe('123.45'); | ||
}); | ||
|
||
it('should return empty string if value is empty', () => { | ||
expect(sanitizeValue('')).toBe(''); | ||
}); | ||
|
||
it('should return empty string if value contains only non-digit characters', () => { | ||
expect(sanitizeValue('abc')).toBe(''); | ||
}); | ||
|
||
it('should handle complex cases', () => { | ||
expect(sanitizeValue('0.0.0')).toBe('0.0'); | ||
expect(sanitizeValue('0.123.456')).toBe('0.123'); | ||
expect(sanitizeValue('00123.45.67abc')).toBe('123.45'); | ||
}); | ||
}); |
21 changes: 16 additions & 5 deletions
21
web-components/src/components/inputs/new/EditableInput/EditableInput.utils.ts
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,8 +1,19 @@ | ||
export const sanitizeValue = (value: string): number => { | ||
export const sanitizeValue = (value: string): string => { | ||
if (value.startsWith('.')) { | ||
return '0.'; | ||
} | ||
if (value === '0' || value.startsWith('0.')) { | ||
return Number(value); | ||
// Disallow 0.[a-z] | ||
if (/^0\.[a-zA-Z]/.test(value)) { | ||
return '0.'; | ||
} | ||
return value.replace(/(\..*?)\..*/g, '$1'); | ||
} | ||
// Strip leading zeros | ||
const sanitized = value.replace(/^0+/, ''); | ||
return sanitized ? Number(sanitized) : 0; | ||
// Strip leading zeros, non digits and multiple dots | ||
const sanitized = value | ||
.replace(/[^0-9.]/g, '') | ||
.replace(/^0+/, '') | ||
.replace(/(\..*?)\..*/g, '$1'); | ||
|
||
return sanitized ? sanitized : ''; | ||
}; |
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
32 changes: 32 additions & 0 deletions
32
web-marketplace/src/components/molecules/CreditsAmount/CreditsAmount.utils.test.tsx
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,32 @@ | ||
import { formatCurrencyAmount } from './CreditsAmount.utils'; | ||
|
||
describe('formatCurrencyAmount', () => { | ||
it('should format a number to max two decimals', () => { | ||
expect(formatCurrencyAmount(123.456)).toBe(123.45); | ||
expect(formatCurrencyAmount(123)).toBe(123); | ||
expect(formatCurrencyAmount(123.4)).toBe(123.4); | ||
}); | ||
|
||
it('should format a string to two decimals', () => { | ||
expect(formatCurrencyAmount('123.456')).toBe(123.45); | ||
expect(formatCurrencyAmount('123')).toBe(123); | ||
expect(formatCurrencyAmount('123.4')).toBe(123.4); | ||
}); | ||
|
||
it('should round up to two decimals if roundUpDecimal is true', () => { | ||
expect(formatCurrencyAmount(123.456, true)).toBe(123.46); | ||
expect(formatCurrencyAmount(123.451, true)).toBe(123.46); | ||
expect(formatCurrencyAmount(123.4, true)).toBe(123.4); | ||
}); | ||
|
||
it('should return 0 for invalid numeric values', () => { | ||
expect(formatCurrencyAmount('abc')).toBe(0); | ||
expect(formatCurrencyAmount(NaN)).toBe(0); | ||
}); | ||
|
||
it('should handle edge cases', () => { | ||
expect(formatCurrencyAmount(0)).toBe(0); | ||
expect(formatCurrencyAmount('0')).toBe(0); | ||
expect(formatCurrencyAmount('')).toBe(0); | ||
}); | ||
}); |
Oops, something went wrong.