search-string icon indicating copy to clipboard operation
search-string copied to clipboard

Does addEntry work with toString?

Open louischoi opened this issue 6 years ago • 4 comments

When trying the mentioned functions with quotes, I am confused where I thought the mixed quotes would be preserved after parsing the stringified query. Is the following behavior intended? or I simply misused the functions?

var SearchString = require("search-string")
const str1 = "";
const searchString1 = SearchString.parse(str1);
searchString1.addEntry('from', '\'"\'"', false);
console.log(searchString1.getConditionArray()[0].value) // '\"'\"

const str2 = searchString1.toString();
const searchString2 = SearchString.parse(str2);
console.log(searchString2.getConditionArray()[0].value) // \"\"

louischoi avatar Jan 02 '19 14:01 louischoi

This looks like the quotes are preserved in the first example to me?

var SearchString = require("search-string")
const str1 = "";
const searchString1 = SearchString.parse(str1);
searchString1.addEntry('from', '\'"\'"', false);
console.log(searchString1.getConditionArray()[0].value) // '\"'\"

const val = searchString1.getConditionArray()[0].value;
console.log(val.length) // 4.

Let me know if this isn't what you expected? The first string is preserved.

ttacon avatar Jan 03 '19 22:01 ttacon

@ttacon Oh sorry for the confusion, the strings are not separate, I only have issue on searchString2 (because I somehow can't .parse() a string with mixed quotes directly so I used this approach to parse it).

...
searchString1.addEntry('from', '\'"\'"', false); // a length 4 string
...
console.log(searchString2.getConditionArray()[0].value) // \"\"
console.log(searchString2.getConditionArray()[0].value.length) // 2

Edit: I would expect the length of searchString2.getConditionArray()[0].value is still 4

louischoi avatar Jan 03 '19 23:01 louischoi

Ah, my apologies for not clearly reading the second example 😅. I believe that this is currently functioning as intended (treating the initial nested " as a token and then appending the last one, thus stripping the single quotes). Definitely interested to hear more if you have a use-case that needs this preservation of quoting?

ttacon avatar Jan 04 '19 14:01 ttacon

I'm trying to follow

treating the initial nested " as a token and then appending the last one, thus stripping the single quotes

Would you mind walking me another example?

let searchString = SearchString.parse(`to:a"b'"c'`); // 7 characters, 2 single quotes, 1 in between, 1 at last
console.log(searchString.getConditionArray()[0].value); // ab'c

Ultimately I wish to see a"b'"c', and I can understand if it shows up as either ab'c' or even abc (by eliminating quote pairs). But now I'm scratching my head because I'm actually seeing ab'c, in which the double quote pair disappears along with the single quote at the last position. Why is the single quote in between the string got preserved but not the last one?

Speaking of use-cases, I plan to use the search keywords to perform character-by-character exact match searching, so if a document actually contains quote pairs, I might not be able to find it when my query string include those quotes. That being said, I could just avoid typing the whole sentence, but the inner phrase instead to workaround this.

louischoi avatar Jan 05 '19 11:01 louischoi