Skip to content

Commit

Permalink
refactor(collections): prepare for noUncheckedIndexedAccess (denola…
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 authored Feb 2, 2024
1 parent 9bc4116 commit 9d0317a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion collections/drop_last_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function dropLastWhile<T>(
predicate: (el: T) => boolean,
): T[] {
let offset = array.length;
while (0 < offset && predicate(array[offset - 1])) offset--;
while (0 < offset && predicate(array[offset - 1] as T)) offset--;

return array.slice(0, offset);
}
2 changes: 1 addition & 1 deletion collections/drop_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function dropWhile<T>(
let offset = 0;
const length = array.length;

while (length > offset && predicate(array[offset])) {
while (length > offset && predicate(array[offset] as T)) {
offset++;
}

Expand Down
5 changes: 2 additions & 3 deletions collections/filter_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ export function filterKeys<T>(
predicate: (key: string) => boolean,
): Record<string, T> {
const ret: Record<string, T> = {};
const keys = Object.keys(record);

for (const key of keys) {
for (const [key, value] of Object.entries(record)) {
if (predicate(key)) {
ret[key] = record[key];
ret[key] = value;
}
}

Expand Down
6 changes: 2 additions & 4 deletions collections/map_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ export function mapKeys<T>(
transformer: (key: string) => string,
): Record<string, T> {
const ret: Record<string, T> = {};
const keys = Object.keys(record);

for (const key of keys) {
for (const [key, value] of Object.entries(record)) {
const mappedKey = transformer(key);

ret[mappedKey] = record[key];
ret[mappedKey] = value;
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion collections/map_keys_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Deno.test({
"/home/deno/food.txt": "Plants, preferably fruit",
"/home/deno/other-dinos.txt": "Noderaptor, Pythonoctorus",
},
(path) => path.split("/").slice(-1)[0],
(path) => (path.split("/").slice(-1) as [string])[0],
],
{
"food.txt": "Plants, preferably fruit",
Expand Down
6 changes: 3 additions & 3 deletions collections/permutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export function permutations<T>(inputArray: Iterable<T>): T[][] {
let i = 1;

while (i < k) {
if (c[i] < i) {
if (c[i]! < i) {
if (i % 2 === 0) {
[array[0], array[i]] = [array[i], array[0]];
[array[0], array[i]] = [array[i], array[0]] as [T, T];
} else {
[array[c[i]], array[i]] = [array[i], array[c[i]]];
[array[c[i]!], array[i]] = [array[i], array[c[i]!]] as [T, T];
}

ret.push([...array]);
Expand Down
16 changes: 8 additions & 8 deletions collections/sort_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ export function sortBy<T>(
const selectors = new Array<ReturnType<typeof selector> | null>(len);
const order = options?.order ?? "asc";

for (let i = 0; i < len; i++) {
indexes[i] = i;
const s = selector(array[i]);
selectors[i] = Number.isNaN(s) ? null : s;
}
array.forEach((item, idx) => {
indexes[idx] = idx;
const s = selector(item);
selectors[idx] = Number.isNaN(s) ? null : s;
});

indexes.sort((ai, bi) => {
let a = selectors[ai];
let b = selectors[bi];
let a = selectors[ai]!;
let b = selectors[bi]!;
if (order === "desc") {
[a, b] = [b, a];
}
Expand All @@ -144,7 +144,7 @@ export function sortBy<T>(
});

for (let i = 0; i < len; i++) {
(indexes as unknown as T[])[i] = array[indexes[i]];
(indexes as unknown as T[])[i] = array[indexes[i] as number] as T;
}

return indexes as unknown as T[];
Expand Down
2 changes: 1 addition & 1 deletion collections/take_last_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function takeLastWhile<T>(
predicate: (el: T) => boolean,
): T[] {
let offset = array.length;
while (0 < offset && predicate(array[offset - 1])) offset--;
while (0 < offset && predicate(array[offset - 1] as T)) offset--;

return array.slice(offset, array.length);
}
2 changes: 1 addition & 1 deletion collections/take_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function takeWhile<T>(
let offset = 0;
const length = array.length;

while (length > offset && predicate(array[offset])) {
while (length > offset && predicate(array[offset] as T)) {
offset++;
}

Expand Down

0 comments on commit 9d0317a

Please sign in to comment.