All files canonicalize.js

98.03% Statements 50/51
90.9% Branches 30/33
100% Functions 4/4
97.95% Lines 48/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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142        1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x                 205x 2x           203x 106x     106x 106x       97x 40x     40x 37x       60x 17x       14x           13x 1x             1x         14x     46x 46x   46x 126x   46x                       357x 152x   205x                     42x   42x   42x 42x 1x                 41x   41x 38x   3x                   1x  
'use strict';
 
/* eslint no-use-before-define: ["error", { "functions": false }] */
 
const { convertZListToItemArray, isZObjectReference, isString, makeMappedResultEnvelope, wrapInQuote } = require( './utils.js' );
const { SchemaFactory } = require( './schema' );
const normalize = require( './normalize.js' );
const { getError } = require( './utils' );
 
const normalFactory = SchemaFactory.NORMAL();
const normalZ1Validator = normalFactory.create( 'Z1' );
const Z4Validator = normalFactory.create( 'Z4_literal' );
const Z5Validator = normalFactory.create( 'Z5_literal' );
const Z6Validator = normalFactory.create( 'Z6_literal' );
const Z7Validator = normalFactory.create( 'Z7_literal' );
const Z9Validator = normalFactory.create( 'Z9_literal' );
const Z99Validator = normalFactory.create( 'Z99_literal' );
const TypedListValidator = normalFactory.create( 'LIST_literal' );
 
/**
 * Canonicalizes a ZObject.
 *
 * @param {Object} zobject a ZObject
 * @return {Object} zobject that has been 'canonicalized'
 */
function canonicalizeObject( zobject ) {
	if ( Z99Validator.validate( zobject ) ) {
		return {
			Z1K1: canonicalize( zobject.Z1K1 ),
			Z99K1: zobject.Z99K1
		};
	}
 
	if ( Z9Validator.validate( zobject ) ) {
		zobject.Z9K1 = canonicalize( zobject.Z9K1 );
 
		// return as string if Z9K1 is a valid reference string
		Eif ( isString( zobject.Z9K1 ) && isZObjectReference( zobject.Z9K1 ) ) {
			return zobject.Z9K1;
		}
	}
 
	if ( Z6Validator.validate( zobject ) ) {
		zobject.Z6K1 = canonicalize( zobject.Z6K1 );
 
		// return as string if Z6/String doesn't need to be escaped, i.e., is not in Zxxxx format
		if ( isString( zobject.Z6K1 ) && !isZObjectReference( zobject.Z6K1 ) ) {
			return zobject.Z6K1;
		}
	}
 
	if ( TypedListValidator.validate( zobject ) ) {
		const itemList = convertZListToItemArray( zobject || [] ).map( ( e ) => canonicalize( e ) );
 
		let itemType;
		// FIXME: Should we search recursively for Z881?
		if (
			Z7Validator.validate( zobject.Z1K1 ) &&
			( canonicalize( zobject.Z1K1.Z7K1 ) === 'Z881' )
		) {
			// If type is a function call to Z881,
			// itemType is the canonical content of Z88K1.
			itemType = canonicalize( zobject.Z1K1.Z881K1 );
		} else Eif (
			Z4Validator.validate( zobject.Z1K1 ) &&
			Z7Validator.validate( zobject.Z1K1.Z4K1 ) &&
			( canonicalize( zobject.Z1K1.Z4K1.Z7K1 ) === 'Z881' )
		) {
			// If type is a literal and Z4K1 is function call to Z881,
			// itemType is the content of Z4K1.Z88K1
			itemType = canonicalize( zobject.Z1K1.Z4K1.Z881K1 );
		} else {
			// Else, itemType is the whole type ZObject transformed into canonical form.
			itemType = canonicalize( zobject.Z1K1 );
		}
		return [ itemType ].concat( itemList );
	}
 
	const keys = Object.keys( zobject );
	const result = {};
 
	for ( let i = 0; i < keys.length; i++ ) {
		result[ keys[ i ] ] = canonicalize( zobject[ keys[ i ] ] );
	}
	return result;
}
 
/**
 * Canonicalizes a ZObject unless it is a string.
 *
 * The input is assumed to be a well-formed ZObject, or else the behaviour is undefined
 *
 * @param {Object} zobject a ZObject
 * @return {Object|undefined} zobject that has been 'canonicalized' or undefined
 */
function canonicalize( zobject ) {
	if ( isString( zobject ) ) {
		return zobject;
	}
	return canonicalizeObject( zobject );
}
 
/**
 * Canonicalizes a normalized ZObject. Returns a Z22/'Evaluation result' containing the
 * canonicalized ZObject or a Z5/Error (in the metadata map of the Z22).
 *
 * @param {Object} zobject a ZObject
 * @return {Object} a Z22
 */
function canonicalizeExport( zobject ) {
	const errors = require( './error.js' );
 
	const normalized = normalize( zobject );
 
	const possibleError = getError( normalized );
	if ( ( Z5Validator.validateStatus( possibleError ) ).isValid() ) {
		return makeMappedResultEnvelope(
			null,
			errors.makeErrorInCanonicalForm(
				errors.error.unable_to_canonicalize,
				[ wrapInQuote( zobject ), possibleError ]
			)
		);
	}
 
	const status = normalZ1Validator.validateStatus( normalized );
 
	if ( status.isValid() ) {
		return makeMappedResultEnvelope( canonicalize( normalized.Z22K1 ), null );
	} else {
		return makeMappedResultEnvelope(
			null,
			errors.makeErrorInCanonicalForm(
				errors.error.unable_to_canonicalize,
				[ wrapInQuote( zobject ), status.getZ5() ]
			)
		);
	}
}
 
module.exports = canonicalizeExport;