Module:Str find word/doc: Difference between revisions

Content deleted Content added
...
 
(593 intermediate revisions by 3 users not shown)
Line 1:
{{Lua|Module rating:String|pre-alphaModule:TableTools|Module:Yesno}}
Implements template '''{{tl|Str find word}}'''.
:[[:Module:String#find]]
:[[:mw:Extension:Scribunto/Lua_reference_manual#Patterns]]
:[[User:DePiep/sfw]]
:[[User:DePiep/sfw/sandbox]]
 
This module islooks intendedfor toa findword ''completebeing words''present in a stringcomma-separated list of words. ForIt example,then inreturns the stringa '{{monocode|1=alpha, foobarTrue}}' the wordor '{{monocode|1=alphaFalse}}' appearsvalue. By default, but the wordTrue-value '{{mono|1=foo}}'returned doesis not:the therefound onlyword isitself; the completeFalse-value wordis '{{mono|1=foobar}}'a blank string.
 
For example, in the source string '{{mono|1=foo, bar}}' the word '{{mono|1=bar}}' appears, but the word '{{mono|1=november}}' does not.
A '''word''' is defined to be consisting of: alphanumerics (a-z, A-Z, 0-9), the hyphen (-) and the underscore (_). For example: {{mono|1=foobar, foo-bar, foo_bar, foo123, Foo, FOO, _foo, 12, -x}}. All other characters are non-word characters ({{nowrap|1='{{mono|1=, ; ( ) <space>}}'}}).
:<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}}
:<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}}
 
''Multiple word check'': The search can be extended to check for multiple words being present.
The module is designed for template code usage. '''For example''', your template uses this ''format options'' as single parameter input:
:{{para|1=format|2=bg-green, ucfirst, wikilink}}
Then you can handle formatting:
:<code><nowiki>{{#invoke:sfw|main|source={{{format|}}}|word=bg-green|yes=&lt;make bg green code&gt;|no=&lt;do nothing&gt;}}</nowiki></code>
 
''AND''-list of words, all words must be present:
<span style="font-size:90%;">(The alternative is to encode and use three parameters: {{para|bg|green}}, {{para|case|ucfirst}}, {{para|link|wikilink}}).</span>
:<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}}
:<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:
All this is more article-editor-friendly (one parameter, and type the word). Also, you have the "and" & "or" option (see below).
:<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}}
:<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)
== Usage ==
Whitespace is trimmed,
===Basic===
 
The module is aimed at template code usage. <small>(The editor does not have to apply Lua patterns like {{code|1=[%a%d]*}})</small>.
}}
==Parameters==
: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''?===
<pre>
{{Str find word
|source =
|word =
}}
|yes =
|no =
</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}}
 
:{{para|source}}: long source string to search in
:{{para|word}}: ''word'' to search for
:{{para|yes}}: return value when word is found
:{{para|no}}: return value when word is not foound
 
===True/False return value===
{| class="wikitable" style=""
: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.
! {{para|1=source}}
 
! {{para|1=word}}
{| class="wikitable"
! result
! note
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foo}}
| {{#invoke:User:DePiep/sfwStr find word |main |explain=true |source=alpha, foo, lima |word=foo|yes=yes|no=no}}
| {{aye}}
|
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|bar}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo-bar}}
| {{nay}}
| full word ''bar'' not in the source
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobar}}
| {{#invoke:User:DePiep/sfwStr find word |main |explain=true |source=alpha, foo, lima|word=foobar|yes=yes|no=no}}
| {{nay}}
| word ''foobar'' not in the source as full word
|}
 
===AND-wordlist checks===
:{{para|word}} can be a ''list of words''. All will be checked with the ''AND''-requirement (each word must be present).
:{{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}}
! {{para|1=andwords}}<br/>{{para|1=word}}
! result
! 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 |andw|bar, april}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
|
|}
 
