General utilities. Some of the utilities are parts of the convenientDiscussions.api object.
- Source:
Methods
(static) addToArrayIfAbsent(arr, el)
- Source:
Helper to add an element to the array if the array doesn't already include the element. Doesn't
add undefined
elements.
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | |
el |
* |
(static) areObjectsEqual(object1, object2) → {boolean}
- Source:
Check if two objects are identical by value. Doesn't handle complex cases. undefined
values are
treated as unexistent (this helps to compare values retrieved from the local storage as JSON:
JSON.stringify()
removes all undefined
values as well).
Parameters:
Name | Type | Description |
---|---|---|
object1 |
object | First object. |
object2 |
object | Second object. |
Returns:
- Type
- boolean
(static) buildEditSummary(options) → {string}
- Source:
Combine the section headline, summary text, and, optionally, summary postfix to create an edit summary.
Parameters:
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
Properties
|
Returns:
- Type
- string
(static) calculateWordOverlap(s1, s2, caseInsensitive) → {number}
- Source:
Calculates the proportion of the number of words (minimum 2 characters long) present in both strings to the total words count.
Parameters:
Name | Type | Description |
---|---|---|
s1 |
string | |
s2 |
string | |
caseInsensitive |
boolean |
Returns:
- Type
- number
(static) canonicalUrlToPageName(url) → {string}
- Source:
Get the page name from a URL in the canonical format (.../wiki/Page
).
Parameters:
Name | Type | Description |
---|---|---|
url |
string |
Returns:
- Type
- string
(static) countOccurrences(string, regexp) → {number}
- Source:
Count occurences of a regexp in a string.
Parameters:
Name | Type | Description |
---|---|---|
string |
string | |
regexp |
RegExp | Regexp. Must have the |
Throws:
Returns:
- Type
- number
(static) decodeHtmlEntities(string) → {string}
- Source:
Decode HTML entities in a string.
It should work as fast as possible, so we use String#indexOf, not String#includes.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) defined(el) → {boolean}
- Source:
Callback for Array#filter to keep only defined values in an array.
Parameters:
Name | Type | Description |
---|---|---|
el |
* |
Returns:
- Type
- boolean
(static) definedAndNotNull(el) → {boolean}
- Source:
Callback for
Array#filter
to keep only defined and not null
values in an array.
Parameters:
Name | Type | Description |
---|---|---|
el |
* |
Returns:
- Type
- boolean
(static) generateFixedPosTimestamp(date, secondsopt) → {string}
- Source:
Generate a timestamp for a date, where string positions for the year, month, etc. are fixed.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
date |
Date | ||
seconds |
string |
<optional> |
|
Returns:
- Type
- string
(static) generatePageNamePattern(string) → {string}
- Source:
Generate a pattern for use in a regular expression for a page name. The generated pattern is
case-insensitive for the first character only, and has any number of any type of space (
or
_
) in place of spaces. The first character is expected not to be a space.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) getCommonGender(users) → {string}
- Source:
Get the gender that is common for a list of users ('unknown'
is treated as 'male'
) or
'unknown'
if there is no such.
Parameters:
Name | Type | Description |
---|---|---|
users |
Array.<module:userRegistry.User> |
Returns:
- Type
- string
(static) getContentLanguageMessages(messages) → {Array.<string>}
- Source:
For internal use. Get text of the localization messages for the content language.
Parameters:
Name | Type | Description |
---|---|---|
messages |
Array.<string> |
Returns:
- Type
- Array.<string>
(static) getDayTimestamp() → {number}
- Source:
Get the timestamp of the current day.
Returns:
- Type
- number
(static) getDbnameForHostname(hostname) → {string}
- Source:
Get the database name for a canonical hostname of a WMF wiki.
Parameters:
Name | Type | Description |
---|---|---|
hostname |
string | Canonical hostname. |
Returns:
- Type
- string
(static) getHeadingLevel(node) → (nullable) {number}
- Source:
Get the level of a heading node (.mw-heading
or <h1>
-
<h6>
).
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | object | Node or object with |
Returns:
- Type
- number
(static) getLastArrayElementOrSelf(value) → {string}
- Source:
If the argument is an array, return its last element. Otherwise, return the value. (To process
URLSearchParams#getAll
return value. In MediaWiki, if there is more than one parameter with some name, the second value
of the parameter is used, while with URLSearchParams#get
it is the first one.)
Parameters:
Name | Type | Description |
---|---|---|
value |
string | Array.<string> |
Returns:
- Type
- string
(static) getNativePromiseState(promise) → {Promise.<string>}
- Source:
Use a
Promise.race()
workaround to get the state of a native promise. Note that it works only with native promises:
it doesn't work with jQuery promises (for example, ones that new mw.Api()
returns).
Parameters:
Name | Type | Description |
---|---|---|
promise |
Promise.<*> |
Returns:
- Type
- Promise.<string>
(static) getQueryParamBooleanValue(param) → (nullable) {boolean}
- Source:
Check if a URL query parameter is true
(1
, yes
, y
) or false
(0
, no
, n
).
Parameters:
Name | Type | Description |
---|---|---|
param |
string |
Returns:
- Type
- boolean
(static) isCommentEdit(summary) → {boolean}
- Source:
Check by an edit summary if an edit is probably an edit of a comment.
Parameters:
Name | Type | Description |
---|---|---|
summary |
string |
Returns:
- Type
- boolean
(static) isHeadingNode(node, onlyHElementsopt) → {boolean}
- Source:
Check whether the provided node is a heading node (.mw-heading
or <h1>
- <h6>
).
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
node |
Node | |||
onlyHElements |
boolean |
<optional> |
false
|
Returns:
- Type
- boolean
(static) isInline(node, countTextNodesAsInlineopt) → (nullable) {boolean}
- Source:
Check if a node is an element with display: inline
or display: inline-block
in the default
browser styles. As an option, it can also treat text nodes as inline elements.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
node |
Node | external:Node | |||
countTextNodesAsInline |
boolean |
<optional> |
false
|
Returns:
- Type
- boolean
(static) isMetadataNode(node) → {boolean}
- Source:
Check whether the provided node is a metadata node (<style>
, <link>
).
Parameters:
Name | Type | Description |
---|---|---|
node |
Node |
Returns:
- Type
- boolean
(static) isProbablyTalkPage(pageName, namespaceNumber) → {boolean}
- Source:
Check if a page is probably a talk page. The namespace number is required.
This function exists mostly because we can't be sure the mediawiki.Title
module has loaded when
the script has started executing (and can't use the Page constructor), and we need to
make this check fast. So, in most cases, Page#isProbablyTalkPage should be used.
Parameters:
Name | Type | Description |
---|---|---|
pageName |
string | |
namespaceNumber |
number |
Returns:
- Type
- boolean
(static) isUndo(summary) → {boolean}
- Source:
Check by an edit summary if an edit is probably an undo.
Parameters:
Name | Type | Description |
---|---|---|
summary |
string |
Returns:
- Type
- boolean
(static) keepWorkerSafeValues(obj, allowedFuncNamesopt) → {object}
- Source:
For internal use. Filter out values of an object that can't be safely passed to worker (see https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | |||
allowedFuncNames |
Array.<string> |
<optional> |
[]
|
Names of the properties that should be passed to the worker despite their values are functions (they are passed in a stringified form). |
Returns:
- Type
- object
(static) mergeRegexps(arrnullable) → (nullable) {RegExp}
- Source:
Turn many regexps into one, putting it in ()
and separating individual expressions by |
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array.<RegExp> | Array.<string> |
<nullable> |
Returns:
- Type
- RegExp
(static) parseWikiUrl(url) → (nullable) {module:utilsGeneral~ParsedWikiUrl}
- Source:
Get the page name, host name and fragment from a URL in one of the standard formats (based on the wgArticlePath and wgScript config values).
Parameters:
Name | Type | Description |
---|---|---|
url |
string |
Returns:
(static) removeDirMarks(text, replaceWithSpace) → {string}
- Source:
Remove left-to-right and right-to-left marks that sometimes are copied from the edit history to the timestamp (for example, https://meta.wikimedia.org/w/index.php?diff=20418518) and also appear after →/← in edit summaries.
Parameters:
Name | Type | Description |
---|---|---|
text |
string | Text to alter. |
replaceWithSpace |
boolean | Replace direction marks with a space instead of removing. |
Returns:
- Type
- string
(static) removeDoubleSpaces(string) → {string}
- Source:
Replaces sequences of spaces with single spaces.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) removeFromArrayIfPresent(arr, el)
- Source:
Helper to remove an element from the array if the array includes the element. Doesn't remove
undefined
elements.
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | |
el |
* |
(static) reorderArray(arr, startIndex, reverseopt) → {Array.<*>}
- Source:
Return an array with a changed start index ([0, 1, 2, 3]
can be transformed into [2, 3, 0, 1]
) and optionally reversed while keeping the start index ([0, 1, 2, 3]
can be transformed
into [2, 1, 0, 3]
).
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array.<*> | |||
startIndex |
number | |||
reverse |
boolean |
<optional> |
false
|
Returns:
- Type
- Array.<*>
(static) sleep(msopt) → {Promise}
- Source:
Wait for a specified number of milliseconds (a wrapper around setTimeout()).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ms |
number |
<optional> |
Nubmer of milliseconds to sleep. |
Returns:
- Type
- Promise
(static) spacesToUnderlines(string) → {string}
- Source:
Transforms spaces to underlines in a string.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) ucFirst(string) → {string}
- Source:
Transform the first letter of a string to upper case, for example: 'wikilink'
→ 'Wikilink'
.
Do it in PHP, not JavaScript, fashion to match the MediaWiki behavior, see
https://phabricator.wikimedia.org/T141723#2513800.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) underlinesToSpaces(string) → {string}
- Source:
Transforms underlines to spaces in a string.
Parameters:
Name | Type | Description |
---|---|---|
string |
string |
Returns:
- Type
- string
(static) unique(el, i, arr) → {boolean}
- Source:
Callback for Array#filter to remove duplicated elements from an array.
Parameters:
Name | Type | Description |
---|---|---|
el |
* | |
i |
number | |
arr |
Array.<*> |
Returns:
- Type
- boolean
Type Definitions
ParsedWikiUrl
- Source:
Properties:
Name | Type | Description |
---|---|---|
pageName |
string | |
hostname |
string | |
fragment |
string |
Type:
- object