All files canonicalize.js

98.03% Statements 50/51
93.54% Branches 29/31
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        1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x     205x 2x           203x 106x     106x 106x       97x 40x     40x 36x       61x 17x       14x           13x 1x             1x         14x     47x 47x   47x 128x   47x         359x 154x   205x                     42x   42x   42x 42x 1x                 41x   41x 38x   3x                   1x  
'use strict';
 
/* eslint no-use-before-define: ["error", { "functions": false }] */
 
const { convertZListToItemArray, isReference, 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' );
 
function canonicalizeObject( o ) {
	if ( Z99Validator.validate( o ) ) {
		return {
			Z1K1: canonicalize( o.Z1K1 ),
			Z99K1: o.Z99K1
		};
	}
 
	if ( Z9Validator.validate( o ) ) {
		o.Z9K1 = canonicalize( o.Z9K1 );
 
		// return as string if Z9K1 is a valid reference string
		Eif ( isString( o.Z9K1 ) && isReference( o.Z9K1 ) ) {
			return o.Z9K1;
		}
	}
 
	if ( Z6Validator.validate( o ) ) {
		o.Z6K1 = canonicalize( o.Z6K1 );
 
		// return as string if Z6/String doesn't need to be escaped, i.e., is not in Zxxxx format
		if ( isString( o.Z6K1 ) && !isReference( o.Z6K1 ) ) {
			return o.Z6K1;
		}
	}
 
	if ( TypedListValidator.validate( o ) ) {
		const itemList = convertZListToItemArray( o ).map( ( e ) => canonicalize( e ) );
 
		let itemType;
		// FIXME: Should we search recursively for Z881?
		if (
			Z7Validator.validate( o.Z1K1 ) &&
			( canonicalize( o.Z1K1.Z7K1 ) === 'Z881' )
		) {
			// If type is a function call to Z881,
			// itemType is the canonical content of Z88K1.
			itemType = canonicalize( o.Z1K1.Z881K1 );
		} else Eif (
			Z4Validator.validate( o.Z1K1 ) &&
			Z7Validator.validate( o.Z1K1.Z4K1 ) &&
			( canonicalize( o.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( o.Z1K1.Z4K1.Z881K1 );
		} else {
			// Else, itemType is the whole type ZObject transformed into canonical form.
			itemType = canonicalize( o.Z1K1 );
		}
		return [ itemType ].concat( itemList );
	}
 
	const keys = Object.keys( o );
	const result = {};
 
	for ( let i = 0; i < keys.length; i++ ) {
		result[ keys[ i ] ] = canonicalize( o[ keys[ i ] ] );
	}
	return result;
}
 
// the input is assumed to be a well-formed ZObject, or else the behaviour is undefined
function canonicalize( o ) {
	if ( isString( o ) ) {
		return o;
	}
	return canonicalizeObject( o );
}
 
/**
 * 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} o a ZObject
 * @return {Object} a Z22
 */
function canonicalizeExport( o ) {
	const errors = require( './error.js' );
 
	const normalized = normalize( o );
 
	const possibleError = getError( normalized );
	if ( ( Z5Validator.validateStatus( possibleError ) ).isValid() ) {
		return makeMappedResultEnvelope(
			null,
			errors.makeErrorInCanonicalForm(
				errors.error.unable_to_canonicalize,
				[ wrapInQuote( o ), 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( o ), status.getZ5() ]
			)
		);
	}
}
 
module.exports = canonicalizeExport;