lib/array.js
function ArrayType (data, length) {
    if (!(this instanceof ArrayType)) {
      return new ArrayType(data, length)
    }
    debug('creating new array instance')
    ArrayIndex.call(this)
    var item_size = ArrayType.BYTES_PER_ELEMENT
    if (0 === arguments.length) {
      // new IntArray()
      // use the "fixedLength" if provided, otherwise throw an Error
      if (fixedLength > 0) {
        this.length = fixedLength
        this.buffer = new Buffer(this.length * item_size)
      } else {
        throw new Error('A "length", "array" or "buffer" must be passed as the first argument')
      }
    } else if ('number' == typeof data) {
      // new IntArray(69)
      this.length = data
      this.buffer = new Buffer(this.length * item_size)
    } else if (isArray(data)) {
      // new IntArray([ 1, 2, 3, 4, 5 ], {len})
      // use optional "length" if provided, otherwise use "fixedLength, otherwise
      // use the Array's .length
      var len = 0
      if (null != length) {
        len = length
      } else if (fixedLength > 0) {
        len = fixedLength
      } else {
        len = data.length
      }
      if (data.length < len) {
        throw new Error('array length must be at least ' + len + ', got ' + data.length)
      }
      this.length = len
      this.buffer = new Buffer(len * item_size)
      for (var i = 0; i < len; i++) {
        this[i] = data[i]
      }
    } else if (Buffer.isBuffer(data)) {
      // new IntArray(Buffer(8))
      var len = 0
      if (null != length) {
        len = length
      } else if (fixedLength > 0) {
        len = fixedLength
      } else {
        len = data.length / item_size | 0
      }
      var expectedLength = item_size * len
      this.length = len
      if (data.length != expectedLength) {
        if (data.length < expectedLength) {
          throw new Error('buffer length must be at least ' + expectedLength + ', got ' + data.length)
        } else {
          debug('resizing buffer from %d to %d', data.length, expectedLength)
          data = data.slice(0, expectedLength)
        }
      }
      this.buffer = data
    }
  }
function ArrayType (data, length) {
    if (!(this instanceof ArrayType)) {
      return new ArrayType(data, length)
    }
    debug('creating new array instance')
    ArrayIndex.call(this)
    var item_size = ArrayType.BYTES_PER_ELEMENT
    if (0 === arguments.length) {
      // new IntArray()
      // use the "fixedLength" if provided, otherwise throw an Error
      if (fixedLength > 0) {
        this.length = fixedLength
        this.buffer = new Buffer(this.length * item_size)
      } else {
        throw new Error('A "length", "array" or "buffer" must be passed as the first argument')
      }
    } else if ('number' == typeof data) {
      // new IntArray(69)
      this.length = data
      this.buffer = new Buffer(this.length * item_size)
    } else if (isArray(data)) {
      // new IntArray([ 1, 2, 3, 4, 5 ], {len})
      // use optional "length" if provided, otherwise use "fixedLength, otherwise
      // use the Array's .length
      var len = 0
      if (null != length) {
        len = length
      } else if (fixedLength > 0) {
        len = fixedLength
      } else {
        len = data.length
      }
      if (data.length < len) {
        throw new Error('array length must be at least ' + len + ', got ' + data.length)
      }
      this.length = len
      this.buffer = new Buffer(len * item_size)
      for (var i = 0; i < len; i++) {
        this[i] = data[i]
      }
    } else if (Buffer.isBuffer(data)) {
      // new IntArray(Buffer(8))
      var len = 0
      if (null != length) {
        len = length
      } else if (fixedLength > 0) {
        len = fixedLength
      } else {
        len = data.length / item_size | 0
      }
      var expectedLength = item_size * len
      this.length = len
      if (data.length != expectedLength) {
        if (data.length < expectedLength) {
          throw new Error('buffer length must be at least ' + expectedLength + ', got ' + data.length)
        } else {
          debug('resizing buffer from %d to %d', data.length, expectedLength)
          data = data.slice(0, expectedLength)
        }
      }
      this.buffer = data
    }
  }
function ArrayType ( data , length ) { if ( ! ( this instanceof ArrayType ) ) { return new ArrayType ( data , length ) } debug ( 'creating new array instance' ) ArrayIndex . call ( this ) var item_size = ArrayType . BYTES_PER_ELEMENT if ( 0 === arguments . length ) { // new IntArray() // use the "fixedLength" if provided, otherwise throw an Error if ( fixedLength > 0 ) { this . length = fixedLength this . buffer = new Buffer ( this . length * item_size ) } else { throw new Error ( 'A "length", "array" or "buffer" must be passed as the first argument' ) } } else if ( 'number' == typeof data ) { // new IntArray(69) this . length = data this . buffer = new Buffer ( this . length * item_size ) } else if ( isArray ( data ) ) { // new IntArray([ 1, 2, 3, 4, 5 ], {len}) // use optional "length" if provided, otherwise use "fixedLength, otherwise // use the Array's .length var len = 0 if ( null != length ) { len = length } else if ( fixedLength > 0 ) { len = fixedLength } else { len = data . length } if ( data . length < len ) { throw new Error ( 'array length must be at least ' + len + ', got ' + data . length ) } this . length = len this . buffer = new Buffer ( len * item_size ) for ( var i = 0 ; i < len ; i ++ ) { this [ i ] = data [ i ] } } else if ( Buffer . isBuffer ( data ) ) { // new IntArray(Buffer(8)) var len = 0 if ( null != length ) { len = length } else if ( fixedLength > 0 ) { len = fixedLength } else { len = data . length / item_size | 0 } var expectedLength = item_size * len this . length = len if ( data . length != expectedLength ) { if ( data . length < expectedLength ) { throw new Error ( 'buffer length must be at least ' + expectedLength + ', got ' + data . length ) } else { debug ( 'resizing buffer from %d to %d' , data . length , expectedLength ) data = data . slice ( 0 , expectedLength ) } } this . buffer = data } }
This is the ArrayType "constructor" that gets returned.
This is the ArrayType constructor that gets returned .