git.iko.soy/rowtype-yoga/purescript-arraybuffer-builder
on master help sign in
docs and tests
commitec26f245a728d6cd4782093461313f0e6084e5b4 authorJames Brock <jamesbrock@gmail.com> dateMon 27 Jul 2020, 23:45 (6 years ago) parent9f7e003
3 files changed+41−9
README.md+8−8 src/Data/ArrayBuffer/Builder.purs+9−1 test/Main.purs+24
README.md+8−8
@@ -22,13 +22,6 @@ This package is designed for the case in which we are serializing some data
2222to be sent to another program which expects a serialization format
2323that we don't control.
2424
25−## Deserialization
26−
27−This package is only for writing `ArrayBuffer`s, not reading them.
28−See
29−[__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
30−for a way to deserialize from `ArrayBuffer` back to Purescript data.
31−
3225## Usage Examples
3326
3427All `ArrayBuffer` building must occur in `Effect`.
@@ -73,7 +66,7 @@ putStringUtf8 s = do
7366 putArrayBuffer stringbuf
7467
7568do
76− arraybuffer <- execPut $ putStringUtf8 "ACAB"
69+ arraybuffer <- execPut $ putStringUtf8 "BLM"
7770```
7871
7972### Serialize an `Array Int`
@@ -96,6 +89,13 @@ do
9689 arraybuffer <- execPut $ putArrayInt32 [1,2,3]
9790```
9891
92+## Deserialization
93+
94+This package is only for writing `ArrayBuffer`s, not reading them.
95+See
96+[__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
97+for a way to deserialize from `ArrayBuffer` back to Purescript data.
98+
9999## References
100100
101101* [MDN `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
src/Data/ArrayBuffer/Builder.purs+9−1
@@ -4,6 +4,11 @@
44-- |
55-- | Writing to an `ArrayBuffer` is an `Effect`ful activity, so most
66-- | functions in this module must be run in a `MonadEffect` context.
7+-- |
8+-- | For operations for working with `ArrayBuffer`, see
9+-- | module
10+-- | [`Data.ArrayBuffer.ArrayBuffer`](https://pursuit.purescript.org/packages/purescript-arraybuffer/docs/Data.ArrayBuffer.ArrayBuffer)
11+-- | in package __purescript-arraybuffer__.
712module Data.ArrayBuffer.Builder
813( PutM
914, Put
@@ -395,4 +400,7 @@ putFloat64le = putArrayBuffer <=< encodeFloat64le
395400-- https://github.com/haskell-perf/strict-bytestring-builders
396401--
397402-- We've tried to design the API for this library with minimal assumptions,
398−-- so that if we want to change the Builder implementation later then we can.
403+-- so that if we want to change the Builder implementation later then we can.
404+--
405+-- Another way this could go:
406+-- https://kodimensional.dev/posts/2019-03-25-comonadic-builders
test/Main.purs+24
@@ -56,3 +56,27 @@ main = do
5656 b3 <- encodeInt8 3
5757 b4 <- encodeInt8 4
5858 pure $ singleton b1 <> (singleton b2 <> singleton b3) <> singleton b4
59+
60+ buildTest "Test 6" [1,2,3,4,5,6,7,8] $ do
61+ b1 <- encodeInt8 1
62+ b2 <- encodeInt8 2
63+ b3 <- encodeInt8 3
64+ b4 <- encodeInt8 4
65+ b5 <- encodeInt8 5
66+ b6 <- encodeInt8 6
67+ b7 <- encodeInt8 7
68+ b8 <- encodeInt8 8
69+ pure $ singleton b1 <> (singleton b2 <> singleton b3) <> singleton b4
70+ <> singleton b5 <> (singleton b6 <> singleton b7) <> singleton b8
71+
72+ buildTest "Test 7" [1,2,3,4,5,6,7,8] $ do
73+ b1 <- encodeInt8 1
74+ b2 <- encodeInt8 2
75+ b3 <- encodeInt8 3
76+ b4 <- encodeInt8 4
77+ b5 <- encodeInt8 5
78+ b6 <- encodeInt8 6
79+ b7 <- encodeInt8 7
80+ b8 <- encodeInt8 8
81+ pure $ singleton b1 <> (singleton b2 <> singleton b3) <> (singleton b4
82+ <> singleton b5) <> (singleton b6 <> singleton b7) <> singleton b8