pkl-community / pkl-typescript

TypeScript language bindings for the Pkl programming language
https://www.npmjs.com/package/@pkl-community/pkl-typescript
Apache License 2.0
102 stars 1 forks source link

Rich APIs for Duration and Datasize types #15

Open jasongwartz opened 7 months ago

jasongwartz commented 7 months ago

The Duration and DataSize types in the Pkl standard library have excellent rich APIs (see Duration and DataSize) for converting between units and comparing (or eg. summing) values

We have basic support for creating TS objects with the properties that come over the wire from pkl server (see here) but those are just key-value pair objects with unit and value properties.

We could consider writing custom class implementations that have similar helper APIs to the Pkl equivalents. We could also consider using valueOf or Proxy to allow mathematical operators (eg. myDataSizeA - myDataSizeB).

DmitryPR commented 6 months ago

Hello can you please elaborate, what do you mean by similar helper APIs, maybe you could provide an example?

jasongwartz commented 6 months ago

@DmitryPR Have you used these types in Pkl? I've linked the API documentation above - there are functions like toUnit(), so in Pkl, you can do things like 75.s.toUnit("min").value (to get 75 seconds in minutes).

Here's how pkl-go has implemented it, which isn't far off of what we have so far: https://github.com/apple/pkl-go/blob/main/pkl/values.go#L92

But it would be useful if we had a Duration class, that had, say, a similar 'toUnit()' function that returned another Duration object in the converted unit.

Currently, the pkl-typescript Duration and DataSize types (as linked above) are just interface types - we would probably want to implement them as classes in order to support the richer APIs, and then instantiate the classes in the decoder logic.

If you're intested in helping contribute to pkl-typescript, feel free to come join us on Discord (link documented here: https://github.com/pkl-community) and join the #bindings-typescript channel!

DmitryPR commented 6 months ago

Allright, now I see what you mean