pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Add isBase64 string property

Open jjuliano opened this issue 1 year ago • 3 comments

This PR adds isBase64 string property.

jjuliano avatar Oct 27 '24 06:10 jjuliano

If we add this, do we need this for the other hash functions as well? Sha, md5 etc? 🤔

StefMa avatar Oct 27 '24 10:10 StefMa

If we add this, do we need this for the other hash functions as well? Sha, md5 etc? 🤔

Maybe it makes sense only for base64 due to the base64Decoded string property. Another approach is to make base64Decoded detect if a string is a base64 through str?.base64Decoded.

jjuliano avatar Oct 27 '24 14:10 jjuliano

What's the use-case for this? Is it for defining a constraint?

Today, you can do this in-language:

function isBase64(str: String) = str.match(Regex("[-A-Za-z0-9+/]*={0,3}"))

Or, a more precise version, and also a little bit more expensive:

import "pkl:test"

function isBase64(str: String) = test.catchOrNull(() -> str.base64Decoded) == null

bioball avatar Oct 28 '24 04:10 bioball

We've rolled this into https://github.com/apple/pkl/pull/1019, and it will land in 0.29.

Thanks!

bioball avatar Jul 11 '25 17:07 bioball