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: 5 additions & 0 deletions src/compat/array/size.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
expect(size(args)).toBe(3);
});

// it('should work with jQuery/MooTools DOM query collections', () => {

Check warning on line 39 in src/compat/array/size.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Remove commented out tests - you may want to use `skip` or `only` instead
// function Foo(elements: unknown[]) {
// Array.prototype.push.apply(this, elements);
// }
Expand Down Expand Up @@ -66,6 +66,11 @@
expect(size(str)).toBe(10);
});

it('should count multi-byte characters in a string as single code points', () => {
expect(size('😀😁😂')).toBe(3);
expect(size('a😀b')).toBe(3);
});

it('should return the `length` of array-like objects', () => {
expect(size({ length: 3 })).toBe(3);
expect(size({ 0: 'a', 1: 'b', length: 2 })).toBe(2);
Expand Down
7 changes: 7 additions & 0 deletions src/compat/array/size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isNil } from '../../predicate/isNil.ts';
import { isArrayLike } from '../predicate/isArrayLike.ts';

// eslint-disable-next-line no-misleading-character-class
const regexMultiByte = /[\u200d\ud800-\udfff\u0300-\u036f\ufe20-\ufe2f\u20d0-\u20ff\ufe0e\ufe0f]/;

/**
* Returns the length of an array, string, or object.
*
Expand Down Expand Up @@ -43,11 +46,15 @@
*/
export function size(collection: object | string | null | undefined): number;

export function size(target: any): number {

Check warning on line 49 in src/compat/array/size.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (isNil(target)) {
return 0;
}

if (typeof target === 'string') {
return regexMultiByte.test(target) ? Array.from(target).length : target.length;
}

if (isArrayLike(target)) {
return target.length;
}
Expand Down