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
123 changes: 123 additions & 0 deletions lib/node_modules/@stdlib/plot/vega/data/tidy/from-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!--

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

-->

# array2tidy

> Convert an array to a "tidy" data set.

<!-- 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 array2tidy = require( '@stdlib/plot/vega/data/tidy/from-array' );
```

#### array2tidy( arr, name, fields )

Converts an array to a "tidy" data set.

```javascript
var x = [ 1, 2, 3 ];

var v = array2tidy( x, 'data', [ 'index', 'value' ] );
// returns <DataSet>
```

The function supports the following parameters:

- **arr**: input array.
- **name**: data set name.
- **fields**: list of field names.

</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 list of field names should be a two-element array where the first element corresponds to the field name of input array element index and the second element corresponds to the field name of the input array element value.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var array2tidy = require( '@stdlib/plot/vega/data/tidy/from-array' );

var arr = discreteUniform( 10, -100, 100 );
var data = array2tidy( arr, 'series', [ 'x', 'y' ] );

var out = data.toJSON();
console.log( out );

console.log( JSON.stringify( out ) );
```

</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,55 @@
/**
* @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 isDataSet = require( '@stdlib/plot/vega/base/assert/is-dataset' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var array2tidy = require( './../lib' );


// MAIN //

bench( format( '%s:len=10', pkg ), function benchmark( b ) {
var fields;
var x;
var i;
var v;

fields = [ 'index', 'value' ];
x = zeroTo( 10 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = array2tidy( x, 'data', fields );
if ( typeof v !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isDataSet( v ) ) {
b.fail( 'should return a data set' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{{alias}}( arr, name, fields )
Converts an array to a "tidy" data set.

The list of field names should be a two-element array where the first
element corresponds to the field name of input array element index and the
second element corresponds to the field name of the input array element
value.

Parameters
----------
arr: ArrayLikeObject
Input array.

name: string
Data set name.

fields: Array<string>
List of field names.

Returns
-------
out: DataSet
Data set instance.

Examples
--------
> var x = [ 1, 2, 3 ];
> var out = {{alias}}( x, 'data', [ 'index', 'value' ] )
<DataSet>

See Also
--------

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

// TypeScript Version: 4.1

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

import { Collection } from '@stdlib/types/array';

/**
* Data set instance.
*/
interface DataSet {
/**
* Serializes an instance to a JSON object.
*
* @returns JSON object
*/
toJSON(): object;
}

/**
* Converts an array to a "tidy" data set.
*
* @param arr - input array
* @param name - data set name
* @param fields - list of field names
* @returns data set instance
*
* @example
* var x = [ 1, 2, 3 ];
*
* var v = array2tidy( x, 'data', [ 'index', 'value' ] );
* // returns <DataSet>
*/
declare function array2tidy<T = unknown>( arr: Collection<T>, name: string, fields: Array<string> ): DataSet;


// EXPORTS //

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


// TESTS //

// The function returns a data set...
{
array2tidy( [ 1, 2, 3 ], 'data', [ 'index', 'value' ] ); // $ExpectType DataSet
}

// The compiler throws an error if the function is provided a first argument which is not a collection...
{
array2tidy( 5, 'data', [ 'index', 'value' ] ); // $ExpectError
array2tidy( true, 'data', [ 'index', 'value' ] ); // $ExpectError
array2tidy( false, 'data', [ 'index', 'value' ] ); // $ExpectError
array2tidy( null, 'data', [ 'index', 'value' ] ); // $ExpectError
array2tidy( {}, 'data', [ 'index', 'value' ] ); // $ExpectError
array2tidy( ( x: number ): number => x, 'data', [ 'index', 'value' ] ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a string...
{
array2tidy( [ 1, 2, 3 ], 5, [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], true, [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], false, [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], null, [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], [], [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], {}, [ 'index', 'value' ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], ( x: number ): number => x, [ 'index', 'value' ] ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which is not an array of strings...
{
array2tidy( [ 1, 2, 3 ], 'data', 5 ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', true ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', false ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', null ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', {} ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', [ 1, 2 ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
array2tidy(); // $ExpectError
array2tidy( [ 1, 2, 3 ] ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data' ); // $ExpectError
array2tidy( [ 1, 2, 3 ], 'data', [ 'index', 'value' ], {} ); // $ExpectError
}
Loading
Loading