Passed
Push — master ( f6c41d...2274e9 )
by Rafael S.
01:50
created

main.js ➔ unpackString   F

Complexity

Conditions 19
Paths 724

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 19
eloc 41
nc 724
nop 3
dl 0
loc 55
rs 0.5999
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like main.js ➔ unpackString often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The byte-data API.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/** @module byteData */
31
32
import endianness from './lib/endianness.js';
33
import {reader_, setUp_, writer_} from './lib/packer.js';
34
import {validateType, validateNotUndefined, validateValueType} from './lib/validation.js';
0 ignored issues
show
Unused Code introduced by
The variable validateType seems to be never used. Consider removing it.
Loading history...
35
36
const UTF8_ERROR = 'Invalid UTF-8 character.';
37
38
/**
39
 * Read a string of UTF-8 characters from a byte buffer.
40
 * @see https://encoding.spec.whatwg.org/#the-encoding
41
 * @see https://stackoverflow.com/a/34926911
42
 * @param {!Uint8Array|!Array<!number>} buffer A byte buffer.
43
 * @param {number=} index The index to read.
44
 * @param {?number=} len The number of bytes to read.
45
 * @return {string}
46
 * @throws {Error} If read a value that is not UTF-8.
47
 */
48
export function unpackString(buffer, index=0, len=null) {
49
  len = len !== null ? index + len : buffer.length;
50
  /** @type {string} */
51
  let str = "";
52
  while(index < len) {
53
    let lowerBoundary = 0x80;
54
    let upperBoundary = 0xBF;
55
    /** @type {number} */
56
    let charCode = buffer[index++];
57
    if (charCode >= 0x00 && charCode <= 0x7F) {
58
      str += String.fromCharCode(charCode);
59
    } else {
60
      /** @type {number} */
61
      let count = 0;
62
      if (charCode >= 0xC2 && charCode <= 0xDF) {
63
        count = 1;
64
      } else if (charCode >= 0xE0 && charCode <= 0xEF ) {
65
        count = 2;
66
        if (buffer[index] === 0xE0) {
67
          lowerBoundary = 0xA0;
68
        }
69
        if (buffer[index] === 0xED) {
70
          upperBoundary = 0x9F;
71
        }
72
      } else if (charCode >= 0xF0 && charCode <= 0xF4 ) {
73
        count = 3;
74
        if (buffer[index] === 0xF0) {
75
          lowerBoundary = 0x90;
76
        }
77
        if (buffer[index] === 0xF4) {
78
          upperBoundary = 0x8F;
79
        }
80
      } else {
81
        throw new Error(UTF8_ERROR);
82
      }
83
      charCode = charCode & (1 << (8 - count - 1)) - 1;
84
      for (let i = 0; i < count; i++) {
85
        charCode = (charCode << 6) | (buffer[index] & 0x3f);
86
        if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) {
87
          throw new Error(UTF8_ERROR);
88
        }
89
        index++;
90
      }
91
      if (charCode <= 0xffff) {
92
        str += String.fromCharCode(charCode);
93
      } else {
94
        charCode -= 0x10000;
95
        str += String.fromCharCode(
96
          ((charCode >> 10) & 0x3ff) + 0xd800,
97
          (charCode & 0x3ff) + 0xdc00);
98
      }
99
    }
100
  }
101
  return str;
102
}
103
104
/**
105
 * Write a string of UTF-8 characters as a byte buffer.
106
 * @see https://encoding.spec.whatwg.org/#utf-8-encoder
107
 * @param {string} str The string to pack.
108
 * @return {!Array<number>} The next index to write on the buffer.
109
 * @throws {Error} If a character in the string is not UTF-8.
110
 */
111
export function packString(str) {
112
  /** @type {!Array<!number>} */
113
  let bytes = [];
114
  for (let i = 0; i < str.length; i++) {
115
    /** @type {number} */
116
    let codePoint = str.codePointAt(i);
117
    if (codePoint < 128) {
118
      bytes.push(codePoint);
119
    } else {
120
      /** @type {number} */
121
      let count = 0;
122
      /** @type {number} */
123
      let offset = 0;
124
      if (codePoint <= 0x07FF) {
125
        count = 1;
126
        offset = 0xC0;
127
      } else if(codePoint <= 0xFFFF) {
128
        count = 2;
129
        offset = 0xE0;
130
      } else if(codePoint <= 0x10FFFF) {
131
        count = 3;
132
        offset = 0xF0;
133
        i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
134
      }
135
      bytes.push((codePoint >> (6 * count)) + offset);
136
      while (count > 0) {
137
        bytes.push(0x80 | (codePoint >> (6 * (count - 1)) & 0x3F));
138
        count--;
139
      }
140
    }
141
  }
142
  return bytes;
143
}
144
145
/**
146
 * Write a string of UTF-8 characters to a byte buffer.
147
 * @param {string} str The string to pack.
148
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
149
 * @param {number=} index The index to write in the buffer.
150
 * @return {number} The next index to write in the buffer.
151
 * @throws {Error} If a character in the string is not valid ASCII.
152
 */
