-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): add use-currency-formatter (#25)
- Loading branch information
1 parent
88726c9
commit d039765
Showing
13 changed files
with
339 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@interlay/ui": patch | ||
"@interlay/hooks": patch | ||
--- | ||
|
||
feat(hooks): add use-currency-formatter |
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
This file was deleted.
Oops, something went wrong.
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
86 changes: 86 additions & 0 deletions
86
packages/hooks/src/use-currency-formatter/__tests__/use-currency-formatter.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,86 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
import { useCurrencyFormatter } from '../use-currency-formatter'; | ||
|
||
describe('useCurrencyFormatter', () => { | ||
describe('simple integer', () => { | ||
it('should format 0 to $0.00', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(0)).toBe('$0.00'); | ||
}); | ||
|
||
it('should format 1 to $1.00', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(1)).toBe('$1.00'); | ||
}); | ||
|
||
it('should format 1.0001 to $1.00', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(1.0001)).toBe('$1.00'); | ||
}); | ||
|
||
it('should format 1.0001 to $1.00', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(1.0001)).toBe('$1.00'); | ||
}); | ||
}); | ||
|
||
describe('decimals', () => { | ||
it('should format 0.1 to $0.100', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(0.1)).toBe('$0.100'); | ||
}); | ||
|
||
it('should format 0.0001 to $0.0001', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(0.0001)).toBe('$0.0001'); | ||
}); | ||
|
||
it('should format 0.0000000009 to $0.0000000009', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(0.0000000009)).toBe('$0.0000000009'); | ||
}); | ||
|
||
it('should format 0.00000000009 to $0.00000000009', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(0.00000000009)).toBe('<$0.00000001'); | ||
}); | ||
}); | ||
|
||
describe('big integer', () => { | ||
it('should format 100000 to $100,000.00', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(100000)).toBe('$100,000.00'); | ||
}); | ||
|
||
it('should format 1000000 to $1M', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter()); | ||
|
||
expect(result.current(1000000)).toBe('$1M'); | ||
}); | ||
}); | ||
|
||
it('should not compact', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter({ compact: false })); | ||
|
||
expect(result.current(0)).toBe('$0.00'); | ||
expect(result.current(0.001)).toBe('$0.00'); | ||
expect(result.current(1)).toBe('$1.00'); | ||
expect(result.current(1000000)).toBe('$1,000,000.00'); | ||
}); | ||
|
||
it('should format EUR', () => { | ||
const { result } = renderHook(() => useCurrencyFormatter({ currency: 'EUR' })); | ||
|
||
expect(result.current(0)).toBe('€0.00'); | ||
}); | ||
}); |
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,2 @@ | ||
export { useCurrencyFormatter } from './use-currency-formatter'; | ||
export type { UseCurrencyFormatterProps } from './use-currency-formatter'; |
133 changes: 133 additions & 0 deletions
133
packages/hooks/src/use-currency-formatter/stories/use-currency-formatter.stories.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,133 @@ | ||
import { StoryObj } from '@storybook/react'; | ||
|
||
import { useCurrencyFormatter } from '../use-currency-formatter'; | ||
|
||
const Render = () => { | ||
const format = useCurrencyFormatter(); | ||
|
||
return ( | ||
<div style={{ display: 'flex', gap: 32 }}> | ||
<div> | ||
<h1>Digits</h1> | ||
<span> | ||
<strong>1 digit:</strong> {format(1.6801231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>2 digit:</strong> {format(10.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>3 digit:</strong> {format(100.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>4 digit:</strong> {format(1000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>5 digit:</strong> {format(10000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>6 digit:</strong> {format(100000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>7 digit:</strong> {format(1000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>8 digit:</strong> {format(10000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>9 digit:</strong> {format(100000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>10 digit:</strong> {format(1000000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>11 digit:</strong> {format(10000000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>12 digit:</strong> {format(100000000000.0001231)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>13 digit:</strong> {format(1000000000000.0001231)} | ||
</span> | ||
</div> | ||
<div> | ||
<h1>Decimals</h1> | ||
<span> | ||
<strong>1 decimal:</strong> {format(0.1)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>2 decimal:</strong> {format(0.01)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>3 decimal:</strong> {format(0.001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>4 decimal:</strong> {format(0.0001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>5 decimal:</strong> {format(0.00001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>6 decimal:</strong> {format(0.000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>7 decimal:</strong> {format(0.0000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>8 decimal:</strong> {format(0.00000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>9 decimal:</strong> {format(0.000000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>10 decimal:</strong> {format(0.0000000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>11 decimal:</strong> {format(0.00000000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>12 decimal:</strong> {format(0.000000000001)} | ||
</span> | ||
<br /> | ||
<span> | ||
<strong>13 decimal:</strong> {format(0.0000000000001)} | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default { | ||
title: 'Hooks', | ||
parameters: { | ||
layout: 'centered' | ||
}, | ||
args: { | ||
style: { minWidth: 300 } | ||
}, | ||
render: Render | ||
}; | ||
|
||
export const UseCurrencyFormatter: StoryObj = {}; |
Oops, something went wrong.