stringx (util v1.3.5)

View Source

Implements miscelaneous string functions

Some implementation authored by Evan Miller: http://www.evanmiller.org/joy-of- erlang.html

Author: Serge Aleynikov saleyn@gmail.com Copyright: 2006 Serge Aleynikov

Summary

Functions

Align rows of terms by stringifying them to uniform column width. If some row doesn't need to be aligned, pass its value as a binary. Options can be

Split a list into batches of N items

Convert format and arguments to binary/list shortening . This function can be used by Elixir, which is missing the equivalent of io_lib.format/2

Convert format and arguments to binary/list shortening . This function can be used by Elixir, which is missing the equivalent of io_lib.format/2

The same as uef_format:format_number/4 with #{} as the forth argument. See: format_number/4.

Formats Number by adding thousands separator between each set of 3 digits to the left of the decimal point, substituting Decimals for the decimal point, and rounding to the specified Precision. Returns a binary value.

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a precision of 2 and decimal digits of 2. The same as format_price/2 with a precision of 2 as the second argument. See uef_format:format_price/2 docs.

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a specified precision as the second argument and decimal digits of 2. The same as uef_format:format_price/3 with #{} as the third argument. See: format_price/3.

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a specified precision as the second argument, decimal digits of 2, and with ccy symbol (or options) as the third argument. If CcySymbol_OR_Options is a map the functions works as format_number/4 with decimal digits of 2 as the third argument and with options as the forth one. If CcySymbol_OR_Options is a binary or a string, the corresponding ccy symbol is added to the left.

Parse a given CSV file.

Parse a given CSV file.

Pretty print table of lists/tuples/maps to list. The following options control formatting behavior

Pretty print list of maps to list. See: pretty_table/3..

Pretty print table of lists/tuples/maps to list. See: pretty_table/3..

Rounds the number to the specified precision.

Rounds the number to the precision of 2.

Convert words in a string to capitalize first letter of each word.

Wrap words list in a string to multiple lines that fit the margin There are two uses of this function: First

Wrap words list in a string to multiple lines that fit the margin Example

Types

ccy_sym()

-type ccy_sym() :: binary() | string().

decimals()

-type decimals() :: 0..253.

format_number_opts()

-type format_number_opts() ::
          #{thousands => binary() | string(),
            decimal_point => binary() | string(),
            ccy_sym => ccy_sym(),
            ccy_pos => left | right,
            ccy_sep => binary() | string(),
            return => binary | list}.

formatted_number()

-type formatted_number() :: binary().

precision()

-type precision() :: integer().

pretty_print_opts()

-type pretty_print_opts() ::
          #{number_pad => char(),
            header => boolean(),
            unicode => boolean(),
            outline => none | full | [top | left | bottom | right],
            th_dir => both | leading | trailing,
            td_dir => both | leading | trailing,
            td_pad => #{integer() => both | leading | trailing},
            td_start => integer(),
            td_exclude => list(),
            td_sep => string(),
            tr_sep => string(),
            tr_sep_td => string(),
            prefix => string(),
            thousands => string() | binary(),
            translate => fun((term()) -> term()),
            footer_rows => integer(),
            td_formats =>
                tuple() |
                fun((ColVal :: term()) ->
                        {string, string()} |
                        {number, string() | number()} |
                        {number, Decimals :: integer(), ColVal :: number()} |
                        {ccy, number()}),
            thousands => string() | binary(),
            ccy_sym => string() | binary(),
            ccy_sep => string() | binary(),
            ccy_pos => left | right}.

Functions

align_rows(Rows)

align_rows(Rows, Options)

-spec align_rows(Rows :: [tuple() | binary() | list()],
                 Options ::
                     [{pad,
                       Dir ::
                           [trailing | leading | both |
                            {Pos :: integer() | last, trailing | leading | both | none}]} |
                      {exclude, Cols :: [integer()]} |
                      {return, Ret :: tuple | list} |
                      {type, binary | charlist} |
                      {prefix, string()} |
                      {ignore_empty, boolean()}]) ->
                    [AlignedRow :: tuple() | list()].

Align rows of terms by stringifying them to uniform column width. If some row doesn't need to be aligned, pass its value as a binary. Options can be:

Rows is a list. All rows must have the same arity except if a row is a binary. Options contain:

  • {pad, Direction} — Column padding direction, where Direction is one of leading, trailing, {Position::integer(), leading|trailing|none}, {last, leading|trailing|none}
  • {type, binary|charlist} — Return columns in the result rows as binaries or charlists (default)
  • {return, tuple|list} — Return result rows as lists or tuples
  • {prefix, string()} — Prefix first item in each row with this string
  • {ignore_empty, boolean()} — Don't pad trailing empty columns if this option is true
  • {exclude, [integer()]} — Exclude given column numbers

aligned_format(Fmt, Rows)

aligned_format(Fmt, Rows, Directions)

batch_split(N, L)

-spec batch_split(integer(), list()) -> [list()].

Split a list into batches of N items

binary_join/2

format(Fmt, Args)

-spec format(binary() | string(), list()) -> binary() | string().

Convert format and arguments to binary/list shortening . This function can be used by Elixir, which is missing the equivalent of io_lib.format/2

format_binary(Fmt, Args)

-spec format_binary(binary() | string(), list()) -> binary().

Convert format and arguments to binary/list shortening . This function can be used by Elixir, which is missing the equivalent of io_lib.format/2

format_integer(Integer)

-spec format_integer(integer()) -> formatted_number().

format_integer(Integer, Opts)

-spec format_integer(integer(), format_number_opts()) -> formatted_number().

format_number/3

-spec format_number(number(), precision(), decimals()) -> formatted_number().

The same as uef_format:format_number/4 with #{} as the forth argument. See: format_number/4.

format_number/4

-spec format_number(number(), precision(), decimals(), format_number_opts()) -> formatted_number().

Formats Number by adding thousands separator between each set of 3 digits to the left of the decimal point, substituting Decimals for the decimal point, and rounding to the specified Precision. Returns a binary value.

format_price(Number)

-spec format_price(Number :: number()) -> FormattedPrice :: formatted_number().

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a precision of 2 and decimal digits of 2. The same as format_price/2 with a precision of 2 as the second argument. See uef_format:format_price/2 docs.

format_price(Number, Precision)

-spec format_price(Number :: number(), Precision :: precision()) -> formatted_number().

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a specified precision as the second argument and decimal digits of 2. The same as uef_format:format_price/3 with #{} as the third argument. See: format_price/3.

format_price(Number, Precision, CcySymbol_OR_Options)

-spec format_price(Number :: number(),
                   Precision :: precision(),
                   CcySymbol_OR_Options :: format_number_opts() | ccy_sym()) ->
                      FormattedPrice :: formatted_number().

Formats Number in price-like style. Returns a binary containing FormattedPrice formatted with a specified precision as the second argument, decimal digits of 2, and with ccy symbol (or options) as the third argument. If CcySymbol_OR_Options is a map the functions works as format_number/4 with decimal digits of 2 as the third argument and with options as the forth one. If CcySymbol_OR_Options is a binary or a string, the corresponding ccy symbol is added to the left.

parse_csv(File)

-spec parse_csv(string()) -> [[string()]].

Parse a given CSV file.

parse_csv(File, Opts)

-spec parse_csv(string(), [fix_lengths | {open, Opts :: list()}]) -> [[string()]].

Parse a given CSV file.

pretty_print_table/1

-spec pretty_print_table([map()]) -> ok.

Pretty print table of lists/tuples/maps to list. The following options control formatting behavior:

  • header — When true (default), output header row
  • number_pad — Leading padding character used for numbers
  • th_dir — Table header row padding direction (both|leading|trailing)
  • td_dir — Table row padding direction (both|leading|trailing)
  • td_start — Don't print columns less than this (e.g. use 2 for records)
  • td_exclulde — List of column ID's (starting with 1) or names to exclude
  • td_pad — A map containing column padding directions #{Col::integer() => both|leading|trailing.
  • td_sep — Column separator (default " | ")

  • tr_sep — Row separator (default "-")
  • tr_sep_td — Column delimiter used in separating rows ("+")
  • prefix — Prepend each row with this string
  • td_formats — A tuple containing column formats. Each value is either a format string passed to io_lib:format/2 or a function taking either one argument fun(Value) -> {number|string, FormattedValue::string()} or three arguments fun(Key,Value,Row::tuple()|map()) -> {number|string, FormattedValue::string()}. This three argument function can perform calculation of the field value based on values of other fields in the Row.
  • unicode — Use unicode outline characters
  • outline — Draw top, left and line box outline (by default only the bottom one is drawn). Values:
    • none - on outline box
    • full - outline box on all 4 sides
    • [top, left, bottom, right] - outline box on given sides

Example:

1> stringx:pretty_print_table(
{a,b,c,d}, [{a, 10, ccc}, {bxxx, 200.00123, 'Done'}, {abc, 100.0, xx}],
#opts{td_dir=both, td_exclude=[d], td_formats=
{undefined, fun(V) when is_integer(V) -> {number, integer_to_list(V)};
(V) when is_float(V)   -> {number, float_to_list(V, [{decimals, 5}])}
end, "~w"}}).
a   |     b     |   c
-----+-----------+-------
a   |        10 |  ccc
bxxx | 200.00123 | 'Done'
-----+-----------+-------

pretty_print_table(HeaderRowKeys, Rows)

-spec pretty_print_table([string() | binary() | atom()] | tuple(), [map() | list()]) -> ok.

pretty_print_table(HeaderRowKeys, Rows, Opts)

-spec pretty_print_table([string() | binary() | atom()] | tuple(),
                         [map() | list()],
                         #opts{number_pad :: char(),
                               header :: boolean(),
                               th_dir :: both | leading | trailing,
                               td_dir :: both | leading | trailing,
                               td_pad :: map(),
                               td_start :: integer(),
                               td_exclude :: list(),
                               td_sep :: string(),
                               tr_sep :: string(),
                               tr_sep_td :: string(),
                               prefix :: string(),
                               translate :: term(),
                               footer_rows :: integer(),
                               td_formats :: undefined | tuple(),
                               thousands :: undefined | string() | binary(),
                               ccy_sym :: undefined | string() | binary(),
                               ccy_sep :: string() | binary(),
                               ccy_pos :: left | right,
                               outline :: none | full | [top | bottom | left | right] | map(),
                               unicode :: boolean()} |
                         map()) ->
                            ok.

pretty_table/1

-spec pretty_table([map()]) -> list().

Pretty print list of maps to list. See: pretty_table/3..

pretty_table(HeaderRowKeys, Rows)

-spec pretty_table([binary() | string() | atom()], [Row :: tuple() | list() | map()]) -> list().

Pretty print table of lists/tuples/maps to list. See: pretty_table/3..

pretty_table/3

-spec pretty_table([string() | binary() | atom()] | tuple(),
                   [Row :: tuple() | list() | map()],
                   Opts ::
                       map() |
                       #opts{number_pad :: char(),
                             header :: boolean(),
                             th_dir :: both | leading | trailing,
                             td_dir :: both | leading | trailing,
                             td_pad :: map(),
                             td_start :: integer(),
                             td_exclude :: list(),
                             td_sep :: string(),
                             tr_sep :: string(),
                             tr_sep_td :: string(),
                             prefix :: string(),
                             translate :: term(),
                             footer_rows :: integer(),
                             td_formats :: undefined | tuple(),
                             thousands :: undefined | string() | binary(),
                             ccy_sym :: undefined | string() | binary(),
                             ccy_sep :: string() | binary(),
                             ccy_pos :: left | right,
                             outline :: none | full | [top | bottom | left | right] | map(),
                             unicode :: boolean()}) ->
                      list().

round_number(Number, Precision)

-spec round_number(Number :: number(), Precision :: integer()) -> float().

Rounds the number to the specified precision.

round_price(Number)

-spec round_price(Number :: number()) -> float().

Rounds the number to the precision of 2.

titlecase(S)

-spec titlecase(string()) -> string().

Convert words in a string to capitalize first letter of each word.

wordwrap/2

-spec wordwrap(string() | binary(), integer()) -> string() | binary().

Wrap words list in a string to multiple lines that fit the margin There are two uses of this function: First:

1> stringx:wordwrap("abc efg exdf"], 8, "\n"). "abc efg\nexdf"

Second:

1> stringx:wordwrap(["abc", "efg", "exdf"], 8, ","). ["abc,efg,","exdf"]

The second use is deprecated. Use wrap_words/3 instead.

wordwrap/3

-spec wordwrap(Word, integer(), string() | binary()) -> Word when Word :: string() | binary().

wrap_words/3

-spec wrap_words([Word], integer(), Word) -> [Word] when Word :: string() | binary().

Wrap words list in a string to multiple lines that fit the margin Example:

1> stringx:wrap_words(["abc", "efg", "exdf"], 8, ","). ["abc,efg,","exdf"]