All files / ext.wikilambda.edit/mixins typeUtils.js

99.13% Statements 115/116
97.64% Branches 83/85
100% Functions 23/23
99.13% Lines 115/116

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771                  58x 58x                 152x                       1883x 87x 1796x 24x 2x 22x 21x   1x     1772x 1458x   314x                         28x 4x     24x 4x 7x     7x     20x 417x     20x 4x   16x                     671x                 111x                 109x                     61x 156x                       47x 107x                   7013x                   5951x 5951x                           3444x 3391x   53x               53x   7x 7x       42x 42x 2x   42x 42x   25x 25x 74x         25x 24x     24x   25x   25x   42x     2x 2x 2x     1x 1x     1x     53x                                 143x     4x               28x                   1x                         4x                           29x                               8x                                 15x                                   7x                                             15x                                           4x                                           2x                                             1x                                                                                     10x                                                                             2x                                                                                                           5x                                     3x                                                     3x                                         1x                 1x                                           20x     20x   20x 4x 4x   2x       2x   1x             1x   1x             1x     20x                             1665x 1x   1664x 209x       1455x 1455x 939x 939x   516x                                           30x 33x 40x   33x 9x     21x       69x 39x   30x 30x 30x         58x  
/**
 * WikiLambda Vue editor: typeUtils mixin
 * Mixin with util functions to handle types and initial values.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../Constants.js' ),
	typeUtils = {
		methods: {
			/**
			 * Whether the given type is defined by a function call
			 *
			 * @param {Object|string} value
			 * @return {boolean}
			 */
			isGenericType: function ( value ) {
				return (
					( typeof value === 'object' ) &&
					( value[ Constants.Z_OBJECT_TYPE ] === Constants.Z_FUNCTION_CALL )
				);
			},
			/**
			 * Gets the key type given its initial value.
			 *
			 * @param {Object|Array|string} value
			 * @return {string}
			 */
			getZObjectType: function ( value ) {
				if ( !value ) {
					return Constants.Z_STRING;
				} else if ( typeof ( value ) === 'object' ) {
					if ( Array.isArray( value ) ) {
						return Constants.Z_TYPED_LIST;
					} else if ( Constants.Z_OBJECT_TYPE in value ) {
						return value[ Constants.Z_OBJECT_TYPE ];
					} else {
						return Constants.Z_OBJECT;
					}
				} else {
					if ( value.match( /^Z\d+$/ ) ) {
						return Constants.Z_REFERENCE;
					} else {
						return Constants.Z_STRING;
					}
				}
			},
			/**
			 * Find a specific Key within an array of object
			 *
			 * @param {string} key
			 * @param {Array} array
			 * @return {Object}
			 */
			findKeyInArray: function ( key, array ) {
				// Exit early if we got a false, a non-array, or an empty array
				if ( !key || !array || !Array.isArray( array ) || array.length === 0 ) {
					return false;
				}
 
				if ( Array.isArray( key ) ) {
					return key.map( function ( k ) {
						return typeUtils.methods.findKeyInArray( k, array );
					} )
						.filter( function ( filterResult ) {
							return !!filterResult;
						} )[ 0 ] || false;
				} else {
					const result = array.filter( function ( item ) {
						return item.key === key;
					} );
 
					if ( result.length === 0 ) {
						return false;
					} else {
						return result[ 0 ];
					}
				}
			},
			/**
			 * Validate if a string is a valid Zid
			 *
			 * @param {string} zid
			 * @return {boolean}
			 */
			isValidZidFormat: function ( zid ) {
				return /^Z\d+$/.test( zid );
			},
			/**
			 * Validate if a string is a valid global Key
			 *
			 * @param {string} key
			 * @return {boolean}
			 */
			isGlobalKey: function ( key ) {
				return /^Z\d+K\d+$/.test( key );
			},
			/**
			 * Get the Zid part of a global Key
			 *
			 * @param {string} key
			 * @return {string}
			 */
			getZidOfGlobalKey: function ( key ) {
				return key.split( 'K' )[ 0 ];
			},
			/**
			 * Get the Z3/Key object given a key string
			 * from a list of Z3/Key items
			 *
			 * @param {string} key
			 * @param {Array} list
			 * @return {Object}
			 */
			getKeyFromKeyList: function ( key, list ) {
				return list.find( function ( item ) {
					return ( item[ Constants.Z_KEY_ID ] === key );
				} );
			},
			/**
			 * Get the Z17/Argument object given a key string
			 * from a list of Z17/Argument items
			 *
			 * @param {string} key
			 * @param {Array} list
			 * @return {Object}
			 */
			getArgFromArgList: function ( key, list ) {
				return list.find( function ( item ) {
					return ( item[ Constants.Z_ARGUMENT_KEY ] === key );
				} );
			},
			/**
			 * Determine if a key indicates a typed list type.
			 *
			 * @param {string} key
			 * @return {boolean}
			 */
			isKeyTypedListType: function ( key ) {
				return key === '0';
			},
			/**
			 * Determines if a key indicates a typed list item. This will be true when a key
			 * is a stringified number greater than 0 (0 indicates the type of a typed list)
			 *
			 * @param {string} key
			 * @return {boolean}
			 */
			isKeyTypedListItem: function ( key ) {
				const numericalKey = Number( key );
				return Number.isInteger( numericalKey ) && numericalKey > 0;
			},
			/**
			 * Transform the value of a Z1K1 key (object type) to a string.
			 * When the type is a reference, return the Zid of the referred type.
			 * When the type is a literal, return the stringified value of Z4K1.
			 * When the type is a function call, return the function ID:
			 * - if noArgs is false, also return the arguments in brackets.
			 *
			 * @param {Object|string} type
			 * @param {boolean} noArgs
			 * @return {string}
			 */
			typeToString: function ( type, noArgs = false ) {
				if ( typeof type === 'string' ) {
					return type;
				} else {
					const mode = typeUtils.methods.typeToString( type[ Constants.Z_OBJECT_TYPE ] );
					let typeString,
						functionZid,
						argKeys,
						argType,
						argTypes,
						argTypesString;
 
					switch ( mode ) {
						case Constants.Z_REFERENCE:
							typeString = type[ Constants.Z_REFERENCE_ID ];
							break;
 
						case Constants.Z_FUNCTION_CALL:
							// Stringify the function Zid
							functionZid = type[ Constants.Z_FUNCTION_CALL_FUNCTION ];
							if ( typeof functionZid === 'object' ) {
								functionZid = typeUtils.methods.typeToString( functionZid );
							}
							typeString = functionZid;
							if ( !noArgs ) {
								// Stringify the function arguments
								argTypes = [];
								argKeys = Object.keys( type ).filter( ( key ) => {
									return (
										( key !== Constants.Z_OBJECT_TYPE ) &&
										( key !== Constants.Z_FUNCTION_CALL_FUNCTION )
									);
								} );
								for ( const argKey of argKeys ) {
									argType = ( typeof type[ argKey ] === 'object' ) ?
										typeUtils.methods.typeToString( type[ argKey ] ) :
										type[ argKey ];
									argTypes.push( argType );
								}
								argTypesString = argTypes.join( ',' );
								// Put everything together
								typeString += `(${ argTypesString })`;
							}
							break;
 
						case Constants.Z_TYPE:
							typeString = type[ Constants.Z_TYPE_IDENTITY ];
							typeString = typeUtils.methods.typeToString( typeString );
							break;
 
						case Constants.Z_ARGUMENT_REFERENCE:
							typeString = type[ Constants.Z_ARGUMENT_REFERENCE_KEY ];
							break;
 
						default:
							typeString = undefined;
					}
 
					return typeString;
				}
			},
			/**
			 * Return the empty structure of builtin types that we will
			 * create when creating these types in the interface.
			 *
			 * The way these scaffoldings are created also determines whether
			 * certain sub-types are initiated as literals or as references
			 * (E.g. Z11K1, monolingual language, is preferred as a reference
			 * to a Z60 than as a literal, so when creating the scaffolding
			 * we give the structure of an empty reference)
			 *
			 * @param {string} type
			 * @return {Object|Array}
			 */
			getScaffolding: function ( type ) {
				switch ( type ) {
					case Constants.Z_STRING:
						// Empty string: ''
						return '';
 
					case Constants.Z_REFERENCE:
						// Empty reference:
						// {
						//  Z1K1: Z9
						//  Z9K1: ''
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
							[ Constants.Z_REFERENCE_ID ]: ''
						};
 
					case Constants.Z_OBJECT:
						// Empty object:
						// {
						//  Z1K1: { Z1K1: Z9, Z9K1: '' }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							}
						};
 
					case Constants.Z_MULTILINGUALSTRING:
						// Empty monolingual string:
						// {
						//  Z1K1: Z12
						//  Z12K1: [ Z11 ]
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRING,
							[ Constants.Z_MULTILINGUALSTRING_VALUE ]: [
								Constants.Z_MONOLINGUALSTRING
							]
						};
 
					case Constants.Z_MONOLINGUALSTRING:
						// Empty monolingual string:
						// {
						//  Z1K1: Z11
						//  Z11K1: { Z1K1: Z9, Z9K1: '' }
						//  Z11K2: ''
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MONOLINGUALSTRING,
							[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_MONOLINGUALSTRING_VALUE ]: ''
						};
 
					case Constants.Z_MONOLINGUALSTRINGSET:
						// Empty monolingual string set:
						// {
						//  Z1K1: Z31,
						//  Z31K1: { Z1K1: Z9, Z9K1: payload.lang },
						//  Z31K2: [ 'Z6', { Z1K1: Z6, Z6K1: payload.value } ]
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MONOLINGUALSTRINGSET,
							[ Constants.Z_MONOLINGUALSTRINGSET_LANGUAGE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_MONOLINGUALSTRINGSET_VALUE ]: [
								Constants.Z_STRING
							]
						};
 
					case Constants.Z_FUNCTION_CALL:
						// Empty function call:
						// {
						//  Z1K1: Z7
						//  Z7K1: { Z1K1: Z9, Z9K1: '' }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
							[ Constants.Z_FUNCTION_CALL_FUNCTION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							}
						};
 
					case Constants.Z_FUNCTION:
						// Empty function
						// {
						//  Z1K1: 'Z8',
						//  Z8K1: [ 'Z17' ],
						//  Z8K2: { Z1K1: 'Z9', Z9K1: '' },
						//  Z8K3: [ 'Z20' ],
						//  Z8K4: [ 'Z14' ],
						//  Z8K5: { Z1K1: 'Z9', Z9K1: '' },
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION,
							[ Constants.Z_FUNCTION_ARGUMENTS ]: [ Constants.Z_ARGUMENT ],
							[ Constants.Z_FUNCTION_RETURN_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_FUNCTION_TESTERS ]: [ Constants.Z_TESTER ],
							[ Constants.Z_FUNCTION_IMPLEMENTATIONS ]: [ Constants.Z_IMPLEMENTATION ],
							[ Constants.Z_FUNCTION_IDENTITY ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							}
						};
 
					case Constants.Z_ARGUMENT:
						// Empty argument declaration:
						// {
						//  Z1K1: 'Z17',
						//  Z17K1: { Z1K1: 'Z9', Z9K1: '' },
						//  Z17K2: { Z1K1: 'Z6', Z6K1: 'Z0K1' },
						//  Z17K3: { Z1K1: 'Z12', Z12K1: [ Z11 ] }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_ARGUMENT,
							[ Constants.Z_ARGUMENT_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_ARGUMENT_KEY ]: '',
							[ Constants.Z_ARGUMENT_LABEL ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRING,
								[ Constants.Z_MULTILINGUALSTRING_VALUE ]: [
									Constants.Z_MONOLINGUALSTRING
								]
							}
						};
 
					case Constants.Z_IMPLEMENTATION:
						// Empty implementation
						// {
						//  Z1K1: 'Z14',
						//  Z14K1: { Z1K1: 'Z9', Z9K1: '' },
						//  Z14K2: { Z1K1: 'Z7', Z7K1: '' }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_IMPLEMENTATION,
							[ Constants.Z_IMPLEMENTATION_FUNCTION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_IMPLEMENTATION_COMPOSITION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
								[ Constants.Z_FUNCTION_CALL_FUNCTION ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							}
						};
 
					case Constants.Z_CODE:
						// Empty code:
						// {
						//  Z1K1: Z16
						//  Z16K1: { Z1K1: Z9, Z9K1: '' },
						//  Z16K2: ''
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_CODE,
							[ Constants.Z_CODE_LANGUAGE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_CODE_CODE ]: ''
						};
 
					case Constants.Z_TESTER:
						// Empty implementation
						// {
						//  Z1K1: 'Z20',
						//  Z20K1: { Z1K1: 'Z9', Z9K1: '' }
						//  Z20K2: {
						//   Z1K1: 'Z7',
						//   Z7K1: { Z1K1: 'Z9', Z9K1: '' }
						//  },
						//  Z20K3: {
						//   Z1K1: 'Z7',
						//   Z7K1: { Z1K1: 'Z9', Z9K1: '' }
						//  }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_TESTER,
							[ Constants.Z_TESTER_FUNCTION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_TESTER_CALL ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
								[ Constants.Z_FUNCTION_CALL_FUNCTION ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							},
							[ Constants.Z_TESTER_VALIDATION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
								[ Constants.Z_FUNCTION_CALL_FUNCTION ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							}
						};
 
					case Constants.Z_PERSISTENTOBJECT:
						// Empty persistent object:
						// {
						//  Z1K1: 'Z2',
						//  Z2K1: { Z1K1: 'Z6', Z6K1: 'Z0' },
						//  Z2K2: {
						//   Z1K1: { Z1K1: 'Z9', Z9K1: '' }
						//  },
						//  Z2K3: {
						//   Z1K1: { Z1K1: 'Z9', Z9K1: 'Z12' }
						//   Z12K1: [ Z11 ]
						//  },
						//  Z2K4: {
						//   Z1K1: { Z1K1: 'Z9', Z9K1: 'Z32' }
						//   Z32K1: [ Z31 ]
						//  },
						//  Z2K5: {
						//   Z1K1: { Z1K1: 'Z9', Z9K1: 'Z12' }
						//   Z12K1: [ Z11 ]
						//  },
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_PERSISTENTOBJECT,
							[ Constants.Z_PERSISTENTOBJECT_ID ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_STRING,
								[ Constants.Z_STRING_VALUE ]: Constants.NEW_ZID_PLACEHOLDER
							},
							[ Constants.Z_PERSISTENTOBJECT_VALUE ]: {
								[ Constants.Z_OBJECT_TYPE ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							},
							[ Constants.Z_PERSISTENTOBJECT_LABEL ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRING,
								[ Constants.Z_MULTILINGUALSTRING_VALUE ]: [ Constants.Z_MONOLINGUALSTRING ]
							},
							[ Constants.Z_PERSISTENTOBJECT_ALIASES ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRINGSET,
								[ Constants.Z_MULTILINGUALSTRINGSET_VALUE ]: [ Constants.Z_MONOLINGUALSTRINGSET ]
							},
							[ Constants.Z_PERSISTENTOBJECT_DESCRIPTION ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRING,
								[ Constants.Z_MULTILINGUALSTRING_VALUE ]: [ Constants.Z_MONOLINGUALSTRING ]
							}
						};
 
					case Constants.Z_TYPE:
						// Empty type:
						// {
						//  Z1K1: 'Z4',
						//  Z4K1: { Z1K1: 'Z9', Z9K1: 'Z0' },
						//  Z4K2: [ 'Z3' ],
						//  Z4K3: { Z1K1: 'Z9', Z9K1: '' },
						//  Z4K4: { Z1K1: 'Z9', Z9K1: '' },
						//  Z4K5: { Z1K1: 'Z9', Z9K1: '' },
						//  Z4K6: { Z1K1: 'Z9', Z9K1: '' },
						//  Z4K7: [ 'Z46' ]
						//  Z4K8: [ 'Z64' ]
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_TYPE,
							[ Constants.Z_TYPE_IDENTITY ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: Constants.NEW_ZID_PLACEHOLDER
							},
							[ Constants.Z_TYPE_KEYS ]: [ Constants.Z_KEY ],
							[ Constants.Z_TYPE_VALIDATOR ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_TYPE_EQUALITY ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_TYPE_RENDERER ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_TYPE_PARSER ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_TYPE_DESERIALISERS ]: [ Constants.Z_DESERIALISER ],
							[ Constants.Z_TYPE_SERIALISERS ]: [ Constants.Z_SERIALISER ]
						};
 
					case Constants.Z_KEY:
						// Empty type:
						// {
						//  Z1K1: 'Z3',
						//  Z3K1: { Z1K1: 'Z9', Z9K1: '' },
						//  Z3K2: '',
						//  Z3K3: {
						//   Z1K1: 'Z12',
						//   Z12K1: [ 'Z11' ]
						//  }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_TYPE,
							[ Constants.Z_KEY_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							},
							[ Constants.Z_KEY_ID ]: '',
							[ Constants.Z_KEY_LABEL ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_MULTILINGUALSTRING,
								[ Constants.Z_MULTILINGUALSTRING_VALUE ]: [ Constants.Z_MONOLINGUALSTRING ]
							}
						};
 
					case Constants.Z_TYPED_LIST:
						// Empty typed list (canonical):
						// [ { Z1K1: 'Z9', Z9K1: '' } ]
						return [
							{
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							}
						];
 
					case Constants.Z_TYPED_PAIR:
						// Empty typed pair:
						// {
						//  Z1K1: {
						//   Z1K1: Z7,
						//   Z7K1: Z882,
						//   Z882K1: { Z1K1: Z9, Z9K1: '' }
						//   Z882K2: { Z1K1: Z9, Z9K1: '' }
						//  },
						//  K1: {}
						//  K2: {}
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
								[ Constants.Z_FUNCTION_CALL_FUNCTION ]: Constants.Z_TYPED_PAIR,
								[ Constants.Z_TYPED_PAIR_TYPE1 ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								},
								[ Constants.Z_TYPED_PAIR_TYPE2 ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							},
							[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ]: {},
							[ Constants.Z_TYPED_OBJECT_ELEMENT_2 ]: {}
						};
 
					case Constants.Z_TYPED_MAP:
						// Empty typed map:
						// {
						//  Z1K1: {
						//   Z1K1: Z7,
						//   Z7K1: Z883,
						//   Z883K1: { Z1K1: Z9, Z9K1: '' }
						//   Z883K2: { Z1K1: Z9, Z9K1: '' }
						//  }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_FUNCTION_CALL,
								[ Constants.Z_FUNCTION_CALL_FUNCTION ]: Constants.Z_TYPED_MAP,
								[ Constants.Z_TYPED_MAP_TYPE1 ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								},
								[ Constants.Z_TYPED_MAP_TYPE2 ]: {
									[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
									[ Constants.Z_REFERENCE_ID ]: ''
								}
							}
						};
 
					case Constants.Z_BOOLEAN:
						// Empty boolean:
						// {
						//  Z1K1: Z40
						//  Z40K1: { Z1K1: Z9, Z9K1: '' }
						// }
						return {
							[ Constants.Z_OBJECT_TYPE ]: Constants.Z_BOOLEAN,
							[ Constants.Z_BOOLEAN_IDENTITY ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_REFERENCE,
								[ Constants.Z_REFERENCE_ID ]: ''
							}
						};
 
					default:
						return undefined;
				}
			},
			/**
			 * Given the canonical representation of a type,
			 * it generates the payload that will be passed to the
			 * createObjectByType method that will create an initial
			 * scaffolding of an object of that type.
			 *
			 * This accounts for two special cases:
			 * 1) Some types are more commonly persisted and referred to rather
			 * than added literally. When these appear, we instance the
			 * scaffolding of a reference instead of a literal
			 * 2) Typed lists, pairs and maps require some special treatment
			 * to extract the value of their type keys before calling to
			 * createObjectByType
			 *
			 * @param {Object|string} keyType
			 * @return {Object}
			 */
			initializePayloadForType: function ( keyType ) {
				// We detect those types that will be added as references rather than literal objects:
				const type = Constants.LINKED_TYPES.includes( keyType ) ?
					Constants.Z_REFERENCE :
					keyType;
				let payload = { type };
				// We need to hardcode the initialization payload for typed list/pair/map cases:
				if ( type[ Constants.Z_FUNCTION_CALL_FUNCTION ] ) {
					const functionId = type[ Constants.Z_FUNCTION_CALL_FUNCTION ];
					switch ( functionId ) {
						case Constants.Z_TYPED_LIST:
							payload = {
								type: Constants.Z_TYPED_LIST,
								value: type[ Constants.Z_TYPED_LIST_TYPE ]
							};
							break;
						case Constants.Z_TYPED_PAIR:
							payload = {
								type: Constants.Z_TYPED_PAIR,
								values: [
									type[ Constants.Z_TYPED_PAIR_TYPE1 ],
									type[ Constants.Z_TYPED_PAIR_TYPE2 ]
								]
							};
							break;
						case Constants.Z_TYPED_MAP:
							payload = {
								type: Constants.Z_TYPED_MAP,
								values: [
									type[ Constants.Z_TYPED_MAP_TYPE1 ],
									type[ Constants.Z_TYPED_MAP_TYPE2 ]
								]
							};
							break;
					}
				}
				return payload;
			},
			/**
			 * Returns whether the value of zobject after
			 * following the values of the nested properties given
			 * by the array of keys is truthy. If the additional
			 * parameter equals is passed, it returns whether the
			 * final zobject is equal to it.
			 *
			 * @param {Object} zobject
			 * @param {Array} keys
			 * @param {Mixed} equals
			 * @return {boolean}
			 */
			isTruthyOrEqual: function ( zobject, keys = [], equals = undefined ) {
				if ( !zobject ) {
					return false;
				}
				if ( keys.length === 0 ) {
					return equals === undefined ?
						!!zobject :
						zobject === equals;
				}
				const head = keys[ 0 ];
				if ( zobject[ head ] ) {
					const tail = keys.slice( 1 );
					return typeUtils.methods.isTruthyOrEqual( zobject[ head ], tail, equals );
				}
				return false;
			},
			/**
			 * Given a list of available languages for an object metadata
			 * (name, description or alias), it checks the user language and
			 * fallback chain and selects which of the available ones is
			 * the best pick (either the user language one, or the closest
			 * fallback)
			 *
			 * @param {Array} allLanguages
			 * @return {Object|undefined}
			 */
			selectBestLanguage: function ( allLanguages ) {
				/**
				 * @param {Array} chain
				 * @param {Array} availableLangs
				 * @return {Object}
				 */
				function findAvailableLang( chain, availableLangs ) {
					// Iterate through the fallback chain and return
					// the first available language found.
					let foundLang;
					for ( const lang of chain ) {
						foundLang = availableLangs.find( ( langObj ) => {
							return ( langObj.langIsoCode === lang );
						} );
						if ( foundLang !== undefined ) {
							return foundLang;
						}
					}
					return foundLang;
				}
 
				// There are no available languages, return undefined
				if ( allLanguages.length === 0 ) {
					return undefined;
				}
				const fallbackChain = mw.language.getFallbackLanguageChain();
				const availableLang = findAvailableLang( fallbackChain, allLanguages );
				return availableLang || allLanguages[ 0 ];
			}
		}
	};
 
module.exports = typeUtils;