All files main.js

100% Statements 218/218
100% Branches 19/19
100% Functions 8/8
100% Lines 218/218

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 9x 9x 11x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 14x 14x 14x 14x 14x 14x 14x 7x 7x 11x 17x 17x 7x 7x 7x 7x 7x 7x 7x 7x 7x 11x 14x 14x 7x 7x 7x 7x 7x 7x 7x 7x 54x 54x 54x 54x 54x 4x 4x 4x 4x 4x 54x 7x 7x 7x 7x 7x 7x 7x 7x 7x 17x 17x 17x 17x 17x 17x 17x 17x 17x 50x 50x 50x 17x 7x 7x 7x 7x 7x 7x 7x 7x 7x 17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 17x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 18x 18x 18x 6x 6x 11x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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.
*/
 
/* eslint-disable no-invalid-this */
 
'use strict';
 
// MODULES //
 
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
var accessors = require( '@stdlib/array/base/accessors' );
var copy = require( '@stdlib/array/base/copy' );
 
 
// MAIN //
 
/**
* Converts each nested array to a composite view.
*
* ## Notes
*
* -   The function assumes that all nested arrays have the same length.
* -   The number of provided array labels should equal the length of each nested array.
* -   Each view in the returned array shares the same memory as the corresponding elements in the input arrays. Accordingly, mutation of either a nested array or a view will mutate the other.
*
* @param {Collection<Collection>} arr - input array
* @param {ArrayLikeObject<string>} fields - list of field names
* @returns {Array<Object>} output array
*
* @example
* var x = [ [ 1, 2 ], [ 3, 4 ] ];
* var fields = [ 'x', 'y' ];
*
* var out = nested2views( x, fields );
* // returns [ <Object>, <Object> ]
*
* var v0 = out[ 0 ].toJSON();
* // returns { 'x': 1, 'y': 2 }
*
* var v1 = out[ 1 ].toJSON();
* // returns { 'x': 3, 'y': 4 }
*
* // Mutate the first nested array:
* x[ 0 ][ 0 ] = 5;
*
* v0 = out[ 0 ].toJSON();
* // returns { 'x': 5, 'y': 2 }
*
* // Set a view property:
* out[ 1 ].y = 'beep';
*
* v1 = out[ 1 ].toJSON();
* // returns { 'x': 3, 'y': 'beep' }
*
* var y = x.slice();
* // returns [ [ 5, 2 ], [ 3, 'beep' ] ]
*/
function nested2views( arr, fields ) {
	var oget;
	var keys;
	var out;
	var M;
	var N;
	var i;
 
	M = arr.length;
	if ( M < 1 ) {
		return [];
	}
	oget = resolveGetter( arr );
	N = oget( arr, 0 ).length;
	if ( N < 1 ) {
		return [];
	}
	// Create a copy of provided fields to prevent external mutation:
	keys = copy( fields );
 
	// eslint-disable-next-line stdlib/jsdoc-typedef-typos
	/**
	* Constructor for creating a composite view.
	*
	* @private
	* @constructor
	* @param {Collection} arr - nested array
	* @param {NonNegativeInteger} i - element index
	* @returns {Datum} datum instance
	*/
	function Datum( arr, i ) {
		var acc = accessors( arr ).accessors;
		setNonEnumerable( this, '_arr', arr );
		setNonEnumerable( this, '_get', acc[ 0 ] );
		setNonEnumerable( this, '_set', acc[ 1 ] );
		setNonEnumerableReadOnly( this, '_i', i );
		return this;
	}
 
	// Define accessors for each field...
	for ( i = 0; i < N; i++ ) {
		setReadWriteAccessor( Datum.prototype, keys[ i ], getValue( i ), setValue( i ) ); // eslint-disable-line max-len
	}
 
	// Define a method for ensuring that cached references are up-to-date:
	setNonEnumerableReadOnly( Datum.prototype, '_updateCache', updateCache );
 
	// Ensure that the returned array correctly serializes to JSON:
	setNonEnumerableReadOnly( Datum.prototype, 'toJSON', toJSON );
 
	// Create a list of composite views...
	out = [];
	for ( i = 0; i < M; i++ ) {
		out.push( new Datum( oget( arr, i ), i ) );
	}
	return out;
 
	/**
	* Updates cached references, if necessary.
	*
	* @private
	*/
	function updateCache() {
		var acc;
		var ref;
 
		ref = oget( arr, this._i );
		if ( ref !== this._arr ) {
			acc = accessors( ref ).accessors;
			this._arr = ref;
			this._get = acc[ 0 ];
			this._set = acc[ 1 ];
		}
	}
 
	/**
	* Returns an accessor for returning the value associated with a field.
	*
	* @private
	* @param {NonNegativeInteger} idx - field index
	* @returns {Function} accessor
	*/
	function getValue( idx ) {
		return get;
 
		/**
		* Returns the value associated with a field.
		*
		* @private
		* @returns {*} result
		*/
		function get() {
			this._updateCache();
			return this._get( this._arr, idx );
		}
	}
 
	/**
	* Returns an accessor for setting the value associated with a field.
	*
	* @private
	* @param {NonNegativeInteger} idx - field index
	* @returns {Function} accessor
	*/
	function setValue( idx ) {
		return set;
 
		/**
		* Sets the value associated with a field.
		*
		* @private
		* @param {*} value - value to set
		*/
		function set( value ) {
			this._updateCache();
			this._set( this._arr, idx, value );
		}
	}
 
	/**
	* Serializes a datum to JSON.
	*
	* @private
	* @returns {Object} JSON object
	*/
	function toJSON() {
		var out;
		var k;
		var i;
 
		out = {};
		for ( i = 0; i < N; i++ ) {
			k = keys[ i ];
			out[ k ] = this[ k ];
		}
		return out;
	}
}
 
 
// EXPORTS //
 
module.exports = nested2views;