Skip to content

Commit ff647cb

Browse files
committed
feat: add vega/base/assert/is-discrete-scale
1 parent a256141 commit ff647cb

File tree

10 files changed

+649
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)