-module(nori@codegen@typescript@shared). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/nori/codegen/typescript/shared.gleam"). -export([type_ref_to_ts/1, to_pascal_case/1, to_camel_case/1, sanitize_identifier/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(" Shared utilities for all TypeScript code generators.\n"). -file("src/nori/codegen/typescript/shared.gleam", 26). -spec primitive_to_ts(nori@codegen@ir:primitive_type()) -> binary(). primitive_to_ts(Prim) -> case Prim of p_string -> <<"string"/utf8>>; p_int -> <<"number"/utf8>>; p_float -> <<"number"/utf8>>; p_bool -> <<"boolean"/utf8>>; p_date_time -> <<"string"/utf8>>; p_date -> <<"string"/utf8>>; p_binary -> <<"Blob"/utf8>>; p_unit -> <<"void"/utf8>> end. -file("src/nori/codegen/typescript/shared.gleam", 12). ?DOC(" Convert a TypeRef to its TypeScript type syntax.\n"). -spec type_ref_to_ts(nori@codegen@ir:type_ref()) -> binary(). type_ref_to_ts(Ref) -> case Ref of {named, Name} -> Name; {primitive, Prim} -> primitive_to_ts(Prim); {array, Item} -> <<<<"Array<"/utf8, (type_ref_to_ts(Item))/binary>>/binary, ">"/utf8>>; {dict, Key, Value} -> <<<<<<<<"Record<"/utf8, (type_ref_to_ts(Key))/binary>>/binary, ", "/utf8>>/binary, (type_ref_to_ts(Value))/binary>>/binary, ">"/utf8>>; {nullable, Inner} -> <<(type_ref_to_ts(Inner))/binary, " | null"/utf8>>; {optional, Inner@1} -> <<(type_ref_to_ts(Inner@1))/binary, " | undefined"/utf8>>; {literal, Value@1} -> <<<<"\""/utf8, Value@1/binary>>/binary, "\""/utf8>>; unknown -> <<"unknown"/utf8>> end. -file("src/nori/codegen/typescript/shared.gleam", 100). -spec has_separators(binary()) -> boolean(). has_separators(Input) -> (gleam_stdlib:contains_string(Input, <<"_"/utf8>>) orelse gleam_stdlib:contains_string( Input, <<"-"/utf8>> )) orelse gleam_stdlib:contains_string(Input, <<" "/utf8>>). -file("src/nori/codegen/typescript/shared.gleam", 106). -spec normalize_separators(binary()) -> binary(). normalize_separators(Input) -> _pipe = Input, _pipe@1 = gleam@string:replace(_pipe, <<" "/utf8>>, <<"_"/utf8>>), gleam@string:replace(_pipe@1, <<"-"/utf8>>, <<"_"/utf8>>). -file("src/nori/codegen/typescript/shared.gleam", 112). -spec lowercase_first(binary()) -> binary(). lowercase_first(Str) -> case gleam_stdlib:string_pop_grapheme(Str) of {ok, {First, Rest}} -> <<(string:lowercase(First))/binary, Rest/binary>>; {error, _} -> Str end. -file("src/nori/codegen/typescript/shared.gleam", 119). -spec capitalize(binary()) -> binary(). capitalize(Str) -> case gleam_stdlib:string_pop_grapheme(Str) of {ok, {First, Rest}} -> <<(string:uppercase(First))/binary, Rest/binary>>; {error, _} -> Str end. -file("src/nori/codegen/typescript/shared.gleam", 40). ?DOC(" Convert a snake_case, kebab-case, or space-separated string to PascalCase.\n"). -spec to_pascal_case(binary()) -> binary(). to_pascal_case(Input) -> case has_separators(Input) of false -> capitalize(Input); true -> _pipe = Input, _pipe@1 = normalize_separators(_pipe), _pipe@2 = gleam@string:split(_pipe@1, <<"_"/utf8>>), _pipe@3 = gleam@list:map(_pipe@2, fun capitalize/1), gleam@string:join(_pipe@3, <<""/utf8>>) end. -file("src/nori/codegen/typescript/shared.gleam", 53). ?DOC(" Convert a snake_case, kebab-case, or space-separated string to camelCase.\n"). -spec to_camel_case(binary()) -> binary(). to_camel_case(Input) -> case has_separators(Input) of false -> lowercase_first(Input); true -> Parts = begin _pipe = Input, _pipe@1 = normalize_separators(_pipe), gleam@string:split(_pipe@1, <<"_"/utf8>>) end, case Parts of [] -> <<""/utf8>>; [First | Rest] -> <<(string:lowercase(First))/binary, (begin _pipe@2 = Rest, _pipe@3 = gleam@list:map(_pipe@2, fun capitalize/1), gleam@string:join(_pipe@3, <<""/utf8>>) end)/binary>> end end. -file("src/nori/codegen/typescript/shared.gleam", 137). -spec is_letter(binary()) -> boolean(). is_letter(Char) -> case string:lowercase(Char) of <<"a"/utf8>> -> true; <<"b"/utf8>> -> true; <<"c"/utf8>> -> true; <<"d"/utf8>> -> true; <<"e"/utf8>> -> true; <<"f"/utf8>> -> true; <<"g"/utf8>> -> true; <<"h"/utf8>> -> true; <<"i"/utf8>> -> true; <<"j"/utf8>> -> true; <<"k"/utf8>> -> true; <<"l"/utf8>> -> true; <<"m"/utf8>> -> true; <<"n"/utf8>> -> true; <<"o"/utf8>> -> true; <<"p"/utf8>> -> true; <<"q"/utf8>> -> true; <<"r"/utf8>> -> true; <<"s"/utf8>> -> true; <<"t"/utf8>> -> true; <<"u"/utf8>> -> true; <<"v"/utf8>> -> true; <<"w"/utf8>> -> true; <<"x"/utf8>> -> true; <<"y"/utf8>> -> true; <<"z"/utf8>> -> true; _ -> false end. -file("src/nori/codegen/typescript/shared.gleam", 169). -spec is_digit(binary()) -> boolean(). is_digit(Char) -> case Char of <<"0"/utf8>> -> true; <<"1"/utf8>> -> true; <<"2"/utf8>> -> true; <<"3"/utf8>> -> true; <<"4"/utf8>> -> true; <<"5"/utf8>> -> true; <<"6"/utf8>> -> true; <<"7"/utf8>> -> true; <<"8"/utf8>> -> true; <<"9"/utf8>> -> true; _ -> false end. -file("src/nori/codegen/typescript/shared.gleam", 126). -spec is_valid_ident_char(binary(), integer()) -> boolean(). is_valid_ident_char(Char, Index) -> case Char of <<"_"/utf8>> -> true; <<"$"/utf8>> -> true; _ -> case Index of 0 -> is_letter(Char); _ -> is_letter(Char) orelse is_digit(Char) end end. -file("src/nori/codegen/typescript/shared.gleam", 77). ?DOC(" Make a string a valid TypeScript identifier.\n"). -spec sanitize_identifier(binary()) -> binary(). sanitize_identifier(Input) -> Sanitized = begin _pipe = Input, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = gleam@list:index_map( _pipe@1, fun(Char, Idx) -> case is_valid_ident_char(Char, Idx) of true -> Char; false -> <<"_"/utf8>> end end ), gleam@string:join(_pipe@2, <<""/utf8>>) end, case gleam@string:first(Sanitized) of {ok, First} -> case is_digit(First) of true -> <<"_"/utf8, Sanitized/binary>>; false -> Sanitized end; {error, _} -> <<"_"/utf8>> end.