Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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:
|
There was a problem hiding this comment.
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
sortKeysinsrc/object/sortKeys.tswith an optional custom compare function. - Add
vitestcoverage for common sorting scenarios insrc/object/sortKeys.spec.ts. - Re-export from
src/object/index.tsand 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.
| const result = {} as Record<string, any>; | ||
|
|
||
| for (let i = 0; i < sortedKeys.length; i++) { | ||
| const key = sortedKeys[i]; |
There was a problem hiding this comment.
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.
| 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; | |
| } |
| 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']); | ||
| }); |
There was a problem hiding this comment.
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.
| Creates a new object with the keys sorted. | ||
|
|
||
| ```typescript | ||
| const sorted = sortKeys(object, compare?); |
There was a problem hiding this comment.
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)).
| const sorted = sortKeys(object, compare?); | |
| const sorted = sortKeys(object); |
| 키가 정렬된 새로운 객체를 생성해요. | ||
|
|
||
| ```typescript | ||
| const sorted = sortKeys(object, compare?); |
There was a problem hiding this comment.
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)).
| const sorted = sortKeys(object, compare?); | |
| const sorted = sortKeys(object, compare); |
| キーがソートされた新しいオブジェクトを作成します。 | ||
|
|
||
| ```typescript | ||
| const sorted = sortKeys(object, compare?); |
There was a problem hiding this comment.
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)).
| const sorted = sortKeys(object, compare?); | |
| const sorted = sortKeys(object, compare); |
| 创建一个键已排序的新对象。 | ||
|
|
||
| ```typescript | ||
| const sorted = sortKeys(object, compare?); |
There was a problem hiding this comment.
代码片段 sortKeys(object, compare?) 不是合法的 TypeScript 调用写法(? 只能用于类型/参数声明,不能用于调用表达式)。建议改为合法的调用示例(例如 sortKeys(object) / sortKeys(object, compare))。
| const sorted = sortKeys(object, compare?); | |
| const sorted = sortKeys(object); |
Summary
sortKeysutility that creates a new object with keys sorted alphabeticallyChanges
src/object/sortKeys.ts— Implementationsrc/object/sortKeys.spec.ts— Tests (7 cases)src/object/index.ts— Re-exportTest plan
yarn vitest run src/object/sortKeys)Closes #1618