Last 12 weeks · 2 commits
4 of 6 standards met
What's wrong with a depth-limited function reorders an object it should have left alone — anything nested inside an array element is processed one level too early. Using the exact path the docs document — lists as depth : / are unaffected (they never use ). The bug only shows with the depth-based function form, and only for objects nested inside arrays. Why it happens should equal the key's nesting level — — which documents ( → ). An array element sits at , so: a nested array recurses at — adds its own for its elements; a nested object must recurse at — its keys are one level deeper than the element, and reports keys at exactly the depth it's given. The bug recursed the object at (the element's own depth), collapsing the element's keys onto it. Fix Snapshot the element depth once () and recurse with for arrays and for objects. Reading this local snapshot — rather than — keeps recursion independent of the user's callback, which is handed the same object and could otherwise mutate . (With the fix, holds for every reported path.) Tests Added an assertion that a key inside an array element is one level deeper than the element. Full suite (41 tests) passes, and the new assertion fails without the fix.
Repository: sindresorhus/sort-keys. Description: Sort the keys of an object Stars: 109, Forks: 22. Primary language: JavaScript. Languages: JavaScript (94.5%), TypeScript (5.5%). License: MIT. Latest release: v6.0.1 (5d ago). Open PRs: 0, open issues: 0. Last activity: 5d ago. Community health: 85%. Top contributors: sindresorhus, kevva, coreyfarrell, hong4rc, BendingBender, jamestalmage, kenrick95, nathanfdunn, papb, Richienb and others.