Skip to content

Commit

Permalink
Minor (#12)
Browse files Browse the repository at this point in the history
* Decoupling clean ups and minor improvements

* Update tests to use the Wikilook global directly instead.

* Minor doc fixes.

* Remove redundant "$()" wrapping in wikilookup.js jQuery adapter.
  The "this" object must be a jQuery object already.

Ref #6.

* jquery: Make $.fn.wikilookup() a chainable method

Enables things like:

```
$('foo')
	.some()
	.wikilookup()
	.thing()
```
  • Loading branch information
Krinkle authored Oct 5, 2020
1 parent 9037cd7 commit 43bebf4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/js/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -41,7 +41,7 @@
this.standardURLs.api;
}

this.isWikipedia = !!this.baseURL.match( /https:\/\/(.+)\.wikipedia.org/ );
this.isWikipedia = !!this.baseURL.match( /https:\/\/(.+)\.wikipedia\.org/ );
};

/**
Expand Down
7 changes: 5 additions & 2 deletions src/js/wikilookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
12 changes: 6 additions & 6 deletions tests/test.Api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
( function () {
QUnit.module( '$.wikilookup.Api' );
QUnit.module( 'Wikilookup.Api' );

QUnit.test( 'getApiUrl', function ( assert ) {
var cases = [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":"<p><b>Physics</b> 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.</p>"}
};

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,
Expand Down
8 changes: 4 additions & 4 deletions tests/test.Processor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
( function () {
QUnit.module( '$.wikilookup.Processor' );
QUnit.module( 'Wikilookup.Processor' );

QUnit.test( 'initialization and getAllTerms', function ( assert ) {
var cases = [
Expand All @@ -24,7 +24,7 @@
];

cases.forEach( function ( testCase ) {
var processor = new $.wikilookup.Processor(
var processor = new Wikilookup.Processor(
$( $.parseHTML( testCase.html ) ),
testCase.config
);
Expand All @@ -39,7 +39,7 @@

QUnit.test( 'sources', function ( assert ) {
var source,
processor = new $.wikilookup.Processor( $( '<div>' ), {
processor = new Wikilookup.Processor( $( '<div>' ), {
sources: {
one: { lang: 'es' },
two: { baseURL: 'https://{{lang}}.privatewiki.com/w/api.php', lang: 'foo' },
Expand All @@ -55,7 +55,7 @@

source = processor.getSource( 'one' );
assert.ok(
source instanceof $.wikilookup.Api,
source instanceof Wikilookup.Api,
'API details instantiated.'
);
} );
Expand Down
4 changes: 2 additions & 2 deletions tests/test.tools.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
( function () {
QUnit.module( '$.wikilookup.tools' );
QUnit.module( 'Wikilookup.tools' );

QUnit.test( 'getPropValue', function ( assert ) {
var cases = [
Expand Down Expand Up @@ -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
);
Expand Down

0 comments on commit 43bebf4

Please sign in to comment.