Skip to content

Commit

Permalink
Universal-ts-utils preparing internal utils folder (#376)
Browse files Browse the repository at this point in the history
* Creating external folder

* Extracting compare to a new internal file and reusing it

* Name change
  • Loading branch information
CarlosGamero authored Nov 6, 2024
1 parent 9dc466e commit ddb9e52
Show file tree
Hide file tree
Showing 24 changed files with 59 additions and 39 deletions.
6 changes: 3 additions & 3 deletions packages/app/universal-ts-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"import": "./dist/node.js"
},
"./*": {
"types": "./dist/*.d.ts",
"import": "./dist/*",
"require": "./dist/*"
"types": "./dist/public/*.d.ts",
"import": "./dist/public/*",
"require": "./dist/public/*"
},
"./package.json": "./package.json"
}
Expand Down
30 changes: 30 additions & 0 deletions packages/app/universal-ts-utils/src/internal/compare.spec.ts
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)
})
})
12 changes: 12 additions & 0 deletions packages/app/universal-ts-utils/src/internal/compare.ts
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
}
20 changes: 10 additions & 10 deletions packages/app/universal-ts-utils/src/node.ts
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'
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { compare } from '../../internal/compare'

/**
* Sorts an array of strings or numbers in either ascending or descending order.
*
Expand All @@ -18,16 +20,3 @@ export const sort = <T extends string[] | number[]>(array: T, order: 'asc' | 'de
order === 'asc' ? copy.sort((a, b) => compare(a, b)) : copy.sort((a, b) => compare(b, a))
) as T
}

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
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { compare } from '../../internal/compare'

type KeysMatching<T extends object, V> = {
[K in keyof T]: T[K] extends V ? K : never
}[keyof T]
Expand Down Expand Up @@ -43,16 +45,3 @@ export const sortByField = <T extends object, K extends KeysMatching<T, string |
? copy.sort((a, b) => compare(a[field] as string | number, b[field] as string | number))
: copy.sort((a, b) => compare(b[field] as string | number, a[field] as string | number))
}

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
}

0 comments on commit ddb9e52

Please sign in to comment.