# All files / serialization cid.js

39.29% Statements 11/28
0% Branches 0/6
0% Functions 0/9
57.89% Lines 11/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

    1x

    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)