Last active
August 29, 2015 14:17
-
-
Save jheth/f5e0d2e12ee529092ed5 to your computer and use it in GitHub Desktop.
Dentaku Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = Dentaku::Calculator.new | |
c.add_function( | |
name: :blank, | |
type: :logical, | |
signature: [:non_group], | |
body: ->(var) { | |
ap "Value: #{var}" | |
return (var.to_s.strip.empty?) | |
} | |
) | |
c.evaluate("BLANK(foo)", foo: nil) | |
# SEARCH | |
c = Dentaku::Calculator.new | |
c.add_function( | |
name: :search, | |
type: :logical, | |
signature: [:string, :string], | |
body: ->(needle, haystack) { | |
return false if needle.nil? || haystack.nil? | |
return false unless needle.is_a?(String) && haystack.is_a?(String) | |
found = false | |
if needle.include?("%") | |
value_without_wildcards = needle.gsub("%", "") | |
if needle.start_with?("%") && needle.end_with?("%") | |
found = haystack.include?(value_without_wildcards) | |
elsif needle.end_with?("%") | |
found = haystack.index(value_without_wildcards) == 0 | |
elsif needle.start_with?("%") | |
found = !!(haystack =~ /#{value_without_wildcards}$/) | |
end | |
else | |
found = haystack.include?(needle) | |
end | |
return found | |
} | |
) | |
c.evaluate("SEARCH(needle, haystack)", needle: "str", haystack: "string") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment