forked from MapComplete/MapComplete
Wikidata-input-field now interprets 'removePostfixes' and 'removePrefixes' as regexes instead of normal strings, fix #1340
This commit is contained in:
parent
c44fbac077
commit
0597d57326
2 changed files with 63 additions and 241 deletions
|
@ -257,11 +257,11 @@ class WikidataTextField extends TextFieldDef {
|
|||
[
|
||||
[
|
||||
"removePrefixes",
|
||||
"remove these snippets of text from the start of the passed string to search. This is either a list OR a hash of languages to a list",
|
||||
"remove these snippets of text from the start of the passed string to search. This is either a list OR a hash of languages to a list. The individual strings are interpreted as case ignoring regexes",
|
||||
],
|
||||
[
|
||||
"removePostfixes",
|
||||
"remove these snippets of text from the end of the passed string to search. This is either a list OR a hash of languages to a list",
|
||||
"remove these snippets of text from the end of the passed string to search. This is either a list OR a hash of languages to a list. The individual strings are interpreted as case ignoring regexes.",
|
||||
],
|
||||
[
|
||||
"instanceOf",
|
||||
|
@ -294,7 +294,8 @@ class WikidataTextField extends TextFieldDef {
|
|||
"square",
|
||||
"plaza",
|
||||
],
|
||||
"nl": ["straat","plein","pad","weg",laan"]
|
||||
"nl": ["straat","plein","pad","weg",laan"],
|
||||
"fr":["route (de|de la|de l'| de le)"]
|
||||
},
|
||||
|
||||
"#": "Remove streets and parks from the search results:"
|
||||
|
@ -360,34 +361,34 @@ Another example is to search for species and trees:
|
|||
if (searchFor !== undefined && options !== undefined) {
|
||||
const prefixes = <string[] | Record<string, string[]>>options["removePrefixes"] ?? []
|
||||
const postfixes = <string[] | Record<string, string[]>>options["removePostfixes"] ?? []
|
||||
const defaultValueCandidate = Locale.language.map((lg) => {
|
||||
const prefixesUnrwapped: RegExp[] = (
|
||||
Array.isArray(prefixes) ? prefixes : prefixes[lg] ?? []
|
||||
).map((s) => new RegExp("^" + s, "i"))
|
||||
const postfixesUnwrapped: RegExp[] = (
|
||||
Array.isArray(postfixes) ? postfixes : postfixes[lg] ?? []
|
||||
).map((s) => new RegExp(s + "$", "i"))
|
||||
let clipped = searchFor
|
||||
|
||||
Locale.language
|
||||
.map((lg) => {
|
||||
const prefixesUnrwapped: string[] = Array.isArray(prefixes)
|
||||
? prefixes
|
||||
: prefixes[lg] ?? []
|
||||
const postfixesUnwrapped: string[] = Array.isArray(postfixes)
|
||||
? postfixes
|
||||
: postfixes[lg] ?? []
|
||||
let clipped = searchFor
|
||||
|
||||
for (const postfix of postfixesUnwrapped) {
|
||||
if (searchFor.endsWith(postfix)) {
|
||||
clipped = searchFor.substring(0, searchFor.length - postfix.length)
|
||||
break
|
||||
}
|
||||
for (const postfix of postfixesUnwrapped) {
|
||||
const match = searchFor.match(postfix)
|
||||
if (match !== null) {
|
||||
clipped = searchFor.substring(0, searchFor.length - match[0].length)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Prefixes are: ", prefixesUnrwapped)
|
||||
for (const prefix of prefixesUnrwapped) {
|
||||
if (searchFor.startsWith(prefix)) {
|
||||
clipped = searchFor.substring(prefix.length)
|
||||
break
|
||||
}
|
||||
for (const prefix of prefixesUnrwapped) {
|
||||
const match = searchFor.match(prefix)
|
||||
if (match !== null) {
|
||||
clipped = searchFor.substring(match[0].length)
|
||||
break
|
||||
}
|
||||
return clipped
|
||||
})
|
||||
.addCallbackAndRun((clipped) => searchForValue.setData(clipped))
|
||||
}
|
||||
return clipped
|
||||
})
|
||||
|
||||
defaultValueCandidate.addCallbackAndRun((clipped) => searchForValue.setData(clipped))
|
||||
}
|
||||
|
||||
let instanceOf: number[] = Utils.NoNull(
|
||||
|
@ -425,7 +426,7 @@ class OpeningHoursTextField extends TextFieldDef {
|
|||
[
|
||||
[
|
||||
"prefix",
|
||||
"Piece of text that will always be added to the front of the generated opening hours. If the OSM-data does not start with this, it will fail to parse",
|
||||
"Piece of text that will always be added to the front of the generated opening hours. If the OSM-data does not start with this, it will fail to parse.",
|
||||
],
|
||||
[
|
||||
"postfix",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue