Comparing version 1.4.1 to 1.5.0
@@ -299,7 +299,7 @@ 'use strict'; | ||
add_test('get list of categorymembers using for_each', async (assert, setup_test, finish_test) => { | ||
setup_test('get list of [[w:ja:Category:Wikimedia Cloud Services]]'); | ||
setup_test('get list of [[w:en:Category:Wikimedia Cloud Services]] using for_each'); | ||
const wiki = new Wikiapi('en'); | ||
let has_category_count = 0; | ||
const list_proto = await wiki.for_each('categorymembers', 'Wikimedia Cloud Services', async function (category) { | ||
const page_list_proto = await wiki.for_each('categorymembers', 'Wikimedia Cloud Services', async function (category) { | ||
const page_data = await wiki.page(category); | ||
@@ -310,9 +310,40 @@ const parsed = page_data.parse(); | ||
//console.log(parsed); | ||
parsed.each('category', (token) => { if (token.name === 'Wikimedia Cloud Services') { has_category_count++; return to_exit; } }); | ||
parsed.each('category', (token) => { | ||
if (token.name === 'Wikimedia Cloud Services') { | ||
has_category_count++; | ||
return to_exit; | ||
} | ||
}); | ||
}); | ||
//console.log(list_proto); | ||
//console.log([list_proto.length, has_category_count]); | ||
//console.log(page_list_proto); | ||
//console.log([page_list_proto.length, has_category_count]); | ||
assert([list_proto.length, has_category_count], 'Count of [[w:ja:Category:Wikimedia Cloud Services]]'); | ||
finish_test('get list of [[w:ja:Category:Wikimedia Cloud Services]]'); | ||
assert([page_list_proto.length, has_category_count], 'Count of [[w:en:Category:Wikimedia Cloud Services]] using for_each'); | ||
finish_test('get list of [[w:en:Category:Wikimedia Cloud Services]] using for_each'); | ||
}); | ||
add_test('get list of categorymembers using for_each_page', async (assert, setup_test, finish_test) => { | ||
setup_test('get list of [[w:en:Category:Wikimedia Cloud Services]] using for_each_page'); | ||
const wiki = new Wikiapi('en'); | ||
let has_category_count = 0; | ||
const page_list = await wiki.categorymembers('Wikimedia Cloud Services'); | ||
await wiki.for_each_page(page_list, (page_data) => { | ||
const parsed = page_data.parse(); | ||
//console.log(parsed); | ||
assert([CeL.wiki.content_of(page_data), parsed.toString()], 'parser check'); | ||
let has_category; | ||
parsed.each('category', (token) => { | ||
if (token.name === 'Wikimedia Cloud Services') { | ||
has_category = true; | ||
} | ||
}); | ||
if (has_category) { | ||
has_category_count++; | ||
} | ||
}); | ||
//console.log([page_list.length, has_category_count]); | ||
assert([page_list.length, has_category_count], 'Count of [[w:en:Category:Wikimedia Cloud Services]] using for_each_page'); | ||
finish_test('get list of [[w:en:Category:Wikimedia Cloud Services]] using for_each_page'); | ||
}); |
{ | ||
"name": "wikiapi", | ||
"title": "JavaScript MediaWiki API for node.js", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Simple way to access MediaWiki API via JavaScript with simple wikitext parser.", | ||
@@ -6,0 +6,0 @@ "keywords": [ "MediaWiki", "MediaWiki API", "wikitext", "ECMAScript 2017" ], |
@@ -36,4 +36,3 @@ 'use strict'; | ||
/** | ||
* wikiapi operator 操作子. | ||
/** * wikiapi operator 操作子. | ||
* | ||
@@ -233,4 +232,51 @@ * @param {String}[API_URL] language code or API URL of MediaWiki project | ||
function wikiapi_for_each(type, title, for_each, options) { | ||
return wikiapi_list.call(this, type, title, Object.assign({ | ||
for_each | ||
}, options)); | ||
} | ||
// -------------------------------------------------------- | ||
/** | ||
* Edit pages list in page_list | ||
* @param {Array}page_list | ||
* @param {Function}for_each_page | ||
* @param {Object}[options] options = { last(): run after all } | ||
*/ | ||
function wikiapi_for_each_page(page_list, for_each_page, options) { | ||
function wikiapi_for_each_page_executor(resolve, reject) { | ||
const wiki = this[KEY_wiki]; | ||
// 一次取得多個頁面內容,以節省傳輸次數。 | ||
wiki.work(Object.assign({ | ||
//no_edit: true | ||
}, options, { | ||
each(page_data/* , messages, config*/) { | ||
Object.defineProperties(page_data, page_data_attributes); | ||
try { | ||
for_each_page.call(this, page_data/* , messages, config*/); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}, | ||
//summary: '', | ||
last() { | ||
if (options && typeof options.last === 'function') { | ||
try { | ||
options.last(); | ||
} catch (e) { | ||
reject(e); | ||
return; | ||
} | ||
} | ||
resolve(); | ||
} | ||
}), page_list); | ||
} | ||
return new Promise(wikiapi_for_each_page_executor.bind(this)); | ||
} | ||
// -------------------------------------------------------- | ||
Object.assign(wikiapi.prototype, { | ||
@@ -246,11 +292,9 @@ login: wikiapi_login, | ||
for_each_page: wikiapi_for_each_page, | ||
for_each: wikiapi_for_each, | ||
data: wikiapi_data, | ||
}); | ||
wikiapi.prototype.for_each = function for_each(type, title, for_each, options) { | ||
return wikiapi_list.call(this, type, title, Object.assign({ | ||
for_each | ||
}, options)); | ||
}; | ||
CeL.wiki.list.type_list.forEach((type) => { | ||
@@ -257,0 +301,0 @@ wikiapi.prototype[type] = function (title, options) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24918
546