lib/fat_free_crm/core_ext/string.rb
def name_permutations
    parts = split(" ")
    Array.new((parts.size - 1)) do |i|
      # ["A", "B", "C", "D"]  =>  [["A B C", "D"], ["A B", "C D"], ["A", "B C D"]]
      [parts[(0..i)].join(" "), parts[(i + 1)..-1].join(" ")]
    end.each_with_object([]) do |perm, arr|
      # Search both [first, last] and [last, first]
      # e.g. for every ["A B C", "D"], also include ["D", "A B C"]
      arr << perm
      arr << perm.reverse
      arr
    end
  end
# Generates all permutations for first and last name, based on the order of parts
# A query with 4 words will generate 6 permutations
Generates all permutations for first and last name, based on the order of parts
A query with 4 words will generate 6 permutations
Generates all permutations for first and last name based on the order of parts A query with 4 words will generate 6 permutations
def name_permutations
    parts = split(" ")
    Array.new((parts.size - 1)) do |i|
      [parts[(0..i)].join(" "), parts[(i + 1)..-1].join(" ")]
    end.each_with_object([]) do |perm, arr|
      arr << perm
      arr << perm.reverse
      arr
    end
  end
def name_permutations parts = split ( " " ) Array . new ( ( parts . size - 1 ) ) do | i | [ parts [ ( 0 .. i ) ] . join ( " " ) , parts [ ( i + 1 ) .. - 1 ] . join ( " " ) ] end . each_with_object ( [ ] ) do | perm , arr | arr << perm arr << perm . reverse arr end end
Generates all permutations for first and last name, based on the order of parts
A query with 4 words will generate 6 permutations
Generates all permutations for first and last name based on the order of parts A query with 4 words will generate 6 permutations
# ["A", "B", "C", "D"]  =>  [["A B C", "D"], ["A B", "C D"], ["A", "B C D"]] # Search both [first, last] and [last, first] # e.g. for every ["A B C", "D"], also include ["D", "A B C"]