All files / ext.wikilambda.edit/store/modules zFunction.js

97.93% Statements 95/97
80% Branches 16/20
100% Functions 39/39
97.84% Lines 91/93

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              18x 18x   18x                               3x       3x       3x                                 299x       299x 18x   281x   281x   190x                                 121x 121x 121x 186x       186x     186x   121x     121x                                 136x         38x                                 125x 125x   25x                             36x 36x   4x                                   140x       140x     140x 107x 107x   140x   42x                             5x 11x         11x     11x 12x                               5x 11x         11x     11x 12x                                 3x   3x         3x     3x   1x 1x                               3x   3x         3x     3x   1x 1x                               3x     3x       3x 3x 3x 3x       3x 3x 3x   3x   3x   1x 1x                           3x     3x       3x 3x 3x 3x       3x 3x 3x   3x   3x   1x 1x                       5x       10x 5x 5x                       5x       10x 5x 5x          
/*!
 * WikiLambda Vue editor: Function Editor Vuex module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../Constants.js' ),
	apiUtils = require( '../../mixins/api.js' ).methods;
 
module.exports = exports = {
	getters: {
		/**
		 * Returns the function Zid of a given function given
		 * the function rowId. If not set, returns undefined
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionIdentity: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {string | undefined}
			 */
			function findIdentity( rowId = 0 ) {
				const functionZid = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_IDENTITY
				], rowId );
				return functionZid ?
					getters.getZReferenceTerminalValue( functionZid.id ) :
					undefined;
			}
			return findIdentity;
		},
		/**
		 * Returns the list of input rows for the function given
		 * given the root object rowId. If no rowId given, starts
		 * from 0, which is safe to use.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionInputs: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			function findZFunctionInputs( rowId = 0 ) {
				const inputsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_ARGUMENTS
				], rowId );
				if ( inputsRow === undefined ) {
					return [];
				}
				const inputs = getters.getChildrenByParentRowId( inputsRow.id );
				// Remove benjamin type item
				return inputs.slice( 1 );
			}
			return findZFunctionInputs;
		},
		/**
		 * Returns the array of language Zids that are present
		 * in a function's input labels. If no arguments or no
		 * labels, returns an empty array.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionInputLangs: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			function findInputLangs( rowId = 0 ) {
				let languages = [];
				const inputs = getters.getZFunctionInputs( rowId );
				for ( const inputRow of inputs ) {
					const inputLabelsRow = getters.getRowByKeyPath( [
						Constants.Z_ARGUMENT_LABEL,
						Constants.Z_MULTILINGUALSTRING_VALUE
					], inputRow.id );
					const inputLangs = inputLabelsRow ?
						getters.getZMultilingualLanguageList( inputLabelsRow.id ) :
						[];
					languages = languages.concat( inputLangs );
				}
				return languages;
			}
 
			return findInputLangs;
		},
		/**
		 * Returns the output type row for the function given
		 * given the root object rowId. If no rowId given, starts
		 * from 0, which is safe to use.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionOutput: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Object|undefined}
			 */
			function findZFunctionOutput( rowId = 0 ) {
				return getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_RETURN_TYPE
				], rowId );
			}
			return findZFunctionOutput;
		},
		/**
		 * Returns the input type row of an input or
		 * argument declaration, given the rowId of the
		 * argument/Z17 object.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentTypeRowId: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {number|undefined}
			 */
			function findArgumentType( rowId ) {
				const argType = getters.getRowByKeyPath( [ Constants.Z_ARGUMENT_TYPE ], rowId );
				return argType ? argType.id : undefined;
			}
			return findArgumentType;
		},
		/**
		 * Returns the terminal string value of the input key
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentKey: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {string|undefined}
			 */
			function findArgumentType( rowId ) {
				const argKey = getters.getRowByKeyPath( [ Constants.Z_ARGUMENT_KEY ], rowId );
				return argKey ? getters.getZStringTerminalValue( argKey.id ) : undefined;
			}
			return findArgumentType;
		},
		/**
		 * Returns the input label row of an input or
		 * argument declaration, given the rowId of the
		 * argument/Z17 object and the language Zid.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentLabelForLanguage: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @param {string} lang
			 * @return {Object|undefined}
			 */
			function findArgumentLabel( rowId, lang ) {
				const argLabelsRow = getters.getRowByKeyPath( [
					Constants.Z_ARGUMENT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				], rowId );
				const argLabels = argLabelsRow ?
					getters.getChildrenByParentRowId( argLabelsRow.id ).slice( 1 ) :
					[];
				const argLabel = argLabels.find( ( monolingual ) => {
					const langZid = getters.getZMonolingualLangValue( monolingual.id );
					return langZid === lang;
				} );
				return argLabel;
			}
			return findArgumentLabel;
		},
		/**
		 * Returns ZIDs for testers connected to the root function.
		 * Note that this returns an array of only items, without the type from index 0.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getConnectedTests: function ( state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Array}
			 */
			return function ( rowId ) {
				const testsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_TESTERS
				], rowId );
 
				Iif ( !testsRow ) {
					return [];
				}
				const childRows = getters.getChildrenByParentRowId( testsRow.id ).slice( 1 );
				return childRows.map( ( row ) => getters.getZReferenceTerminalValue( row.id ) );
			};
		},
		/**
		 * Returns ZIDs for implementations connected to the root function.
		 * Note that this returns an array of only items, without the type from index 0.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getConnectedImplementations: function ( state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			return function ( rowId ) {
				const implementationsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_IMPLEMENTATIONS
				], rowId );
 
				Iif ( !implementationsRow ) {
					return [];
				}
				const childRows = getters.getChildrenByParentRowId( implementationsRow.id ).slice( 1 );
				return childRows.map( ( row ) => getters.getZReferenceTerminalValue( row.id ) );
			};
		}
	},
	actions: {
		/**
		 * Adds the given tests to the current function's list of
		 * of connected tests and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the tests to connect
		 * @return {Promise}
		 */
		connectTests: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_TESTERS
			], payload.rowId );
 
			return context
				.dispatch( 'pushValuesToList', { rowId: listRow.id, values: payload.zids } )
				.then( () => {
					return context.dispatch( 'submitZObject', '' ).catch( ( e ) => {
						// Reset old ZObject if something failed
						context.commit( 'setZObject', zobjectCopy );
						throw e;
					} );
				} );
		},
		/**
		 * Adds the given implementations to the current function's list of
		 * of connected implementations and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the implementations to connect
		 * @return {Promise}
		 */
		connectImplementations: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_IMPLEMENTATIONS
			], payload.rowId );
 
			return context
				.dispatch( 'pushValuesToList', { rowId: listRow.id, values: payload.zids } )
				.then( () => {
					return context.dispatch( 'submitZObject', '' ).catch( ( e ) => {
						// Reset old ZObject if something failed
						context.commit( 'setZObject', zobjectCopy );
						throw e;
					} );
				} );
		},
		/**
		 * Removes the given tests from the the current function's list of
		 * of connected tests and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the tests to disconnect
		 * @return {Promise}
		 */
		disconnectTests: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			// Find the item rows to delete from the list
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_TESTERS
			], payload.rowId );
			const listItems = context.getters.getChildrenByParentRowId( listRow.id ).slice( 1 );
			const deleteRows = listItems.filter( ( row ) => {
				const reference = context.getters.getZReferenceTerminalValue( row.id );
				return payload.zids.includes( reference );
			} );
 
			// Delete items from the list and recalculate the list keys
			for ( const row of deleteRows ) {
				context.dispatch( 'removeRowChildren', row.id );
				context.dispatch( 'removeRow', row.id );
			}
			context.dispatch( 'recalculateTypedListKeys', listRow.id );
 
			return context.dispatch( 'submitZObject', '' ).catch( ( e ) => {
				// Reset old ZObject if something failed
				context.commit( 'setZObject', zobjectCopy );
				throw e;
			} );
		},
		/**
		 * Removes the given implementations from the the current function's list of
		 * of connected implementations and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the implementations to disconnect
		 * @return {Promise}
		 */
		disconnectImplementations: function ( context, payload ) {
			const zobjectCopy = context.getters.getZObjectCopy;
 
			// Find the item rows to delete from the list
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_IMPLEMENTATIONS
			], payload.rowId );
			const listItems = context.getters.getChildrenByParentRowId( listRow.id ).slice( 1 );
			const deleteRows = listItems.filter( ( row ) => {
				const reference = context.getters.getZReferenceTerminalValue( row.id );
				return payload.zids.includes( reference );
			} );
 
			// Delete items from the list and recalculate the list keys
			for ( const row of deleteRows ) {
				context.dispatch( 'removeRowChildren', row.id );
				context.dispatch( 'removeRow', row.id );
			}
			context.dispatch( 'recalculateTypedListKeys', listRow.id );
 
			return context.dispatch( 'submitZObject', '' ).catch( ( e ) => {
				// Reset old ZObject if something failed
				context.commit( 'setZObject', zobjectCopy );
				throw e;
			} );
		},
		/**
		 * Triggers the fetch of all (connected and disconnected) tests for the i
		 * given function Zid. Also fetches other relevant object Zids.
		 *
		 * @param {Object} context
		 * @param {string} functionZid
		 * @return {Promise}
		 */
		fetchTests: function ( context, functionZid ) {
			return apiUtils.fetchFunctionObjects( {
				functionZid,
				type: Constants.Z_TESTER
			} ).then( ( items ) => {
				const zids = items.map( ( item ) => item.zid );
				context.dispatch( 'fetchZids', { zids } );
				return zids;
			} );
		},
		/**
		 * Triggers the fetch of all (connected and disconnected) implementations for the given
		 * functionZid. Also fetches other relevant object Zids.
		 *
		 * @param {Object} context
		 * @param {string} functionZid
		 * @return {Promise}
		 */
		fetchImplementations: function ( context, functionZid ) {
			return apiUtils.fetchFunctionObjects( {
				functionZid,
				type: Constants.Z_IMPLEMENTATION
			} ).then( ( items ) => {
				const zids = items.map( ( item ) => item.zid );
				context.dispatch( 'fetchZids', { zids } );
				return zids;
			} );
		}
	}
};