Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/scalar-dtype/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Scalar Data Type

> Determine the default ndarray [data type][@stdlib/ndarray/dtypes] for a provided scalar value.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
```

#### scalarDataType( value )

Returns the default ndarray [data type][@stdlib/ndarray/dtypes] for a provided scalar value.

```javascript
var dt = scalarDataType( 3.0 );
// returns 'float64'

dt = scalarDataType( true );
// returns 'bool'

dt = scalarDataType( null );
// returns 'generic'
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- The function resolves data types as follows:

- **number**: the default real-valued floating-point data type.
- **boolean**: the default boolean data type.
- **complex number**: a corresponding complex number data type, falling back to the default complex-valued floating-point data type when a provided complex-like object has an unrecognized data type.
- **everything else**: `'generic'`.

- Only primitive numbers and booleans are recognized. A `Number` or `Boolean` object resolves to `'generic'`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );

var values = [
3.14,
-1.0,
true,
false,
new Complex128( 1.0, 2.0 ),
'beep',
null,
[ 1, 2, 3 ]
];

var i;
for ( i = 0; i < values.length; i++ ) {
console.log( '%s => %s', String( values[ i ] ), scalarDataType( values[ i ] ) );
}
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var pkg = require( './../package.json' ).name;
var scalarDataType = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
3.14,
-1.0,
true,
false,
new Complex128( 1.0, 2.0 ),
'beep',
null,
[ 1, 2, 3 ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = scalarDataType( values[ i%values.length ] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
32 changes: 32 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

{{alias}}( value )
Returns the default ndarray data type for a provided scalar value.

If provided a number, the function returns the default real-valued
floating-point data type. If provided a boolean, the function returns the
default boolean data type. If provided a complex number, the function
returns a corresponding complex number data type. For all other values,
the function returns 'generic'.

Parameters
----------
value: any
Scalar value.

Returns
-------
dt: string
ndarray data type.

Examples
--------
> var dt = {{alias}}( 3.0 )
'float64'
> dt = {{alias}}( true )
'bool'
> dt = {{alias}}( null )
'generic'

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { DataType } from '@stdlib/types/ndarray';

/**
* Returns the default ndarray data type for a provided scalar value.
*
* @param value - scalar value
* @returns ndarray data type
*
* @example
* var dt = scalarDataType( 3.0 );
* // returns 'float64'
*
* @example
* var dt = scalarDataType( true );
* // returns 'bool'
*/
declare function scalarDataType( value: any ): DataType;


// EXPORTS //

export = scalarDataType;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import scalarDataType = require( './index' );


// TESTS //

// The function returns a string...
{
scalarDataType( 3.0 ); // $ExpectType DataType
scalarDataType( true ); // $ExpectType DataType
scalarDataType( null ); // $ExpectType DataType
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
scalarDataType(); // $ExpectError
scalarDataType( 3.0, 2.0 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var Complex128 = require( '@stdlib/complex/float64/ctor' );
var scalarDataType = require( './../lib' );

var values = [
3.14,
-1.0,
true,
false,
new Complex128( 1.0, 2.0 ),
'beep',
null,
[ 1, 2, 3 ]
];

var i;
for ( i = 0; i < values.length; i++ ) {
console.log( '%s => %s', String( values[ i ] ), scalarDataType( values[ i ] ) );
}
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Determine the default ndarray data type for a provided scalar value.
*
* @module @stdlib/ndarray/base/scalar-dtype
*
* @example
* var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
*
* var dt = scalarDataType( 3.0 );
* // returns 'float64'
*
* dt = scalarDataType( true );
* // returns 'bool'
*/

// MODULES //

var scalarDataType = require( './main.js' );


// EXPORTS //

module.exports = scalarDataType;
Loading