Skip to content

Instantly share code, notes, and snippets.

@jheth
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheth/f5e0d2e12ee529092ed5 to your computer and use it in GitHub Desktop.
Save jheth/f5e0d2e12ee529092ed5 to your computer and use it in GitHub Desktop.
Dentaku Search
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