UTF8Str provides two extra length properties:
x = UTF8Str("Foo © bar ???? baz ☃ qux"); x.length // 22 x.byteLength // 27 (correct!) x.charLength // 21 (correct!)
Which all can return different results depending on what the String contains. This is confusing.
UTF8Str provides the following two simpler slots for accessing the String's contents:
x.chars // [ 'F','o','o',' ','©',' ','b','a','r',' ','????'' ','b','a','z',' ','☃',' ','q','u','x' ] x.bytes // Uint8Array [ 70, 111, 111, 32, 194, 169, 32, 98, 97, 114, 32, 240, 157, 140, 134, 32, 98, 97, 122, 32, 226, 152, 131, 32, 113, 117, 120 ]
x.bytes x.byteLength x.chars x.charLength
Personally I wanted a way to access the bytes of Strings, rather than their values.
None of the regular
String methods have been coverted to work with Unicode characters instead of with parts of Unicode characters. Maybe this will be on the roadmap.
.fromBytesto create a UTF8 String from an array of byte values.
.fromUTF8Binaryto create a UTF8 String from a String of UTF8 byte values that have not been coalesced into codepoints.
The aim of UTF8Str is as follows:
UTF8Str aims to be a lightweight way to do a few useful things with String bytes and characters, in a way that respects Unicode, in the smallest amount of code, with the greatest amount of clarity possible. Efficiency is not an aim. Simple, maintainable code that does something useful is the aim. If efficiency happens it's a bonus.
Open an issue or submit a pull request. Where to start?
.fromBytes:heavy_check_mark: (done in 1.0.2)
.fromUTF8Binary:heavy_check_mark: (done 1.0.2)
Still there is no baked in way to get byte length or the character length as part of the native String class, and these new additions simply make the code of UTF8Str simpler, clearer and more concise.