# All files / src/serialization cid.js

100% Statements 23/23
100% Branches 6/6
100% Functions 7/7
100% Lines 16/16

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    331x
    1x

    33x

    33x

    1x
    82x

    1x
    506x
    82x
    14x

    82x

    1x
    506x
    82x
    14x

    82x

     import CID from 'cids'
    import { bufferFromJson, bufferToJson } from './buffer'

    export const cidFromJson = (obj) => new CID(obj.version, obj.codec, bufferFromJson(obj.hash))

    export const cidToJson = (cid) => ({
      __ipfsPostMsgProxyType: 'CID',
      codec: cid.codec,
      version: cid.version,
      hash: bufferToJson(cid.multihash)
    })

    export const isCid = CID.isCID
    export const isCidJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'CID'

    export const preCidFromJson = (index) => {
      return (...args) => {
        if (isCidJson(args[index])) {
          args[index] = cidFromJson(args[index])
        }
        return args
      }
    }

    export const preCidToJson = (index) => {
      return (...args) => {
        if (isCid(args[index])) {
          args[index] = cidToJson(args[index])
        }
        return args
      }
    }