diff --git a/src/array.ts b/src/array.ts index 4ca20a1..b0de4a3 100644 --- a/src/array.ts +++ b/src/array.ts @@ -1,3 +1,4 @@ +import { clamp } from './math' import { Arrayable, Nullable } from './types' /** @@ -125,3 +126,25 @@ export function range(...args: any): number[] { return arr } + +/** + * Move element in an Array + * + * @category Array + * @param arr + * @param from + * @param to + */ +export function move(arr: T[], from: number, to: number) { + arr.splice(to, 0, arr.splice(from, 1)[0]) + return arr +} + +/** + * Clamp a number to the index ranage of an array + * + * @category Array + */ +export function clampArrayRange(n: number, arr: readonly unknown[]) { + return clamp(n, 0, arr.length - 1) +} diff --git a/src/guards.ts b/src/guards.ts index 76b5003..cafd215 100644 --- a/src/guards.ts +++ b/src/guards.ts @@ -1,6 +1,7 @@ /** * Type guard to filter out null-ish values * + * @category Guards * @example array.filter(notNullish) */ export function notNullish(v: T | null | undefined): v is NonNullable { @@ -10,6 +11,7 @@ export function notNullish(v: T | null | undefined): v is NonNullable { /** * Type guard to filter out null values * + * @category Guards * @example array.filter(noNull) */ export function noNull(v: T | null): v is Exclude { @@ -19,6 +21,7 @@ export function noNull(v: T | null): v is Exclude { /** * Type guard to filter out null-ish values * + * @category Guards * @example array.filter(notUndefined) */ export function notUndefined(v: T): v is Exclude { @@ -28,6 +31,7 @@ export function notUndefined(v: T): v is Exclude { /** * Type guard to filter out falsy values * + * @category Guards * @example array.filter(isTruthy) */ export function isTruthy(v: T): v is NonNullable {