Skip to content

Commit

Permalink
refactor[isTupleOf]: reduce the use of array slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Milly committed Aug 13, 2024
1 parent 4dac22d commit 1e80cdd
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions is/tuple_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export function isTupleOf<
if (!isArray(x) || x.length < predTup.length) {
return false;
}
const rest = x.slice(0, -predTup.length);
const trail = x.slice(-predTup.length);
return predTup.every((pred, i) => pred(trail[i])) && predRest(rest);
const offset = x.length - predTup.length;
return predTup.every((pred, i) => pred(x[offset + i])) &&
predRest(x.slice(0, offset));
},
"isTupleOf",
predRest,
Expand All @@ -154,8 +154,8 @@ export function isTupleOf<
if (!isArray(x) || x.length < predTup.length) {
return false;
}
const rest = x.slice(predTup.length);
return predTup.every((pred, i) => pred(x[i])) && predRest(rest);
return predTup.every((pred, i) => pred(x[i])) &&
predRest(x.slice(predTup.length));
},
"isTupleOf",
predTup,
Expand All @@ -169,11 +169,10 @@ export function isTupleOf<
if (!isArray(x) || x.length < (predTup.length + predTrail.length)) {
return false;
}
const rest = x.slice(predTup.length, -predTrail.length);
const trail = x.slice(-predTrail.length);
const offset = x.length - predTrail.length;
return predTup.every((pred, i) => pred(x[i])) &&
predTrail.every((pred, i) => pred(trail[i])) &&
predRest(rest);
predTrail.every((pred, i) => pred(x[offset + i])) &&
predRest(x.slice(predTup.length, offset));
},
"isTupleOf",
predTup,
Expand Down

0 comments on commit 1e80cdd

Please sign in to comment.