All files / base/entries2views/lib main.js

45.25% Statements 81/179
100% Branches 1/1
0% Functions 0/1
45.25% Lines 81/179

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 1801x 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 1x                                                                                                                                                                                                     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 setReadOnlyAccessor = require( '@stdlib/utils/define-read-only-accessor' );
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
var accessors = require( '@stdlib/array/base/accessors' );
 
 
// MAIN //
 
/**
* Converts array entries to an array of composite views.
*
* ## 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.
* -   For each element of the returned array, the index view field is read-only and cannot be mutated.
*
* @param {Collection} arr - input array
* @param {ArrayLikeObject<string>} fields - list of field names
* @returns {Array<Object>} output array
*
* @example
* var x = [ 1, 2, 3 ];
* var fields = [ 'x', 'y' ];
*
* var out = entries2views( x, fields );
* // returns [ <Object>, <Object>, <Object> ]
*
* var v0 = out[ 0 ].toJSON();
* // returns { 'x': 0, 'y': 1 }
*
* var v1 = out[ 1 ].toJSON();
* // returns { 'x': 1, 'y': 2 }
*
* var v2 = out[ 2 ].toJSON();
* // returns { 'x': 2, 'y': 3 }
*
* // Mutate the input array:
* x[ 0 ] = 5;
*
* v0 = out[ 0 ].toJSON();
* // returns { 'x': 0, 'y': 5 }
*
* // Set a view property:
* out[ 1 ].y = 'beep';
*
* v1 = out[ 1 ].toJSON();
* // returns { 'x': 1, 'y': 'beep' }
*
* var y = x.slice();
* // returns [ 5, 'beep', 3 ]
*/
function entries2views( arr, fields ) {
	var fget;
	var get;
	var set;
	var acc;
	var out;
	var N;
	var k;
	var v;
	var i;

	N = arr.length;
	if ( N < 1 ) {
		return [];
	}
	// Resolve element accessors:
	acc = accessors( arr ).accessors;
	get = acc[ 0 ];
	set = acc[ 1 ];

	// Cache field names:
	fget = resolveGetter( fields );
	k = fget( fields, 0 );
	v = fget( fields, 1 );

	// eslint-disable-next-line stdlib/jsdoc-typedef-typos
	/**
	* Constructor for creating a composite view.
	*
	* @private
	* @constructor
	* @param {NonNegativeInteger} i - element index
	* @returns {Datum} datum instance
	*/
	function Datum( i ) {
		setNonEnumerableReadOnly( this, '_i', i );
		return this;
	}

	// Define accessors for each field:
	setReadOnlyAccessor( Datum.prototype, k, getIndex );
	setReadWriteAccessor( Datum.prototype, v, getValue, setValue );

	// 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 < N; i++ ) {
		out.push( new Datum( i ) );
	}
	return out;

	/**
	* Returns the entry index.
	*
	* @private
	* @returns {*} result
	*/
	function getIndex() {
		return this._i;
	}

	/**
	* Returns the value associated with a field.
	*
	* @private
	* @returns {*} result
	*/
	function getValue() {
		return get( arr, this._i );
	}

	/**
	* Sets the value associated with a field.
	*
	* @private
	* @param {*} value - value to set
	*/
	function setValue( value ) {
		set( arr, this._i, value );
	}

	/**
	* Serializes a datum to JSON.
	*
	* @private
	* @returns {Object} JSON object
	*/
	function toJSON() {
		var out;

		out = {};
		out[ k ] = this[ k ];
		out[ v ] = this[ v ];
		return out;
	}
}
 
 
// EXPORTS //
 
module.exports = entries2views;