Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/array/take.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toInteger } from '../compat/util/toInteger.ts';

/**
* Returns a new array containing the first `count` elements from the input array `arr`.
* If `count` is greater than the length of `arr`, the entire array is returned.
Expand All @@ -20,6 +22,7 @@
* // Returns [1, 2, 3]
* take([1, 2, 3], 5);
*/
export function take<T>(arr: readonly T[], count: number): T[] {
export function take<T>(arr: readonly T[], count?: number, guard?: unknown): T[] {
count = guard || count === undefined ? 1 : toInteger(count);
return arr.slice(0, count);
}
8 changes: 5 additions & 3 deletions src/array/takeRight.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toInteger } from '../compat/util/toInteger.ts';

/**
* Returns a new array containing the last `count` elements from the input array `arr`.
* If `count` is greater than the length of `arr`, the entire array is returned.
Expand All @@ -19,10 +21,10 @@
* // Returns [1, 2, 3]
* takeRight([1, 2, 3], 5);
*/
export function takeRight<T>(arr: readonly T[], count = 1): T[] {
if (count <= 0) {
export function takeRight<T>(arr: readonly T[], count = 1, guard?: unknown): T[] {
count = guard || count === undefined ? 1 : toInteger(count);
if (count <= 0 || arr == null || arr.length === 0) {
return [];
}

return arr.slice(-count);
}
9 changes: 8 additions & 1 deletion src/compat/object/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isIterateeCall } from '../_internal/isIterateeCall.ts';
import { eq } from '../util/eq.ts';

/**
Expand Down Expand Up @@ -164,7 +165,13 @@ export function defaults<T extends object, S extends object>(object: T, ...sourc
object = Object(object);
const objectProto = Object.prototype;

for (let i = 0; i < sources.length; i++) {
let length = sources.length;
const guard = length > 2 ? sources[2] : undefined;
if (guard && isIterateeCall(sources[0], sources[1], guard)) {
length = 1;
}

for (let i = 0; i < length; i++) {
const source = sources[i];
const keys = Object.keys(source) as Array<keyof S>;

Expand Down
13 changes: 11 additions & 2 deletions src/compat/string/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { isIterateeCall } from '../_internal/isIterateeCall.ts';
import { toInteger } from '../util/toInteger.ts';
import { toString } from '../util/toString.ts';

/**
* Repeats the given string n times.
*
Expand All @@ -12,6 +16,11 @@
* repeat('abc', 0); // ''
* repeat('abc', 2); // 'abcabc'
*/
export function repeat(str: string, n: number): string {
return str.repeat(n);
export function repeat(str: string, n?: number, guard?: unknown): string {
if (guard ? isIterateeCall(str, n, guard) : n === undefined) {
n = 1;
} else {
n = toInteger(n);
}
return toString(str).repeat(n);
}
4 changes: 2 additions & 2 deletions src/compat/string/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { toString } from '../util/toString.ts';
* // => ['fred', 'barney', 'pebbles']
*
*/
export function words(str?: string | object, pattern: RegExp | string = CASE_SPLIT_PATTERN): string[] {
export function words(str?: string | object, pattern: RegExp | string = CASE_SPLIT_PATTERN, guard?: unknown): string[] {
const input = toString(str);

pattern = guard ? CASE_SPLIT_PATTERN : pattern;
const words = Array.from(input.match(pattern) ?? []);

return words.filter(x => x !== '');
Expand Down