-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #10
- Loading branch information
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import nth from 'lodash-es/nth'; | ||
|
||
console.log(nth([1, 2], 0), nth([1, 2], -1), nth(null, 0)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { nth } from '@s-libs/micro-dash'; | ||
|
||
console.log(nth([1, 2], 0), nth([1, 2], -1), nth(null, 0)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { nth } from './nth'; | ||
|
||
describe('nth()', () => { | ||
// | ||
// stolen from https://github.com/lodash/lodash | ||
// | ||
|
||
const array = ['a', 'b', 'c', 'd']; | ||
|
||
it('should get the nth element of `array`', () => { | ||
expect(nth(array, 0)).toBe('a'); | ||
expect(nth(array, 1)).toBe('b'); | ||
expect(nth(array, 2)).toBe('c'); | ||
expect(nth(array, 3)).toBe('d'); | ||
}); | ||
|
||
it('should work with a negative `n`', () => { | ||
expect(nth(array, -1)).toBe('d'); | ||
expect(nth(array, -2)).toBe('c'); | ||
expect(nth(array, -3)).toBe('b'); | ||
expect(nth(array, -4)).toBe('a'); | ||
}); | ||
|
||
it('should return `undefined` for empty arrays', () => { | ||
expect(nth(null, 1)).toBe(undefined); | ||
expect(nth(undefined, 1)).toBe(undefined); | ||
expect(nth([], 1)).toBe(undefined); | ||
}); | ||
|
||
it('should return `undefined` for non-indexes', () => { | ||
expect(nth([1, 2], Infinity)).toBeUndefined(); | ||
expect(nth([1, 2], 2)).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Nil } from '../interfaces'; | ||
|
||
/** | ||
* Gets the element at index `n` of `array`. If `n` is negative, the nth element from the end is returned. | ||
* | ||
* Differences from lodash: | ||
* - does not handle a fractional value for `index` | ||
* | ||
* Contribution to minified bundle size, when it is the only function imported: | ||
* - Lodash: 1,533 bytes | ||
* - Micro-dash: 96 bytes | ||
*/ | ||
|
||
export function nth<T>(array: ReadonlyArray<T>, index: number): T; | ||
export function nth<T>( | ||
array: ReadonlyArray<T> | Nil, | ||
index: number, | ||
): T | undefined; | ||
|
||
export function nth<T>( | ||
array: ReadonlyArray<T> | Nil, | ||
index: number, | ||
): T | undefined { | ||
if (array) { | ||
return array[index < 0 ? array.length + index : index]; | ||
} | ||
} |