Skip to content

Commit df45988

Browse files
committed
Bug-fix.
Changelog excerpt: - Some of the more unusual available number formatting choices (e.g., choices not using base-10 or Arabic numerals) didn't mesh well with the JavaScript code responsible for using them; Fixed.
1 parent 1e282e0 commit df45988

File tree

6 files changed

+127
-25
lines changed

6 files changed

+127
-25
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,5 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
179179
[2023.12.12; Maikuolan]: Split the code for most of the various front-end pages, which the view method was responsible for, into their own distinct files.
180180

181181
[2023.12.26; Maikuolan]: Refactored the page greeting.
182+
183+
[2023.12.29; Bug-fix; Maikuolan]: Some of the more unusual available number formatting choices (e.g., choices not using base-10 or Arabic numerals) didn't mesh well with the JavaScript code responsible for using them; Fixed.

assets/numberJs.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* JavaScript version of Maikuolan\Common\NumberFormatter's format method.
3+
* Formats the supplied number according to definitions.
4+
*
5+
* @param {any} Number The number to format (int, float, string, etc).
6+
* @param {number} Decimals The number of decimal places (optional).
7+
* @returns {string} The formatted number, or an empty string on failure.
8+
*/
9+
function nft(Number, Decimals = 0) {
10+
let ConversionSet = JSON.parse('%s');
11+
let GroupSeparator = '%s';
12+
let GroupSize = %s;
13+
let GroupOffset = %s;
14+
let DecimalSeparator = '%s';
15+
let Base = %s;
16+
if (Base < 2 || Base > 36) {
17+
return '';
18+
}
19+
var DecPos = String(Number).indexOf('.');
20+
let Fraction;
21+
if (DecPos !== -1) {
22+
if (Decimals > 0 && DecimalSeparator && !ConversionSet['.']) {
23+
Fraction = (String(Number).substring(DecPos + 1)) || '';
24+
let Len = Fraction.length;
25+
if (Len > 0) {
26+
Fraction = convertFraction(Fraction, 10, Base, Decimals);
27+
Fraction = Fraction.substring(0, Decimals);
28+
Len = Fraction.length;
29+
}
30+
if (Len < Decimals) {
31+
Fraction += '0'.repeat(Decimals - Len);
32+
}
33+
}
34+
Number = String(Number).substring(0, DecPos);
35+
} else {
36+
Number = String(Number);
37+
}
38+
if (Base !== 10) {
39+
Number = parseInt(Number).toString(Base);
40+
}
41+
let Formatted = '';
42+
let WholeLen = -1;
43+
if (typeof ConversionSet['=' + Number] != 'undefined') {
44+
Formatted = ConversionSet['=' + Number];
45+
WholeLen = -1;
46+
} else {
47+
WholeLen = Number.length;
48+
}
49+
let ThouPos = GroupOffset;
50+
for (let Unit = 0, Pos = WholeLen - 1; Pos > -1; Pos--, Unit++) {
51+
if (ThouPos >= GroupSize) {
52+
ThouPos = 1;
53+
Formatted = GroupSeparator + Formatted;
54+
} else {
55+
ThouPos++;
56+
}
57+
var Key = Number.substring(Pos, Pos + 1);
58+
var Double = (Pos > 0) ? Number.substring(Pos - 1, Pos) + Key : '';
59+
let Power = '';
60+
let Digit = '';
61+
if (typeof ConversionSet['^' + Unit + '+' + Double] != 'undefined') {
62+
Digit = ConversionSet['^' + Unit + '+' + Double];
63+
} else if (typeof ConversionSet['^' + Unit + '+' + Key] != 'undefined') {
64+
Digit = ConversionSet['^' + Unit + '+' + Key];
65+
} else if (typeof ConversionSet['+' + Key] != 'undefined') {
66+
Digit = ConversionSet['+' + Key];
67+
} else {
68+
Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key;
69+
if (typeof ConversionSet['^' + Unit + '*' + Key] != 'undefined') {
70+
Power = ConversionSet['^' + Unit + '*' + Key];
71+
} else if (typeof ConversionSet['^' + Unit] != 'undefined') {
72+
Power = ConversionSet['^' + Unit];
73+
}
74+
}
75+
Formatted = Digit + Power + Formatted;
76+
}
77+
if (Fraction && Decimals && DecimalSeparator && !ConversionSet['.']) {
78+
Formatted += DecimalSeparator;
79+
for (let Pos = 0, Len = Fraction.length; Pos < Len; Pos++) {
80+
var Key = Fraction.substring(Pos, Pos + 1);
81+
let Power = '';
82+
let Digit = '';
83+
if (typeof ConversionSet['^-' + Pos + '+' + Key] != 'undefined') {
84+
Digit = ConversionSet['^-' + Pos + '+' + Key];
85+
} else if (typeof ConversionSet['-+' + Key] != 'undefined') {
86+
Digit = ConversionSet['-+' + Key];
87+
} else {
88+
if (typeof ConversionSet['-' + Key] != 'undefined') {
89+
Digit = ConversionSet['-' + Key];
90+
} else {
91+
Digit = (typeof ConversionSet[Key] != 'undefined') ? ConversionSet[Key] : Key;
92+
}
93+
if (typeof ConversionSet['^-' + Pos + '*' + Key] != 'undefined') {
94+
Power = ConversionSet['^-' + Pos + '*' + Key];
95+
} else if (typeof ConversionSet['^-' + Pos] != 'undefined') {
96+
Power = ConversionSet['^-' + Pos];
97+
}
98+
}
99+
Formatted += Digit + Power;
100+
}
101+
}
102+
var DecLen = DecimalSeparator.length;
103+
if (DecLen && Formatted.substring(0, DecLen) === DecimalSeparator) {
104+
Formatted = Formatted.substring(DecLen);
105+
}
106+
return Formatted;
107+
}

