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
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!--

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

-->

# str2enum

> Return the enumeration constant associated with a KMeans distance metric string.

<!-- 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 str2enum = require( '@stdlib/ml/base/cluster/kmeans/metric-str2enum' );
```

#### str2enum( metric )

Returns the enumeration constant associated with a KMeans distance metric string.

```javascript
var v = str2enum( 'cosine' );
// returns <number>
```

If unable to resolve an enumeration constant, the function returns `null`.

```javascript
var v = str2enum( 'beep' );
// returns null
```

</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

- Downstream consumers of this function should **not** rely on specific integer values (e.g., `SQEUCLIDEAN == 0`). Instead, the function should be used in an opaque manner.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var str2enum = require( '@stdlib/ml/base/cluster/kmeans/metric-str2enum' );

var v = str2enum( 'cosine' );
// returns <number>

v = str2enum( 'sqeuclidean' );
// returns <number>
```

</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">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var str2enum = require( './../lib' );


// MAIN //

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

values = [
'sqeuclidean',
'cosine',
'cityblock',
'correlation'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = str2enum( values[ i%values.length ] );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( !isInteger( out ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

{{alias}}( metric )
Returns the enumeration constant associated with a KMeans distance metric
string.

Downstream consumers of this function should *not* rely on specific integer
values (e.g., `SQEUCLIDEAN == 0`). Instead, the function should be used in an
opaque manner.

Parameters
----------
metric: string
Distance metric.

Returns
-------
out: integer|null
Enumeration constant.

Examples
--------
> var out = {{alias}}( 'cosine' )
<number>

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @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

/**
* Returns the enumeration constant associated with a KMeans distance metric string.
*
* ## Notes
*
* - Downstream consumers of this function should **not** rely on specific integer values (e.g., `SQEUCLIDEAN == 0`). Instead, the function should be used in an opaque manner.
*
* @param metric - distance metric string
* @returns enumeration constant
*
* @example
* var v = str2enum( 'cosine' );
* // returns <number>
*/
declare function str2enum( metric: string ): number | null;


// EXPORTS //

export = str2enum;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @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 str2enum = require( './index' );


// TESTS //

// The function returns a number or null...
{
str2enum( 'cosine' ); // $ExpectType number | null
}

// The compiler throws an error if not provided a string...
{
str2enum( 10 ); // $ExpectError
str2enum( true ); // $ExpectError
str2enum( false ); // $ExpectError
str2enum( null ); // $ExpectError
str2enum( undefined ); // $ExpectError
str2enum( [] ); // $ExpectError
str2enum( {} ); // $ExpectError
str2enum( ( x: number ): number => x ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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 str2enum = require( './../lib' );

var v = str2enum( 'cosine' );
console.log( v );
// => <number>

v = str2enum( 'sqeuclidean' );
console.log( v );
// => <number>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @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';

/**
* Return the enumeration constant associated with a KMeans distance metric string.
*
* @module @stdlib/ml/base/cluster/kmeans/metric-str2enum
*
* @example
* var str2enum = require( '@stdlib/ml/base/cluster/kmeans/metric-str2enum' );
*
* var v = str2enum( 'cosine' );
* // returns <number>
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading
Loading