-
Notifications
You must be signed in to change notification settings - Fork 0
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
17 changed files
with
649 additions
and
18 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,50 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { ensureIsArray } from './ensure-is-array'; | ||
|
||
describe('ensureIsArray', () => { | ||
test('should wrap a non-array value in an array', () => { | ||
const result = ensureIsArray(1); | ||
|
||
expect(result).toEqual([1]); | ||
}); | ||
|
||
test('should return the same array if the input is already an array', () => { | ||
const array = [1, 2, 3]; | ||
const result = ensureIsArray(array); | ||
|
||
expect(result).toBe(array); | ||
}); | ||
|
||
test('should wrap a string in an array if it is not an array', () => { | ||
const result = ensureIsArray('hello'); | ||
|
||
expect(result).toEqual(['hello']); | ||
}); | ||
|
||
test('should return the same array if the input is an array of strings', () => { | ||
const array = ['a', 'b', 'c']; | ||
const result = ensureIsArray(array); | ||
|
||
expect(result).toBe(array); | ||
}); | ||
|
||
test('should wrap an object in an array if it is not an array', () => { | ||
const obj = { key: 'value' }; | ||
const result = ensureIsArray(obj); | ||
|
||
expect(result).toEqual([obj]); | ||
}); | ||
|
||
test('should return the same array if the input is an array of objects', () => { | ||
const array = [{ key: 'value' }, { key: 'anotherValue' }]; | ||
const result = ensureIsArray(array); | ||
|
||
expect(result).toBe(array); | ||
}); | ||
|
||
test('should return an empty array when an empty array is passed', () => { | ||
const result = ensureIsArray([]); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
}); |
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,82 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { group } from './group'; | ||
|
||
describe('group', () => { | ||
test('should group items by a given key (number keys)', () => { | ||
const items = [ | ||
{ id: 1, category: 'fruit' }, | ||
{ id: 2, category: 'vegetable' }, | ||
{ id: 3, category: 'fruit' }, | ||
{ id: 4, category: 'meat' }, | ||
]; | ||
|
||
const result = group(items, item => item.id % 2); | ||
|
||
expect(result).toEqual({ | ||
0: [ | ||
{ id: 2, category: 'vegetable' }, | ||
{ id: 4, category: 'meat' }, | ||
], | ||
1: [ | ||
{ id: 1, category: 'fruit' }, | ||
{ id: 3, category: 'fruit' }, | ||
], | ||
}); | ||
}); | ||
|
||
test('should group items by a given key (string keys)', () => { | ||
const items = [ | ||
{ name: 'apple', type: 'fruit' }, | ||
{ name: 'carrot', type: 'vegetable' }, | ||
{ name: 'banana', type: 'fruit' }, | ||
{ name: 'chicken', type: 'meat' }, | ||
]; | ||
|
||
const result = group(items, item => item.type); | ||
|
||
expect(result).toEqual({ | ||
fruit: [ | ||
{ name: 'apple', type: 'fruit' }, | ||
{ name: 'banana', type: 'fruit' }, | ||
], | ||
vegetable: [{ name: 'carrot', type: 'vegetable' }], | ||
meat: [{ name: 'chicken', type: 'meat' }], | ||
}); | ||
}); | ||
|
||
test('should handle an empty array', () => { | ||
const items: Array<{ name: string; type: string }> = []; | ||
const result = group(items, item => item.type); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
|
||
test('should handle unique grouping key', () => { | ||
const items = [ | ||
{ name: 'apple', id: 1 }, | ||
{ name: 'carrot', id: 2 }, | ||
{ name: 'banana', id: 3 }, | ||
]; | ||
|
||
const result = group(items, item => item.id); | ||
|
||
expect(result).toEqual({ | ||
1: [{ name: 'apple', id: 1 }], | ||
2: [{ name: 'carrot', id: 2 }], | ||
3: [{ name: 'banana', id: 3 }], | ||
}); | ||
}); | ||
|
||
test('should handle grouping by a calculated key', () => { | ||
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
|
||
const result = group(items, item => | ||
item % 3 === 0 ? 'divisible by 3' : 'other', | ||
); | ||
|
||
expect(result).toEqual({ | ||
'divisible by 3': [3, 6, 9], | ||
other: [1, 2, 4, 5, 7, 8, 10], | ||
}); | ||
}); | ||
}); |
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,46 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { formatPrice, type Currency } from './index'; | ||
|
||
describe('formatPrice', () => { | ||
test('should format price with default locale and cents enabled', () => { | ||
const money = { amount: '1234.56', currencyCode: 'EUR' } satisfies Currency; | ||
const result = formatPrice(money); | ||
expect(result).toBe('1 234,56 €'); | ||
}); | ||
|
||
test('should format price with custom locale and cents enabled', () => { | ||
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency; | ||
const result = formatPrice(money, 'en-US'); | ||
expect(result).toBe('$1,234.56'); | ||
}); | ||
|
||
test('should format price with quantity multiplier', () => { | ||
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency; | ||
const result = formatPrice(money, 'en-US', 2); | ||
expect(result).toBe('$2,469.12'); | ||
}); | ||
|
||
test('should format price with cents disabled when applicable', () => { | ||
const money = { amount: '1234.00', currencyCode: 'USD' }satisfies Currency; | ||
const result = formatPrice(money, 'en-US', 1, true); | ||
expect(result).toBe('$1,234'); | ||
}); | ||
|
||
test('should still include cents if the amount has fractional part even if disableCents is true', () => { | ||
const money = { amount: '1234.56', currencyCode: 'USD' }satisfies Currency; | ||
const result = formatPrice(money, 'en-US', 1, true); | ||
expect(result).toBe('$1,234.56'); | ||
}); | ||
|
||
test('should format price correctly with a different currency code', () => { | ||
const money = { amount: '1234.56', currencyCode: 'GBP' }satisfies Currency; | ||
const result = formatPrice(money, 'en-GB'); | ||
expect(result).toBe('£1,234.56'); | ||
}); | ||
|
||
test('should handle large quantities correctly', () => { | ||
const money = { amount: '1000.00', currencyCode: 'EUR' }satisfies Currency; | ||
const result = formatPrice(money, 'fr', 100); | ||
expect(result).toBe('100 000,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
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 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
Oops, something went wrong.