assets/default/scripts.js renamed to assets/scripts.js

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FrontEnd.php

+14-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: Front-end handler (last modified: 2023.12.26).
11+
* This file: Front-end handler (last modified: 2023.12.29).
1212
*/
1313

1414
namespace phpMussel\FrontEnd;
@@ -907,6 +907,11 @@ private function getAssetPath(string $Asset, bool $CanFail = false): string
907907
return $this->AssetsPath . 'default' . DIRECTORY_SEPARATOR . $Asset;
908908
}
909909

910+
/** Assets base directory. */
911+
if (is_readable($this->AssetsPath . $Asset)) {
912+
return $this->AssetsPath . $Asset;
913+
}
914+
910915
/** Failure. */
911916
if ($CanFail) {
912917
return '';
@@ -915,31 +920,20 @@ private function getAssetPath(string $Asset, bool $CanFail = false): string
915920
}
916921

917922
/**
918-
* Generates JavaScript code for localising numbers locally.
923+
* JavaScript code for localising numbers locally.
919924
*
920925
* @return string The JavaScript code.
921926
*/
922-
private function numberJS(): string
927+
private function numberJs(): string
923928
{
924-
if ($this->NumberFormatter->ConversionSet === 'Western') {
925-
$ConvJS = 'return l10nd';
926-
} else {
927-
$ConvJS = 'var nls=[' . $this->NumberFormatter->getSetCSV(
928-
$this->NumberFormatter->ConversionSet
929-
) . '];return nls[l10nd]||l10nd';
930-
}
931929
return sprintf(
932-
'function l10nn(l10nd){%4$s};function nft(r){var x=r.indexOf(\'.\')!=-1?' .
933-
'\'%1$s\'+r.replace(/^.*\./gi,\'\'):\'\',n=r.replace(/\..*$/gi,\'\').rep' .
934-
'lace(/[^0-9]/gi,\'\'),t=n.length;for(e=\'\',b=%5$d,i=1;i<=t;i++){b>%3$d' .
935-
'&&(b=1,e=\'%2$s\'+e);var e=l10nn(n.substring(t-i,t-(i-1)))+e;b++}var t=' .
936-
'x.length;for(y=\'\',b=1,i=1;i<=t;i++){var y=l10nn(x.substring(t-i,t-(i-' .
937-
'1)))+y}return e+y}',
938-
$this->NumberFormatter->DecimalSeparator,
930+
$this->Loader->readFile($this->getAssetPath('numberJs.js')),
931+
$this->NumberFormatter->getSetJSON($this->NumberFormatter->ConversionSet),
939932
$this->NumberFormatter->GroupSeparator,
940933
$this->NumberFormatter->GroupSize,
941-
$ConvJS,
942-
$this->NumberFormatter->GroupOffset + 1
934+
$this->NumberFormatter->GroupOffset,
935+
$this->NumberFormatter->DecimalSeparator,
936+
$this->NumberFormatter->Base
943937
);
944938
}
945939

@@ -1263,7 +1257,7 @@ private function initialPrepwork(array &$FE, string $Title = '', string $Tips =
12631257
$FE['FE_Tip'] = $this->Loader->parse([], $Tips);
12641258

12651259
/** Load main front-end JavaScript data. */
1266-
$FE['JS'] = $JS ? $this->Loader->readFile($this->getAssetPath('scripts.js')) : '';
1260+
$FE['JS'] = $JS ? "\n" . $this->Loader->readFile($this->getAssetPath('scripts.js')) : '';
12671261
}
12681262

12691263
/**

src/pages/config.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The configuration page (last modified: 2023.12.13).
11+
* This file: The configuration page (last modified: 2023.12.29).
1212
*/
1313

1414
namespace phpMussel\FrontEnd;
@@ -21,7 +21,7 @@
2121
$this->initialPrepwork($FE, $this->Loader->L10N->getString('link.Configuration'), $this->Loader->L10N->getString('tip.Configuration'));
2222

2323
/** Append number localisation JS. */
24-
$FE['JS'] .= $this->numberJS() . "\n";
24+
$FE['JS'] .= $this->numberJs() . "\n";
2525

2626
/** Directive template. */
2727
$ConfigurationRow = $this->Loader->readFile($this->getAssetPath('_config_row.html'));

src/pages/siginfo.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The signature information page (last modified: 2023.12.13).
11+
* This file: The signature information page (last modified: 2023.12.29).
1212
*/
1313

1414
namespace phpMussel\FrontEnd;
@@ -21,7 +21,7 @@
2121
$this->initialPrepwork($FE, $this->Loader->L10N->getString('link.Signature Information'), $this->Loader->L10N->getString('tip.Signature Information'));
2222

2323
/** Append number localisation JS. */
24-
$FE['JS'] .= $this->numberJS() . "\n";
24+
$FE['JS'] .= $this->numberJs() . "\n";
2525

2626
$FE['InfoRows'] = '';
2727
$FE['SigInfoMenuOptions'] = '';

0 commit comments

Comments
 (0)