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
8 changes: 6 additions & 2 deletions docs/.vitepress/components/FlavorDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ function toggle() {

function select(option: FlavorSpec) {
open.value = false;
if (option.value === currentFlavor.value.value) return;
if (option.value === currentFlavor.value.value) {
return;
}
router.go(flavorIntroPath({ flavor: option, localePrefix: localePrefix.value }));
}

function onDocumentPointerDown(event: PointerEvent) {
if (!open.value || !root.value) return;
if (!open.value || !root.value) {
return;
}
if (!root.value.contains(event.target as Node)) {
open.value = false;
}
Expand Down
4 changes: 3 additions & 1 deletion docs/.vitepress/components/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ const openCategories = ref(new Set(['array']));
const sandpackKey = ref(0);

const docUrl = computed(() => {
if (!selectedFunction.value || !selectedCategory.value) return null;
if (!selectedFunction.value || !selectedCategory.value) {
return null;
}
const locale = lang.value;
const prefix = locale && locale !== 'en' && locale !== 'root' ? `/${locale}` : '';
return `${prefix}/reference/${selectedCategory.value}/${selectedFunction.value}`;
Expand Down
33 changes: 33 additions & 0 deletions src/compat/array/reduce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
});

it(`should support empty collections without an initial \`accumulator\` value`, () => {
const actual: any[] = [];

Check warning on line 20 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const expected = lodashStable.map(empties, lodashStable.noop);

lodashStable.each(empties, value => {
Expand Down Expand Up @@ -54,13 +54,46 @@
expect(actual).toBe(undefined);
});

it('should respect nullish initial `accumulator` values', () => {
const array = [1, 2, 3];
const object = { a: 1, b: 2 };

for (const accumulator of [null, undefined]) {
let args: unknown[] | undefined;

reduce(
array,
function () {
// eslint-disable-next-line
args || (args = Array.prototype.slice.call(arguments));
},
accumulator
);

expect(args).toEqual([accumulator, 1, 0, array]);

args = undefined;

reduce(
object,
function () {
// eslint-disable-next-line
args || (args = Array.prototype.slice.call(arguments));
},
accumulator
);

expect(args).toEqual([accumulator, 1, 'a', object]);
}
});

it(`should return \`undefined\` for empty collections when no \`accumulator\` is given (test in IE > 9 and modern browsers)`, () => {
const array: any[] = [];

Check warning on line 91 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const object = { 0: 1, length: 0 };

if ('__proto__' in array) {
array.__proto__ = object;
expect(reduce(array, lodashStable.noop)).toBe(undefined);

Check warning on line 96 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling `expect` inside conditional statements
}
// eslint-disable-next-line
// @ts-ignore
Expand All @@ -74,7 +107,7 @@
});

it('should provide correct `iteratee` arguments when iterating an array', () => {
let args: any;

Check warning on line 110 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

reduce(
array,
Expand All @@ -101,7 +134,7 @@
});

it('should provide correct `iteratee` arguments when iterating an object', () => {
let args: any;

Check warning on line 137 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const object = { a: 1, b: 2 };
const firstKey = head(keys(object));

Expand Down Expand Up @@ -134,7 +167,7 @@
});

it(`should use \`isArrayLike\` to determine whether a value is array-like`, () => {
const isIteratedAsObject = function (object: any) {

Check warning on line 170 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let result = false;

reduce(
Expand All @@ -142,7 +175,7 @@
() => {
result = true;
},
0 as any

Check warning on line 178 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
);
return result;
};
Expand All @@ -163,7 +196,7 @@

it(`should ignore added \`object\` properties`, () => {
let count = 0;
const object: any = { a: 1 };

Check warning on line 199 in src/compat/array/reduce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

reduce(
object,
Expand Down
6 changes: 4 additions & 2 deletions src/compat/array/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@
* reduce(obj, (acc, value) => acc + value, 0); // => 6
*/
export function reduce(
collection: ArrayLike<any> | Record<any, any> | null | undefined,

Check warning on line 130 in src/compat/array/reduce.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 130 in src/compat/array/reduce.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
iteratee: (accumulator: any, value: any, index: any, collection: any) => any = identity,
accumulator?: any
): any {
const hasAccumulator = arguments.length >= 3;

if (!collection) {
return accumulator;
}
Expand All @@ -141,14 +143,14 @@
if (isArrayLike(collection)) {
keys = range(0, collection.length);

if (accumulator == null && collection.length > 0) {
if (!hasAccumulator && collection.length > 0) {
accumulator = (collection as ArrayLike<any>)[0];
startIndex += 1;
}
} else {
keys = Object.keys(collection);

if (accumulator == null) {
if (!hasAccumulator && keys.length > 0) {
accumulator = (collection as any)[keys[0]];
startIndex += 1;
}
Expand Down
33 changes: 33 additions & 0 deletions src/compat/array/reduceRight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,39 @@ describe('reduceRight', () => {
expect(actual).toBe(undefined);
});

it('should respect nullish initial `accumulator` values', () => {
const array = [1, 2, 3];
const object = { a: 1, b: 2 };

for (const accumulator of [null, undefined]) {
let args: unknown[] | undefined;

reduceRight(
array,
function () {
// eslint-disable-next-line
args || (args = Array.prototype.slice.call(arguments));
},
accumulator
);

expect(args).toEqual([accumulator, 3, 2, array]);

args = undefined;

reduceRight(
object,
function () {
// eslint-disable-next-line
args || (args = Array.prototype.slice.call(arguments));
},
accumulator
);

expect(args).toEqual([accumulator, 2, 'b', object]);
}
});

it(`should return \`undefined\` for empty collections when no \`accumulator\` is given (test in IE > 9 and modern browsers)`, () => {
const array: any[] = [];
const object = { 0: 1, length: 0 };
Expand Down
6 changes: 4 additions & 2 deletions src/compat/array/reduceRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export function reduceRight(
iteratee: (accumulator: any, value: any, index: any, collection: any) => any = identity,
accumulator?: any
): any {
const hasAccumulator = arguments.length >= 3;

if (!collection) {
return accumulator;
}
Expand All @@ -182,7 +184,7 @@ export function reduceRight(
if (isArrayLike(collection)) {
keys = range(0, collection.length).reverse();

if (accumulator == null && collection.length > 0) {
if (!hasAccumulator && collection.length > 0) {
accumulator = (collection as ArrayLike<any>)[collection.length - 1];
startIndex = 1;
} else {
Expand All @@ -191,7 +193,7 @@ export function reduceRight(
} else {
keys = Object.keys(collection).reverse();

if (accumulator == null) {
if (!hasAccumulator && keys.length > 0) {
accumulator = (collection as any)[keys[0]];
startIndex = 1;
} else {
Expand Down