Skip to content

Commit 5bb201c

Browse files
AlexaDeWitjacereda
authored andcommitted
Add buffer encoding and decoding functions
Refactor and cleanup Add prop tests for arraybuffer and base64 encoding Node compat for base64 Cleanup and working toward isomorphic interface between node and browser Need to determine where test failures are occurring. Better testing needed Better test feedback Delete faulty browser implementations Green tests, but still needing a browser implementation for base64 and arraybuffer manipulation return to old reliable fromStr function Working towards a unified interface for buffer handling Add test vector for b64 Remove test vector TYPE ERROR Cleanup preparing some front-end testing tools Remove old references to mocha Remove base64 encoding as it is growing in complexity. Moving base64 to its own pursuit package Remove stuff after change of mind Last of the cleanup
1 parent 963eead commit 5bb201c

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ output/
33
bower_components/
44
node_modules/
55
.psci
6-
.psci_modules/
6+
.psci_modules/

src/Data/ArrayBuffer/ArrayBuffer.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@ exports.create = function(s) {
66
return function () {
77
return new ArrayBuffer(s);
88
};
9-
}
9+
};
1010

1111
exports.byteLength = function(a) {
1212
return a.byteLength;
13-
}
13+
};
1414

1515
exports.sliceImpl = function(s, e, a) {
1616
return function () {
1717
return a.slice(s, e);
1818
};
19-
}
19+
};
2020

2121
exports.fromArray = function(s) {
2222
return (new Uint8Array(s)).buffer;
23-
}
23+
};
2424

2525
exports.fromIntArray = function(s) {
2626
return (new Uint8Array(s)).buffer;
27-
}
27+
};
28+
29+
exports.fromInt16Array = function(s) {
30+
return (new Uint16Array(s)).buffer;
31+
};
2832

2933
exports.fromString = function(s) {
30-
var l = s.length;
31-
var ab = new ArrayBuffer(l * 2);
32-
var a = new Uint16Array(ab);
33-
for (var i = 0; i < l; i++)
34-
a[i] = s.charCodeAt(i);
35-
return a.buffer;
36-
}
34+
var buf = new ArrayBuffer(s.length*2);
35+
var bufView = new Uint16Array(buf);
36+
for (var i=0, strLen=s.length; i<strLen; i++) {
37+
bufView[i] = s.charCodeAt(i);
38+
}
39+
return buf;
40+
};
41+
42+
exports.decodeToString = function(buffer) {
43+
const uintBuffer = new Uint16Array(buffer);
44+
const reducer = function(accum, point) {
45+
// use concat instead of es6 syntax for compatibility
46+
return accum.concat([String.fromCharCode(point)]);
47+
};
48+
const points = uintBuffer.reduce(reducer, new Array());
49+
return points.join("");
50+
};

src/Data/ArrayBuffer/ArrayBuffer.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Data.ArrayBuffer.ArrayBuffer ( create
44
, fromArray
55
, fromIntArray
66
, fromString
7+
, decodeToString
78
) where
89

910
import Effect (Effect)
@@ -30,3 +31,6 @@ foreign import fromIntArray :: Array Int -> ArrayBuffer
3031

3132
-- | Convert a string into an `ArrayBuffer` representation.
3233
foreign import fromString :: String -> ArrayBuffer
34+
35+
foreign import decodeToString :: ArrayBuffer -> String
36+

test/Main.purs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import Effect (Effect)
66
import Data.ArrayBuffer.ArrayBuffer as AB
77
import Data.ArrayBuffer.DataView as DV
88
import Data.ArrayBuffer.Typed as TA
9+
import Data.ArrayBuffer.Types as AT
910
import Data.Maybe (Maybe(..), isNothing)
1011
import Data.UInt (fromInt, pow)
11-
import Test.QuickCheck (quickCheck', (<?>))
12+
import Test.QuickCheck (quickCheck', (<?>), quickCheck)
13+
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
14+
15+
newtype ABuffer = ABuffer AT.ArrayBuffer
16+
17+
instance arbitraryArrayBuffer :: Arbitrary ABuffer where
18+
arbitrary = map (ABuffer <<< AB.fromString) arbitrary
1219

1320
assertEffEquals :: forall a. Eq a => Show a => a -> Effect a -> Effect Unit
1421
assertEffEquals expectedValue computation = do
@@ -36,6 +43,8 @@ main = do
3643
assertEquals 4 $ AB.byteLength $ AB.fromArray [1.0, 2.0, 3.0, 4.0]
3744
assertEquals 4 $ AB.byteLength $ AB.fromIntArray [1, 2, 3, 4]
3845
assertEquals 8 $ AB.byteLength $ AB.fromString "hola"
46+
assertEquals 8 $ AB.byteLength $ AB.fromString "hóla"
47+
assertEquals 10 $ AB.byteLength $ AB.fromString "hóla¡"
3948
assertEquals 8 $ AB.byteLength $ DV.buffer $ DV.whole ab8
4049
assertEquals 8 $ AB.byteLength $ DV.buffer $ TA.dataView $ TA.asInt8Array $ DV.whole ab8
4150

@@ -47,6 +56,12 @@ main = do
4756
assertEffEquals Nothing $ TA.at fourElementInt8Array 4
4857
assertEffEquals Nothing $ TA.at fourElementInt8Array (-1)
4958

59+
quickCheck
60+
\(s) ->
61+
s == (AB.decodeToString $ AB.fromString s)
62+
<?> "Isormorphic arraybuffer conversion with string failed for input "
63+
<> s
64+
5065
assertEquals [1.0, 2.0, 3.0] $ TA.toArray <<< TA.asInt8Array <<< DV.whole $ AB.fromArray [1.0, 2.0, 3.0]
5166

5267
twoElementDataView <- do
@@ -66,3 +81,4 @@ main = do
6681
DV.setUint8 dv t 2
6782
DV.setUint8 dv t 3
6883
DV.getUint32be dv 0
84+

0 commit comments

Comments
 (0)