diff --git a/src/Data.Proquint.purs b/src/Data.Proquint.purs index 03bada2..ca5b94b 100644 --- a/src/Data.Proquint.purs +++ b/src/Data.Proquint.purs @@ -23,9 +23,8 @@ import Data.Show.Generic (genericShow) import Data.String as String import Data.Traversable (for, traverse) import Data.Tuple.Nested ((/\)) -import Effect (Effect) import Effect.Unsafe (unsafePerformEffect) -import Node.Buffer (Buffer, BufferValueType(..)) +import Node.Buffer (BufferValueType(..)) import Node.Buffer as Buffer import Node.Buffer.Immutable (ImmutableBuffer) import Node.Buffer.Immutable as Buffer.Immutable @@ -181,6 +180,9 @@ derive instance Eq Segment instance Show Segment where show = genericShow +segmentZero :: Segment +segmentZero = Segment ConB VoA ConB VoA ConB + segmentBits :: Segment -> Int segmentBits (Segment a b c d e) = let @@ -231,6 +233,16 @@ derive instance Eq Proquint instance Show Proquint where show = genericShow +pad :: Int -> Proquint -> Proquint +pad n pq@(Proquint segs) = + let + len = Array.length segs + in + if len < n then + Proquint $ Array.replicate (n - len) segmentZero <> segs + else + pq + toString :: Proquint -> String toString (Proquint segments) = intercalate "-" $ map segmentToString segments @@ -246,12 +258,12 @@ fromInt n = fromBits $ unsafePerformEffect do toInt :: Proquint -> Maybe Int toInt pq@(Proquint segs) - | Array.length segs <= 2 = Int.fromNumber $ Buffer.Immutable.read UInt32BE 0 $ toBits pq + | Array.length segs <= 2 = Int.fromNumber $ Buffer.Immutable.read UInt32BE 0 $ toBits $ pad 2 pq | otherwise = Nothing toBigInt :: Proquint -> Maybe BigInt toBigInt pq@(Proquint segs) - | Array.length segs <= 4 = Just $ Buffer.Immutable.BigInt.readBigUInt64BE 0 $ toBits pq + | Array.length segs <= 4 = Just $ Buffer.Immutable.BigInt.readBigUInt64BE 0 $ toBits $ pad 4 pq | otherwise = Nothing fromBigInt :: BigInt -> Proquint