import { AnyArray } from '../shared' ; import { become } from './become' ; import { commitArray } from './commitArray' ; import { commitIndices } from './commitIndices' ; export function append < T extends AnyArray > ( base , first , ... rest ) ; export function append ( base , ... values ) { const copy = base . slice ( ) ; copy . push ( ... values ) ; if ( become ( base , copy ) ) { commitIndices ( base , copy , base . length , values . length ) ; commitArray ( base , copy ) ; } return copy ; } export function prepend < T extends AnyArray > ( base , first , ... rest ) ; export function prepend ( base , ... values ) { const copy = values . concat ( base ) ; if ( become ( base , copy ) ) { commitIndices ( base , copy , 0 , copy . length ) ; commitArray ( base , copy ) ; } return copy ; } export function insert < T extends AnyArray > ( base , index , first , ... rest ) ; export function insert ( base , index , ... values ) { while ( index < 0 ) index += base . length ; const copy = base . slice ( 0 , index ) . concat ( values , base . slice ( index ) ) ; if ( become ( base , copy ) ) { commitIndices ( base , copy , index , copy . length - index ) ; commitArray ( base , copy ) ; } return copy ; } export function concat < T extends AnyArray > ( base , first , ... rest ) ; export function concat ( base , ... values ) { const copy = base . concat ( ... values ) ; const delta = copy . length - base . length ; if ( delta == 0 ) { return base ; } if ( become ( base , copy ) ) { commitIndices ( base , copy , base . length , delta ) ; commitArray ( base , copy ) ; } return copy ; } export function remove < T extends AnyArray > ( base , index , count = 1 ) { while ( index < 0 ) index += base . length ; if ( count <= 0 || index >= base . length || index + count <= 0 ) { return base ; } const copy = index <= 0 ? base . slice ( index + count ) : index + count >= base . length ? base . slice ( 0 , index ) : base . slice ( 0 , index ) . concat ( base . slice ( index + count ) ) ; if ( become ( base , copy ) ) { commitIndices ( base , copy , index , base . length - index ) ; commitArray ( base , copy ) ; } return copy ; }