@@ -51,6 +51,8 @@ var fromIteratorMap = require( './from_iterator_map.js' );
5151// VARIABLES //
5252
5353var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport ( ) ;
54+ var LITTLE_ENDIAN = 'little-endian' ;
55+ var BIG_ENDIAN = 'big-endian' ;
5456var DTYPES = [ 'float64' , 'float32' , 'int32' , 'int16' , 'uint32' , 'uint16' ] ;
5557var DTYPE2SET = {
5658 'float64' : 'setFloat64' ,
@@ -99,7 +101,18 @@ function byteOrder( value ) {
99101* @returns {boolean } boolean indicating whether a byte order is little-endian byte order
100102*/
101103function isLittleEndian ( value ) {
102- return ( value === 'little-endian' ) ;
104+ return ( value === LITTLE_ENDIAN ) ;
105+ }
106+
107+ /**
108+ * Resolves a byte order string from a boolean flag.
109+ *
110+ * @private
111+ * @param {boolean } isLE - flag indicating whether an array is little-endian
112+ * @returns {string } resolved byte order
113+ */
114+ function flag2byteOrder ( isLE ) {
115+ return ( isLE ) ? LITTLE_ENDIAN : BIG_ENDIAN ;
103116}
104117
105118/**
@@ -634,6 +647,41 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
634647 return this . _length ;
635648 } ) ;
636649
650+ /**
651+ * Returns a new array with each element being the result of a provided callback function.
652+ *
653+ * @private
654+ * @name map
655+ * @memberof TypedArray.prototype
656+ * @type {Function }
657+ * @param {Function } fcn - function to invoke
658+ * @param {* } [thisArg] - function invocation context
659+ * @throws {TypeError } `this` must be a typed array instance
660+ * @throws {TypeError } first argument must be a function
661+ * @returns {TypedArray } new typed array
662+ */
663+ setReadOnly ( TypedArray . prototype , 'map' , function map ( fcn , thisArg ) {
664+ var obuf ;
665+ var out ;
666+ var buf ;
667+ var i ;
668+ var v ;
669+ if ( ! isTypedArray ( this ) ) {
670+ throw new TypeError ( format ( 'invalid invocation. `this` is not %s %s.' , CHAR2ARTICLE [ dtype [ 0 ] ] , CTOR_NAME ) ) ;
671+ }
672+ if ( ! isFunction ( fcn ) ) {
673+ throw new TypeError ( format ( 'invalid argument. First argument must be a function. Value: `%s`.' , fcn ) ) ;
674+ }
675+ buf = this . _buffer ;
676+ out = new this . constructor ( flag2byteOrder ( this . _isLE ) , this . _length ) ;
677+ obuf = out . _buffer ; // eslint-disable-line no-underscore-dangle
678+ for ( i = 0 ; i < this . _length ; i ++ ) {
679+ v = fcn . call ( thisArg , buf [ GETTER ] ( i * BYTES_PER_ELEMENT , this . _isLE ) , i , this ) ;
680+ obuf [ SETTER ] ( i * BYTES_PER_ELEMENT , v , this . _isLE ) ;
681+ }
682+ return out ;
683+ } ) ;
684+
637685 /**
638686 * Sets an array element.
639687 *
0 commit comments