padStart Functionality

ReScript seems to be missing multiple native JavaScript functions, like padStart. Is there any preprogramed way to use them or do they need to be hand-coded? I currently have

  Mimics MDN's `padStart` function
let padStart = (original, num, padding) => {
  let numAdd = Js.Math.max_int(0, num - original->Js.String2.length)
  let timesAdd = numAdd / padding->Js.String2.length
  let extraChars = numAdd->mod(padding->Js.String2.length)
  padding->Js.String2.repeat(timesAdd) ++
  padding->Js.String2.substring(~from=0, ~to_=extraChars) ++

as my custom approach but was wondering if there is any plans to create a native approach or if such an approach already exists.

I think you can use @send and external to bind this method of String

1 Like

Playground example

@send external padStart: (string, int) => string = "padStart"
@send external padStartWith: (string, int, string) => string = "padStart"

Js.Console.log(padStart("foo", 5)) // or "foo"->padStart(5)
Js.Console.log(padStartWith("foo", 5, "x")) // or "foo"->padStartWith(5, "x")

See this section of the docs for more info.

1 Like

Can you file an issue to add bindings to Js.String for the missing methods?

padStart is introduced in ES8.
Bindings for string polyfills would be sufficient for now I guess