Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"js/ts.preferences.importModuleSpecifierEnding": "js",
"js/ts.preferences.preferTypeOnlyAutoImports": true,
"js/ts.tsdk.path": "node_modules/typescript/lib",
"typescript.native-preview.tsdk": "node_modules/@typescript/native-preview",
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"url": "https://github.com/webdeveric/utils/issues"
},
"homepage": "https://github.com/webdeveric/utils/#readme",
"packageManager": "pnpm@10.33.1+sha512.05ba3c1d5d1c18f68df06470d74055e62d41fc110a0c660db1b2dfb2785327f04cf0f68345d4609bc52089e7fa0343c31593b2f9594e2c5d5da426230acc9820",
"packageManager": "pnpm@10.33.2+sha512.a90faf6feeab71ad6c6e57f94e0fe1a12f5dcc22cd754db40ae9593eb6a3e0b6b12e3540218bb37ae083404b1f2ce6db2a4121e979829b4aff94b99f49da1cf8",
"scripts": {
"clean": "rimraf ./dist/",
"prebuild": "pnpm clean",
Expand All @@ -88,8 +88,8 @@
"@commitlint/config-conventional": "^20.5.0",
"@commitlint/types": "^20.5.0",
"@types/node": "^24.12.2",
"@typescript/native-preview": "7.0.0-dev.20260420.1",
"@vitest/coverage-v8": "^4.1.4",
"@typescript/native-preview": "7.0.0-dev.20260423.1",
"@vitest/coverage-v8": "^4.1.5",
"@webdeveric/eslint-config-ts": "^0.12.0",
"@webdeveric/prettier-config": "^0.4.0",
"commitlint": "^20.5.0",
Expand All @@ -107,6 +107,6 @@
"rimraf": "^6.1.3",
"semantic-release": "^25.0.3",
"validate-package-exports": "^0.23.0",
"vitest": "^4.1.4"
"vitest": "^4.1.5"
}
}
368 changes: 186 additions & 182 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/getSortedObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { isAnyObject } from './predicate/isAnyObject.js';

import type { CompareFn } from './types/functions.js';
import type { KeyValueTuple } from './types/tuples.js';

const defaultCompareFunction: CompareFn<string | symbol> = (left, right) =>
typeof left === 'symbol' || typeof right === 'symbol' ? 0 : left.localeCompare(right);

export function getSortedObject<Data extends object>(data: Data, compareFunction?: CompareFn<string | symbol>): Data;

export function getSortedObject<Data extends object>(
data: Data[],
compareFunction?: CompareFn<string | symbol>,
): Data[];

export function getSortedObject<Data extends object>(
data: Data | Data[],
compareFunction: CompareFn<string | symbol> = defaultCompareFunction,
): Data | Data[] {
if (Array.isArray(data)) {
return data.map((item) => getSortedObject(item, compareFunction));
}

const entries = Reflect.ownKeys(data)
.sort(compareFunction)
.map((key): KeyValueTuple<string | symbol, unknown> => {
const value = data[key as keyof typeof data];

return [key, isAnyObject(value) ? getSortedObject(value, compareFunction) : value];
});

return Object.fromEntries(entries) as Data;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from './getOwnProperties.js';
export * from './getPackageName.js';
export * from './getPaths.js';
export * from './getRandomItem.js';
export * from './getSortedObject.js';
export * from './getType.js';
export * from './has.js';
export * from './hasAdditionalProperties.js';
Expand All @@ -44,6 +45,7 @@ export * from './iterateForever.js';
export * from './joinStrings.js';
export * from './jsonParse.js';
export * from './looksLikeURL.js';
export * from './memo.js';
export * from './normalize.js';
export * from './numbers.js';
export * from './parseNumber.js';
Expand Down
1 change: 1 addition & 0 deletions src/predicate/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './nullish.js';
export * from './optional.js';
export * from './range.js';
export * from './shape.js';
export * from './simple.js';
export * from './stringLength.js';
export * from './tuple.js';
export * from './typeOf.js';
Expand Down
95 changes: 95 additions & 0 deletions test/getSortedObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { describe, expect, it } from 'vitest';

import { getSortedObject } from '../src/getSortedObject.js';
import { isString } from '../src/predicate/isString.js';
import { byReverseOf } from '../src/sort/factory/byReverseOf.js';

describe('getSortedObject()', () => {
it('should return a sorted object', () => {
const input = {
b: 2,
a: 1,
c: 3,
};

const serializedInput = JSON.stringify(input);

const expectedSerializedOutput = JSON.stringify({
a: 1,
b: 2,
c: 3,
});

const serializedOutput = getSortedObject(input);

expect(JSON.stringify(serializedOutput)).toEqual(expectedSerializedOutput);
expect(serializedInput).not.toEqual(serializedOutput);
});

it('handles nested objects', () => {
const input = {
b: {
d: 4,
c: 3,
},
a: [
{ e: 5, f: 6 },
{ h: 8, g: 7 },
],
c: 3,
};

const serializedInput = JSON.stringify(input);

const expectedSerializedOutput = JSON.stringify({
a: [
{ e: 5, f: 6 },
{ g: 7, h: 8 },
],
b: {
c: 3,
d: 4,
},
c: 3,
});

const serializedOutput = getSortedObject(input);

expect(JSON.stringify(serializedOutput)).toEqual(expectedSerializedOutput);
expect(serializedInput).not.toEqual(serializedOutput);
});

it('handles empty objects', () => {
expect(JSON.stringify(getSortedObject({}))).toEqual('{}');
expect(JSON.stringify(getSortedObject([]))).toEqual('[]');
});

it('handles array of objects', () => {
expect(JSON.stringify(getSortedObject([{ b: 2, a: 1, c: 3 }]))).toEqual('[{"a":1,"b":2,"c":3}]');
});

it('accepts custom comparator', () => {
expect(
JSON.stringify(
getSortedObject(
[{ b: 2, a: 1, c: 3 }],
byReverseOf((left, right) => {
return isString(left) && isString(right) ? left.localeCompare(right) : 0;
}),
),
),
).toEqual('[{"c":3,"b":2,"a":1}]');
});

it('handles Symbol properties', () => {
const symbolProperty = Symbol('property');

const input = {
b: 2,
a: 1,
[symbolProperty]: 'symbolValue',
};

expect(Reflect.ownKeys(getSortedObject(input))).toEqual(['a', 'b', symbolProperty]);
});
});
20 changes: 20 additions & 0 deletions test/predicate/isUInt32.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from 'vitest';

import { uInt32Min, uInt32Max } from '../../src/numbers.js';
import { isUInt32 } from '../../src/predicate/isUInt32.js';

describe('isUInt32()', () => {
it.each([0, 1234, uInt32Min, uInt32Max])('Returns true for valid input', (value) => {
expect(isUInt32(value)).toBeTruthy();
});

it.each([null, undefined, Number.MAX_SAFE_INTEGER, -1])('Returns false for invalid input', (value) => {
expect(isUInt32(value)).toBeFalsy();
});

it('Can be used with Array.filter()', () => {
expect([-1, 0, null, 2, undefined, 4, Number.MAX_SAFE_INTEGER, Number.POSITIVE_INFINITY].filter(isUInt32)).toEqual([
0, 2, 4,
]);
});
});
Loading