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

100% Statements 74/74
100% Branches 12/12
100% Functions 4/4
100% Lines 74/74

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 757x 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 47x 60x 39x 39x 21x 47x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 190x 190x 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 );
		},
 
		/**
		 * Check if two arrays are equal.
		 *
		 * @param {Array} arr1 - The first array to compare.
		 * @param {Array} arr2 - The second array to compare.
		 * @return {boolean} - True if the arrays are equal, false otherwise.
		 */
		arraysAreEqual: function ( arr1, arr2 ) {
			return arr1.length === arr2.length && arr1.every( ( value, index ) => value === arr2[ index ] );
		}
	}
};