Repository: nodejs/node
File: lib/buffer.js (JavaScript)

Code before:
  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      if (!isUint8Array(buf)) {
        // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
        // Instead, find the proper error code for this.
        throw new ERR_INVALID_ARG_TYPE(
          `list[${i}]`, ['Buffer', 'Uint8Array'], buf);
      }
      length += TypedArrayPrototypeGetByteLength(buf);
    }

    const buffer = allocate(length);
    let pos = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      const bufLength = TypedArrayPrototypeGetByteLength(buf);
      TypedArrayPrototypeSet(buffer, buf, pos);
      pos += bufLength;
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
    }
  }

  const buffer = allocate(length);
  let pos = 0;
  for (let i = 0; i < list.length; i++) {
    const buf = list[i];
    const bufLength = TypedArrayPrototypeGetByteLength(buf);
    if (pos + bufLength > length) {
      TypedArrayPrototypeSet(buffer,
                             TypedArrayPrototypeSlice(buf, 0, length - pos),
                             pos);
      pos = length;
      break;
    }
    TypedArrayPrototypeSet(buffer, buf, pos);

Review comment (suggestion):
Nit: my preference would still be for an assert here, since `pos !== length` is de facto an invalid state.

Code after:

  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      if (!isUint8Array(buf)) {
        // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
        // Instead, find the proper error code for this.
        throw new ERR_INVALID_ARG_TYPE(
          `list[${i}]`, ['Buffer', 'Uint8Array'], buf);
      }
      length += TypedArrayPrototypeGetByteLength(buf);
    }

    const buffer = allocate(length);
    let pos = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      const bufLength = TypedArrayPrototypeGetByteLength(buf);
      TypedArrayPrototypeSet(buffer, buf, pos);
      pos += bufLength;
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
    }
  }

  const buffer = allocate(length);
  let pos = 0;
  for (let i = 0; i < list.length; i++) {
    const buf = list[i];
    const bufLength = TypedArrayPrototypeGetByteLength(buf);
    if (pos + bufLength > length) {
      TypedArrayPrototypeSet(buffer,
                             TypedArrayPrototypeSubarray(buf, 0, length - pos),
                             pos);
      pos = length;
      break;
    }