###*
 * Apeman string module
 * @name apeman.string
###
ap.string = ((string)->

  PATTERNS = {
    ZENKAKU: /[！-～]/g,
    HANKAKU: /[\!-\~]/g,
    HIRAGANA: /[ぁ-ん]/g,
    KATAKANA: /[ァ-ン]/g
  }
  # Covert to ひらがな.
  string.toHiragana = (str) ->
    str && str.replace(PATTERNS.KATAKANA, ($0) ->
      String.fromCharCode($0.charCodeAt(0) - 0x60)
    )
  
  # Convert to カタカナ.
  string.toKatakana = (str) ->
    str && str.replace(PATTERNS.HIRAGANA, ($0) ->
      String.fromCharCode($0.charCodeAt(0) + 0x60)
    )
  
  #* Convert to 半角.
  string.toHankaku = (str) ->
    str && str.replace(PATTERNS.ZENKAKU, ($0) ->
      String.fromCharCode($0.charCodeAt(0) - 0xFEE0)
    )
  
  # Convert to 全角.
  string.toZenkaku = (str) ->
    str && str.replace(PATTERNS.HANKAKU, ($0) ->
      String.fromCharCode($0.charCodeAt(0) + 0xFEE0)
    )
  
  # 曖昧一致
  string.ambiguousMatch = (searchWord, pattern) ->
    return null unless searchWord
    return null unless pattern
  
    toHiragana = string.toHiragana
    toKatakana = string.toKatakana
    toZenkaku = string.toZenkaku
    toHankaku = string.toHankaku
  
    matchable = toZenkaku(toHiragana(searchWord.trim()))
    for convert in [toHiragana, toKatakana, toZenkaku, toHankaku]
      matched = convert(pattern).match(matchable)
      return matched if matched
    null
  string
)({})

