Name
is an abstraction of human-readable identifiers made up of words. This abstraction
allows us to use the same identifiers across various naming conventions used by the different
frontend and backend languages Morphir integrates with.
name = fromList [ "value", "in", "u", "s", "d" ]
toTitleCase name --> "ValueInUSD"
toCamelCase name --> "valueInUSD"
toSnakeCase name --> "value_in_USD"
We frequently use abbreviations in a business context to be more concise. From a naming perspective abbreviations are challanging because they are not real words and behave slightly differently. In this module we treat abbreviations as a list of single-letter words. This approach fits nicely into camel and title case naming conventions but when using snake-case the direct translation would look unnatural:
toSnakeCase name -- "value_in_u_s_d" ?
To resolve this and help creating human-readable strings we added functionality to turn abbreviations into upper-case words. We treat any series of single letter words as an abbreviation:
toSnakeCase name --> "value_in_USD"
List String
Type that represents a name that is made up of words.
fromList : List String -> Name
Convert a list of strings into a name.
toList : Name -> List String
Convert a name to a list of strings.
fromString : String -> Name
Translate a string into a name by splitting it into words. The algorithm is designed to work with most well-known naming conventions or mix of them. The general rule is that consecutive letters and numbers are treated as words, upper-case letters and non-alphanumeric characters start a new word.
fromString "fooBar_baz 123"
--> Name.fromList [ "foo", "bar", "baz", "123" ]
fromString "valueInUSD"
--> Name.fromList [ "value", "in", "u", "s", "d" ]
fromString "ValueInUSD"
--> Name.fromList [ "value", "in", "u", "s", "d" ]
fromString "value_in_USD"
--> Name.fromList [ "value", "in", "u", "s", "d" ]
fromString "_-%"
--> Name.fromList []
toTitleCase : Name -> String
Turns a name into a title-case string.
toTitleCase (fromList [ "foo", "bar", "baz", "123" ])
--> "FooBarBaz123"
toTitleCase (fromList [ "value", "in", "u", "s", "d" ])
--> "ValueInUSD"
toCamelCase : Name -> String
Turns a name into a camel-case string.
toCamelCase (fromList [ "foo", "bar", "baz", "123" ])
--> "fooBarBaz123"
toCamelCase (fromList [ "value", "in", "u", "s", "d" ])
--> "valueInUSD"
toSnakeCase : Name -> String
Turns a name into a snake-case string.
toSnakeCase (fromList [ "foo", "bar", "baz", "123" ])
--> "foo_bar_baz_123"
toSnakeCase (fromList [ "value", "in", "u", "s", "d" ])
--> "value_in_USD"
toHumanWords : Name -> List String
Turns a name into a list of human-readable strings. The only difference
compared to toList
is how it handles abbreviations. They will
be turned into a single upper-case word instead of separate single-character
words.
toHumanWords (fromList [ "value", "in", "u", "s", "d" ])
--> [ "value", "in", "USD" ]