Skip to content

Commit d4d1ecd

Browse files
committed
feat: add plot/vega/base/assert/is-field-array
1 parent b830dae commit d4d1ecd

10 files changed

Lines changed: 623 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# isFieldArray
22+
23+
> Test if an input value is an array of [fields][@stdlib/plot/vega/field/ctor].
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 isFieldArray = require( '@stdlib/plot/vega/base/assert/is-field-array' );
41+
```
42+
43+
#### isFieldArray( value )
44+
45+
Tests if an input value is an array of [fields][@stdlib/plot/vega/field/ctor].
46+
47+
```javascript
48+
var Field = require( '@stdlib/plot/vega/field/ctor' );
49+
50+
var v = new Field({
51+
'field': 'amount'
52+
});
53+
var bool = isFieldArray( [ v ] );
54+
// returns true
55+
```
56+
57+
If provided an empty array, the function returns `false`.
58+
59+
```javascript
60+
var bool = isFieldArray( [] );
61+
// returns false
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var Field = require( '@stdlib/plot/vega/field/ctor' );
86+
var isFieldArray = require( '@stdlib/plot/vega/base/assert/is-field-array' );
87+
88+
var v = new Field({
89+
'field': 'amount'
90+
});
91+
var bool = isFieldArray( [ v ] );
92+
// returns true
93+
94+
bool = isFieldArray( {} );
95+
// returns false
96+
97+
bool = isFieldArray( 'foo' );
98+
// returns false
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- 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. -->
106+
107+
<section class="references">
108+
109+
</section>
110+
111+
<!-- /.references -->
112+
113+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
114+
115+
<section class="related">
116+
117+
</section>
118+
119+
<!-- /.related -->
120+
121+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="links">
124+
125+
[@stdlib/plot/vega/field/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/plot/vega/field/ctor
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var Field = require( '@stdlib/plot/vega/field/ctor' );
26+
var pkg = require( './../package.json' ).name;
27+
var isFieldArray = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::true', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
new Field({
39+
'field': 'amount'
40+
}),
41+
new Field({
42+
'field': 'inputName',
43+
'as': 'outputName'
44+
})
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
out = isFieldArray( [ values[ i%values.length ] ] );
50+
if ( typeof out !== 'boolean' ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isBoolean( out ) ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( pkg+'::false', function benchmark( b ) {
63+
var values;
64+
var out;
65+
var v;
66+
var i;
67+
68+
values = [
69+
'foo',
70+
'bar',
71+
'',
72+
'beep',
73+
'boop',
74+
[],
75+
{}
76+
];
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
v = values[ i%values.length ];
81+
out = isFieldArray( v );
82+
if ( typeof out !== 'boolean' ) {
83+
b.fail( 'should return a boolean' );
84+
}
85+
}
86+
b.toc();
87+
if ( !isBoolean( out ) ) {
88+
b.fail( 'should return a boolean' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( value )
3+
Tests if an input value is an array of field instances.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating if an input value is an array of field instances.
14+
15+
Examples
16+
--------
17+
> var opts = { 'field': 'amount' };
18+
> var v = new {{alias:@stdlib/plot/vega/field/ctor}}( opts );
19+
> var bool = {{alias}}( [ v ] )
20+
true
21+
> bool = {{alias}}( {} )
22+
false
23+
24+
See Also
25+
--------
26+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
* Tests whether an input value is an array of field instances.
23+
*
24+
* @param v - value to test
25+
* @returns boolean indicating whether an input value is an array of field instances
26+
*
27+
* @example
28+
* var Field = require( '@stdlib/plot/vega/field/ctor' );
29+
*
30+
* var v = new Field({
31+
* 'field': 'amount'
32+
* });
33+
* var bool = isFieldArray( [ v ] );
34+
* // returns true
35+
*
36+
* bool = isFieldArray( {} );
37+
* // returns false
38+
*
39+
* bool = isFieldArray( 'foo' );
40+
* // returns false
41+
*/
42+
declare function isFieldArray( v: any ): boolean;
43+
44+
45+
// EXPORTS //
46+
47+
export = isFieldArray;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isFieldArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isFieldArray( {} ); // $ExpectType boolean
27+
isFieldArray( 'foo' ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided an unsupported number of arguments...
31+
{
32+
isFieldArray(); // $ExpectError
33+
isFieldArray( undefined, 123 ); // $ExpectError
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 Field = require( '@stdlib/plot/vega/field/ctor' );
22+
var isFieldArray = require( './../lib' );
23+
24+
var v = new Field({
25+
'field': 'amount'
26+
});
27+
var bool = isFieldArray( [ v ] );
28+
console.log( bool );
29+
// => true
30+
31+
bool = isFieldArray( {} );
32+
console.log( bool );
33+
// => false
34+
35+
bool = isFieldArray( 'foo' );
36+
console.log( bool );
37+
// => false

0 commit comments

Comments
 (0)