Skip to content

Commit b203ecf

Browse files
authored
Merge pull request metabrainz#2182 from reosarevok/guess-case-refactoring
MBS-11805 (I): Flow types and refactoring for guess case code (first half)
2 parents f81fa5d + 22aa33b commit b203ecf

19 files changed

+927
-810
lines changed

root/static/scripts/edit/components/GuessCaseOptions.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import bracketed from '../../common/utility/bracketed';
1414
import getBooleanCookie from '../../common/utility/getBooleanCookie';
1515
import setCookie from '../../common/utility/setCookie';
1616
import * as modes from '../../guess-case/modes';
17+
import type {GuessCaseModeNameT} from '../../guess-case/types';
1718
import gc from '../../guess-case/MB/GuessCase/Main';
1819

1920
/* eslint-disable flowtype/sort-keys */
2021
export type ActionT =
21-
| {+type: 'set-mode', +mode: string}
22+
| {+type: 'set-mode', +modeName: GuessCaseModeNameT}
2223
| {+type: 'set-keep-upper-case', +enabled: boolean}
2324
| {+type: 'set-upper-case-roman', +enabled: boolean};
2425
/* eslint-enable flowtype/sort-keys */
@@ -27,7 +28,7 @@ export type DispatchT = (ActionT) => void;
2728

2829
export type StateT = {
2930
+keepUpperCase: boolean,
30-
+mode: string,
31+
+modeName: GuessCaseModeNameT,
3132
+upperCaseRoman: boolean,
3233
};
3334

