diff --git a/src/js/Api.js b/src/js/Api.js index b978c15..671e759 100644 --- a/src/js/Api.js +++ b/src/js/Api.js @@ -6,14 +6,14 @@ * * @constructor * @param {Object} [config] Configuration options - * @param {string} [config.lang] Language parameter, for dynamic base URLs {{lang}} variable - * @param {string} [config.useRestbase] Use the restbase structure and expected response from the API. + * @param {string} [config.lang='en'] Language parameter, for dynamic base URLs `{{lang}}` variable + * @param {string} [config.useRestbase=false] Use the restbase structure and expected response from the API. * Default value (false) means the expected API response and sent parameters conform to MediaWiki's internal * API behavior. * @param {string} [config.baseURL] A set base url for the API endpoint. Can include two parameters that - * will be used: - * - '{{lang}}' Chosen language, in case the domain has variants, like https://{{lang}}.wikipedia.org - * - '{{pageName}}' Requested page name will be placed in that URL entrypoint. This is mostly relevant + * will be substituted: + * - `{{lang}}` Chosen language, in case the domain has variants, like `https://{{lang}}.wikipedia.org` + * - `{{pageName}}` Requested page name will be placed in that URL entrypoint. This is mostly relevant * for restbase, which adds the page name parameter at the end of the request URL */ var Api = function ( config ) { @@ -41,7 +41,7 @@ this.standardURLs.api; } - this.isWikipedia = !!this.baseURL.match( /https:\/\/(.+)\.wikipedia.org/ ); + this.isWikipedia = !!this.baseURL.match( /https:\/\/(.+)\.wikipedia\.org/ ); }; /** diff --git a/src/js/wikilookup.js b/src/js/wikilookup.js index 8e968ca..e41ff15 100644 --- a/src/js/wikilookup.js +++ b/src/js/wikilookup.js @@ -4,11 +4,14 @@ * * @param {Object} [options] Configuration options * See Wikilookup.Processor for configuration options + * @return {jQuery} */ $.fn.wikilookup = function ( options ) { - var processor = new Wikilookup.Processor( $( this ), options ); + var processor = new Wikilookup.Processor( this, options ); // Store instance - $( this ).data( 'wl-processor', processor ); + this.data( 'wl-processor', processor ); + + return this; }; }( jQuery ) ); diff --git a/tests/test.Api.js b/tests/test.Api.js index d13ccd4..4cbcc0d 100644 --- a/tests/test.Api.js +++ b/tests/test.Api.js @@ -1,5 +1,5 @@ ( function () { - QUnit.module( '$.wikilookup.Api' ); + QUnit.module( 'Wikilookup.Api' ); QUnit.test( 'getApiUrl', function ( assert ) { var cases = [ @@ -48,7 +48,7 @@ ]; cases.forEach( function ( testCase ) { - var api = new $.wikilookup.Api( { baseURL: testCase.baseURL } ); + var api = new Wikilookup.Api( { baseURL: testCase.baseURL } ); assert.deepEqual( api.getApiUrl( testCase.pageName, testCase.lang ), testCase.expected, @@ -156,7 +156,7 @@ ]; cases.forEach( function ( testCase ) { - var api = new $.wikilookup.Api( testCase.construct ); + var api = new Wikilookup.Api( testCase.construct ); assert.deepEqual( api.getApiParams( testCase.pageName ), testCase.expected, @@ -196,7 +196,7 @@ ]; cases.forEach( function ( testCase ) { - var api = new $.wikilookup.Api(); + var api = new Wikilookup.Api(); assert.deepEqual( api.getCacheKey( testCase.pageName, testCase.lang ), testCase.expected, @@ -256,14 +256,14 @@ restbase: {"type":"standard","title":"Physics","displaytitle":"Physics","namespace":{"id":0,"text":""},"wikibase_item":"Q413","titles":{"canonical":"Physics","normalized":"Physics","display":"Physics"},"pageid":22939,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/CollageFisica.jpg/320px-CollageFisica.jpg","width":320,"height":231},"originalimage":{"source":"https://upload.wikimedia.org/wikipedia/commons/9/9a/CollageFisica.jpg","width":831,"height":600},"lang":"en","dir":"ltr","revision":"902734855","tid":"f71eb4a0-9785-11e9-80a0-4fab8e21055b","timestamp":"2019-06-20T21:42:30Z","description":"Study of the fundamental properties of matter and energy","content_urls":{"desktop":{"page":"https://en.wikipedia.org/wiki/Physics","revisions":"https://en.wikipedia.org/wiki/Physics?action=history","edit":"https://en.wikipedia.org/wiki/Physics?action=edit","talk":"https://en.wikipedia.org/wiki/Talk:Physics"},"mobile":{"page":"https://en.m.wikipedia.org/wiki/Physics","revisions":"https://en.m.wikipedia.org/wiki/Special:History/Physics","edit":"https://en.m.wikipedia.org/wiki/Physics?action=edit","talk":"https://en.m.wikipedia.org/wiki/Talk:Physics"}},"api_urls":{"summary":"https://en.wikipedia.org/api/rest_v1/page/summary/Physics","metadata":"https://en.wikipedia.org/api/rest_v1/page/metadata/Physics","references":"https://en.wikipedia.org/api/rest_v1/page/references/Physics","media":"https://en.wikipedia.org/api/rest_v1/page/media/Physics","edit_html":"https://en.wikipedia.org/api/rest_v1/page/html/Physics","talk_page_html":"https://en.wikipedia.org/api/rest_v1/page/html/Talk:Physics"},"extract":"Physics is the natural science that studies matter, its motion and behavior through space and time, and that studies the related entities of energy and force. Physics is one of the most fundamental scientific disciplines, and its main goal is to understand how the universe behaves.","extract_html":"

