Jump to content

Module talk:String2

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by GKFX (talk | contribs) at 19:17, 8 April 2021 (Added matchAny function: new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

New function "label" added

I added new function "label" which capitalize only first letter for fetched wikidata labels (diff). Because Wikidata English labels generally begin with a lowercase letter (d:Help:Label#Capitalization). New function is almost same as "sentence" function except that "label" doesn't lowering the rest of text. If there are any questions or problems, feel free to report here. Thanks! --Was a bee (talk) 03:03, 17 September 2017 (UTC)[reply]

Some objection. A "label" has multiple options. Especially: it is "free" (as in: irrelevant, not defining). If this option wants to imply "page title as a wikilink label", then change the option (parameter) name. -DePiep (talk)
I've removed the label function as obselete in favour of ucfirst. --RexxS (talk) 15:16, 13 November 2018 (UTC)[reply]

Testing posnq

  • {{#invoke:String2 |posnq |This is a piece of text to be searched |ext}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |This is a piece of text to be searched |ent}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |This is a piece of text to be searched |"ext"}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |This is a piece of text to be searched |"ent"}}Script error: The function "posnq" does not exist.
  • {{Posnq |This is a piece of text to be searched |piece of }}Template:Posnq
  • {{Posnq |This is a piece oftext to be searched |piece of }}Template:Posnq
  • {{Posnq |This is a piece of text to be searched |"piece of "}}Template:Posnq
  • {{Posnq |This is a piece oftext to be searched |"piece of "}}Template:Posnq
  • {{Posnq | {{start date|2018|November|30}} | bday dtstart published updated }}Template:Posnq
  • {{Posnq | 30 November 2018 | bday dtstart published updated }}Template:Posnq

ucfirst bug

There is an script error on articles like 999 (album) and Casual Viewin' USA, whith an error on line 35, which originates on line 34. "%a" on line 34 did not match numbers after the pipe in my test, although it does find an letter after the pipe. As both of these articles have an number after the pipe, they both give an script error. The script needs to deal with the possibility of an number after an pipe.--Snaevar (talk) 23:13, 9 October 2019 (UTC)[reply]

@Snaevar: I noticed many articles with that problem and I believe I have just fixed the module. Johnuniq (talk) 09:11, 10 October 2019 (UTC)[reply]

ucfirst bug, part 2

E.g. {{#invoke:String2 | ucfirst |Đorđe Balašević chronology }} returns ĐOrđe Balašević chronology, as if the function does not realize Đ is a letter and is capitalizing O instead. Note {{ucfirst:Đorđe Balašević chronology}} returns Đorđe Balašević chronology, as it should. Oddly, {{#invoke:String2 | ucfirst |đorđe Balašević chronology }} works correctly (i.e. Đorđe Balašević chronology is returned). {{Infobox album}} chronology is affected, although these kinds of errors seem to be very rare. GregorB (talk) 18:24, 24 July 2020 (UTC)[reply]

@GregorB: Almost all of the time, the standard Lua string library calls manage to cope although they only deal with single-byte character codes. Once in a while an application needs to work with UTF-8, and this is one of those cases. I've updated the ucfirst call to use the mw.ustring library which handles UTF-8 characters properly. Now we should get:
  • {{#invoke:String2 | ucfirst |Đorđe Balašević chronology }} → Đorđe Balašević chronology
  • {{#invoke:String2 | ucfirst |đorđe Balašević chronology }} → Đorđe Balašević chronology
Thanks for spotting that, and please let me know if you find any more issues. Cheers --RexxS (talk) 19:24, 24 July 2020 (UTC)[reply]
That was super quick, thanks! GregorB (talk) 19:50, 24 July 2020 (UTC)[reply]

New function "findlast" added

Function findlast finds the last item in a list. The first unnamed parameter is the list. The second, optional unnamed parameter is the list separator (default = comma space). It returns the whole list if the separator is not found.

The list is trimmed of leading and trailing whitespace; the separator is not (so that leading or trailing spaces can be included).

One issue is that using Lua special pattern characters as the separator will probably cause problems.

Examples:

  • Normal usage: {{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291}} → 6,145,291
  • Separator not found: {{#invoke:String2 |findlast | 5, 932, 992,532, 6,074,702, 6,145,291 |;}} → 5, 932, 992,532, 6,074,702, 6,145,291
  • One item list: {{#invoke:String2 |findlast | 6,074,702 }} → 6,074,702
  • List missing{{#invoke:String2 |findlast |}}
  • Space as separator: {{#invoke:String2 |findlast | 5 932 992,532 6,074,702 6,145,291 }} → 5 932 992,532 6,074,702 6,145,291

Any bug reports welcome. --RexxS (talk) 20:33, 19 November 2020 (UTC)[reply]

Upgrading posnq

Now supports named parameters: source, target, plain, nomatch; and UTC characters:

  • {{#invoke:String2 |posnq |source=This is a piece of text |target=ece}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=true}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |source=This is a piece of text |target=%s |plain=false}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |source=This is a piece of text |target=ece |nomatch=0}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |source=This is a piece of text |target=xyz |nomatch=0}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |This is a piece of text |" of" |true |0}}Script error: The function "posnq" does not exist.
  • {{#invoke:String2 |posnq |source=Meet at Café Nero |target=afé}}Script error: The function "posnq" does not exist.

Any bug reports welcome. --RexxS (talk) 00:08, 8 December 2020 (UTC)[reply]

Added matchAny function

I have added a matchAny function to the sandbox; it takes any number of patterns and returns the index of the first which matches, if any. Demo usage at Template:Infobox animanga/Header/sandbox. Comments welcome. (I'm a new template editor so I can make this change myself if no objections.) User:GKFXtalk 19:17, 8 April 2021 (UTC)[reply]