Skip to content

Commit 6d91993

Browse files
committed
feat: add array/base/nulls
1 parent d05b88f commit 6d91993

File tree

10 files changed

+536
-0
lines changed

10 files changed

+536
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# nulls
22+
23+
> Create a "generic" array filled with null values.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var nulls = require( '@stdlib/array/base/nulls' );
41+
```
42+
43+
#### nulls( len )
44+
45+
Returns a "generic" array filled with `null` values.
46+
47+
```javascript
48+
var out = nulls( 3 );
49+
// returns [ null, null, null ]
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var nulls = require( '@stdlib/array/base/nulls' );
74+
75+
// Create a null value array:
76+
var arr = nulls( 10 );
77+
78+
console.log( arr );
79+
// => [ null, null, null, null, null, null, null, null, null, null ]
80+
```
81+
82+
</section>
83+
84+
<!-- /.examples -->
85+
86+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="references">
89+
90+
</section>
91+
92+
<!-- /.references -->
93+
94+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
95+
96+
<section class="related">
97+
98+
</section>
99+
100+
<!-- /.related -->
101+
102+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="links">
105+
106+
</section>
107+
108+
<!-- /.links -->
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var nulls = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var out;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
out = nulls( len );
55+
if ( out.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isArray( out ) ) {
61+
b.fail( 'should return an array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
89+
f = createBenchmark( len );
90+
bench( pkg+':len='+len, f );
91+
}
92+
}
93+
94+
main();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( len )
3+
Returns a "generic" array filled with null values.
4+
5+
Parameters
6+
----------
7+
len: integer
8+
Array length.
9+
10+
Returns
11+
-------
12+
out: Array
13+
Output array.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 3 )
18+
[ null, null, null ]
19+
20+
See Also
21+
--------
22+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns a "generic" array filled with nulls.
23+
*
24+
* @param len - array length
25+
* @returns output array
26+
*
27+
* @example
28+
* var out = nulls( 3 );
29+
* // returns [ null, null, null ]
30+
*/
31+
declare function nulls( len: number ): Array<null>;
32+
33+
34+
// EXPORTS //
35+
36+
export = nulls;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import nulls = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
nulls( 3 ); // $ExpectType null[]
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument which is not a number...
30+
{
31+
nulls( 'abc' ); // $ExpectError
32+
nulls( true ); // $ExpectError
33+
nulls( false ); // $ExpectError
34+
nulls( null ); // $ExpectError
35+
nulls( [] ); // $ExpectError
36+
nulls( {} ); // $ExpectError
37+
nulls( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided an unsupported number of arguments...
41+
{
42+
nulls(); // $ExpectError
43+
nulls( 3, 2 ); // $ExpectError
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var nulls = require( './../lib' );
22+
23+
// Create a null value array:
24+
var arr = nulls( 10 );
25+
26+
console.log( arr );
27+
// => [ null, null, null, null, null, null, null, null, null, null ]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Create a "generic" array filled with null values.
23+
*
24+
* @module @stdlib/array/base/nulls
25+
*
26+
* @example
27+
* var nulls = require( '@stdlib/array/base/nulls' );
28+
*
29+
* var out = nulls( 3 );
30+
* // returns [ null, null, null ]
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)