179 lines
3.1 KiB
TypeScript
179 lines
3.1 KiB
TypeScript
![]() |
export class Arrays
|
||
|
{
|
||
|
|
||
|
static combine<T>( t:T, array:T[] ):T[]
|
||
|
{
|
||
|
let combined = [ t ];
|
||
|
|
||
|
if ( array )
|
||
|
{
|
||
|
combined = combined.concat( array );
|
||
|
}
|
||
|
|
||
|
return combined;
|
||
|
}
|
||
|
|
||
|
static combine2<T>( t:T, t2:T, array:T[] ):T[]
|
||
|
{
|
||
|
let combined = [ t, t2 ];
|
||
|
|
||
|
if ( array )
|
||
|
{
|
||
|
combined = combined.concat( array );
|
||
|
}
|
||
|
|
||
|
return combined;
|
||
|
}
|
||
|
|
||
|
static combine3<T>( t:T, t2:T, t3:T, array:T[] ):T[]
|
||
|
{
|
||
|
let combined = [ t, t2, t3 ];
|
||
|
|
||
|
if ( array )
|
||
|
{
|
||
|
combined = combined.concat( array );
|
||
|
}
|
||
|
|
||
|
return combined;
|
||
|
}
|
||
|
|
||
|
static pushIfNotInside<T>( array:T[], element:T )
|
||
|
{
|
||
|
let index = array.indexOf( element );
|
||
|
|
||
|
if ( index !== - 1)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
array.push( element );
|
||
|
}
|
||
|
|
||
|
|
||
|
static create<T>( size:number, creator:( i:number ) => T ):T[]
|
||
|
{
|
||
|
let created:T[] = [];
|
||
|
|
||
|
for ( let i = 0; i < size; i++ )
|
||
|
{
|
||
|
let t = creator( i );
|
||
|
created.push( t );
|
||
|
}
|
||
|
|
||
|
return created;
|
||
|
}
|
||
|
|
||
|
static isEmpty<T>( source:T[] )
|
||
|
{
|
||
|
return source.length === 0;
|
||
|
}
|
||
|
|
||
|
static isLast<T>( source:T[], index:number )
|
||
|
{
|
||
|
return index === ( source.length -1 ) ;
|
||
|
}
|
||
|
|
||
|
static getLast<T>( source:T[] ):T
|
||
|
{
|
||
|
return source[ source.length - 1 ];
|
||
|
}
|
||
|
|
||
|
static forEachIndex<T>( source:T[], callback:( t:T, index:number, normalized?:number )=>void )
|
||
|
{
|
||
|
let normalizer = 1 / ( source.length - 1 );
|
||
|
|
||
|
for ( let i = 0; i < source.length; i++ )
|
||
|
{
|
||
|
let normalized = i * normalizer;
|
||
|
callback( source[ i ], i, normalized );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static transfer<T>( source:T[], target:T[] )
|
||
|
{
|
||
|
this.transferRange( source, 0, target, 0, source.length );
|
||
|
}
|
||
|
|
||
|
static transferRange<T>( source:T[], sourceOffset:number, target:T[], targetOffset:number, numItems:number )
|
||
|
{
|
||
|
for ( let i = 0; i < numItems; i++ )
|
||
|
{
|
||
|
target[ i + targetOffset ] = source[ i + sourceOffset ];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static copyRange<T>( array:T[], start:number, length:number )
|
||
|
{
|
||
|
return array.slice( start, start + length );
|
||
|
}
|
||
|
|
||
|
static copyRangeFromStart<T>( array:T[], start:number )
|
||
|
{
|
||
|
return array.slice( start, array.length );
|
||
|
}
|
||
|
|
||
|
static insert<T>( array:T[], element:T, index:number )
|
||
|
{
|
||
|
array.splice( index, 0, element );
|
||
|
}
|
||
|
|
||
|
static moveToFront<T>( array:T[], element:T )
|
||
|
{
|
||
|
if ( array[ 0 ] === element )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Arrays.remove( array, element );
|
||
|
Arrays.insert( array, element, 0 );
|
||
|
}
|
||
|
|
||
|
static remove<T>( array:T[], element:T )
|
||
|
{
|
||
|
if ( ! array || ! element )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let index = array.indexOf( element );
|
||
|
|
||
|
if ( index === -1 )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
array.splice( index, 1 );
|
||
|
}
|
||
|
|
||
|
static addIfNotPresent<T>( array:T[], t:T)
|
||
|
{
|
||
|
let index = array.indexOf( t );
|
||
|
|
||
|
if ( index !== -1 )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
array.push( t );
|
||
|
}
|
||
|
|
||
|
static removeAt<T>( array:T[], index:number )
|
||
|
{
|
||
|
if ( ! array )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ( index < 0 || index > array.length )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
array.splice( index, 1 );
|
||
|
}
|
||
|
|
||
|
static removeAllAfter( array:any[], index:number )
|
||
|
{
|
||
|
array.splice( index, array.length - index );
|
||
|
}
|
||
|
}
|