mediawiki-projects-list 3.0.1 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.d.ts +10 -4
  2. package/index.js +57 -20
  3. package/package.json +1 -1
  4. package/projects.json +1263 -1263
package/index.d.ts CHANGED
@@ -64,11 +64,17 @@ declare module 'mediawiki-projects-list' {
64
64
  note: string | null;
65
65
  };
66
66
 
67
- /** List of MediaWiki projects */
68
- export const wikiProjects: WikiProject[];
67
+ /** Map of MediaWiki projects */
68
+ export const wikiProjects: Map<string, WikiProject>;
69
69
 
70
- /** List of frontend proxies */
71
- export const frontendProxies: FrontendProxy[];
70
+ /** Map of frontend proxies */
71
+ export const frontendProxies: Map<string, FrontendProxy>;
72
+
73
+ /** Get a MediaWiki project by domain hostname */
74
+ export function getWikiProject(hostname: string): WikiProject | null;
75
+
76
+ /** Get a frontend proxy by domain hostname */
77
+ export function getFrontendProxy(hostname: string): FrontendProxy | null;
72
78
 
73
79
  export function inputToWikiProject(input: string): {
74
80
  fullArticlePath: string;
package/index.js CHANGED
@@ -56,10 +56,27 @@ const functionCache = {
56
56
  };
57
57
 
58
58
  /**
59
- * List of MediaWiki projects
60
- * @type {WikiProject[]}
59
+ * @param {Map<string, mapValueType>} map
60
+ * @param {string} keyString
61
+ * @returns {?mapValueType}
62
+ * @template mapValueType
61
63
  */
62
- const wikiProjects = PROJECTS.wikiProjects.map( wikiProject => {
64
+ function getMapValue(map, keyString) {
65
+ if ( !keyString ) return null;
66
+ let parts = keyString.split('.');
67
+ while ( parts.length > 0 ) {
68
+ let key = parts.join('.');
69
+ if ( map.has(key) ) return map.get(key);
70
+ parts.shift();
71
+ }
72
+ return null;
73
+ }
74
+
75
+ /**
76
+ * Map of MediaWiki projects
77
+ * @type {Map<string, WikiProject>}
78
+ */
79
+ const wikiProjects = new Map(PROJECTS.wikiProjects.map( wikiProject => {
63
80
  if ( wikiProject.idString ) {
64
81
  wikiProject.idString.separator ??= wikiProjectSchema.idString.properties.separator.default;
65
82
  wikiProject.idString.direction ??= wikiProjectSchema.idString.properties.direction.default;
@@ -69,22 +86,40 @@ const wikiProjects = PROJECTS.wikiProjects.map( wikiProject => {
69
86
  wikiProject.extensions ??= wikiProjectSchema.extensions.default.slice();
70
87
  wikiProject.urlSpaceReplacement ??= wikiProjectSchema.urlSpaceReplacement.default;
71
88
  wikiProject.note ??= wikiProjectSchema.note.default;
72
- return wikiProject;
73
- } );
89
+ return [wikiProject.name, wikiProject];
90
+ } ));
74
91
 
75
92
  /**
76
- * List of frontend proxies
77
- * @type {FrontendProxy[]}
93
+ * Map of frontend proxies
94
+ * @type {Map<string, FrontendProxy>}
78
95
  */
79
- const frontendProxies = PROJECTS.frontendProxies.map( frontendProxy => {
96
+ const frontendProxies = new Map(PROJECTS.frontendProxies.map( frontendProxy => {
80
97
  if ( frontendProxy.idString ) {
81
98
  frontendProxy.idString.separator ??= frontendProxySchema.idString.properties.separator.default;
82
99
  frontendProxy.idString.direction ??= frontendProxySchema.idString.properties.direction.default;
83
100
  }
84
101
  frontendProxy.relativeFix ??= frontendProxySchema.relativeFix.default;
85
102
  frontendProxy.note ??= frontendProxySchema.note.default;
86
- return frontendProxy;
87
- } );
103
+ return [frontendProxy.name, frontendProxy];
104
+ } ));
105
+
106
+ /**
107
+ * Get a MediaWiki project by domain hostname
108
+ * @param {string} hostname
109
+ * @returns {?WikiProject}
110
+ */
111
+ function getWikiProject(hostname) {
112
+ return getMapValue(wikiProjects, hostname);
113
+ }
114
+
115
+ /**
116
+ * Get a frontend proxy by domain hostname
117
+ * @param {string} hostname
118
+ * @returns {?FrontendProxy}
119
+ */
120
+ function getFrontendProxy(hostname) {
121
+ return getMapValue(frontendProxies, hostname);
122
+ }
88
123
 
89
124
  /**
90
125
  *
@@ -94,7 +129,7 @@ const frontendProxies = PROJECTS.frontendProxies.map( frontendProxy => {
94
129
  function inputToWikiProject(input) {
95
130
  if ( functionCache.inputToWikiProject.has(input) ) return structuredClone(functionCache.inputToWikiProject.get(input));
96
131
  let result = null;
97
- let wikiProject = wikiProjects.find( wikiProject => input.split('/').slice(0, 3).some( part => part.endsWith( wikiProject.name ) ) );
132
+ let wikiProject = getWikiProject(input.split('/').slice(0, 3).find( part => part && part.includes( '.' ) ));
98
133
  if ( wikiProject ) {
99
134
  let articlePath = ( wikiProject.regexPaths ? '/' : wikiProject.articlePath.split('?')[0] ).replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
100
135
  let scriptPath = ( wikiProject.regexPaths ? '/' : wikiProject.scriptPath ).replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
@@ -133,11 +168,11 @@ function inputToWikiProject(input) {
133
168
  function urlToIdString(url) {
134
169
  if ( functionCache.urlToIdString.has(url.href) ) return functionCache.urlToIdString.get(url.href);
135
170
  let result = null;
136
- /** @type {WikiProject|FrontendProxy|undefined} */
137
- let project = wikiProjects.find( wikiProject => wikiProject.idString && url.hostname.endsWith( wikiProject.name ) );
138
- if ( !project ) project = frontendProxies.find( frontendProxy => frontendProxy.idString && url.hostname.endsWith( frontendProxy.name ) );
139
- if ( project ) {
140
- let regex = url.href.match( new RegExp( project.regex ) )?.slice(2);
171
+ /** @type {?WikiProject|FrontendProxy} */
172
+ let project = getWikiProject(url.hostname);
173
+ if ( !project ) project = getFrontendProxy(url.hostname);
174
+ if ( project?.idString ) {
175
+ let regex = url.href.match( new RegExp( project.regex ) )?.slice(2).filter( part => part );
141
176
  if ( regex?.length ) {
142
177
  if ( project.idString.direction === 'desc' ) regex.reverse();
143
178
  result = regex.join(project.idString.separator);
@@ -160,8 +195,8 @@ function idStringToUrl(idString, projectName) {
160
195
  return ( result ? new URL(result) : result );
161
196
  }
162
197
  let result = null;
163
- let project = wikiProjects.find( wikiProject => wikiProject.idString && wikiProject.name === projectName )?.idString;
164
- if ( !project ) project = frontendProxies.find( frontendProxy => frontendProxy.idString && frontendProxy.name === projectName )?.idString;
198
+ let project = getWikiProject(projectName)?.idString;
199
+ if ( !project ) project = getFrontendProxy(projectName)?.idString;
165
200
  if ( project ) {
166
201
  let regex = idString.match( new RegExp( '^' + project.regex + '$' ) )?.[1].split(project.separator);
167
202
  if ( regex && regex.length <= project.scriptPaths.length ) {
@@ -180,7 +215,7 @@ function idStringToUrl(idString, projectName) {
180
215
  function inputToFrontendProxy(input) {
181
216
  if ( functionCache.inputToFrontendProxy.has(input) ) return structuredClone(functionCache.inputToFrontendProxy.get(input));
182
217
  let result = null;
183
- let frontendProxy = frontendProxies.find( frontendProxy => input.split('/').slice(0, 3).some( part => part.endsWith( frontendProxy.name ) ) );
218
+ let frontendProxy = getFrontendProxy(input.split('/').slice(0, 3).find( part => part && part.includes( '.' ) ));
184
219
  if ( frontendProxy ) {
185
220
  let regex = input.match( new RegExp( frontendProxy.regex ) );
186
221
  if ( regex ) {
@@ -209,7 +244,7 @@ function urlToFix(url) {
209
244
  let hostname = url.split('/')[2];
210
245
  if ( functionCache.urlToFix.has(hostname) ) return functionCache.urlToFix.get(hostname);
211
246
  let result = null;
212
- let frontendProxy = frontendProxies.find( frontendProxy => hostname.endsWith( frontendProxy.name ) );
247
+ let frontendProxy = getFrontendProxy(hostname);
213
248
  if ( frontendProxy ) {
214
249
  let splitLength = frontendProxy.namePath.split('/').length;
215
250
  let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => {
@@ -264,6 +299,8 @@ function urlToFix(url) {
264
299
  module.exports = {
265
300
  wikiProjects,
266
301
  frontendProxies,
302
+ getWikiProject,
303
+ getFrontendProxy,
267
304
  inputToWikiProject,
268
305
  urlToIdString,
269
306
  idStringToUrl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediawiki-projects-list",
3
- "version": "3.0.1",
3
+ "version": "4.0.1",
4
4
  "type": "commonjs",
5
5
  "description": "List of MediaWiki projects for use in discord-wiki-bot",
6
6
  "main": "index.js",