-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Universal-ts-utils preparing internal utils folder (#376)
* Creating external folder * Extracting compare to a new internal file and reusing it * Name change
- Loading branch information
1 parent
9dc466e
commit ddb9e52
Showing
24 changed files
with
59 additions
and
39 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
30 changes: 30 additions & 0 deletions
30
packages/app/universal-ts-utils/src/internal/compare.spec.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,30 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { compare } from './compare' | ||
|
||
describe('compare', () => { | ||
it('should correctly compare two strings', () => { | ||
expect(compare('apple', 'banana')).toBeLessThan(0) | ||
expect(compare('orange', 'orange')).toBe(0) | ||
expect(compare('zebra', 'apple')).toBeGreaterThan(0) | ||
}) | ||
|
||
it('should correctly compare two numbers', () => { | ||
expect(compare(1, 2)).toBeLessThan(0) | ||
expect(compare(10, 10)).toBe(0) | ||
expect(compare(15, 5)).toBeGreaterThan(0) | ||
}) | ||
|
||
it('should handle comparison of same strings', () => { | ||
expect(compare('test', 'test')).toBe(0) | ||
}) | ||
|
||
it('should handle comparison of same numbers', () => { | ||
expect(compare(42, 42)).toBe(0) | ||
}) | ||
|
||
it('should handle comparison with empty strings', () => { | ||
expect(compare('', 'a')).toBeLessThan(0) | ||
expect(compare('a', '')).toBeGreaterThan(0) | ||
expect(compare('', '')).toBe(0) | ||
}) | ||
}) |
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,12 @@ | ||
export const compare = <T extends string | number>(a: T, b: T): number => { | ||
let result = 0 | ||
if (typeof a === 'string' && typeof b === 'string') { | ||
// Sort strings using localeCompare | ||
result = a.localeCompare(b) | ||
} else if (typeof a === 'number' && typeof b === 'number') { | ||
// Sort numbers using basic comparison | ||
result = a - b | ||
} | ||
|
||
return result | ||
} |
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,15 +1,15 @@ | ||
// We don't want to have a typical index file due to how FE bundlers work. Please see `readme` for more info. | ||
|
||
// array | ||
export * from './array/areStringArraysEqual.js' | ||
export * from './array/chunk.js' | ||
export * from './array/callChunked.js' | ||
export * from './array/isNonEmptyArray.js' | ||
export * from './array/removeFalsy.js' | ||
export * from './array/removeNullish.js' | ||
export * from './array/unique.js' | ||
export * from './array/sort.js' | ||
export * from './array/sortByField.js' | ||
export * from './public/array/areStringArraysEqual.js' | ||
export * from './public/array/chunk.js' | ||
export * from './public/array/callChunked.js' | ||
export * from './public/array/isNonEmptyArray.js' | ||
export * from './public/array/removeFalsy.js' | ||
export * from './public/array/removeNullish.js' | ||
export * from './public/array/unique.js' | ||
export * from './public/array/sort.js' | ||
export * from './public/array/sortByField.js' | ||
|
||
// object | ||
export * from './object/areDeepEqual.js' | ||
export * from './public/object/areDeepEqual.js' |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.