# All files / node_modules/netcdfjs/src types.js

15.55% Statements 7/45
0% Branches 0/27
0% Functions 0/6
15.9% Lines 7/44

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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158

    18x

    18x

    18x
    18x
    18x
    18x
    18x
     'use strict';

    const notNetcdf = require('./utils').notNetcdf;

    const types = {
      BYTE: 1,
      CHAR: 2,
      SHORT: 3,
      INT: 4,
      FLOAT: 5,
      DOUBLE: 6
    };

    /**
     * Parse a number into their respective type
     * @ignore
     * @param {number} type - integer that represents the type
     * @return {string} - parsed value of the type
     */
    function num2str(type) {
      switch (Number(type)) {
        case types.BYTE:
          return 'byte';
        case types.CHAR:
          return 'char';
        case types.SHORT:
          return 'short';
        case types.INT:
          return 'int';
        case types.FLOAT:
          return 'float';
        case types.DOUBLE:
          return 'double';
          /* istanbul ignore next */
        default:
          return 'undefined';
      }
    }

    /**
     * Parse a number type identifier to his size in bytes
     * @ignore
     * @param {number} type - integer that represents the type
     * @return {number} -size of the type
     */
    function num2bytes(type) {
      switch (Number(type)) {
        case types.BYTE:
          return 1;
        case types.CHAR:
          return 1;
        case types.SHORT:
          return 2;
        case types.INT:
          return 4;
        case types.FLOAT:
          return 4;
        case types.DOUBLE:
          return 8;
          /* istanbul ignore next */
        default:
          return -1;
      }
    }

    /**
     * Reverse search of num2str
     * @ignore
     * @param {string} type - string that represents the type
     * @return {number} - parsed value of the type
     */
    function str2num(type) {
      switch (String(type)) {
        case 'byte':
          return types.BYTE;
        case 'char':
          return types.CHAR;
        case 'short':
          return types.SHORT;
        case 'int':
          return types.INT;
        case 'float':
          return types.FLOAT;
        case 'double':
          return types.DOUBLE;
          /* istanbul ignore next */
        default:
          return -1;
      }
    }

    /**
     * Auxiliary function to read numeric data
     * @ignore
     * @param {number} size - Size of the element to read
     * @param {function} bufferReader - Function to read next value
     * @return {Array<number>|number}
     */
    function readNumber(size, bufferReader) {
      if (size !== 1) {
        var numbers = new Array(size);
        for (var i = 0; i < size; i++) {
          numbers[i] = bufferReader();
        }
        return numbers;
      } else {
        return bufferReader();
      }
    }

    /**
     * Given a type and a size reads the next element
     * @ignore
     * @param {IOBuffer} buffer - Buffer for the file data
     * @param {number} type - Type of the data to read
     * @param {number} size - Size of the element to read
     * @return {string|Array<number>|number}
     */
    function readType(buffer, type, size) {
      switch (type) {
        case types.BYTE:
          return buffer.readBytes(size);
        case types.CHAR:
          return trimNull(buffer.readChars(size));
        case types.SHORT:
          return readNumber(size, buffer.readInt16.bind(buffer));
        case types.INT:
          return readNumber(size, buffer.readInt32.bind(buffer));
        case types.FLOAT:
          return readNumber(size, buffer.readFloat32.bind(buffer));
        case types.DOUBLE:
          return readNumber(size, buffer.readFloat64.bind(buffer));
          /* istanbul ignore next */
        default:
          notNetcdf(true, `non valid type ${type}`);
          return undefined;
      }
    }

    /**
     * Removes null terminate value
     * @ignore
     * @param {string} value - String to trim
     * @return {string} - Trimmed string
     */
    function trimNull(value) {
    I  if (value.charCodeAt(value.length - 1) === 0) {
        return value.substring(0, value.length - 1);
      }
      return value;
    }

    module.exports = types;
    module.exports.num2str = num2str;
    module.exports.num2bytes = num2bytes;
    module.exports.str2num = str2num;
    module.exports.readType = readType;