Skip to content

Commit

Permalink
feat(array): move
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 1, 2021
1 parent 3391034 commit 5544bf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { clamp } from './math'
import { Arrayable, Nullable } from './types'

/**
Expand Down Expand Up @@ -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<T>(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)
}
4 changes: 4 additions & 0 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Type guard to filter out null-ish values
*
* @category Guards
* @example array.filter(notNullish)
*/
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
Expand All @@ -10,6 +11,7 @@ export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
/**
* Type guard to filter out null values
*
* @category Guards
* @example array.filter(noNull)
*/
export function noNull<T>(v: T | null): v is Exclude<T, null> {
Expand All @@ -19,6 +21,7 @@ export function noNull<T>(v: T | null): v is Exclude<T, null> {
/**
* Type guard to filter out null-ish values
*
* @category Guards
* @example array.filter(notUndefined)
*/
export function notUndefined<T>(v: T): v is Exclude<T, undefined> {
Expand All @@ -28,6 +31,7 @@ export function notUndefined<T>(v: T): v is Exclude<T, undefined> {
/**
* Type guard to filter out falsy values
*
* @category Guards
* @example array.filter(isTruthy)
*/
export function isTruthy<T>(v: T): v is NonNullable<T> {
Expand Down

0 comments on commit 5544bf0

Please sign in to comment.