Skip to content

Commit

Permalink
Use Array.prototype.find
Browse files Browse the repository at this point in the history
  • Loading branch information
Pchelolo committed Aug 7, 2018
1 parent d080c03 commit 9a6b084
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ const phpCharToUpper = require('./mediawiki.Title.phpCharToUpper.js');
*/
const UTF_8_REPLACEMENT = "�";

// Polyfill for array.find for node 0.10 support.
function arrayFind(array, predicate) {
for (let i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return array[i];
}
}
return undefined;
}


/**
* Convert db-key to readable text.
* @param {string} s
Expand Down Expand Up @@ -63,17 +52,16 @@ function text(s) {
function _getNSIndex(nsName, siteInfo) {
const canonicalName = name => name.toUpperCase().replace(/_/g, ' ');
const name = canonicalName(nsName);
let index = arrayFind(Object.keys(siteInfo.namespaces), (nsId) => {
let index = Object.keys(siteInfo.namespaces).find((nsId) => {
const ns = siteInfo.namespaces[nsId];
return ns.canonical && canonicalName(ns.canonical) === name
|| ns['*'] && canonicalName(ns['*']) === name
|| ns.name && canonicalName(ns.name) === name;
});
if (!index) {
// Not found within canonical names, try aliases
index = arrayFind(siteInfo.namespacealiases, (alias) => {
return name === canonicalName(alias['*'] || alias.alias);
});
index = siteInfo.namespacealiases.find(alias =>
name === canonicalName(alias['*'] || alias.alias));
if (index !== undefined) {
index = `${index.id}`;
}
Expand Down Expand Up @@ -305,10 +293,8 @@ function _checkMaxLength(title, namespace) {
function _fixSpecialName(title, siteInfo) {
const parts = title.split('/');
const first = parts[0].toUpperCase();
const alias = arrayFind(siteInfo.specialpagealiases || [], (o) => {
return arrayFind(o.aliases, (a) => {
return a.toUpperCase() === first;
}) !== undefined;
const alias = (siteInfo.specialpagealiases || []).find((o) => {
return o.aliases.find(a => a.toUpperCase() === first) !== undefined;
});
if (alias) {
parts[0] = alias.aliases[0];
Expand Down

0 comments on commit 9a6b084

Please sign in to comment.