From 3048b74b2f7c80ed731a5f526fbde50b3d6e4e52 Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 13 Dec 2025 00:27:47 -0500 Subject: [PATCH] Handle sorting with undefined values --- package.json | 2 +- src/array.ts | 2 ++ tests/object.spec.ts | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0baf591..8f60df0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.28.2", + "version": "0.28.3", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/array.ts b/src/array.ts index 384b3cb..8ed0502 100644 --- a/src/array.ts +++ b/src/array.ts @@ -122,6 +122,8 @@ export function sortByProp(prop: string, reverse = false) { return function (a: any, b: any) { const aVal = dotNotation(a, prop); const bVal = dotNotation(b, prop); + if(aVal === undefined) return 1; + if(bVal === undefined) return -1; if(typeof aVal == 'number' && typeof bVal == 'number') return (reverse ? -1 : 1) * (aVal - bVal); if(aVal > bVal) return reverse ? -1 : 1; diff --git a/tests/object.spec.ts b/tests/object.spec.ts index 93a81f4..403eb29 100644 --- a/tests/object.spec.ts +++ b/tests/object.spec.ts @@ -62,6 +62,11 @@ describe('Object utilities', () => { dotNotation(obj, 'd.e.f', 5); expect(obj.d.e.f).toBe(5); }); + it('undefined', () => { + const obj: any = {a: 1}; + const resp = dotNotation(obj, 'a.b'); + expect(resp).toBe(undefined); + }) }); describe('encodeQuery', () => {