===OR-wordlist checks===
;{{para |orwords}}, {{para |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=orwords}}
! result
! note
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
|
| {{#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}}
|
|
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
|
|-
| {{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
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
|
| {{#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=sources|2=alpha, foobarfoo, lima, delta}}
| {{para |wordandw|foobar, april}}
| {{para |orw|nov, dec}}
| {{#invoke:User:DePiep/sfw|main|source=alpha, foobar, lima|word=foo|yes=yes|no=no}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| 'foo' is not the complete word
| none of the OR-words is present, so false
|}
 
===both AND-words and OR-words to check===
===More options===
;{{para |andwords}} {{para |orwords}}
:Short parameter names, case-sensitive
:checks ''both'' lists for their own check: "ANd-words" must all be present, "OR-words" must have at least one present.
<pre>
:Result: True if both check results are True, otherwise False.
|s =
|w =
|case=
|yes =
|no =
</pre>
 
{| class="wikitable" style=""
:{{para|source}} or {{para|s}}
|-
:{{para|word}} or {{para|w}}
:! {{para|yes1=source}}<br/>{{para|1=s}}
:! {{para|no1=andwords}}<br/>{{para|1=word}}
! {{para|1=orwords}}
! result
! 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
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
|
|
| {{#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)
: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=&lt;blank>}}
| {{para|word|beta}}
| {{#invoke:Str find word |main |source= |word=beta}}
| {{nay}}
|-
| {{para|1=source|2=alpha, beta, gamma}}
| {{para|word|&lt;blank>}}
| {{#invoke:Str find word |main |alpha, beta, gamma |word=}}
| {{nay}}
|}
<noinclude>
==untested; noinclude from main /doc==
: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=""
Line 74 ⟶ 247:
! {{para|1=source}}<br/> {{para|1=s}}
! {{para|1=word}}<br/>{{para|1=w}}
! {{para|1=case}}
! result
! note
Line 79 ⟶ 253:
| {{para|1=s|2=alpha, foo, lima}}
| {{para|w|foo}}
|
| {{#invoke:User:DePiep/sfw|main|s=alpha, foo, lima|w=foo|yes=yes|no=no}}
| &rarr; {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima|w=foo|out-true=yes|out-false=no}}
|
|-
| {{para|1=sources|2=alpha, foo, lima}}
| {{para|wordw|foobar}}
|
| {{#invoke:User:DePiep/sfw|main|source=alpha, foo, lima|word=foobar|yes=yes|no=no}}
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|out-true=yes|out-false=no}}
|
|-
| {{para|1=sources|2=alpha, foobar, lima}}
| {{para|wordw|foo}}
|
| {{#invoke:User:DePiep/sfw|main|source=alpha, foobar, lima|word=foo|yes=yes|no=no}}
| &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=sources|2=alpha, FOO, lima}}
| {{para|wordw|FOOfoo}}
|
| {{#invoke:User:DePiep/sfw|main|source=alpha, FOO, lima|word=foo|yes=yes|no=no|case=false}}
| &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}} ''(default)''
| {{para|case|false}}: a=A ''(default)''
|-
| {{para|1=sources|2=alpha, FOO, lima}}
| {{para|wordw|FOOfoo}}
|
| {{#invoke:User:DePiep/sfw|main|source=alpha, FOO, lima|word=foo|yes=yes|no=no|case=true}}
| &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}}
| {{para|case|true}}: a=/=A
|}
 
===Rephrase True, False result: {{para|1=out-true=, out-false|2=}}===
==parameters==
{| class="wikitable"
<pre>
|-
{{#invoke|main|
! colspan="5" style="padding-left:3em; text-align:left;" | {{para|out-true|out-true, a hit}}<br/>{{para|out-false|out-falset found}}
|source =
|-
|word =
| {{para|1=source|2=alpha, foo, lima}}
|andwords =
| {{para|word|foo}}
|orwords =
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo|out-true=Yes, a hit|out-false=No, not found}}
|case =
| {{aye}}
|yes =
|-
|no =
| {{para|1=source|2=alpha, foo, lima}}
|dbg =
| {{para|word|foobar}}
}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|out-true=Yes, a hit|out-false=No, not found}}
</pre>
| {{nay}}
<pre>
|}
{{#invoke|main|
|s =
|w =
|andw =
|orw =
|case =
|yes =
|no =
|dbg =
}}
</pre>
 
===boolean words===
<pre>
;{{para|booleans}}
{{#invoke|main|
:{{para|booleans|true}} will convert ''all boolean words'' into "True/False".
|1 =
:per [[:module:Yesno]] ({{code|1=Yes, true, yes, 1}} = true) (todo: document value 2?, nil, blanketc. Irrespective of case?)
|2 =
:other words not changed ("horse" remains horse, not T/F).
|3 =
:Note: per logic definition, the ''word'' "false" is returned (as being found).
|4 =
 
|5 =
{| class="wikitable"
|6 =
|7 =-
! colspan="5" style="padding-left:3em; text-align:left;" | {{para|1=booleans|2=true}}
|8 =
|-
}}
| {{para|1=source|2=YeS}}
</pre>
| {{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===
:under construction
 
===synonyms===
:under construction
</noinclude>
 
==Errors and analysis==
* The logic itself does not produce errors. There is always a True of False result.
 
* Pages with Unknown parameters are listed in {{clc|Category:Pages using str find word with unknown parameters}}.
 
:There are no logical errors foreseen: any flaw in the input will return a False result (for example, no input strings to check were entered).
<includeonly>{{Sandbox other||
<!-- Categories below this line; interwikis at Wikidata -->
 
* {{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}}
<includeonly>{{sandbox other||
[[:Category:Modules that manipulate strings]]
}}</includeonly>