Module:Str find word/doc: Difference between revisions

Content deleted Content added
...
 
(107 intermediate revisions by 2 users not shown)
Line 1:
{{Lua|Module:String|Module:TableTools|Module:Yesno}}
:TOC: [[#code]] ({{edit|Module:Str find word}})
Implements template '''{{Module:tl|Str find word/links/doc}}'''.
 
{{Module rating|pre-alpha}}
This module looks for a word being present in a comma-separated list of words. It then returns a {{code|1=True}} or {{code|1=False}} value. By default, the True-value returned is the found word itself; the False-value is a blank string.
{{Lua|Module:Yesno|Module:TableTools|Module:String}}
This module looks for a word being present in a comma-separated list of words. It then returns a {{code|True}} or {{code|False}} value.
By default, the True-value returned is the found word itself; the False-value is a blank string. Implements {{tl|Str find word}}.
 
For example, in the source string '{{mono|1=foo, bar}}' the word '{{mono|1=bar}}' appears, but the word '{{mono|1=november}}' does not.
:True: <code><nowiki>{{#invoke:Str find word |main |source=foo, bar |word=bar}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; >{{#invoke:Str find word |main |source=foo, bar |word=bar}}<
:False: <code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |word=november}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; >{{#invoke:Str find word |main |source=alpha, beta, gamma |word=november}}<
 
Complete words: the complete word is '{{mono|1=foobar}}' ... example needed ...
 
''Multiple word check'': The search can be extended to check for multiple words being present.
 
''AND''-list of words, all words must be present:
:True: <code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; >{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}}<
:False: <code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; >{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}}<
 
''OR''-list of words, at least one off the words must be present:
:True: <code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; >{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}}<
:False: <code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; >{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}}<
 
Complete words: the complete word is '{{mono|1=foobar}}' (example needed)
Whitespace is trimmed,
 
The module is aimed at template code usage. <small>(The editor does not have to apply Lua patterns like {{code|1=[%a%d]*}})</small>.
}}
== Usage ==
==Parameters==
:{{red|This documentation is not up to date}}. See template {{tl|Str find word}}.
:Main parameters (including their synonyms)
{|
|<pre>
{{Str find word
|source= s=
|word= andwords= andw=
|orwords= orw=
|case=
|out-true=
|out-false=
}}
</pre>
|}
<pre>{{Str find word |s= |w= |andw= |orw= |case= |out-true= |out-false=}}</pre>
 
===Basic: ''word'' is in ''source-wordlist''?===
Core function:
<pre>
{{Str find word
Line 34 ⟶ 46:
}}
</pre>
:<code><nowiki>{{Str find word |source=cow, horse, dog, fish |word=dog}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{Str find word |source=cow, horse, dog, fish |word=dog}}
:<code><nowiki>{{Str find word |source=cow, horse, dog, fish |word=cat}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{Str find word |source=cow, horse, dog, fish |word=cat}}
 
 
:When the ''word'' is found in the ''source'' (True), the function returns the ''word'' itself (by default), or {{para|yes}} value (when entered non-empty).
===True/False return value===
:When not found (False), the function returns a empty string ({{code|''}}), or the {{para|no}} value when entered.
:When the ''word'' is found in the ''source'' (True), the function returns the ''word'' itself (by default), or {{para|out-true}} value (when entered non-empty).
:When not found (False), the function returns a empty string ({{code|''}}), or the {{para|out-false}} value when entered.
 
{| class="wikitable"
Line 42 ⟶ 58:
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foo}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo}}
| {{aye}}
|
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobarbar}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo-bar|yes=Yes, hit|no=No, not found}}
| {{nay}}
| full word ''foo-bar'' not in the source
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobar}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo-bar, lima|word=foo|yes=Yes, hit|no=No, not found}}
| {{nay}}
| word ''foofoobar'' not in the source as full word
|}
 
===AllAND-wordlist parameterschecks===
:{{para|word}} can be a ''list of words''. All will be checked with the ''AND''-requirement (each word must be present).
:All parameters, in three options
:{{para |andwords}} accepts a ''list of words'' that all will be checked with the ''AND''-requirement (each word must be present).
{|
:Alway, ''both'' {{para|word}} and {{para |andwords}} are combined into one single ''AND''-wordlist.
 
{| class="wikitable" style=""
|-
! {{para|1=source}}<br/>{{para|1=s}}
|<pre>
! {{para|1=andwords}}<br/>{{para|1=word}}
{{Str find word
! result
|string =
! note
|word =
|-
|andwords =
| {{para|1=s|2=alpha, foo, lima, delta}}
|orwords =
| {{para |andw|delta, lima}}
|case=
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
|yes =
| both words present in the source
|no =
|-
|explain =
| {{para|1=s|2=alpha, foo, lima, delta}}
}}
| {{para |andw|foo, nov}}
</pre>
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
|<pre>
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
{{Str find word
|-
|s =
| {{para|1=s|2=alpha, foo, lima, delta}}
|w =
| {{para |andw=|delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
|orw =
| {{para|w}} and {{para |andw}} are combined
|case=
|-
|yes =
| {{para|1=s|2=alpha, foo, lima, delta}}
|no =
| {{para |andw|bar, april}}
|explain =
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
}}
|
</pre>
|<pre>
{{Str find word
|s= string =
|word = andwords= andw=
|orwords = orw=
|case=
|yes =
|no =
|explain =
<!-- |SEP=todo -->
}}
</pre>
|}
<pre>{{Str find word |s= |w= |andw= |orw= |case= |yes= |no= |explain=}}</pre>
 
===MoreOR-wordlist optionschecks===
:''todo'' ;{{para|yes, no|orwords}}, return{{para options|orw}}
:Accepts a ''list of words'' that all will be checked by the ''OR-requirement'' ("at least one of the words must be present).
 
{| class="wikitable" style=""
|-
! {{para|1=source}}<br/> {{para|1=s}}
!
! {{para|1=word}}<br/>{{para|1=w}}
! {{para|1=caseorwords}}
! result
! note
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
| {{para|w|foo}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |wandw=foo|yes=yes|no=nodelta, lima}}
| both words present in the source
|
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
| {{para|w|foobar}}
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima|word=foobar|yes=yes|no=no}}
|
|-
| {{para|1=s|2=alpha, foobarfoo, lima, delta}}
| {{para |wandw|foodelta}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |sources=alpha, foobarfoo, lima, delta |word=foo|yes=yes|noandw=nodelta}}
| {{para|w}} and {{para |andw}} are combined
| 'foo' is not the complete word
|-
| {{para|1=s|2=alpha, FOOfoo, lima, delta}}
| {{para|w|foo}}
|
| {{para |orw|delta, lima}}
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, FOO, lima|word=foo|yes=yes|no=no|case=false}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| {{para|case|false}}: a=A ''(default)''
| both OR-words are present in the source
|-
| {{para|1=s|2=alpha, FOOfoo, lima, delta}}
| {{para|w|foo}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, FOO, lima|word=foo|yes=yes|no=no|case=true}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| {{para|case|true}}: a=/=A
| OR-logic: one of the words is present ({{para|foo}}), so true
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|alpha, lima}}
| {{para |orw|foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
|
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|bar, april}}
| {{para |orw|nov, dec}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| none of the OR-words is present, so false
|}
 
===both AND-,words and OR-wordlistwords to checkscheck===
;{{para |andwords}} {{para |orwords}}
:{{para|word}} as noted, can be a ''list of words''. All will be checked with the ''AND''-requirement (each word must be present).
:{{para|andwords}} accepts achecks ''listboth'' oflists for their own check: "ANd-words''" thatmust all will be checkedpresent, with"OR-words" themust ''AND''-requirementhave (eachat wordleast must beone present).
:Result: True if both check results are True, otherwise False.
::When both {{para|word}}, {{para|andwords}} are present, they are combined into one single ''AND''-wordlist.
:{{para|orwords}} accepts a ''list of words'' that all will be checked with the ''OR''-requirement (at least one of the words must be present).
 
{| class="wikitable" style=""
|-
! {{para|1=source}}<br/> {{para|1=s}}
! {{para|1=andwords}}<br/>{{para|1=andwword}}
! {{para|1=orwords}}<br/>{{para|1=orw}}
! result TODO fix
! note
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|delta, lima}}
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| both words present in the source
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|foo, nov}}
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|delta}}
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{para|w}} and {{para |andw}} are combined
 
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
| {{para |orw|delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| both OR-words are present in the source
|-
Line 186 ⟶ 203:
|
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| OR-logic: one of the words is present ({{para|foo}}), so true
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|alpha, lima}}
| {{para |orw|foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
|
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|bar, april}}
| {{para |orw|nov, dec}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| none of the OR-words is present, so false
|}
 
===Edge cases ===
 
;Blank list(s)
==True, False rephrase options {{para|yes, no}}
:Empty input string(s) will conclude negative:
{| class="wikitable"
|-
! colspan="5" style="padding-left:3em; text-align:left;" | {{para|1=source, andwords, orwords|2=&lt;blank>}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima|word=}}
| {{nay}}
| no {{para|word}} entered
|-
| {{para|1=source|2=alpha, foo, lima&lt;blank>}}
| {{para|word|foobarbeta}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima|word=foo-bar|yes=Yes, hit|no=No, not foundbeta}}
| {{nay}}
| full word ''foo-bar'' not in the source
|-
| {{para|1=source|2=alpha, foobeta, limagamma}}
| {{para|word|foobar&lt;blank>}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo-barbeta, limagamma |word=foo|yes=Yes, hit|no=No, not found}}
| {{nay}}
|}
| word ''foo'' not in the source as full word
<noinclude>
|- <!-- ===== ===== ===== ===== ===== ===== ===== -->
==untested; noinclude from main /doc==
! colspan="5" style="padding-left:3em; text-align:left;" | {{code|1=...}} {{para|yes|Yes, hit}} {{para|no|Not found}}
:As of Mar 2023, not supported.
===casesensitive ===
:{{para|casesensitive|true}}, {{para|case|true}} Case-sensistive (A=/=a) DEFAULT
:{{para|casesensitive|false}}, {{para|case|false}} Not case-sensistive (A=a)
 
{| class="wikitable" style=""
|-
! {{para|1=source}}<br/> {{para|1=s}}
! {{para|1=word}}<br/>{{para|1=w}}
! {{para|1=case}}
! result
! note
|-
| {{para|1=s|2=alpha, foo, lima}}
| {{para|w|foo}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima|w=foo|out-true=yes|out-false=no}}
|
|-
| {{para|1=s|2=alpha, foo, lima}}
| {{para|w|foobar}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|out-true=yes|out-false=no}}
|
|-
| {{para|1=s|2=alpha, foobar, lima}}
| {{para|w|foo}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, foobar, lima |word=foo|out-true=yes|out-false=no}}
| 'foo' is not the complete word
|-
| {{para|1=s|2=alpha, FOO, lima}}
| {{para|w|foo}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=false}}
| {{para|case|false}}: a=A ''(default)''
|-
| {{para|1=s|2=alpha, FOO, lima}}
| {{para|w|foo}}
|
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=true}}
| {{para|case|true}}: a=/=A
|}
 
===Rephrase True, False result: {{para|1=out-true=, out-false|2=}}===
{| class="wikitable"
|-
! colspan="5" style="padding-left:3em; text-align:left;" | {{para|out-true|out-true, a hit}}<br/>{{para|out-false|out-falset found}}
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foo}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo|yesout-true=Yes, a hit|noout-false=No, not found}}
| {{aye}}
|
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobar}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|yesout-true=Yes, a hit|noout-false=No, not found}}
| {{nay}}
|
|}
 
===boolean words===
===Edge cases: (blank input,) ===
;{{para|booleans}}
:''todo''
:{{para|booleans|true}} will convert ''all boolean words'' into "True/False".
==issues and ideas==
:per [[:module:Yesno]] ({{code|1=Yes, true, yes, 1}} = true) (todo: document value 2?, nil, blanketc. Irrespective of case?)
* Keep the order, Foo, bar -> foo(1), bar(2)
:other words not changed ("horse" remains horse, not T/F).
::eg table col userinput options, then cols in that order?
:Note: per logic definition, the ''word'' "false" is returned (as being found).
 
{| class="wikitable"
* Check comma example above ({{code|1="Hello, world"}}) (currently returns: 2 hitwords & sep=comma Dec2022)
|-
! colspan="5" style="padding-left:3em; text-align:left;" | {{para|1=booleans|2=true}}
|-
| {{para|1=source|2=YeS}}
| {{para|1=word|2=true}}
| {{#invoke:Str find word |main |booleans=true |source=YeS |word=1}}
|
|-
| {{para|1=source|2=0}}
| {{para|1=word|2=FalSe}}
| {{#invoke:Str find word |main |booleans=true |source=0 |word=FalSe}}
| ''note:'' the ''word'' "false" is found and returned
|}
 
===quotes, escape===
*Make sure all boolean logic is by [[:Module:Yesno]]
:under construction
:''and'' resolve {{code|nil}}
 
===synonyms===
*option {{para|sep}} for output list >,&nbsp;< dflt
:under construction
</noinclude>
 
==Errors and analysis==
Line 261 ⟶ 336:
* {{para|explain|true}}: when in {{button|Preview}}, display a report on input, output and the logical steps (debug).
* {{para|explain|testcases}}: When set in ns {{mono|1=Template}} or {{mono|1=User}} on subpage {{mono|1=/testcases}}, the Preview report is static (shown permanently).
{{String-handling templates|state=collapsed}}
===code===
<includeonly>{{sandbox other||
[[:Category:Modules that manipulate strings]]