@tsdotnet/array-copy
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -16,6 +16,6 @@ /*! | ||
* @param destinationIndex | ||
* @param length An optional limit to stop copying. | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The destination array. | ||
*/ | ||
export function arrayCopyTo(source, destination, sourceIndex = 0, destinationIndex = 0, length = Infinity) { | ||
export function arrayCopyTo(source, destination, sourceIndex = 0, destinationIndex = 0, count = Infinity) { | ||
if (!source) | ||
@@ -27,17 +27,20 @@ throw new ArgumentNullException('source', CBN); | ||
throw new ArgumentOutOfRangeException('sourceIndex', sourceIndex, CBL0); | ||
if (destinationIndex < 0) | ||
throw new ArgumentOutOfRangeException('destinationIndex', destinationIndex, CBL0); | ||
const sourceLength = source.length; | ||
if (!sourceLength) | ||
if (!sourceLength || count < 1) | ||
return destination; | ||
if (sourceIndex >= sourceLength) | ||
throw new ArgumentOutOfRangeException('sourceIndex', sourceIndex, 'Must be less than the length of the source array.'); | ||
// deal with ArrayLike issues. | ||
if (destination.length < 0) | ||
throw new ArgumentOutOfRangeException('destinationIndex', destinationIndex, CBL0); | ||
const maxLength = source.length - sourceIndex; | ||
if (isFinite(length) && length > maxLength) | ||
throw new ArgumentOutOfRangeException('destination.length', destination.length, CBL0); | ||
const max = source.length - sourceIndex; | ||
if (isFinite(count) && count > max) | ||
throw new ArgumentOutOfRangeException('sourceIndex', sourceIndex, 'Source index + length cannot exceed the length of the source array.'); | ||
length = Math.min(length, maxLength); | ||
const newLength = destinationIndex + length; | ||
count = Math.min(count, max); | ||
const newLength = destinationIndex + count; | ||
if (newLength > destination.length) | ||
destination.length = newLength; | ||
for (let i = 0; i < length; i++) { | ||
for (let i = 0; i < count; i++) { | ||
destination[destinationIndex + i] = source[sourceIndex + i]; | ||
@@ -52,9 +55,9 @@ } | ||
* @param sourceIndex | ||
* @param length | ||
* @returns {any} | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The copy of the source array. | ||
*/ | ||
function arrayCopy(source, sourceIndex = 0, length = Infinity) { | ||
function arrayCopy(source, sourceIndex = 0, count = Infinity) { | ||
if (!source) | ||
return source; // may have passed zero? undefined? or null? | ||
return arrayCopyTo(source, arrayInit(Math.min(length, Math.max(source.length - sourceIndex, 0))), sourceIndex, 0, length); | ||
return arrayCopyTo(source, arrayInit(Math.min(count, Math.max(source.length - sourceIndex, 0))), sourceIndex, 0, count); | ||
} | ||
@@ -61,0 +64,0 @@ // eslint-disable-next-line @typescript-eslint/no-namespace |
@@ -15,6 +15,6 @@ /*! | ||
* @param destinationIndex | ||
* @param length An optional limit to stop copying. | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The destination array. | ||
*/ | ||
export declare function arrayCopyTo<T, TDestination extends ArrayLikeWritable<T>>(source: ArrayLike<T>, destination: TDestination, sourceIndex?: number, destinationIndex?: number, length?: number): TDestination; | ||
export declare function arrayCopyTo<T, TDestination extends ArrayLikeWritable<T>>(source: ArrayLike<T>, destination: TDestination, sourceIndex?: number, destinationIndex?: number, count?: number): TDestination; | ||
/** | ||
@@ -25,6 +25,6 @@ * Creates a copy of the array-like object. | ||
* @param sourceIndex | ||
* @param length | ||
* @returns {any} | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The copy of the source array. | ||
*/ | ||
declare function arrayCopy<T>(source: ArrayLike<T>, sourceIndex?: number, length?: number): T[]; | ||
declare function arrayCopy<T>(source: ArrayLike<T>, sourceIndex?: number, count?: number): T[]; | ||
declare namespace arrayCopy { | ||
@@ -31,0 +31,0 @@ /** |
@@ -19,6 +19,6 @@ "use strict"; | ||
* @param destinationIndex | ||
* @param length An optional limit to stop copying. | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The destination array. | ||
*/ | ||
function arrayCopyTo(source, destination, sourceIndex = 0, destinationIndex = 0, length = Infinity) { | ||
function arrayCopyTo(source, destination, sourceIndex = 0, destinationIndex = 0, count = Infinity) { | ||
if (!source) | ||
@@ -30,17 +30,20 @@ throw new ArgumentNullException_1.default('source', CBN); | ||
throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, CBL0); | ||
if (destinationIndex < 0) | ||
throw new ArgumentOutOfRangeException_1.default('destinationIndex', destinationIndex, CBL0); | ||
const sourceLength = source.length; | ||
if (!sourceLength) | ||
if (!sourceLength || count < 1) | ||
return destination; | ||
if (sourceIndex >= sourceLength) | ||
throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, 'Must be less than the length of the source array.'); | ||
// deal with ArrayLike issues. | ||
if (destination.length < 0) | ||
throw new ArgumentOutOfRangeException_1.default('destinationIndex', destinationIndex, CBL0); | ||
const maxLength = source.length - sourceIndex; | ||
if (isFinite(length) && length > maxLength) | ||
throw new ArgumentOutOfRangeException_1.default('destination.length', destination.length, CBL0); | ||
const max = source.length - sourceIndex; | ||
if (isFinite(count) && count > max) | ||
throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, 'Source index + length cannot exceed the length of the source array.'); | ||
length = Math.min(length, maxLength); | ||
const newLength = destinationIndex + length; | ||
count = Math.min(count, max); | ||
const newLength = destinationIndex + count; | ||
if (newLength > destination.length) | ||
destination.length = newLength; | ||
for (let i = 0; i < length; i++) { | ||
for (let i = 0; i < count; i++) { | ||
destination[destinationIndex + i] = source[sourceIndex + i]; | ||
@@ -56,9 +59,9 @@ } | ||
* @param sourceIndex | ||
* @param length | ||
* @returns {any} | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The copy of the source array. | ||
*/ | ||
function arrayCopy(source, sourceIndex = 0, length = Infinity) { | ||
function arrayCopy(source, sourceIndex = 0, count = Infinity) { | ||
if (!source) | ||
return source; // may have passed zero? undefined? or null? | ||
return arrayCopyTo(source, array_init_1.default(Math.min(length, Math.max(source.length - sourceIndex, 0))), sourceIndex, 0, length); | ||
return arrayCopyTo(source, array_init_1.default(Math.min(count, Math.max(source.length - sourceIndex, 0))), sourceIndex, 0, count); | ||
} | ||
@@ -65,0 +68,0 @@ // eslint-disable-next-line @typescript-eslint/no-namespace |
{ | ||
"name": "@tsdotnet/array-copy", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A commonly used array copy utility. (arrayCopy and arrayCopyTo)", | ||
@@ -5,0 +5,0 @@ "author": "electricessence", |
@@ -9,3 +9,61 @@ # ![alt text](https://avatars1.githubusercontent.com/u/64487547?s=30&v=4 "tsdotnet") tsdotnet / array-copy | ||
Includes `arrayCopy<T>` and `arrayCopyTo<T>`. | ||
## Usage | ||
```typescript | ||
import arrayCopy, {arrayCopyTo} from '@tsdotnet/array-copy' | ||
const myCopy = arrayCopy(source); | ||
// myCopy is really a shortcut for: | ||
const myOtherCopy = arrayCopyTo([]); | ||
``` | ||
or | ||
```typescript | ||
import arrayCopy from '@tsdotnet/array-copy' | ||
const myOtherCopy = arrayCopy.to([]); // same as arrayCopyTo. | ||
``` | ||
## Exported | ||
*For flexibility, these functions can operate on objects that have a length but are not instances of Array.* (`ArrayLikeWritable<T>`) | ||
### arrayCopyTo | ||
```typescript | ||
/** | ||
* Copies one array to another. | ||
* @param source | ||
* @param destination | ||
* @param sourceIndex | ||
* @param destinationIndex | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The destination array. | ||
*/ | ||
export function arrayCopyTo<T, TDestination extends ArrayLikeWritable<T>> ( | ||
source: ArrayLike<T>, | ||
destination: TDestination, | ||
sourceIndex: number = 0, | ||
destinationIndex: number = 0, | ||
count: number = Infinity | ||
): TDestination | ||
``` | ||
### arrayCopy | ||
```typescript | ||
/** | ||
* Creates a copy of the array-like object. | ||
* Similar to Array.slice(index, length). | ||
* @param source | ||
* @param sourceIndex | ||
* @param count An optional limit to stop copying. Finite values must be no more than the source.length minus the sourceIndex. | ||
* @returns The copy of the source array. | ||
*/ | ||
export default function arrayCopy<T> ( | ||
source: ArrayLike<T>, | ||
sourceIndex: number = 0, | ||
count: number = Infinity | ||
): T[] | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20448
88
283
69