@@ -42,8 +43,8 @@ export type PropsT = $ReadOnly<{
4243

4344
export function createInitialState(): StateT {
4445
return {
45-
keepUpperCase: gc.CFG_UC_UPPERCASED,
46-
mode: gc.modeName,
46+
keepUpperCase: gc.CFG_KEEP_UPPERCASED,
47+
modeName: gc.modeName,
4748
upperCaseRoman: getBooleanCookie('guesscase_roman'),
4849
};
4950
}
@@ -54,17 +55,15 @@ export function runReducer(
5455
): void {
5556
switch (action.type) {
5657
case 'set-mode': {
57-
const modeName = action.mode;
58+
const modeName = action.modeName;
5859
gc.modeName = modeName;
59-
const mode = modes[modeName];
60-
gc.mode = mode;
6160
setCookie('guesscase_mode', modeName);
62-
state.mode = modeName;
61+
state.modeName = modeName;
6362
break;
6463
}
6564
case 'set-keep-upper-case': {
6665
const enabled = action.enabled;
67-
gc.CFG_UC_UPPERCASED = enabled;
66+
gc.CFG_KEEP_UPPERCASED = enabled;
6867
setCookie('guesscase_keepuppercase', enabled);
6968
state.keepUpperCase = enabled;
7069
break;
@@ -81,14 +80,14 @@ export function runReducer(
8180
const GuessCaseOptions = ({
8281
dispatch,
8382
keepUpperCase,
84-
mode: modeName,
83+
modeName,
8584
upperCaseRoman,
8685
}: PropsT): React.Element<'div'> => {
8786
function handleModeChange(event) {
8887
const newModeName = event.target.value;
8988

9089
if (newModeName !== gc.modeName) {
91-
dispatch({mode: newModeName, type: 'set-mode'});
90+
dispatch({modeName: newModeName, type: 'set-mode'});
9291
}
9392
}
9493

@@ -122,7 +121,7 @@ const GuessCaseOptions = ({
122121
</a>,
123122
)}
124123
<p>
125-
{expand2react(gc.mode?.description ?? '')}
124+
{expand2react(modes[modeName].description ?? '')}
126125
</p>
127126
<label>
128127
<input

root/static/scripts/edit/components/GuessCaseOptionsPopover.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const GuessCaseOptionsPopover = (React.memo(({
3030
dispatch,
3131
isOpen,
3232
keepUpperCase,
33-
mode,
33+
modeName,
3434
toggle,
3535
upperCaseRoman,
3636
}: Props): React.Element<typeof ButtonPopover> => {
@@ -48,7 +48,7 @@ const GuessCaseOptionsPopover = (React.memo(({
4848
<GuessCaseOptions
4949
dispatch={dispatch}
5050
keepUpperCase={keepUpperCase}
51-
mode={mode}
51+
modeName={modeName}
5252
upperCaseRoman={upperCaseRoman}
5353
/>
5454
<div
@@ -72,7 +72,7 @@ const GuessCaseOptionsPopover = (React.memo(({
7272
), [
7373
dispatch,
7474
keepUpperCase,
75-
mode,
75+
modeName,
7676
upperCaseRoman,
7777
]);
7878

root/static/scripts/guess-case/MB/Control/GuessCase.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ var mode = ko.computed({
7575

7676
if (modeName !== gc.modeName) {
7777
gc.modeName = modeName;
78-
gc.mode = modes[modeName];
7978
setCookie('guesscase_mode', modeName);
8079
}
81-
return gc.mode;
80+
return modes[modeName];
8281
},
8382
deferEvaluation: true,
8483
});
@@ -91,7 +90,7 @@ guessCaseOptions.help = ko.computed({
9190
});
9291

9392
guessCaseOptions.keepUpperCase.subscribe(function (value) {
94-
gc.CFG_UC_UPPERCASED = value;
93+
gc.CFG_KEEP_UPPERCASED = value;
9594
setCookie('guesscase_keepuppercase', value);
9695
});
9796

@@ -113,7 +112,7 @@ ko.bindingHandlers.guessCase = {
113112
}
114113

115114
if (!guessCaseOptions.keepUpperCase.peek()) {
116-
guessCaseOptions.keepUpperCase(gc.CFG_UC_UPPERCASED);
115+
guessCaseOptions.keepUpperCase(gc.CFG_KEEP_UPPERCASED);
117116
}
118117

119118
if (!guessCaseOptions.upperCaseRoman.peek()) {

root/static/scripts/guess-case/MB/GuessCase/Handler/Area.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import MB from '../../../../common/MB';
1010
import * as flags from '../../../flags';
11+
import * as modes from '../../../modes';
1112
import * as utils from '../../../utils';
1213

1314
MB.GuessCase = (MB.GuessCase) ? MB.GuessCase : {};
@@ -33,7 +34,7 @@ MB.GuessCase.Handler.Area = function (gc) {
3334
(
3435
self.doIgnoreWords() ||
3536
self.doFeaturingArtistStyle() ||
36-
gc.mode.doWord() ||
37+
modes[gc.modeName].doWord() ||
3738
self.doNormalWord()
3839
);
3940
flags.context.number = false;

root/static/scripts/guess-case/MB/GuessCase/Handler/Artist.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import MB from '../../../../common/MB';
1111
import * as flags from '../../../flags';
1212
import * as utils from '../../../utils';
13+
import input from '../Input';
14+
import output from '../Output';
1315

1416
MB.GuessCase = (MB.GuessCase) ? MB.GuessCase : {};
1517
MB.GuessCase.Handler = (MB.GuessCase.Handler) ? MB.GuessCase.Handler : {};
@@ -25,31 +27,31 @@ MB.GuessCase.Handler.Artist = function (gc) {
2527
*/
2628
self.checkSpecialCase = function (is) {
2729
if (is) {
28-
if (!gc.re.ARTIST_EMPTY) {
30+
if (!gc.regexes.ARTIST_EMPTY) {
2931
// Match empty
30-
gc.re.ARTIST_EMPTY = /^\s*$/i;
32+
gc.regexes.ARTIST_EMPTY = /^\s*$/i;
3133
// Match "unknown" and variants
32-
gc.re.ARTIST_UNKNOWN = /^[\(\[]?\s*Unknown\s*[\)\]]?$/i;
34+
gc.regexes.ARTIST_UNKNOWN = /^[\(\[]?\s*Unknown\s*[\)\]]?$/i;
3335
// Match "none" and variants
34-
gc.re.ARTIST_NONE = /^[\(\[]?\s*none\s*[\)\]]?$/i;
36+
gc.regexes.ARTIST_NONE = /^[\(\[]?\s*none\s*[\)\]]?$/i;
3537
// Match "no artist" and variants
36-
gc.re.ARTIST_NOARTIST = /^[\(\[]?\s*no[\s-]+artist\s*[\)\]]?$/i;
38+
gc.regexes.ARTIST_NOARTIST = /^[\(\[]?\s*no[\s-]+artist\s*[\)\]]?$/i;
3739
// Match "not applicable" and variants
38-
gc.re.ARTIST_NOTAPPLICABLE = /^[\(\[]?\s*not[\s-]+applicable\s*[\)\]]?$/i;
40+
gc.regexes.ARTIST_NOTAPPLICABLE = /^[\(\[]?\s*not[\s-]+applicable\s*[\)\]]?$/i;
3941
// Match "n/a" and variants
40-
gc.re.ARTIST_NA = /^[\(\[]?\s*n\s*[\\\/]\s*a\s*[\)\]]?$/i;
42+
gc.regexes.ARTIST_NA = /^[\(\[]?\s*n\s*[\\\/]\s*a\s*[\)\]]?$/i;
4143
}
42-
if (is.match(gc.re.ARTIST_EMPTY)) {
44+
if (is.match(gc.regexes.ARTIST_EMPTY)) {
4345
return self.SPECIALCASE_UNKNOWN;
44-
} else if (is.match(gc.re.ARTIST_UNKNOWN)) {
46+
} else if (is.match(gc.regexes.ARTIST_UNKNOWN)) {
4547
return self.SPECIALCASE_UNKNOWN;
46-
} else if (is.match(gc.re.ARTIST_NONE)) {
48+
} else if (is.match(gc.regexes.ARTIST_NONE)) {
4749
return self.SPECIALCASE_UNKNOWN;
48-
} else if (is.match(gc.re.ARTIST_NOARTIST)) {
50+
} else if (is.match(gc.regexes.ARTIST_NOARTIST)) {
4951
return self.SPECIALCASE_UNKNOWN;
50-
} else if (is.match(gc.re.ARTIST_NOTAPPLICABLE)) {
52+
} else if (is.match(gc.regexes.ARTIST_NOTAPPLICABLE)) {
5153
return self.SPECIALCASE_UNKNOWN;
52-
} else if (is.match(gc.re.ARTIST_NA)) {
54+
} else if (is.match(gc.regexes.ARTIST_NA)) {
5355
return self.SPECIALCASE_UNKNOWN;
5456
}
5557
}
@@ -61,9 +63,9 @@ MB.GuessCase.Handler.Artist = function (gc) {
6163
* in the common word handlers.
6264
*/
6365
self.doWord = function () {
64-
gc.o.appendSpaceIfNeeded();
65-
gc.i.capitalizeCurrentWord();
66-
gc.o.appendCurrentWord();
66+
output.appendSpaceIfNeeded();
67+
input.capitalizeCurrentWord();
68+
output.appendCurrentWord();
6769

6870
flags.resetContext();
6971
flags.context.number = false;
@@ -80,16 +82,16 @@ MB.GuessCase.Handler.Artist = function (gc) {
8082
var append = '';
8183

8284
// Strip Jr./Sr. from the string, and append at the end.
83-
if (!gc.re.SORTNAME_SR) {
84-
gc.re.SORTNAME_SR = /,\s*Sr[\.]?$/i;
85-
gc.re.SORTNAME_JR = /,\s*Jr[\.]?$/i;
85+
if (!gc.regexes.SORTNAME_SR) {
86+
gc.regexes.SORTNAME_SR = /,\s*Sr[\.]?$/i;
87+
gc.regexes.SORTNAME_JR = /,\s*Jr[\.]?$/i;
8688
}
8789

88-
if (artist.match(gc.re.SORTNAME_SR)) {
89-
artist = artist.replace(gc.re.SORTNAME_SR, '');
90+
if (artist.match(gc.regexes.SORTNAME_SR)) {
91+
artist = artist.replace(gc.regexes.SORTNAME_SR, '');
9092
append = ', Sr.';
91-
} else if (artist.match(gc.re.SORTNAME_JR)) {
92-
artist = artist.replace(gc.re.SORTNAME_JR, '');
93+
} else if (artist.match(gc.regexes.SORTNAME_JR)) {
94+
artist = artist.replace(gc.regexes.SORTNAME_JR, '');
9395
append = ', Jr.';
9496
}
9597
var names = artist.split(' ');
@@ -99,23 +101,23 @@ MB.GuessCase.Handler.Artist = function (gc) {
99101
* are sorted at the end.
100102
*/
101103
var reorder = false;
102-
if (!gc.re.SORTNAME_DJ) {
103-
gc.re.SORTNAME_DJ = /^DJ$/i; // match DJ
104-
gc.re.SORTNAME_THE = /^The$/i; // match The
105-
gc.re.SORTNAME_LOS = /^Los$/i; // match Los
106-
gc.re.SORTNAME_DR = /^Dr\.$/i; // match Dr.
104+
if (!gc.regexes.SORTNAME_DJ) {
105+
gc.regexes.SORTNAME_DJ = /^DJ$/i; // match DJ
106+
gc.regexes.SORTNAME_THE = /^The$/i; // match The
107+
gc.regexes.SORTNAME_LOS = /^Los$/i; // match Los
108+
gc.regexes.SORTNAME_DR = /^Dr\.$/i; // match Dr.
107109
}
108110
var firstName = names[0];
109-
if (firstName.match(gc.re.SORTNAME_DJ)) {
111+
if (firstName.match(gc.regexes.SORTNAME_DJ)) {
110112
append = (', DJ' + append); // handle DJ xyz -> xyz, DJ
111113
names[0] = null;
112-
} else if (firstName.match(gc.re.SORTNAME_THE)) {
114+
} else if (firstName.match(gc.regexes.SORTNAME_THE)) {
113115
append = (', The' + append); // handle The xyz -> xyz, The
114116
names[0] = null;
115-
} else if (firstName.match(gc.re.SORTNAME_LOS)) {
117+
} else if (firstName.match(gc.regexes.SORTNAME_LOS)) {
116118
append = (', Los' + append); // handle Los xyz -> xyz, Los
117119
names[0] = null;
118-
} else if (firstName.match(gc.re.SORTNAME_DR)) {
120+
} else if (firstName.match(gc.regexes.SORTNAME_DR)) {
119121
append = (', Dr.' + append); // handle Dr. xyz -> xyz, Dr.
120122
names[0] = null;
121123
reorder = true; // reorder doctors.

0 commit comments

Comments
 (0)