-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
2 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
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,37 @@ | ||
--- | ||
title: stride | ||
description: stride | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
Creates a new array by taking every nth element from the input array. | ||
|
||
## Type | ||
|
||
```ts | ||
type stride = (step: number) => <E>(array: E[]) => E[] | ||
``` | ||
## Parameters | ||
- `step` (number): The step size for selecting elements from the input array. | ||
- `array` (Array): An array of elements from which to select elements. | ||
## Returns | ||
- (Array): A new array containing every nth element from the input array. | ||
## Example | ||
```ts | ||
import {stride} from 'fnxt/array'; | ||
|
||
const array = [1, 2, 3, 4, 5, 6, 7]; | ||
const stride = stride(3); | ||
stride(array) // -> [1, 4, 7] | ||
``` | ||
|
||
## See Also | ||
|
||
- [skip](./skip) | ||
- [take](./take) |
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
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,31 @@ | ||
import {expect} from 'chai'; | ||
import {checkThrow} from '../../../support/checkThrow'; | ||
import {stride} from '../../../../src/array'; | ||
|
||
describe('stride', () => { | ||
|
||
it('should stride 1', () => { | ||
const fn = stride(1); | ||
expect(fn([1, 2, 3, 4, 5, 6])).to.eql([1, 2, 3, 4, 5, 6],); | ||
}); | ||
|
||
it('should stride 2', () => { | ||
const fn = stride(2); | ||
expect(fn([1, 2, 3, 4, 5, 6])).to.eql([1, 3, 5],); | ||
}); | ||
|
||
it('should stride 3', () => { | ||
const fn = stride(3); | ||
expect(fn([1, 2, 3, 4])).to.eql([1, 4],); | ||
}); | ||
|
||
it('should stride empty', () => { | ||
const fn = stride(2); | ||
expect(fn([])).to.eql([]); | ||
}); | ||
|
||
it('should throw if null or undefined', () => { | ||
const fn = stride(1); | ||
checkThrow(fn); | ||
}); | ||
}); |
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,7 @@ | ||
export const stride = (step: number) => <E>(array: E[]): E[] => { | ||
const result = []; | ||
for (let i = 0; i < array.length; i += step) { | ||
result.push(array[i]); | ||
} | ||
return result; | ||
}; |