diff --git a/packages/app/universal-ts-utils/package.json b/packages/app/universal-ts-utils/package.json index 617dc5fa..11829f23 100644 --- a/packages/app/universal-ts-utils/package.json +++ b/packages/app/universal-ts-utils/package.json @@ -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" } diff --git a/packages/app/universal-ts-utils/src/internal/compare.spec.ts b/packages/app/universal-ts-utils/src/internal/compare.spec.ts new file mode 100644 index 00000000..671f8376 --- /dev/null +++ b/packages/app/universal-ts-utils/src/internal/compare.spec.ts @@ -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) + }) +}) diff --git a/packages/app/universal-ts-utils/src/internal/compare.ts b/packages/app/universal-ts-utils/src/internal/compare.ts new file mode 100644 index 00000000..85fa3168 --- /dev/null +++ b/packages/app/universal-ts-utils/src/internal/compare.ts @@ -0,0 +1,12 @@ +export const compare = (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 +} diff --git a/packages/app/universal-ts-utils/src/node.ts b/packages/app/universal-ts-utils/src/node.ts index 906d995a..dc70e50f 100644 --- a/packages/app/universal-ts-utils/src/node.ts +++ b/packages/app/universal-ts-utils/src/node.ts @@ -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' diff --git a/packages/app/universal-ts-utils/src/array/areStringArraysEqual.spec.ts b/packages/app/universal-ts-utils/src/public/array/areStringArraysEqual.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/areStringArraysEqual.spec.ts rename to packages/app/universal-ts-utils/src/public/array/areStringArraysEqual.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/areStringArraysEqual.ts b/packages/app/universal-ts-utils/src/public/array/areStringArraysEqual.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/areStringArraysEqual.ts rename to packages/app/universal-ts-utils/src/public/array/areStringArraysEqual.ts diff --git a/packages/app/universal-ts-utils/src/array/callChunked.spec.ts b/packages/app/universal-ts-utils/src/public/array/callChunked.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/callChunked.spec.ts rename to packages/app/universal-ts-utils/src/public/array/callChunked.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/callChunked.ts b/packages/app/universal-ts-utils/src/public/array/callChunked.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/callChunked.ts rename to packages/app/universal-ts-utils/src/public/array/callChunked.ts diff --git a/packages/app/universal-ts-utils/src/array/chunk.spec.ts b/packages/app/universal-ts-utils/src/public/array/chunk.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/chunk.spec.ts rename to packages/app/universal-ts-utils/src/public/array/chunk.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/chunk.ts b/packages/app/universal-ts-utils/src/public/array/chunk.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/chunk.ts rename to packages/app/universal-ts-utils/src/public/array/chunk.ts diff --git a/packages/app/universal-ts-utils/src/array/isNonEmptyArray.spec.ts b/packages/app/universal-ts-utils/src/public/array/isNonEmptyArray.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/isNonEmptyArray.spec.ts rename to packages/app/universal-ts-utils/src/public/array/isNonEmptyArray.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/isNonEmptyArray.ts b/packages/app/universal-ts-utils/src/public/array/isNonEmptyArray.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/isNonEmptyArray.ts rename to packages/app/universal-ts-utils/src/public/array/isNonEmptyArray.ts diff --git a/packages/app/universal-ts-utils/src/array/removeFalsy.spec.ts b/packages/app/universal-ts-utils/src/public/array/removeFalsy.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/removeFalsy.spec.ts rename to packages/app/universal-ts-utils/src/public/array/removeFalsy.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/removeFalsy.ts b/packages/app/universal-ts-utils/src/public/array/removeFalsy.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/removeFalsy.ts rename to packages/app/universal-ts-utils/src/public/array/removeFalsy.ts diff --git a/packages/app/universal-ts-utils/src/array/removeNullish.spec.ts b/packages/app/universal-ts-utils/src/public/array/removeNullish.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/removeNullish.spec.ts rename to packages/app/universal-ts-utils/src/public/array/removeNullish.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/removeNullish.ts b/packages/app/universal-ts-utils/src/public/array/removeNullish.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/removeNullish.ts rename to packages/app/universal-ts-utils/src/public/array/removeNullish.ts diff --git a/packages/app/universal-ts-utils/src/array/sort.spec.ts b/packages/app/universal-ts-utils/src/public/array/sort.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/sort.spec.ts rename to packages/app/universal-ts-utils/src/public/array/sort.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/sort.ts b/packages/app/universal-ts-utils/src/public/array/sort.ts similarity index 72% rename from packages/app/universal-ts-utils/src/array/sort.ts rename to packages/app/universal-ts-utils/src/public/array/sort.ts index ac75fd1d..2ede8285 100644 --- a/packages/app/universal-ts-utils/src/array/sort.ts +++ b/packages/app/universal-ts-utils/src/public/array/sort.ts @@ -1,3 +1,5 @@ +import { compare } from '../../internal/compare' + /** * Sorts an array of strings or numbers in either ascending or descending order. * @@ -18,16 +20,3 @@ export const sort = (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 = (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 -} diff --git a/packages/app/universal-ts-utils/src/array/sortByField.spec.ts b/packages/app/universal-ts-utils/src/public/array/sortByField.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/sortByField.spec.ts rename to packages/app/universal-ts-utils/src/public/array/sortByField.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/sortByField.ts b/packages/app/universal-ts-utils/src/public/array/sortByField.ts similarity index 81% rename from packages/app/universal-ts-utils/src/array/sortByField.ts rename to packages/app/universal-ts-utils/src/public/array/sortByField.ts index 5d008707..338e65d9 100644 --- a/packages/app/universal-ts-utils/src/array/sortByField.ts +++ b/packages/app/universal-ts-utils/src/public/array/sortByField.ts @@ -1,3 +1,5 @@ +import { compare } from '../../internal/compare' + type KeysMatching = { [K in keyof T]: T[K] extends V ? K : never }[keyof T] @@ -43,16 +45,3 @@ export const sortByField = 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 = (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 -} diff --git a/packages/app/universal-ts-utils/src/array/unique.spec.ts b/packages/app/universal-ts-utils/src/public/array/unique.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/unique.spec.ts rename to packages/app/universal-ts-utils/src/public/array/unique.spec.ts diff --git a/packages/app/universal-ts-utils/src/array/unique.ts b/packages/app/universal-ts-utils/src/public/array/unique.ts similarity index 100% rename from packages/app/universal-ts-utils/src/array/unique.ts rename to packages/app/universal-ts-utils/src/public/array/unique.ts diff --git a/packages/app/universal-ts-utils/src/object/areDeepEqual.spec.ts b/packages/app/universal-ts-utils/src/public/object/areDeepEqual.spec.ts similarity index 100% rename from packages/app/universal-ts-utils/src/object/areDeepEqual.spec.ts rename to packages/app/universal-ts-utils/src/public/object/areDeepEqual.spec.ts diff --git a/packages/app/universal-ts-utils/src/object/areDeepEqual.ts b/packages/app/universal-ts-utils/src/public/object/areDeepEqual.ts similarity index 100% rename from packages/app/universal-ts-utils/src/object/areDeepEqual.ts rename to packages/app/universal-ts-utils/src/public/object/areDeepEqual.ts