import { curry3 , id , memoize } from '@typed/functions' ; import { equals , greaterThan , greaterThanOrEqual , ifElse , lessThan , or } from '@typed/logic' ; import { List } from '../types' ; import { arrayFrom } from '../arrayFrom' ; const findMovedIndex = memoize ( __findMovedIndex ) ; name export const move = curry3 ( function move < A > ( fromIndex , toIndex , list ) : Array < A > { const length = list . length ; const newArray = Array ( length ) ; const outOfBounds = or ( lessThan ( 0 ) , greaterThanOrEqual ( length ) ) ; if ( outOfBounds ( toIndex ) || outOfBounds ( fromIndex ) ) return arrayFrom ( list ) ; for ( let i = 0 ; i < length ; ++ i ) newArray [ i ] = list [ findMovedIndex ( i , fromIndex , toIndex ) ] ; return newArray ; } ) ; const increment = ( x ) => x + 1 ; const decrement = ( x ) => x - 1 ; function __findMovedIndex ( i , fromIndex , toIndex ) { if ( equals ( i , toIndex ) ) return fromIndex ; return ifElse ( ( ) => lessThan ( toIndex , fromIndex ) , ifElse ( between ( fromIndex , toIndex ) , id , increment ) , ifElse ( between ( toIndex , fromIndex ) , id , decrement ) , i ) ; } function between ( min , max ) { return or ( lessThan ( min ) , greaterThan ( max ) ) ; } export type MoveArity3 = { < A > ( fromIndex , toIndex , list ) : Array < A > ; < A > ( fromIndex , toIndex ) : MoveArity1 < A > ; < A > ( fromIndex ) : MoveArity2 < A > ; } ; export type MoveArity2 < A > = { ( toIndex , list ) : Array < A > ; ( toIndex ) : MoveArity1 < A > ; } ; export type MoveArity1 < A > = { ( list ) : Array < A > ; } ;