A
arktype•8mo ago
francis

Is there a way to do a case insensitive literal match?

I can pipe a string.lower to a lower-case literal but that seems complicated for this.
16 Replies
ssalbdivad
ssalbdivad•8mo ago
You could use a regex like type(/^foo$/i)
francis
francisOP•8mo ago
hm, which is uglier:
optionOne: /^success$/i,
optionTwo: type("string.lower").pipe(type("'success'")),
optionOne: /^success$/i,
optionTwo: type("string.lower").pipe(type("'success'")),
šŸ¤”
ssalbdivad
ssalbdivad•8mo ago
You could do type("string.lower").to("'success'") They're not equivalent though That one will always return "success" The regex would validate that if it were lowercase it would be success, but it would return the original string Also IMO neither of them are that ugly, I can't really imagine a much cleaner API for something like that šŸ˜…
francis
francisOP•8mo ago
hm... and now how about if I wanted to ignore whitespace as well? 🫠 this doesn't seem to do what I expect: let temp = type('string.trim').to('string.lower').to("'success'")
ssalbdivad
ssalbdivad•8mo ago
In what way does it not do what you expect
francis
francisOP•8mo ago
ah, so, it only applies the first and last operations in the pipeline
ssalbdivad
ssalbdivad•8mo ago
What does?
francis
francisOP•8mo ago
if I have string.trim first and string.lower second, success validates but Success does not in the inverse case, Success validates but success doesn't
ssalbdivad
ssalbdivad•8mo ago
That does seem wrong I will look into that
francis
francisOP•8mo ago
here's a one liner to try: type('string.lower').to('string.trim').to("'success'")('Success') vs type('string.lower').to('string.trim').to("'success'")('success ') those should both pass, but only one does this is with latest RC.12
ssalbdivad
ssalbdivad•8mo ago
I suppose it would be more efficient anyways to just do type("string").pipe(s => s.trim().toLowerCase()).to("'success'")
francis
francisOP•8mo ago
and it's order dependent! if you put string.trim first, it inverts the success and error from the example above
ssalbdivad
ssalbdivad•8mo ago
Hmm yeah it's something to do with the redundant intermediate validation. This works
const tt = type("string.lower")
.pipe(s => s.trim())
.to("'success'")
const tt = type("string.lower")
.pipe(s => s.trim())
.to("'success'")
I'm off this weekend but if you want to open an issue I will fix this in the next release
francis
francisOP•8mo ago
I'll add one - thank you!
francis
francisOP•8mo ago
GitHub
Pipeline of string keywords appears to skip intermediate options Ā· ...
Report a bug šŸ”Ž Search Terms string, keywords, string.lower, string.trim, case 🧩 Context OS: macOS 15.0 CPU: (10) arm64 Apple M1 Pro Memory: 92.36 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node:...
ssalbdivad
ssalbdivad•8mo ago
Thanks!

Did you find this page helpful?