Skip to content

Commit 48247c1

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

10 files changed

Lines changed: 635 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)