All files / ext.wikilambda.app/store/modules/wikidata lexemes.js

100% Statements 144/144
95.65% Branches 22/23
100% Functions 12/12
100% Lines 144/144

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 1457x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 8x 8x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 7x 7x  
/*!
 * WikiLambda Vue editor: Vuex Wikidata Lexemes 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 = {
	state: {
		lexemes: {}
	},
	getters: {
		/**
		 * Returns the lexeme object of a given ID,
		 * the fetch Promise if the fetch request is
		 * on the fly, or undefined if it hasn't been
		 * requested yet.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getLexemeData: function ( state ) {
			/**
			 * @param {string} id
			 * @return {Object|Promise|undefined}
			 */
			function findLexemeData( id ) {
				return state.lexemes[ id ];
			}
			return findLexemeData;
		},
		/**
		 * Returns the lexeme form object of a given ID,
		 * the fetch Promise if the fetch request is
		 * on the fly, or undefined if it hasn't been
		 * requested yet.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getLexemeFormData: function ( state ) {
			/**
			 * @param {string} id
			 * @return {Object|Promise|undefined}
			 */
			function findLexemeFormData( id ) {
				const [ lexemeId ] = id.split( '-' );
				const lexemeData = state.lexemes[ lexemeId ];
				return ( lexemeData && lexemeData.forms ) ?
					lexemeData.forms.find( ( item ) => item.id === id ) :
					undefined;
			}
			return findLexemeFormData;
		},
		/**
		 * Given the rowId of the Wikidata Lexeme entity
		 * returns the rowId of the Lexeme Id string.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getLexemeIdRow: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Object|undefined}
			 */
			function findLexemeId( rowId ) {
				return getters.getWikidataEntityIdRow( rowId, Constants.Z_WIKIDATA_LEXEME );
			}
			return findLexemeId;
		},
		/**
		 * Given the rowId of the Wikidata Lexeme Form entity
		 * returns the rowId of the Lexeme Form Id string.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getLexemeFormIdRow: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Object|undefined}
			 */
			function findLexemeFormId( rowId ) {
				return getters.getWikidataEntityIdRow( rowId, Constants.Z_WIKIDATA_LEXEME_FORM );
			}
			return findLexemeFormId;
		}
	},
	mutations: {
		/**
		 * Stores the lexeme data indexed by its Id
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.id
		 * @param {Object} payload.data
		 */
		setLexemeData: function ( state, payload ) {
			// Select only subset of Lexeme data; title, forms and lemmas
			const unwrap = ( ( { title, forms, lemmas } ) => ( { title, forms, lemmas } ) );
			state.lexemes[ payload.id ] = unwrap( payload.data );
		}
	},
	actions: {
		/**
		 * Calls Wikidata Action API to fetch Wikidata Lexemes
		 * given their Ids.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Array} payload.ids
		 * @return {Promise}
		 */
		fetchLexemes: function ( context, payload ) {
			// Filter out the fetched or fetching lexeme Ids
			const ids = payload.ids.filter( ( id ) => context.getters.getLexemeData( id ) === undefined );
			if ( ids.length === 0 ) {
				// If list is empty, do nothing
				return;
			}
			const request = {
				language: context.getters.getUserLangCode,
				ids: ids.join( '|' )
			};
			const promise = apiUtils.fetchWikidataEntities( request )
				.then( ( data ) => {
					// Once received, store lexeme Ids with their data
					const fetched = data.entities ? Object.keys( data.entities ) : [];
					fetched.forEach( ( id ) => context.commit( 'setLexemeData', { id, data: data.entities[ id ] } ) );
					return data;
				} );
 
			// Store lexeme Ids with their resolving promise
			ids.forEach( ( id ) => context.commit( 'setLexemeData', { id, data: promise } ) );
			return promise;
		}
	}
};