Skip to content

Commit

Permalink
feat(micro-dash): add nth()
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
ersimont committed Dec 14, 2020
1 parent 42ca23a commit 61a37b1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions projects/micro-dash-sizes/src/app/array/nth.lodash.ts
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));
3 changes: 3 additions & 0 deletions projects/micro-dash-sizes/src/app/array/nth.microdash.ts
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));
1 change: 1 addition & 0 deletions projects/micro-dash/src/lib/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { difference } from './difference';
export { flatten } from './flatten';
export { initial } from './initial';
export { last } from './last';
export { nth } from './nth';
export { pull } from './pull';
export { pullAll } from './pull-all';
export { pullAt } from './pull-at';
Expand Down
34 changes: 34 additions & 0 deletions projects/micro-dash/src/lib/array/nth.spec.ts
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();
});
});
27 changes: 27 additions & 0 deletions projects/micro-dash/src/lib/array/nth.ts
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];
}
}

0 comments on commit 61a37b1

Please sign in to comment.