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

83.58% Statements 56/67
91.66% Branches 44/48
64.7% Functions 11/17
83.58% Lines 56/67

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                  54x 54x                 1487x 16x 1471x 493x 62x 431x 424x   7x     978x 859x   119x                         8712x 666x     8046x 96x 191x     191x     7950x 23984x     7950x 1556x   6394x                     33x                                                                                             8x 1x   7x 3x 4x 1x   3x   2x   1x         9x   9x 19x 5x 14x 5x       9x     1072x                           21x 10x   11x     11x   5x 5x     4x 4x     2x 2x                   11x                                             45x                                                                 45x           54x  
/**
 * 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';
 
var Constants = require( '../Constants.js' ),
	typeUtils = {
		methods: {
			/**
			 * 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 {
					var 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 );
				} );
			},
			zObjectToString: function ( zObject ) {
				if ( typeof zObject === 'undefined' ) {
					return '';
				}
				if ( typeof zObject === 'string' ) {
					return zObject;
				} else if ( Array.isArray( zObject ) ) {
					return '[ ' + zObject.map( typeUtils.methods.zObjectToString ).join( ', ' ) + ' ]';
				} else {
					switch ( zObject.Z1K1 ) {
						case Constants.Z_BOOLEAN:
							return zObject[ Constants.Z_BOOLEAN_IDENTITY ] === Constants.Z_BOOLEAN_TRUE;
						default:
							return JSON.stringify( zObject );
					}
				}
			},
			typedListToArray( typedList, array ) {
				array = array || [];
 
				for ( var item in typedList ) {
					if ( item === Constants.Z_TYPED_OBJECT_ELEMENT_1 ) {
						array.push( typedList[ item ] );
					} else if ( item === Constants.Z_TYPED_OBJECT_ELEMENT_2 ) {
						typeUtils.methods.typedListToArray( typedList[ item ], array );
					}
				}
 
				return array;
			},
			isFunctionItemAttached( item, attachedItems ) {
				return attachedItems.indexOf( item ) > -1;
			},
 
			/**
			 * 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 and the arguments in
			 * brackets.
			 *
			 * @param {Object|string} type
			 * @return {string}
			 */
			typeToString: function ( type ) {
				if ( typeof type === 'string' ) {
					return type;
				} else {
					const mode = typeUtils.methods.typeToString( type[ Constants.Z_OBJECT_TYPE ] );
					let typeString;
 
					switch ( mode ) {
						case Constants.Z_REFERENCE:
							typeString = type[ Constants.Z_REFERENCE_ID ];
							break;
 
						case Constants.Z_FUNCTION_CALL:
							typeString = type[ Constants.Z_FUNCTION_CALL_FUNCTION ];
							break;
 
						case Constants.Z_TYPE:
							typeString = type[ Constants.Z_TYPE_IDENTITY ];
							break;
 
						case Constants.Z_ARGUMENT_REFERENCE:
							typeString = type[ Constants.Z_ARGUMENT_REFERENCE_KEY ];
							break;
 
						default:
							typeString = undefined;
					}
 
					return ( typeof typeString === 'object' ) ?
						typeUtils.methods.typeToString( typeString ) :
						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)
			 *
			 * FIXME: Once we deprecate the old code from changeType, getScaffolding
			 * should not return undefined, but have the Empty object (Z1) return as
			 * default case.
			 *
			 * @param {string} type
			 * @return {Object|Array}
			 */
			getScaffolding: function ( type ) {
				switch ( type ) {
					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_MONOLINGUALSTRING:
						// Empty monolingual string:
						// {
						//  Z1K1: Z11
						//  Z11K1: { Z1K1: Z9, Z9K1: '' }
						//  Z11K2: { Z1K1: Z6, Z6K1: '' }
						// }
						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 ]: {
								[ Constants.Z_OBJECT_TYPE ]: Constants.Z_STRING,
								[ Constants.Z_STRING_VALUE ]: ''
							}
						};
 
					default:
						return undefined;
				}
			}
		}
	};
 
module.exports = typeUtils;