Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 9x 9x 18x 18x 27x 17x 17x 13x 13x 4x 4x 1x 1x 1x 27x 1x 1x 1x 1x 1x 1x 27x 27x 27x 1x 1x 1x 1x 27x 27x 1x 1x 27x 27x 1x 1x 1x 1x 1x 1x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 27x 1x 1x 27x 1x 1x 1x 1x 1x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; // MODULES // var isCollection = require( '@stdlib/assert/is-collection' ); var isInteger = require( '@stdlib/math/base/assert/is-integer' ); var format = require( '@stdlib/string/format' ); var floor = require( '@stdlib/math/base/special/floor' ); var max = require( '@stdlib/math/base/special/max' ); var min = require( '@stdlib/math/base/special/min' ); var copyWithinTypedArrays = require( './typed_arrays.js' ); // MAIN // /** * Copies a sequence of elements within a collection to another location in the same collection. * * @param {Collection} collection - input collection * @param {integer} target - target index * @param {integer} [start=0] - source start index * @param {integer} [end=collection.length] - source end index * @throws {TypeError} first argument must be a collection * @returns {Collection} modified input collection * * @example * var arr = [ 1, 2, 3, 4, 5 ]; * var out = copyWithin( arr, 0, 3 ); * // returns [ 4, 5, 3, 4, 5 ] * * @example * var arr = [ 1, 2, 3, 4, 5 ]; * var out = copyWithin( arr, 0, 3, 4 ); * // returns [ 4, 2, 3, 4, 5 ] * * @example * var arr = [ 1, 2, 3, 4, 5 ]; * var out = copyWithin( arr, -2, -3, -1 ); * // returns [ 1, 2, 3, 3, 4 ] */ function copyWithin( collection, target, start, end ) { var result; var len; var t; var s; var e; var i; var n; if ( !isCollection( collection ) ) { throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); } // If the collection has a built-in copyWithin method, use it: if ( typeof collection.copyWithin === 'function' ) { if ( arguments.length === 2 ) { return collection.copyWithin( target ); } if ( arguments.length === 3 ) { return collection.copyWithin( target, start ); } return collection.copyWithin( target, start, end ); } // Use optimized implementations for typed arrays: result = copyWithinTypedArrays( collection, target, start, end ); if ( result !== null ) { return result; } len = collection.length; // Convert target to integer and handle negative indices: if ( isInteger( target ) ) { t = target; } else { t = floor( target ); } if ( t < 0 ) { t = max( len + t, 0 ); } else { t = min( t, len ); } // Convert start to integer and handle negative indices: if ( start === void 0 ) { s = 0; } else if ( isInteger( start ) ) { s = start; } else { s = floor( start ); } if ( s < 0 ) { s = max( len + s, 0 ); } else { s = min( s, len ); } // Convert end to integer and handle negative indices: if ( end === void 0 ) { e = len; } else if ( isInteger( end ) ) { e = end; } else { e = floor( end ); } if ( e < 0 ) { e = max( len + e, 0 ); } else { e = min( e, len ); } // Calculate the number of elements to copy: n = min( e - s, len - t ); // Handle case where copying would overlap: if ( t < s ) { // Copy from left to right: for ( i = 0; i < n; i++ ) { if ( s + i in collection ) { collection[ t + i ] = collection[ s + i ]; } else { delete collection[ t + i ]; } } } else if ( t > s ) { // Copy from right to left to avoid overwriting elements: for ( i = n - 1; i >= 0; i-- ) { if ( s + i in collection ) { collection[ t + i ] = collection[ s + i ]; } else { delete collection[ t + i ]; } } } return collection; } // EXPORTS // module.exports = copyWithin; |