-module(hanguleam@batchim). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/hanguleam/batchim.gleam"). -export([get_batchim/1, has_batchim_with_options/2, has_batchim/1]). -export_type([batchim_only_filter/0, batchim_error/0, has_batchim_options/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type batchim_only_filter() :: single_only | double_only. -type batchim_error() :: empty_string | {invalid_character, binary()}. -type has_batchim_options() :: {has_batchim_options, batchim_only_filter()} | any_batchim. -file("src/hanguleam/batchim.gleam", 153). -spec get_batchim_type(list(binary())) -> hanguleam@types:batchim_type(). get_batchim_type(Components) -> case erlang:length(Components) of 1 -> single; 2 -> double; _ -> no_batchim end. -file("src/hanguleam/batchim.gleam", 161). -spec filter_batchim( hanguleam@types:batchim_type(), gleam@option:option(batchim_only_filter()) ) -> boolean(). filter_batchim(Batchim_type, Filter) -> case Filter of none -> Batchim_type /= no_batchim; {some, single_only} -> Batchim_type =:= single; {some, double_only} -> Batchim_type =:= double end. -file("src/hanguleam/batchim.gleam", 172). -spec get_batchim_index(integer()) -> integer(). get_batchim_index(Codepoint) -> case 28 of 0 -> 0; Gleam@denominator -> (Codepoint - 16#AC00) rem Gleam@denominator end. -file("src/hanguleam/batchim.gleam", 61). ?DOC( " Extracts detailed batchim information from the last character of a Korean string.\n" " Returns comprehensive information about the batchim including its type and components.\n" " This is an enhanced version that provides more detail than `has_batchim`.\n" "\n" " ## Return Value\n" "\n" " - `Ok(BatchimInfo)`: Contains character, batchim type, and component breakdown\n" " - `Error(EmptyString)`: Input string is empty\n" " - `Error(InvalidCharacter(char))`: Last character is not complete Hangul\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " get_batchim(\"강\")\n" " // -> Ok(BatchimInfo(character: \"강\", batchim_type: Single, components: [\"ㅇ\"]))\n" " \n" " get_batchim(\"값\")\n" " // -> Ok(BatchimInfo(character: \"값\", batchim_type: Double, components: [\"ㅂ\", \"ㅅ\"]))\n" " \n" " get_batchim(\"가\")\n" " // -> Ok(BatchimInfo(character: \"가\", batchim_type: NoBatchim, components: []))\n" " \n" " get_batchim(\"hello\")\n" " // -> Error(InvalidCharacter(\"o\"))\n" " \n" " get_batchim(\"\")\n" " // -> Error(EmptyString)\n" " \n" " get_batchim(\"한글\")\n" " // -> Ok(BatchimInfo(character: \"글\", batchim_type: Single, components: [\"ㄹ\"]))\n" " ```\n" ). -spec get_batchim(binary()) -> {ok, hanguleam@types:batchim_info()} | {error, batchim_error()}. get_batchim(Text) -> gleam@result:'try'( begin _pipe = gleam@string:last(Text), gleam@result:map_error(_pipe, fun(_) -> empty_string end) end, fun(Char) -> gleam@result:'try'( begin _pipe@1 = hanguleam@internal@unicode:get_codepoint_result_from_char( Char ), gleam@result:map_error( _pipe@1, fun(_) -> {invalid_character, Char} end ) end, fun(Codepoint) -> case hanguleam@internal@unicode:is_complete_hangul( Codepoint ) of false -> {error, {invalid_character, Char}}; true -> Batchim_index = get_batchim_index(Codepoint), gleam@result:'try'( begin _pipe@2 = hanguleam@internal@utils:get_value_by_index( Batchim_index, [<<""/utf8>>, <<"ㄱ"/utf8>>, <<"ㄲ"/utf8>>, <<"ㄳ"/utf8>>, <<"ㄴ"/utf8>>, <<"ㄵ"/utf8>>, <<"ㄶ"/utf8>>, <<"ㄷ"/utf8>>, <<"ㄹ"/utf8>>, <<"ㄺ"/utf8>>, <<"ㄻ"/utf8>>, <<"ㄼ"/utf8>>, <<"ㄽ"/utf8>>, <<"ㄾ"/utf8>>, <<"ㄿ"/utf8>>, <<"ㅀ"/utf8>>, <<"ㅁ"/utf8>>, <<"ㅂ"/utf8>>, <<"ㅄ"/utf8>>, <<"ㅅ"/utf8>>, <<"ㅆ"/utf8>>, <<"ㅇ"/utf8>>, <<"ㅈ"/utf8>>, <<"ㅊ"/utf8>>, <<"ㅋ"/utf8>>, <<"ㅌ"/utf8>>, <<"ㅍ"/utf8>>, <<"ㅎ"/utf8>>] ), gleam@result:map_error( _pipe@2, fun(_) -> {invalid_character, Char} end ) end, fun(Batchim) -> Components = begin _pipe@3 = Batchim, _pipe@4 = hanguleam@internal@unicode:disassemble_consonant_string( _pipe@3 ), gleam@string:to_graphemes(_pipe@4) end, Batchim_type = get_batchim_type(Components), {ok, {batchim_info, Char, Batchim_type, Batchim, Components}} end ) end end ) end ). -file("src/hanguleam/batchim.gleam", 138). ?DOC( " Checks if the last character of a Korean string has a batchim (final consonant) with filtering options.\n" " Returns `False` for empty strings, non-Korean characters, or incomplete Hangul.\n" " \n" " ## Options\n" " \n" " - `AnyBatchim`: Checks for any batchim (single or double)\n" " - `HasBatchimOptions(only: SingleOnly)`: Only single batchim\n" " - `HasBatchimOptions(only: DoubleOnly)`: Only double batchim\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import hanguleam/batchim.{AnyBatchim, HasBatchimOptions, SingleOnly, DoubleOnly}\n" " \n" " has_batchim_with_options(\"값\", AnyBatchim)\n" " // -> True\n" " \n" " has_batchim_with_options(\"갑\", HasBatchimOptions(only: SingleOnly))\n" " // -> True\n" " \n" " has_batchim_with_options(\"값\", HasBatchimOptions(only: SingleOnly))\n" " // -> False (값 has double batchim ㅂ+ㅅ)\n" " \n" " has_batchim_with_options(\"값\", HasBatchimOptions(only: DoubleOnly))\n" " // -> True\n" " ```\n" ). -spec has_batchim_with_options(binary(), has_batchim_options()) -> boolean(). has_batchim_with_options(Text, Options) -> Filter@1 = case Options of any_batchim -> none; {has_batchim_options, Filter} -> {some, Filter} end, case get_batchim(Text) of {ok, Info} -> filter_batchim(erlang:element(3, Info), Filter@1); {error, _} -> false end. -file("src/hanguleam/batchim.gleam", 108). ?DOC( " Checks if the last character of a Korean string has a batchim (final consonant).\n" " Returns `False` for empty strings, non-Korean characters, or incomplete Hangul.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " has_batchim(\"값\")\n" " // -> True\n" " \n" " has_batchim(\"토\")\n" " // -> False\n" " \n" " has_batchim(\"hello\")\n" " // -> False\n" " ```\n" ). -spec has_batchim(binary()) -> boolean(). has_batchim(Text) -> has_batchim_with_options(Text, any_batchim).