Skip to content

Commit

Permalink
chore(micro-dash): accept readonly arrays when possible
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
ersimont committed Apr 7, 2021
1 parent 38cd5a7 commit 7fc161e
Show file tree
Hide file tree
Showing 51 changed files with 140 additions and 102 deletions.
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - Lodash: 2,206 bytes
* - Micro-dash: 177 bytes
*/
export function chunk<T>(array: T[], size = 1): T[][] {
export function chunk<T>(array: readonly T[], size = 1): T[][] {
size = Math.max(Math.trunc(size), 0);
const chunks = [];
for (let i = 0; i < array.length; i += Math.max(1, size)) {
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { identity } from '../util';
* - Lodash: 96 bytes
* - Micro-dash: 35 bytes
*/
export function compact<T>(array: Array<T>): Array<Exclude<T, Falsey>> {
export function compact<T>(array: readonly T[]): Array<Exclude<T, Falsey>> {
return array.filter(identity) as Array<Exclude<T, Falsey>>;
}
5 changes: 4 additions & 1 deletion projects/micro-dash/src/lib/array/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* - Lodash: 1,410 bytes
* - Micro-dash: 36 bytes
*/
export function concat<T>(array: T[], ...values: Array<T | T[]>): T[] {
export function concat<T>(
array: readonly T[],
...values: Array<T | readonly T[]>
): T[] {
return array.concat(...values);
}
5 changes: 4 additions & 1 deletion projects/micro-dash/src/lib/array/difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { flatten } from './flatten';
* - Lodash: 6,275 bytes
* - Micro-dash: 278 bytes
*/
export function difference<T>(array: T[], ...values: T[][]): T[] {
export function difference<T>(
array: readonly T[],
...values: readonly T[][]
): T[] {
return pullAll(array.slice(), flatten(values));
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* - Lodash: 1,183 bytes
* - Micro-dash: 29 bytes
*/
export function flatten<T>(array: Array<T | T[]>): T[] {
export function flatten<T>(array: ReadonlyArray<T | readonly T[]>): T[] {
return ([] as T[]).concat(...array);
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* - Lodash: 207 bytes
* - Micro-dash: 33 bytes
*/
export function initial<T>(array: T[]): T[] {
export function initial<T>(array: readonly T[]): T[] {
return array.slice(0, -1);
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
* - Lodash: 68 bytes
* - Micro-dash: 43 bytes
*/
export function last<T>(array: T[]): T {
export function last<T>(array: readonly T[]): T {
return array[array.length - 1];
}
9 changes: 3 additions & 6 deletions projects/micro-dash/src/lib/array/nth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import { Nil } from '../interfaces';
* - Micro-dash: 96 bytes
*/

export function nth<T>(array: ReadonlyArray<T>, index: number): T;
export function nth<T>(
array: ReadonlyArray<T> | Nil,
index: number,
): T | undefined;
export function nth<T>(array: readonly T[], index: number): T;
export function nth<T>(array: readonly T[] | Nil, index: number): T | undefined;

export function nth<T>(
array: ReadonlyArray<T> | Nil,
array: readonly T[] | Nil,
index: number,
): T | undefined {
if (array) {
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/sorted-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Nil } from '../interfaces';
* - Lodash: 1,266 bytes
* - Micro-dash: 277 bytes
*/
export function sortedIndex<T>(array: T[] | Nil, value: T): number {
export function sortedIndex<T>(array: readonly T[] | Nil, value: T): number {
let min = 0;
let max = array ? array.length - 1 : 0;
while (max > min) {
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import { flatten } from './flatten';
* - Lodash: 6,214 bytes
* - Micro-dash: 72 bytes
*/
export function union<T>(...arrays: T[][]): T[] {
export function union<T>(...arrays: readonly T[][]): T[] {
return Array.from(new Set(flatten(arrays)));
}
5 changes: 4 additions & 1 deletion projects/micro-dash/src/lib/array/uniq-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { ValueIteratee } from '../interfaces';
* - Lodash: 8,958 bytes
* - Micro-dash: 115 bytes
*/
export function uniqBy<T>(array: T[], iteratee: ValueIteratee<T, any>): T[] {
export function uniqBy<T>(
array: readonly T[],
iteratee: ValueIteratee<T, any>,
): T[] {
const seen = new Set<T>();
return array.filter((element) => {
const key = iteratee(element);
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/uniq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* - Lodash: 4,573 bytes
* - Micro-dash: 40 bytes
*/
export function uniq<T>(array: T[]): T[] {
export function uniq<T>(array: readonly T[]): T[] {
return Array.from(new Set(array));
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/array/without.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
* - Lodash: 5,650 bytes
* - Micro-dash: 64 bytes
*/
export function without<T>(array: T[], ...values: T[]): T[] {
export function without<T>(array: readonly T[], ...values: readonly T[]): T[] {
return array.filter((item) => values.indexOf(item) === -1);
}
30 changes: 15 additions & 15 deletions projects/micro-dash/src/lib/array/zip-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import { transform } from '../object';
* - Micro-dash: 303 bytes
*/

export function zipObject<K extends [Key], V extends [any, ...any[]]>(
props: K,
values: V,
): { [k in K[0]]: V[0] };
export function zipObject<K extends [Key, Key], V extends [any, any, ...any[]]>(
props: K,
values: V,
): { [k in K[0]]: V[0] } & { [k in K[1]]: V[1] };
export function zipObject<
K extends [Key, Key, Key],
V extends [any, any, any, ...any[]]
K extends readonly [Key],
V extends readonly [any, ...any[]]
>(props: K, values: V): { [k in K[0]]: V[0] };
export function zipObject<
K extends readonly [Key, Key],
V extends readonly [any, any, ...any[]]
>(props: K, values: V): { [k in K[0]]: V[0] } & { [k in K[1]]: V[1] };
export function zipObject<
K extends readonly [Key, Key, Key],
V extends readonly [any, any, any, ...any[]]
>(
props: K,
values: V,
): { [k in K[0]]: V[0] } & { [k in K[1]]: V[1] } & { [k in K[2]]: V[2] };
export function zipObject<
K extends [Key, Key, Key, Key],
V extends [any, any, any, any, ...any[]]
K extends readonly [Key, Key, Key, Key],
V extends readonly [any, any, any, any, ...any[]]
>(
props: K,
values: V,
Expand All @@ -35,11 +35,11 @@ export function zipObject<
{ [k in K[2]]: V[2] } &
{ [k in K[3]]: V[3] };
export function zipObject<K extends Key, V>(
props: K[],
values: V[],
props: readonly K[],
values: readonly V[],
): { [k in K]: V | undefined };

export function zipObject(props: Key[], values: any[]): any {
export function zipObject(props: readonly Key[], values: readonly any[]): any {
return transform(props, (accumulator: any, prop, index) => {
accumulator[prop] = values[index];
});
Expand Down
29 changes: 18 additions & 11 deletions projects/micro-dash/src/lib/array/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ import { times } from '../util';
* - Micro-dash: 190 bytes
*/

export function zip<T1, T2>(array1: T1[], array2: T2[]): Array<[T1, T2]>;
export function zip<T1, T2>(
array1: readonly T1[],
array2: readonly T2[],
): Array<[T1, T2]>;
export function zip<T1, T2, T3>(
array1: T1[],
array2: T2[],
array3: T3[],
array1: readonly T1[],
array2: readonly T2[],
array3: readonly T3[],
): Array<[T1, T2, T3]>;
export function zip<T1, T2, T3, T4>(
array1: T1[],
array2: T2[],
array3: T3[],
array4: T4[],
array1: readonly T1[],
array2: readonly T2[],
array3: readonly T3[],
array4: readonly T4[],
): Array<[T1, T2, T3, T4]>;
export function zip<T>(...arrays: Array<T[]>): T[][];
export function zip<T>(...arrays: Array<T[] | Nil>): Array<Array<T | Nil>>;
export function zip<T>(...arrays: readonly T[][]): T[][];
export function zip<T>(
...arrays: ReadonlyArray<readonly T[] | Nil>
): Array<Array<T | Nil>>;

export function zip<T>(...arrays: Array<T[] | Nil>): Array<Array<T | Nil>> {
export function zip<T>(
...arrays: ReadonlyArray<readonly T[] | Nil>
): Array<Array<T | Nil>> {
const length = Math.max(0, ...arrays.map((a) => (a ? a.length : 0)));
return times(length, (i) => arrays.map((a) => (a ? a[i] : undefined)));
}
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/every.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { forEach } from './for-each';
*/

export function every<T>(
array: T[] | undefined,
array: readonly T[] | undefined,
predicate: ArrayIteratee<T, any>,
): boolean;
export function every<T>(
Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import { forEach } from './for-each';
*/

export function filter<I, O>(
array: I[] | Nil,
array: readonly I[] | Nil,
predicate: ArrayNarrowingIteratee<O>,
): Array<Extract<I, O> | Extract<O, I>>;
export function filter<T>(
array: T[] | Nil,
array: readonly T[] | Nil,
predicate: ArrayIteratee<T, boolean>,
): T[];

Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ type PossibleKeyMatches<T, O> = {

// array: value narrowing
export function find<I, O>(
array: I[] | Nil,
array: readonly I[] | Nil,
predicate: ArrayNarrowingIteratee<O>,
fromIndex?: number,
): Extract<I, O> | Extract<O, I> | undefined;

// array
export function find<T>(
array: T[] | Nil,
array: readonly T[] | Nil,
predicate: ArrayIteratee<T, boolean>,
fromIndex?: number,
): T | undefined;
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/flat-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { map } from './map';
*/

export function flatMap<I, O>(
array: I[] | Nil,
array: readonly I[] | Nil,
iteratee: ArrayIteratee<I, O | O[]>,
): O[];
export function flatMap<T, O>(
Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/for-each-right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { forOwnRightOfNonArray } from '../object/for-own-right';
*/

export function forEachRight<T>(
array: T[] | undefined,
array: readonly T[] | undefined,
iteratee: ArrayIteratee<T, void | boolean>,
): T[];
export function forEachRight<T>(
Expand All @@ -29,7 +29,7 @@ export function forEachRight(collection: any, iteratee: any): any {

/** @hidden */
export function forEachRightOfArray<T>(
array: T[],
array: readonly T[],
iteratee: ArrayIteratee<T, void | boolean>,
): void {
for (let i = array.length; --i >= 0; ) {
Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/for-each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { forOwnOfNonArray } from '../object/for-own';
* - Micro-dash: 236 bytes
*/

export function forEach<T extends any[] | Nil>(
export function forEach<T extends readonly any[] | Nil>(
array: T,
iteratee: ArrayIteratee<NonNullable<T>[number], void | boolean>,
): T;
Expand All @@ -29,7 +29,7 @@ export function forEach(collection: any, iteratee: any): any {

/** @hidden */
export function forEachOfArray<T>(
array: T[],
array: readonly T[],
iteratee: ArrayIteratee<T, void | boolean>,
): void {
for (let i = 0, len = array.length; i < len; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/group-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { transform } from '../object/transform';
* - Micro-dash: 331 bytes
*/
export function groupBy<T, K extends Key>(
collection: T[] | ObjectWith<T> | Nil,
collection: readonly T[] | ObjectWith<T> | Nil,
iteratee: ValueIteratee<T, K>,
): { [k in K]: IfIndexType<K, T[], T[] | undefined> } {
return transform(
Expand Down
8 changes: 6 additions & 2 deletions projects/micro-dash/src/lib/collection/includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { isString, toArray } from '../lang';
* - Lodash: 4,546 bytes
* - Micro-dash: 319 bytes
*/
export function includes<T extends any[] | object | string>(
export function includes<T extends readonly any[] | object | string>(
collection: T,
value: T extends any[] ? T[0] : T extends string ? string : T[keyof T],
value: T extends readonly any[]
? T[0]
: T extends string
? string
: T[keyof T],
fromIndex = 0,
): boolean {
if (isString(collection)) {
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/key-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { forEach } from './for-each';
*/

export function keyBy<T, K extends Key>(
array: T[] | Nil,
array: readonly T[] | Nil,
iteratee: ValueIteratee<T, K>,
): IfIndexType<K, { [key in K]: T }, { [key in K]?: T }>;
export function keyBy<T, K extends Key>(
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { forEach } from './for-each';
*/

export function map<I, O>(
array: I[] | undefined,
array: readonly I[] | undefined,
iteratee: ArrayIteratee<I, O>,
): O[];
export function map<T, O>(
Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/reduce-right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { doReduce } from './reduce-utils';
* - Micro-dash: 357 bytes
*/

export function reduceRight<T extends any[] | Nil>(
export function reduceRight<T extends readonly any[] | Nil>(
array: T,
iteratee: (
accumulator: NonNullable<T>[number],
Expand All @@ -19,7 +19,7 @@ export function reduceRight<T extends any[] | Nil>(
) => NonNullable<T>[number],
): NonNullable<T>[number] | IfCouldBe<T, Nil, undefined>;
export function reduceRight<E, A>(
array: E[] | Nil,
array: readonly E[] | Nil,
iteratee: (accumulator: A, value: E, index: number) => A,
accumulator: A,
): A;
Expand Down
4 changes: 2 additions & 2 deletions projects/micro-dash/src/lib/collection/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { doReduce } from './reduce-utils';
* - Micro-dash: 361 bytes
*/

export function reduce<T extends any[] | Nil>(
export function reduce<T extends readonly any[] | Nil>(
array: T,
iteratee: (
accumulator: NonNullable<T>[number],
Expand All @@ -19,7 +19,7 @@ export function reduce<T extends any[] | Nil>(
) => NonNullable<T>[number],
): NonNullable<T>[number] | IfCouldBe<T, Nil, undefined>;
export function reduce<E, A>(
array: E[] | Nil,
array: readonly E[] | Nil,
iteratee: (accumulator: A, value: E, index: number) => A,
accumulator: A,
): A;
Expand Down
2 changes: 1 addition & 1 deletion projects/micro-dash/src/lib/collection/sample-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { map } from './map';
* - Micro-dash: 706 bytes
*/

export function sampleSize<T>(array: T[] | Nil, n?: number): T[];
export function sampleSize<T>(array: readonly T[] | Nil, n?: number): T[];
export function sampleSize<T>(object: T | Nil, n?: number): Array<T[keyof T]>;

export function sampleSize(collection: any, n = 1): any[] {
Expand Down
Loading

0 comments on commit 7fc161e

Please sign in to comment.