bringBadgeToFront

Sorts a comma-separated list of badges so that a given badge is listed first.

Note: It does not sort the badges alphabetically, and as such multiple calls may result in the order not ending up quite as imagined. Perform the procedure in reverse if possible, to guarantee the last badge is the first.

version(TwitchSupport && WithTwitchPlugin)
private pure @safe
void
bringBadgeToFront
(
ref string badges
,
const string badge
)

Parameters

badges string

A reference to the comma-separated string of badges to sort in place.

badge string

The badge to bring to the front.

Examples

1 {
2     string badges = "subscriber/14,broadcaster/1";
3     bringBadgeToFront(badges, "broadcaster/");
4     assert((badges == "broadcaster/1,subscriber/14"), badges);
5 }
6 {
7     string badges = "broadcaster/1,broadcaster/1";
8     bringBadgeToFront(badges, "broadcaster/");
9     assert((badges == "broadcaster/1,broadcaster/1"), badges);
10 }
11 {
12     string badges = "vip/1,subscriber/14";
13     bringBadgeToFront(badges, "broadcaster/");
14     assert((badges == "vip/1,subscriber/14"), badges);
15 }
16 {
17     string badges;
18     bringBadgeToFront(badges, "broadcaster/");
19     assert(!badges.length, badges);
20 }
21 {
22     string badges = "hirfharf";
23     bringBadgeToFront(badges, "broadcaster/");
24     assert((badges == "hirfharf"), badges);
25 }
26 {
27     string badges = "subscriber/14,moderator/1";
28     bringBadgeToFront(badges, "moderator/");
29     assert((badges == "moderator/1,subscriber/14"), badges);
30 }
31 {
32     string badges = "broadcaster/1,asdf/9999";
33     bringBadgeToFront(badges, "asdf/");
34     assert((badges == "asdf/9999,broadcaster/1"), badges);
35 }
36 {
37     string badges = "subscriber/91,broadcaster/1,sub-gifter/25";
38     bringBadgeToFront(badges, "vip/");
39     bringBadgeToFront(badges, "moderator/");
40     bringBadgeToFront(badges, "broadcaster/");
41     assert((badges == "broadcaster/1,subscriber/91,sub-gifter/25"), badges);
42 }
43 {
44     string badges = "f/1,b/2,d/3,c/4,a/5,e/6";
45     static immutable string[3] badgeOrder =
46     [
47         "a/",
48         "b/",
49         "c/",
50     ];
51 
52     foreach_reverse (immutable badge; badgeOrder[])
53     {
54         bringBadgeToFront(badges, badge);
55     }
56 
57     assert((badges == "a/5,b/2,c/4,f/1,d/3,e/6"), badges);
58 }