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

Any plans to handle encodings? #60

Open
Rudxain opened this issue Apr 24, 2022 · 1 comment
Open

Any plans to handle encodings? #60

Rudxain opened this issue Apr 24, 2022 · 1 comment

Comments

@Rudxain
Copy link

Rudxain commented Apr 24, 2022

I think this library should include functions to convert Strings to TypedArrays and viceversa, with the option to interpret as UTF-8 or UTF-16 depending on use case. So if a string is to be interpreted as UTF-8, the corresponding TypedArray should be a Uint8Array, otherwise UTF-16 is used and a Uint16Array of code-units is returned. If an arbitrary TypedArray is provided, it'll be read as a string of octets, and those octets will be converted to a string depending on the chosen encoding. UTF-16 has endianess and BOMs, so a function that handles implicit and explicit endianess would be slightly more complicated.

Another thing related to encodings are binary-to-text encodings like Hexadecimal, base64, base85, etc. JS already has base64 support with atob and btoa, but hex and base85 are missing, which could be provided by this library.

I don't know if these features should be added to this library because Voca seems to be intended for high-level (not low-level) use cases, and adding base85 support would be pointless because it's rarely used. Any constructive criticism is appreciated

@Rudxain
Copy link
Author

Rudxain commented Aug 29, 2022

Additionally, it's a good idea for the query category to include at least 1 of the following validators:

/** Check if string only has Basic Multilingual Plane code-points */
const isBMP = s => typeof s == 'string' && [...s].length === s.length

/**
Check if valid ASCII
@param printable limits the range to printable chars only
*/
const isASCII = (s, printable = false) =>
	typeof s == 'string' && (printable ? /^[\x20-\x7e]*$/g : /^[\x00-\x7f]*$/g).test(s)

/** Check if valid binary string */
const isBinStr = s => typeof s == 'string' && /^[\x00-\xff]*$/g.test(s)

Checking if a string is valid ASCII or BMP is useful. Checking if a value is a valid binary string is only (usually) useful when dealing with atob and btoa, so that can be ignored

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

No branches or pull requests

1 participant