query
On this page

fixedWidthString

lib.strings.fixedWidthString

Docs pulled from | This Revision | 8 minutes ago


Create a fixed width string with additional prefix to match required width.

This function will fail if the input string is longer than the requested length.

Inputs

width
1. Function argument
filler
2. Function argument
str
3. Function argument

Type

fixedWidthString :: int -> string -> string -> string

Examples

lib.strings.fixedWidthString usage example

fixedWidthString 5 "0" (toString 15)
=> "00015"

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

fixedWidthString = width: filler: str:
    let
      strw = lib.stringLength str;
      reqWidth = width - (lib.stringLength filler);
    in
      assert lib.assertMsg (strw <= width)
        "fixedWidthString: requested string length (${
          toString width}) must not be shorter than actual length (${
            toString strw})";
      if strw == width then str else filler + fixedWidthString reqWidth filler str;