-
Notifications
You must be signed in to change notification settings - Fork 4k
/
constants.js
62 lines (59 loc) · 2.57 KB
/
constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* List of translation functions exposed by the `@wordpress/i18n` package.
*
* @type {Set<string>} Translation functions.
*/
const TRANSLATION_FUNCTIONS = new Set( [ '__', '_x', '_n', '_nx' ] );
/**
* Regular expression matching format placeholder syntax.
*
* The pattern for matching named arguments is a naive and incomplete matcher
* against valid JavaScript identifier names.
*
* via Mathias Bynens:
*
* >An identifier must start with $, _, or any character in the Unicode
* >categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase
* >letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter
* >number (Nl)”.
* >
* >The rest of the string can contain the same characters, plus any U+200C zero
* >width non-joiner characters, U+200D zero width joiner characters, and
* >characters in the Unicode categories “Non-spacing mark (Mn)”, “Spacing
* >combining mark (Mc)”, “Decimal digit number (Nd)”, or “Connector
* >punctuation (Pc)”.
*
* If browser support is constrained to those supporting ES2015, this could be
* made more accurate using the `u` flag:
*
* ```
* /^[$_\p{L}\p{Nl}][$_\p{L}\p{Nl}\u200C\u200D\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*$/u;
* ```
*
* @see http://www.pixelbeat.org/programming/gcc/format_specs.html
* @see https://mathiasbynens.be/notes/javascript-identifiers#valid-identifier-names
*
* @type {RegExp}
*/
const REGEXP_SPRINTF_PLACEHOLDER =
/%(((\d+)\$)|(\(([$_a-zA-Z][$_a-zA-Z0-9]*)\)))?[ +0#-]*\d*(\.(\d+|\*))?(ll|[lhqL])?([cduxXefgsp%])/g;
// ▲ ▲ ▲ ▲ ▲ ▲ ▲ type
// │ │ │ │ │ └ Length (unsupported)
// │ │ │ │ └ Precision / max width
// │ │ │ └ Min width (unsupported)
// │ │ └ Flags (unsupported)
// └ Index └ Name (for named arguments)
/**
* "Unordered" means there's no position specifier: '%s', not '%2$s'.
*
* @see https://github.com/WordPress/WordPress-Coding-Standards/blob/2f927b0ba2bfcbffaa8f3251c086e109302d6622/WordPress/Sniffs/WP/I18nSniff.php#L62-L81
*
* @type {RegExp}
*/
const REGEXP_SPRINTF_PLACEHOLDER_UNORDERED =
/(?:(?<!%)%[+-]?(?:(?:0|'.)?-?[0-9]*(?:\.(?:[ 0]|'.)?[0-9]+)?|(?:[ ])?-?[0-9]+(?:\.(?:[ 0]|'.)?[0-9]+)?)[bcdeEfFgGosuxX])/;
module.exports = {
TRANSLATION_FUNCTIONS,
REGEXP_SPRINTF_PLACEHOLDER,
REGEXP_SPRINTF_PLACEHOLDER_UNORDERED,
};