Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich APIs for Duration and Datasize types #15

Open
jasongwartz opened this issue Mar 9, 2024 · 3 comments
Open

Rich APIs for Duration and Datasize types #15

jasongwartz opened this issue Mar 9, 2024 · 3 comments
Labels
good first issue Good for newcomers

Comments

@jasongwartz
Copy link
Contributor

jasongwartz commented Mar 9, 2024

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).

@jasongwartz jasongwartz changed the title Support Duration and Datasize types Rich APIs for Duration and Datasize types Mar 30, 2024
@jasongwartz jasongwartz added the good first issue Good for newcomers label Mar 30, 2024
@DmitryPR
Copy link

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

@jasongwartz
Copy link
Contributor Author

@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
Copy link

Allright, now I see what you mean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants