# All files / serialization cid.js

53.57% Statements 15/28
0% Branches 0/6
44.44% Functions 4/9
68.42% Lines 13/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    33
    34
    35
    36
    371x
    1x
    1x

    1x

    1x

    1x
    1x

    1x
    22x

    1x
    22x

    1x
    1x
     import CID from 'cids'
    import { bufferFromJson, bufferToJson } from './buffer'
    import preArrayOf from './utils/pre-array-of'

    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
      }
    }

    export const preArrayOfCidToJson = (index) => preArrayOf(index, isCid, cidToJson)
    export const preArrayOfCidFromJson = (index) => preArrayOf(index, isCidJson, cidFromJson)