This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returns the length of an array or arity of a function
- Loading branch information
Showing
8 changed files
with
26 additions
and
6 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,7 @@ | ||
import length from '../length'; | ||
|
||
test('Core.length', () => { | ||
expect(length([])).toBe(0); | ||
expect(length([1, 2, 3])).toBe(3); | ||
expect(length((a, b) => a + b)).toBe(2); | ||
}); |
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
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 @@ | ||
export default arr => arr.length; |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import length from './length'; | ||
|
||
export default (n, arr) => { | ||
const index = n < 0 ? arr.length + n : n; | ||
const index = n < 0 ? length(arr) + n : n; | ||
return typeof arr === 'string' ? arr.charAt(index) : arr[index]; | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import length from './length'; | ||
|
||
export default (fn, init, arr) => { | ||
let l = init; | ||
for (let i = 0; i < arr.length; i++) l = fn(l, arr[i]); | ||
for (let i = 0; i < length(arr); i++) l = fn(l, arr[i]); | ||
return l; | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import length from './length'; | ||
|
||
export default (fn, init, arr) => { | ||
let l = init; | ||
for (let i = arr.length - 1; i >= 0; i--) l = fn(l, arr[i]); | ||
for (let i = length(arr) - 1; i >= 0; i--) l = fn(l, arr[i]); | ||
return l; | ||
}; |