Skip to content

Commit

Permalink
improve some array types to handle readonly arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Jan 14, 2024
1 parent 3d8a883 commit 23e739c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-rice-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@grogarden/util": patch
---

improve some array types to handle readonly arrays
8 changes: 6 additions & 2 deletions src/lib/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type {Array_Element} from '$lib/types.js';

// TODO try to cange to readonly again, see if upstream errors are tolerably fixed
export const EMPTY_ARRAY: any[] = Object.freeze([]) as any;

export const to_array = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);
export const to_array = <T>(value: T): T extends readonly any[] ? T : T[] =>
Array.isArray(value) ? value : ([value] as any);

export const remove_unordered = (array: any[], index: number): void => {
array[index] = array[array.length - 1];
Expand All @@ -11,7 +15,7 @@ export const remove_unordered = (array: any[], index: number): void => {
* Returns a function that returns the next item in the `array`
* in a linear sequence, looping back to index 0 when it reaches the end.
*/
export const to_next = <T>(array: T[]): (() => T) => {
export const to_next = <T extends readonly any[]>(array: T): (() => Array_Element<T>) => {
let i = -1;
return () => {
i++;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test__random_item('a and b', () => {
const items = ['a', 'b'];
const results = [];
for (let i = 0; i < 20; i++) {
const result = random_item(items)!;
const result = random_item(items);
assert.ok(items.includes(result));
results.push(result);
}
Expand All @@ -97,7 +97,7 @@ test__random_item('1 to 5', () => {
const items = [1, 2, 3, 4, 5];
const results = [];
for (let i = 0; i < 100; i++) {
const result = random_item(items)!;
const result = random_item(items);
assert.ok(items.includes(result));
results.push(result);
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/random.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {Array_Element} from '$lib/types.js';

export const random_float = (min: number, max: number, random = Math.random): number =>
random() * (max - min) + min;

Expand All @@ -11,8 +13,10 @@ export const random_int = (min: number, max: number, random = Math.random): numb

export const random_boolean = (random = Math.random): boolean => random() > 0.5;

export const random_item = <T>(arr: T[], random = Math.random): T =>
arr[random_int(0, arr.length - 1, random)];
export const random_item = <T extends readonly any[]>(
arr: T,
random = Math.random,
): Array_Element<T> => arr[random_int(0, arr.length - 1, random)];

/**
* Mutates `array` with random ordering.
Expand Down

0 comments on commit 23e739c

Please sign in to comment.