Skip to content

Commit 4b30ed7

Browse files
committed
🐛 fix: take/drop*() for -ve indices/length
1 parent 470b4b3 commit 4b30ed7

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "extra-array",
3-
"version": "4.1.10",
3+
"version": "4.1.11",
44
"description": "An array is a collection of values, stored contiguously.",
55
"main": "index.js",
66
"module": "index.mjs",

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,7 @@ export function findRight<T>(x: T[], ft: TestFunction<T>): T {
20332033
* @returns x[0..n]
20342034
*/
20352035
export function take<T>(x: T[], n: number=1): T[] {
2036+
var n = Math.max(n, 0);
20362037
return x.slice(0, n);
20372038
}
20382039
export {take as left};
@@ -2045,7 +2046,9 @@ export {take as left};
20452046
* @returns x[0..n]
20462047
*/
20472048
export function takeRight<T>(x: T[], n: number=1): T[] {
2048-
return x.slice(x.length-n);
2049+
var X = x.length;
2050+
var n = Math.min(Math.max(n, 0), X);
2051+
return x.slice(X-n);
20492052
}
20502053
export {takeRight as right};
20512054

@@ -2079,6 +2082,7 @@ export function takeWhileRight<T>(x: T[], ft: TestFunction<T>): T[] {
20792082
* @returns x[n..]
20802083
*/
20812084
export function drop<T>(x: T[], n: number=1): T[] {
2085+
var n = Math.max(n, 0);
20822086
return x.slice(n);
20832087
}
20842088

@@ -2090,7 +2094,9 @@ export function drop<T>(x: T[], n: number=1): T[] {
20902094
* @returns x[0..-n]
20912095
*/
20922096
export function dropRight<T>(x: T[], n: number=1): T[] {
2093-
return x.slice(0, x.length-n);
2097+
var X = x.length;
2098+
var n = Math.min(Math.max(n, 0), X);
2099+
return x.slice(0, X-n);
20942100
}
20952101

20962102

0 commit comments

Comments
 (0)