153
export function packStringTo(str, buffer, index=0) {
154
  /** @type {!Array<!number>} */
155
  let bytes = packString(str);
156
  for (let i = 0; i < bytes.length; i++) {
157
    buffer[index++] = bytes[i];
158
  }
159
  return index;
160
}
161
162
// Numbers
163
/**
164
 * Pack a number as a byte buffer.
165
 * @param {number} value The number.
166
 * @param {!Object} theType The type definition.
167
 * @return {!Array<number>} The packed value.
168
 * @throws {Error} If the type definition is not valid.
169
 * @throws {Error} If the value is not valid.
170
 */
171
export function pack(value, theType) {
172
  /** @type {!Array<!number>} */
173
  let output = [];
174
  packTo(value, theType, output);
175
  return output;
176
}
177
178
/**
179
 * Pack a number to a byte buffer.
180
 * @param {number} value The value.
181
 * @param {!Object} theType The type definition.
182
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
183
 * @param {number=} index The index to write.
184
 * @return {number} The next index to write.
185
 * @throws {Error} If the type definition is not valid.
186
 * @throws {Error} If the value is not valid.
187
 */
188
export function packTo(value, theType, buffer, index=0) {
189
  return packArrayTo([value], theType, buffer, index);
190
}
191
192
/**
193
 * Pack an array of numbers as a byte buffer.
194
 * @param {!Array<number>|!TypedArray} values The values.
195
 * @param {!Object} theType The type definition.
196
 * @return {!Array<number>} The packed values.
197
 * @throws {Error} If the type definition is not valid.
198
 * @throws {Error} If any of the values are not valid.
199
 */
200
export function packArray(values, theType) {
201
  /** @type {!Array<!number>} */
202
  let output = [];
203
  packArrayTo(values, theType, output);
204
  return output;
205
}
206
207
/**
208
 * Pack a array of numbers to a byte buffer.
209
 * @param {!Array<number>|!TypedArray} values The value.
210
 * @param {!Object} theType The type definition.
211
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
212
 * @param {number=} index The buffer index to write.
213
 * @return {number} The next index to write.
214
 * @throws {Error} If the type definition is not valid.
215
 * @throws {Error} If the value is not valid.
216
 */
217
export function packArrayTo(values, theType, buffer, index=0) {
218
  setUp_(theType);
219
  for (let i=0; i < values.length; i++) {
220
    validateNotUndefined(values[i]);
221
    validateValueType(values[i]);
222
    /** @type {number} */
223
    let len = index + theType.offset;
224
    while (index < len) {
225
      index = writer_(buffer, values[i], index);
226
    }
227
    if (theType.be) {
228
      endianness(
229
        buffer, theType.offset, index - theType.offset, index);
230
    }
231
  }
232
  return index;
233
}
234
235
/**
236
 * Unpack a number from a byte buffer.
237
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
238
 * @param {!Object} theType The type definition.
239
 * @param {number=} index The buffer index to read.
240
 * @return {number}
241
 * @throws {Error} If the type definition is not valid
242
 */
243
export function unpack(buffer, theType, index=0) {
244
  setUp_(theType);
245
  if ((theType.offset + index) > buffer.length) {
246
    throw Error('Bad buffer length.');
247
  }
248
  if (theType.be) {
249
    endianness(buffer, theType.offset, index, index + theType.offset);
250
  }
251
  /** @type {number} */
252
  let value = reader_(buffer, index);
253
  if (theType.be) {
254
    endianness(buffer, theType.offset, index, index + theType.offset);
255
  }
256
  return value;
257
}
258
259
/**
260
 * Unpack an array of numbers from a byte buffer.
261
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
262
 * @param {!Object} theType The type definition.
263
 * @param {number=} index The start index. Assumes 0.
264
 * @param {?number=} end The end index. Assumes the buffer length.
265
 * @return {!Array<number>}
266
 * @throws {Error} If the type definition is not valid
267
 */
268
export function unpackArray(buffer, theType, index=0, end=buffer.length) {
269
  /** @type {!Array<!number>} */
270
  let output = [];
271
  unpackArrayTo(buffer, theType, output, index, end);
272
  return output;
273
}
274
275
/**
276
 * Unpack a array of numbers to a typed array.
277
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
278
 * @param {!Object} theType The type definition.
279
 * @param {!TypedArray|!Array<!number>} output The output array.
280
 * @param {number=} index The start index. Assumes 0.
281
 * @param {?number=} end The end index. Assumes the buffer length.
282
 * @throws {Error} If the type definition is not valid
283
 */
284
export function unpackArrayTo(buffer, theType, output, index=0, end=buffer.length) {
285
  setUp_(theType);
286
  while ((end - index) % theType.offset) {
287
      end--;
288
  }
289
  for (let i = 0; index < end; index += theType.offset, i++) {
0 ignored issues
show
Unused Code introduced by
The loop variable i is initialized by the loop but not used in the test. Consider using another type of loop if this is the intended behavior.
Loading history...
290
    output[i] = unpack(buffer, theType, index);
291
  }
292
}
293