Skip to content

Commit

Permalink
castSegments: Remove unnecessary handling of nested array and non-arr…
Browse files Browse the repository at this point in the history
…ay cases that don't actually occur
  • Loading branch information
ericyhwang committed Apr 29, 2024
1 parent 2908684 commit 67745a3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
13 changes: 3 additions & 10 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,15 @@ class AsyncGroup {
}
}

function castSegment(segment: string): string | number {
function castSegment(segment: string | number): string | number {
return (typeof segment === 'string' && isArrayIndex(segment))
? +segment // sneaky op to convert numeric string to number
: segment;
}

export function castSegments(segments: Readonly<string | string[]>) {
if (typeof segments === 'string') {
return castSegment(segments);
}
export function castSegments(segments: Readonly<Array<string | number>>) {
// Cast number path segments from strings to numbers
return segments.map(segment =>
Array.isArray(segment)
? castSegments(segment)
: castSegment(segment)
);
return segments.map(segment => castSegment(segment));
}

export function contains(segments, testSegments) {
Expand Down
5 changes: 0 additions & 5 deletions test/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,5 @@ describe('util', function() {
expect(actual).to.eql(['foo', 3, 3]);
expect(actual).to.not.equal(segments); // args not mutated
});

it('handles plain strings', () => {
expect(util.castSegments('foo.bar')).to.eql('foo.bar');
expect(util.castSegments('6')).to.eql(6);
});
});
});

0 comments on commit 67745a3

Please sign in to comment.