Skip to content

feat(object): implement sortKeys#1674

Open
guesung wants to merge 1 commit intotoss:mainfrom
guesung:feat/sortKeys
Open

feat(object): implement sortKeys#1674
guesung wants to merge 1 commit intotoss:mainfrom
guesung:feat/sortKeys

Conversation

@guesung
Copy link
Copy Markdown
Contributor

@guesung guesung commented Mar 31, 2026

Summary

  • Implement sortKeys utility that creates a new object with keys sorted alphabetically
  • Supports optional custom compare function for different sorting behavior
  • Based on sort-keys by Sindre Sorhus, with a simpler API following es-toolkit's design principles

Changes

  • src/object/sortKeys.ts — Implementation
  • src/object/sortKeys.spec.ts — Tests (7 cases)
  • src/object/index.ts — Re-export
  • Docs in 4 languages (en, ko, ja, zh_hans)

Test plan

  • All 7 unit tests pass (yarn vitest run src/object/sortKeys)
  • ESLint: 0 errors
  • TypeScript: no new type errors
image

Closes #1618

Copilot AI review requested due to automatic review settings March 31, 2026 06:40
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
es-toolkit Ready Ready Preview, Comment Mar 31, 2026 6:46am

Request Review

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.97%. Comparing base (cd553ee) to head (7d29d4b).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1674   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files         497      498    +1     
  Lines        4662     4669    +7     
  Branches     1348     1348           
=======================================
+ Hits         4661     4668    +7     
  Misses          1        1           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new sortKeys utility to the object module to return a new object whose (string) keys are sorted deterministically (default alphabetical, optionally via a custom comparator), along with unit tests, re-export, and multilingual documentation to expose it as part of the public API surface.

Changes:

  • Implement sortKeys in src/object/sortKeys.ts with an optional custom compare function.
  • Add vitest coverage for common sorting scenarios in src/object/sortKeys.spec.ts.
  • Re-export from src/object/index.ts and add reference docs (en/ko/ja/zh_hans).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/object/sortKeys.ts New implementation of key-sorting object utility.
src/object/sortKeys.spec.ts New unit tests covering default/custom ordering and immutability.
src/object/index.ts Re-export sortKeys from the object entrypoint.
docs/reference/object/sortKeys.md English reference documentation for sortKeys.
docs/ko/reference/object/sortKeys.md Korean reference documentation for sortKeys.
docs/ja/reference/object/sortKeys.md Japanese reference documentation for sortKeys.
docs/zh_hans/reference/object/sortKeys.md Simplified Chinese reference documentation for sortKeys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/object/sortKeys.ts
const result = {} as Record<string, any>;

for (let i = 0; i < sortedKeys.length; i++) {
const key = sortedKeys[i];
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result[key] = object[key] will trigger the __proto__ setter on normal objects if the input contains an own enumerable "__proto__" key (e.g. from Object.create(null)), which can mutate result's prototype (prototype pollution). Consider skipping unsafe keys (via the existing isUnsafeProperty helper) and/or defining properties with Object.defineProperty (or creating result with a null prototype) so assigning "__proto__" cannot change the prototype chain.

Suggested change
const key = sortedKeys[i];
const key = sortedKeys[i];
// Skip unsafe keys that could lead to prototype pollution.
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue;
}

Copilot uses AI. Check for mistakes.
Comment on lines +4 to +14
describe('sortKeys', () => {
it('should sort object keys alphabetically by default', () => {
const result = sortKeys({ c: 3, a: 1, b: 2 });
expect(Object.keys(result)).toEqual(['a', 'b', 'c']);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should sort keys with a custom compare function', () => {
const result = sortKeys({ a: 1, b: 2, c: 3 }, (a, b) => b.localeCompare(a));
expect(Object.keys(result)).toEqual(['c', 'b', 'a']);
});
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no test covering objects that contain an own enumerable "__proto__" key. Given other object utilities explicitly guard against unsafe keys, add a test ensuring sortKeys does not mutate the returned object's prototype when sorting such inputs.

Copilot uses AI. Check for mistakes.
Creates a new object with the keys sorted.

```typescript
const sorted = sortKeys(object, compare?);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeScript snippet sortKeys(object, compare?) is not valid TypeScript syntax (the ? marker can’t be used in a call expression). Update the snippet to a valid call (e.g. sortKeys(object) and sortKeys(object, compare)).

Suggested change
const sorted = sortKeys(object, compare?);
const sorted = sortKeys(object);

Copilot uses AI. Check for mistakes.
키가 정렬된 새로운 객체를 생성해요.

```typescript
const sorted = sortKeys(object, compare?);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeScript snippet sortKeys(object, compare?) is not valid TypeScript syntax (the ? marker can’t be used in a call expression). Update the snippet to a valid call (e.g. sortKeys(object) and sortKeys(object, compare)).

Suggested change
const sorted = sortKeys(object, compare?);
const sorted = sortKeys(object, compare);

Copilot uses AI. Check for mistakes.
キーがソートされた新しいオブジェクトを作成します。

```typescript
const sorted = sortKeys(object, compare?);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeScript snippet sortKeys(object, compare?) is not valid TypeScript syntax (the ? marker can’t be used in a call expression). Update the snippet to a valid call (e.g. sortKeys(object) and sortKeys(object, compare)).

Suggested change
const sorted = sortKeys(object, compare?);
const sorted = sortKeys(object, compare);

Copilot uses AI. Check for mistakes.
创建一个键已排序的新对象。

```typescript
const sorted = sortKeys(object, compare?);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码片段 sortKeys(object, compare?) 不是合法的 TypeScript 调用写法(? 只能用于类型/参数声明,不能用于调用表达式)。建议改为合法的调用示例(例如 sortKeys(object) / sortKeys(object, compare))。

Suggested change
const sorted = sortKeys(object, compare?);
const sorted = sortKeys(object);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add a sortKeys utility to Object utilities

3 participants