You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
994 B

1 year ago
  1. 'use strict';
  2. const _has = (obj, key) => {
  3. return Object.prototype.hasOwnProperty.call(obj, key);
  4. };
  5. module.exports.assign = function (obj /*from1, from2, from3, ...*/) {
  6. const sources = Array.prototype.slice.call(arguments, 1);
  7. while (sources.length) {
  8. const source = sources.shift();
  9. if (!source) { continue; }
  10. if (typeof source !== 'object') {
  11. throw new TypeError(source + 'must be non-object');
  12. }
  13. for (const p in source) {
  14. if (_has(source, p)) {
  15. obj[p] = source[p];
  16. }
  17. }
  18. }
  19. return obj;
  20. };
  21. // Join array of chunks to single array.
  22. module.exports.flattenChunks = (chunks) => {
  23. // calculate data length
  24. let len = 0;
  25. for (let i = 0, l = chunks.length; i < l; i++) {
  26. len += chunks[i].length;
  27. }
  28. // join chunks
  29. const result = new Uint8Array(len);
  30. for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
  31. let chunk = chunks[i];
  32. result.set(chunk, pos);
  33. pos += chunk.length;
  34. }
  35. return result;
  36. };