Skip to content

Commit 09ff0ce

Browse files
committed
fix: add missing index normalization and add associated tests
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 305348b commit 09ff0ce

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

lib/node_modules/@stdlib/array/base/fill/lib/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ function fill( x, value, start, end ) {
151151
if ( hasMethod( x, 'fill' ) ) {
152152
return x.fill( value, start, end );
153153
}
154+
if ( start < 0 ) {
155+
start += x.length;
156+
if ( start < 0 ) {
157+
start = 0;
158+
}
159+
}
160+
if ( end < 0 ) {
161+
end += x.length; // if `end` is still negative, that is fine, as `x` will be returned un-mutated
162+
} else if ( end > x.length ) {
163+
end = x.length;
164+
}
154165
obj = arraylike2object( x );
155166
if ( obj.accessorProtocol ) {
156167
return accessors( obj, value, start, end );

lib/node_modules/@stdlib/array/base/fill/test/test.js

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,27 @@ tape( 'the function fills an array-like object (generic)', function test( t ) {
4747
t.strictEqual( actual, x, 'returns expected value' );
4848
t.deepEqual( actual, expected, 'returns expected value' );
4949

50+
x = [ 1, 2, 3 ];
51+
expected = [ 4, 4, 4 ];
52+
actual = fill( x, 4, -10, x.length );
53+
54+
t.strictEqual( actual, x, 'returns expected value' );
55+
t.deepEqual( actual, expected, 'returns expected value' );
56+
5057
x = [ 1, 2, 3 ];
5158
expected = [ 1, 5, 5 ];
5259
actual = fill( x, 5, 1, x.length );
5360

5461
t.strictEqual( actual, x, 'returns expected value' );
5562
t.deepEqual( actual, expected, 'returns expected value' );
5663

64+
x = [ 1, 2, 3 ];
65+
expected = [ 5, 5, 3 ];
66+
actual = fill( x, 5, -3, -1 );
67+
68+
t.strictEqual( actual, x, 'returns expected value' );
69+
t.deepEqual( actual, expected, 'returns expected value' );
70+
5771
x = [ 1, 2, 3 ];
5872
expected = [ 1, 5, 3 ];
5973
actual = fill( x, 5, 1, x.length-1 );
@@ -68,6 +82,13 @@ tape( 'the function fills an array-like object (generic)', function test( t ) {
6882
t.strictEqual( actual, x, 'returns expected value' );
6983
t.deepEqual( actual, expected, 'returns expected value' );
7084

85+
x = [ 1, 2, 3 ];
86+
expected = [ 1, 2, 3 ];
87+
actual = fill( x, 5, 10, 3 );
88+
89+
t.strictEqual( actual, x, 'returns expected value' );
90+
t.deepEqual( actual, expected, 'returns expected value' );
91+
7192
t.end();
7293
});
7394

@@ -92,7 +113,7 @@ tape( 'the function fills an array-like object (float64)', function test( t ) {
92113

93114
x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
94115
expected = new Float64Array( [ 1.0, 5.0, 3.0 ] );
95-
actual = fill( x, 5.0, 1, -1 );
116+
actual = fill( x, 5.0, -2, -1 );
96117

97118
t.strictEqual( actual, x, 'returns expected value' );
98119
t.deepEqual( actual, expected, 'returns expected value' );
@@ -126,6 +147,13 @@ tape( 'the function fills an array-like object (int32)', function test( t ) {
126147
t.strictEqual( actual, x, 'returns expected value' );
127148
t.deepEqual( actual, expected, 'returns expected value' );
128149

150+
x = new Int32Array( [ 1, 2, 3 ] );
151+
expected = new Int32Array( [ 5, 5, 3 ] );
152+
actual = fill( x, 5, -3, -1 );
153+
154+
t.strictEqual( actual, x, 'returns expected value' );
155+
t.deepEqual( actual, expected, 'returns expected value' );
156+
129157
x = new Int32Array( [ 1, 2, 3 ] );
130158
expected = new Int32Array( [ 5, 2, 3 ] );
131159
actual = fill( x, 5, 0, x.length-2 );
@@ -140,6 +168,20 @@ tape( 'the function fills an array-like object (int32)', function test( t ) {
140168
t.strictEqual( actual, x, 'returns expected value' );
141169
t.deepEqual( actual, expected, 'returns expected value' );
142170

171+
x = new Int32Array( [ 1, 2, 3 ] );
172+
expected = new Int32Array( [ 1, 2, 3 ] );
173+
actual = fill( x, 5, 0, -10 );
174+
175+
t.strictEqual( actual, x, 'returns expected value' );
176+
t.deepEqual( actual, expected, 'returns expected value' );
177+
178+
x = new Int32Array( [ 1, 2, 3 ] );
179+
expected = new Int32Array( [ 1, 2, 3 ] );
180+
actual = fill( x, 5, 10, x.length );
181+
182+
t.strictEqual( actual, x, 'returns expected value' );
183+
t.deepEqual( actual, expected, 'returns expected value' );
184+
143185
t.end();
144186
});
145187

@@ -176,6 +218,15 @@ tape( 'the function fills an array-like object (accessors)', function test( t )
176218
t.strictEqual( actual, obj, 'returns expected value' );
177219
t.deepEqual( x, expected, 'returns expected value' );
178220

221+
x = [ 1, 2, 3, 4 ];
222+
expected = [ 1, 5, 5, 4 ];
223+
224+
obj = new AccessorArray( x );
225+
actual = fill( obj, 5, -3, -1 );
226+
227+
t.strictEqual( actual, obj, 'returns expected value' );
228+
t.deepEqual( x, expected, 'returns expected value' );
229+
179230
x = [ 1, 2, 3, 4 ];
180231
expected = [ 1, 2, 5, 4 ];
181232

@@ -194,6 +245,24 @@ tape( 'the function fills an array-like object (accessors)', function test( t )
194245
t.strictEqual( actual, obj, 'returns expected value' );
195246
t.deepEqual( x, expected, 'returns expected value' );
196247

248+
x = [ 1, 2, 3, 4 ];
249+
expected = [ 1, 2, 3, 4 ];
250+
251+
obj = new AccessorArray( x );
252+
actual = fill( obj, 5, 10, x.length );
253+
254+
t.strictEqual( actual, obj, 'returns expected value' );
255+
t.deepEqual( x, expected, 'returns expected value' );
256+
257+
x = [ 1, 2, 3, 4 ];
258+
expected = [ 1, 2, 3, 4 ];
259+
260+
obj = new AccessorArray( x );
261+
actual = fill( obj, 5, 0, -10 );
262+
263+
t.strictEqual( actual, obj, 'returns expected value' );
264+
t.deepEqual( x, expected, 'returns expected value' );
265+
197266
t.end();
198267
});
199268

@@ -240,6 +309,25 @@ tape( 'the function fills an array-like object (array-like)', function test( t )
240309
t.strictEqual( actual, x, 'returns expected value' );
241310
t.deepEqual( actual, expected, 'returns expected value' );
242311

312+
x = {
313+
'length': 4,
314+
'0': 1,
315+
'1': 2,
316+
'2': 3,
317+
'3': 4
318+
};
319+
expected = {
320+
'length': 4,
321+
'0': 5,
322+
'1': 5,
323+
'2': 5,
324+
'3': 4
325+
};
326+
actual = fill( x, 5, -4, -1 );
327+
328+
t.strictEqual( actual, x, 'returns expected value' );
329+
t.deepEqual( actual, expected, 'returns expected value' );
330+
243331
x = {
244332
'length': 4,
245333
'0': 1,
@@ -297,6 +385,44 @@ tape( 'the function fills an array-like object (array-like)', function test( t )
297385
t.strictEqual( actual, x, 'returns expected value' );
298386
t.deepEqual( actual, expected, 'returns expected value' );
299387

388+
x = {
389+
'length': 4,
390+
'0': 1,
391+
'1': 2,
392+
'2': 3,
393+
'3': 4
394+
};
395+
expected = {
396+
'length': 4,
397+
'0': 1,
398+
'1': 2,
399+
'2': 3,
400+
'3': 4
401+
};
402+
actual = fill( x, 5, 10, x.length );
403+
404+
t.strictEqual( actual, x, 'returns expected value' );
405+
t.deepEqual( actual, expected, 'returns expected value' );
406+
407+
x = {
408+
'length': 4,
409+
'0': 1,
410+
'1': 2,
411+
'2': 3,
412+
'3': 4
413+
};
414+
expected = {
415+
'length': 4,
416+
'0': 1,
417+
'1': 2,
418+
'2': 3,
419+
'3': 4
420+
};
421+
actual = fill( x, 5, 0, -10 );
422+
423+
t.strictEqual( actual, x, 'returns expected value' );
424+
t.deepEqual( actual, expected, 'returns expected value' );
425+
300426
t.end();
301427
});
302428

0 commit comments

Comments
 (0)