All files / mobile.startup/page pageJSONParser.js

86.66% Statements 13/15
80% Branches 16/20
100% Functions 1/1
86.66% Lines 13/15

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 521x 1x                     34x 34x     34x   34x           34x       34x   34x 2x       34x         34x                   1x  
const Page = require( '../Page' );
const util = require( '../util' );
 
/**
 * Create a Page object from an API response.
 *
 * @memberof Page
 * @param {Object} resp as representing a page in the API
 * @return {Page}
 */
function parse( resp ) {
	var revision, displayTitle,
		thumb = resp.thumbnail,
		pageprops = resp.pageprops || {
			displaytitle: mw.html.escape( resp.title )
		},
		terms = resp.terms || resp.entityterms;
 
	Eif ( pageprops || terms ) {
		// The label is either the display title or the label pageprop
		// (the latter used by Wikidata)
		// Long term we want to consolidate these.
		// Note that pageprops.displaytitle is HTML, while
		// terms.label[0] is plain text.
		displayTitle = terms && terms.label ?
			mw.html.escape( terms.label[0] ) : pageprops.displaytitle;
	}
	// Add Wikidata descriptions if available (T101719)
	resp.wikidataDescription = resp.description || undefined;
 
	if ( thumb ) {
		resp.thumbnail.isLandscape = thumb.width > thumb.height;
	}
 
	// page may or may not exist.
	Iif ( resp.revisions && resp.revisions[0] ) {
		revision = resp.revisions[0];
		resp.lastModified = new Date( revision.timestamp );
	}
 
	return new Page(
		util.extend( resp, {
			id: resp.pageid,
			isMissing: !!resp.missing,
			url: mw.util.getUrl( resp.title ),
			displayTitle // this is HTML!
		} )
	);
}
 
module.exports = { parse };