Physics is the natural science that studies matter, its motion and behavior through space and time, and that studies the related entities of energy and force. Physics is one of the most fundamental scientific disciplines, and its main goal is to understand how the universe behaves.

"} }; - api = new $.wikilookup.Api( { useRestbase: true } ); + api = new Wikilookup.Api( { useRestbase: true } ); assert.deepEqual( api.processApiResult( apiResults.restbase ), expected.restbase, 'Resetbase result' ); - api = new $.wikilookup.Api( { useRestbase: false } ); + api = new Wikilookup.Api( { useRestbase: false } ); assert.deepEqual( api.processApiResult( apiResults.api ), expected.api, diff --git a/tests/test.Processor.js b/tests/test.Processor.js index f3d39c6..1c0973b 100644 --- a/tests/test.Processor.js +++ b/tests/test.Processor.js @@ -1,5 +1,5 @@ ( function () { - QUnit.module( '$.wikilookup.Processor' ); + QUnit.module( 'Wikilookup.Processor' ); QUnit.test( 'initialization and getAllTerms', function ( assert ) { var cases = [ @@ -24,7 +24,7 @@ ]; cases.forEach( function ( testCase ) { - var processor = new $.wikilookup.Processor( + var processor = new Wikilookup.Processor( $( $.parseHTML( testCase.html ) ), testCase.config ); @@ -39,7 +39,7 @@ QUnit.test( 'sources', function ( assert ) { var source, - processor = new $.wikilookup.Processor( $( '
' ), { + processor = new Wikilookup.Processor( $( '
' ), { sources: { one: { lang: 'es' }, two: { baseURL: 'https://{{lang}}.privatewiki.com/w/api.php', lang: 'foo' }, @@ -55,7 +55,7 @@ source = processor.getSource( 'one' ); assert.ok( - source instanceof $.wikilookup.Api, + source instanceof Wikilookup.Api, 'API details instantiated.' ); } ); diff --git a/tests/test.tools.js b/tests/test.tools.js index aabb52b..a1de650 100644 --- a/tests/test.tools.js +++ b/tests/test.tools.js @@ -1,5 +1,5 @@ ( function () { - QUnit.module( '$.wikilookup.tools' ); + QUnit.module( 'Wikilookup.tools' ); QUnit.test( 'getPropValue', function ( assert ) { var cases = [ @@ -37,7 +37,7 @@ cases.forEach( function ( testCase ) { assert.deepEqual( - $.wikilookup.tools.getPropValue( testCase.obj, testCase.props ), + Wikilookup.tools.getPropValue( testCase.obj, testCase.props ), testCase.expected, testCase.msg );