# All files / src serialization.js

100% Statements 27/27
100% Branches 16/16
100% Functions 6/6
100% Lines 27/27

    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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    521x

    2255x
    5778x

    2255x

    7878x
    85x
    7793x
    4496x
    3297x
    984x
    2313x
    58x
    58x
    58x

    2255x

    3403x
    7560x

    3403x

    10497x
    12x
    10485x
    6172x
    4313x
    670x
    3643x
    240x

    3403x

     const PRIMITIVES = [
      'string', 'number', 'boolean', 'function', 'symbol' ]

    function modifyObject(doc) {
      Object.keys(doc).forEach(key => {
        doc[key] = deserialize(doc[key])
      })
      return doc
    }

    export function deserialize(value) {
      if (value == null) {
        return value
      } else if (PRIMITIVES.indexOf(typeof value) !== -1) {
        return value
      } else if (Array.isArray(value)) {
        return value.map(deserialize)
      } else if (value.$reql_type$ === 'TIME') {
        const date = new Date()
        date.setTime(value.epoch_time * 1000)
        return date
      } else {
        return modifyObject(value)
      }
    }

    function jsonifyObject(doc) {
      Object.keys(doc).forEach(key => {
        doc[key] = serialize(doc[key])
      })
      return doc
    }

    export function serialize(value) {
      if (value == null) {
        return value
      } else if (PRIMITIVES.indexOf(typeof value) !== -1) {
        return value
      } else if (Array.isArray(value)) {
        return value.map(serialize)
      } else if (value instanceof Date) {
        return {
          $reql_type$: 'TIME',
          epoch_time: value.getTime() / 1000,
          // Rethink will serialize this as "+00:00", but accepts Z
          timezone: 'Z',
        }
      } else {
        return jsonifyObject(value)
      }
    }