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

98.41% Statements 62/63
83.33% Branches 5/6
100% Functions 3/3
98.41% Lines 62/63

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 647x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 15x 15x 15x 15x 15x 15x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 2x 2x 2x   1x 1x 7x 7x  
/**
 * WikiLambda Vue editor: miscellaneous utilities mixins
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
module.exports = exports = {
	methods: {
		/**
		 * Get the text of the edit summary message for when changes are made to a Function
		 *
		 * @param {string} message The key of the message to fetch
		 * @param {string[]} ZIDs The ZIDs of the affected linked Implementations or Testers
		 * @return {string} The rendered message
		 */
		createConnectedItemsChangesSummaryMessage: function ( message, ZIDs ) {
			// Messages that can be used here:
			// * wikilambda-updated-implementations-approved-summary
			// * wikilambda-updated-implementations-deactivated-summary
			// * wikilambda-updated-testers-approved-summary
			// * wikilambda-updated-testers-deactivated-summary
			return mw.message( message ).params( [ mw.language.listToText( ZIDs ) ] ).text();
		},
 
		/**
		 * Safely retrieves the value of a nested property within an object.
		 *
		 * This function will not throw an error if any part of the path is `null` or `undefined`.
		 *
		 * @param {Object} obj - The object from which to retrieve the property.
		 * @param {string} path - The path to the desired property, specified as a string with dot notation.
		 * @return {*} - The value of the nested property, or `undefined`
		 * if any part of the path is `null` or `undefined`.
		 *
		 * @example
		 *
		 * const error = {
		 *     error: {
		 *         message: 'Something went wrong!'
		 *     }
		 * };
		 *
		 * const message = getNestedProperty(error, 'error.message');
		 * console.log(message); // Output: 'Something went wrong!'
		 *
		 * const code = getNestedProperty(error, 'error.code');
		 * console.log(code); // Output: undefined
		 *
		 * const nonExistent = getNestedProperty(null, 'error.message');
		 * console.log(nonExistent); // Output: undefined
		 */
		getNestedProperty: function ( obj, path ) {
			return path.split( '.' ).reduce( ( acc, part ) => {
				if ( acc && acc[ part ] !== undefined && acc[ part ] !== null ) {
					return acc[ part ];
				}
				return undefined;
			}, obj );
		}
	}
};