[gnome-maps/wip/mlundblad/wikidata: 2/3] wikipedia: Add function to validate a Wikidata reference
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/wikidata: 2/3] wikipedia: Add function to validate a Wikidata reference
- Date: Mon, 10 Oct 2022 20:37:50 +0000 (UTC)
commit 9525f3947f3d5cb69bcd3c2f89ddf1ed4d88957e
Author: Marcus Lundblad <ml dfupdate se>
Date: Fri Oct 7 23:21:37 2022 +0200
wikipedia: Add function to validate a Wikidata reference
src/wikipedia.js | 12 ++++++++++++
tests/wikipediaTest.js | 10 ++++++++++
2 files changed, 22 insertions(+)
---
diff --git a/src/wikipedia.js b/src/wikipedia.js
index cb4745fd..51c27f65 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -32,6 +32,11 @@ import * as Utils from './utils.js';
*/
const WP_REGEX = /^[a-z][a-z][a-z]?(\-[a-z]+)?$|^simple$/;
+/**
+ * Regex matching Wikidata tags
+ */
+const WIKIDATA_REGEX = /Q\d+/;
+
/**
* Wikidata properties
*/
@@ -79,6 +84,13 @@ export function isValidWikipedia(wiki) {
return wpCode.match(WP_REGEX) !== null;
}
+/**
+ * Determine if a Wikidata reference tag is valid (of the form Qnnn)
+ */
+export function isValidWikidata(wikidata) {
+ return wikidata.match(WIKIDATA_REGEX) !== null;
+}
+
/*
* Fetch various metadata about a Wikipedia article, given the wiki language
* and article title.
diff --git a/tests/wikipediaTest.js b/tests/wikipediaTest.js
index 29e6086a..09212f23 100644
--- a/tests/wikipediaTest.js
+++ b/tests/wikipediaTest.js
@@ -38,3 +38,13 @@ JsUnit.assertTrue(Wikipedia.isValidWikipedia('zh-yue:粵文維基百科'));
// invalid references
JsUnit.assertFalse(Wikipedia.isValidWikipedia('https://en.wikipedia.org/wiki/Article'));
JsUnit.assertFalse(Wikipedia.isValidWikipedia('Article with no edition'));
+
+// valid wikidata references
+JsUnit.assertTrue(Wikipedia.isValidWikidata('Q1234'));
+JsUnit.assertTrue(Wikipedia.isValidWikidata('Q1'));
+JsUnit.assertTrue(Wikipedia.isValidWikidata('Q100000000'));
+
+// invalid wikidata references
+JsUnit.assertFalse(Wikipedia.isValidWikidata('1234'));
+JsUnit.assertFalse(Wikipedia.isValidWikidata('AAAA'));
+JsUnit.assertFalse(Wikipedia.isValidWikidata('en:Article'));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]