node
node copied to clipboard
[Implement] Naive Buffer.from, and friends
Implements Buffer.from<T>(value: T)
and a few other convenience functions.
There are lots of obvious problems with not having the function overloads, but there are different paths we can take.
For instance, because there are no optional parameters, we must assume that the default encoding for strings is UTF8
.
// utf-16 has a work around in AssemblyScript
let buff = Buffer.from(String.UTF16.encode(value));
// using Buffer.fromString(value, "utf16le");
Things to note about this function:
-
String[]
objects do something complicated: string -> f64 -> u8 to remain compatible with node.
> Buffer.from(["3", "257.3", "15", "Infinity", "NaN"])
<Buffer 03 01 0F 00 00>
-
String
objects need to be converted toUTF8
-
Buffer
objects share their view of the sameArrayBuffer
-
ArrayBuffer
objects are simply attached to a newBuffer
-
ArrayBufferView
objects need to convert their values tou8
.
@RedDwarfian and @dcodeIO thoughts on this pull request?
It is not clear in the Naive Buffer.from
code where non-string arrays such as Array<u8>
are handled. I would suggest adding a comment to clarify that.
We might like to pull the ASCII encoding pull request first. #27
Otherwise, this is ready for review.