All files / executors/javascript utils.js

81.63% Statements 40/49
58.82% Branches 20/34
100% Functions 9/9
81.63% Lines 40/49

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    13x 13x 13x     8x 8x             18x       18x       18x       18x       2x   2x 2x 2x     2x     2x 2x       2x       18x     18x     18x     18x     18x   18x                       18x     13x 13x 13x 13x 13x 13x                       20x 20x 1x   19x     19x     19x   19x 19x 19x     13x                  
'use strict';
 
const { inspect } = require( 'util' );
const { isString } = require( './function-schemata/javascript/src/utils.js' );
const stableStringify = require( 'json-stable-stringify-without-jsonify' );
 
function soupUpZ1K1( Z1K1 ) {
	Eif ( isString( Z1K1 ) ) {
		return { Z1K1: 'Z9', Z9K1: Z1K1 };
	}
	return Z1K1;
}
 
// TODO (T282891): All isZWhatev functions should use function-schemata.
function isZReference( Z9 ) {
	return Z9 !== undefined && Z9.Z1K1 === 'Z9' && isString( Z9.Z9K1 );
}
 
function isZFunctionCall( Z7 ) {
	return Z7 !== undefined && Z7.Z1K1 !== undefined && Z7.Z1K1.Z9K1 === 'Z7';
}
 
function isZType( Z4 ) {
	return Z4 !== undefined && Z4.Z1K1 !== undefined && Z4.Z1K1.Z9K1 === 'Z4';
}
 
function isZFunction( Z8 ) {
	return Z8 !== undefined && Z8.Z1K1 !== undefined && Z8.Z1K1.Z9K1 === 'Z8';
}
 
function getListType( theList ) {
	const Z1K1s = new Set();
	let firstZ1K1;
	for ( const element of theList ) {
		Eif ( firstZ1K1 === undefined ) {
			firstZ1K1 = element.Z1K1;
		}
		// TODO (T293915): Use ZObjectKeyFactory to create string representations.
		Z1K1s.add( stableStringify( element.Z1K1 ) );
	}
	let elementType;
	Eif ( Z1K1s.size === 1 ) {
		elementType = soupUpZ1K1( JSON.parse( Z1K1s.values().next().value ) );
	} else {
		elementType = soupUpZ1K1( 'Z1' );
	}
	return elementType;
}
 
function getZID( Z4 ) {
	Iif ( isZFunction( Z4 ) ) {
		return getZID( Z4.Z8K5 );
	}
	Iif ( isZReference( Z4 ) ) {
		return Z4.Z9K1;
	}
	Iif ( isZFunctionCall( Z4 ) ) {
		return getZID( Z4.Z7K1 );
	}
	Iif ( isZType( Z4 ) ) {
		return getZID( Z4.Z4K1 );
	}
	Eif ( isString( Z4 ) ) {
		// If Z4 is a string, original object was a Z6 or a Z9.
		return Z4;
	}
	return null;
}
 
/**
 * Determine the ZID corresponding to the type of a ZObject.
 *
 * @param {Object} ZObject
 * @return {string} the object's type ZID
 */
function getZObjectType( ZObject ) {
	return getZID( ZObject.Z1K1 );
}
 
const typeMap = new Map();
typeMap.set( 'String', 'Z6' );
typeMap.set( 'Null', 'Z21' );
typeMap.set( 'Boolean', 'Z40' );
typeMap.set( 'Array', 'Z881' );
typeMap.set( 'Map', 'Z883' );
 
/**
 * Infer the type of a JS object and try to find the corresponding ZID.
 * String -> Z6
 * Null -> Z21
 * Boolean -> Z40
 *
 * @param {Object} theObject
 * @return {string} the ZID corresponding to the appropriate serialized type
 */
function getZIDForJSType( theObject ) {
	const inspected = inspect( theObject );
	if ( inspected.startsWith( 'ZPair' ) ) {
		return 'Z882';
	}
	Iif ( inspected.startsWith( 'ZObject' ) ) {
		return 'DEFAULT';
	}
	Iif ( inspected.startsWith( 'ZReference' ) ) {
		return 'Z9';
	}
	const typeRegex = /\[object (\w*)\]/;
	// Object.toString will return [object <TYPE>]; <TYPE> is what we're after.
	const typeString = Object.prototype.toString.call( theObject ).replace( typeRegex, '$1' );
	const ZID = typeMap.get( typeString );
	return ZID;
}
 
module.exports = {
	getListType,
	getZIDForJSType,
	getZID,
	getZObjectType,
	isString,
	isZType,
	soupUpZ1K1
};