README.md (30795B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Uint8Array 22 23 > [Typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of 8-bit unsigned integers in the platform byte order. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var Uint8Array = require( '@stdlib/array/uint8' ); 41 ``` 42 43 #### Uint8Array() 44 45 A [typed array][mdn-typed-array] constructor which returns a [typed array][mdn-typed-array] representing an array of 8-bit unsigned integers in the platform byte order. 46 47 <!-- eslint-disable stdlib/require-globals --> 48 49 ```javascript 50 var arr = new Uint8Array(); 51 // returns <Uint8Array> 52 ``` 53 54 #### Uint8Array( length ) 55 56 Returns a [typed array][mdn-typed-array] having a specified length. 57 58 <!-- eslint-disable stdlib/require-globals --> 59 60 ```javascript 61 var arr = new Uint8Array( 5 ); 62 // returns <Uint8Array>[ 0, 0, 0, 0, 0 ] 63 ``` 64 65 #### Uint8Array( typedarray ) 66 67 Creates a [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. 68 69 <!-- eslint-disable stdlib/require-globals --> 70 71 ```javascript 72 var Float32Array = require( '@stdlib/array/float32' ); 73 74 var arr1 = new Float32Array( [ 5.0, 5.0, 5.0 ] ); 75 var arr2 = new Uint8Array( arr1 ); 76 // returns <Uint8Array>[ 5, 5, 5 ] 77 ``` 78 79 #### Uint8Array( obj ) 80 81 Creates a [typed array][mdn-typed-array] from an array-like `object` or iterable. 82 83 <!-- eslint-disable stdlib/require-globals --> 84 85 ```javascript 86 var arr = new Uint8Array( [ 5.0, 5.0, 5.0 ] ); 87 // returns <Uint8Array>[ 5, 5, 5 ] 88 ``` 89 90 #### Uint8Array( buffer\[, byteOffset\[, length]] ) 91 92 Returns a [typed array][mdn-typed-array] view of an [`ArrayBuffer`][@stdlib/array/buffer]. 93 94 <!-- eslint-disable stdlib/require-globals --> 95 96 ```javascript 97 var ArrayBuffer = require( '@stdlib/array/buffer' ); 98 99 var buf = new ArrayBuffer( 4 ); 100 var arr = new Uint8Array( buf, 0, 4 ); 101 // returns <Uint8Array>[ 0, 0, 0, 0 ] 102 ``` 103 104 * * * 105 106 ### Properties 107 108 <a name="static-prop-bytes-per-element"></a> 109 110 #### Uint8Array.BYTES_PER_ELEMENT 111 112 Number of bytes per view element. 113 114 <!-- eslint-disable stdlib/require-globals --> 115 116 ```javascript 117 var nbytes = Uint8Array.BYTES_PER_ELEMENT; 118 // returns 1 119 ``` 120 121 <a name="static-prop-name"></a> 122 123 #### Uint8Array.name 124 125 [Typed array][mdn-typed-array] constructor name. 126 127 <!-- eslint-disable stdlib/require-globals --> 128 129 ```javascript 130 var str = Uint8Array.name; 131 // returns 'Uint8Array' 132 ``` 133 134 <a name="prop-buffer"></a> 135 136 #### Uint8Array.prototype.buffer 137 138 **Read-only** property which returns the [`ArrayBuffer`][@stdlib/array/buffer] referenced by the [typed array][mdn-typed-array]. 139 140 <!-- eslint-disable stdlib/require-globals --> 141 142 ```javascript 143 var arr = new Uint8Array( 5 ); 144 var buf = arr.buffer; 145 // returns <ArrayBuffer> 146 ``` 147 148 <a name="prop-byte-length"></a> 149 150 #### Uint8Array.prototype.byteLength 151 152 **Read-only** property which returns the length (in bytes) of the [typed array][mdn-typed-array]. 153 154 <!-- eslint-disable stdlib/require-globals --> 155 156 ```javascript 157 var arr = new Uint8Array( 5 ); 158 var byteLength = arr.byteLength; 159 // returns 5 160 ``` 161 162 <a name="prop-byte-offset"></a> 163 164 #### Uint8Array.prototype.byteOffset 165 166 **Read-only** property which returns the offset (in bytes) of the [typed array][mdn-typed-array] from the start of its [`ArrayBuffer`][@stdlib/array/buffer]. 167 168 <!-- eslint-disable stdlib/require-globals --> 169 170 ```javascript 171 var arr = new Uint8Array( 5 ); 172 var byteOffset = arr.byteOffset; 173 // returns 0 174 ``` 175 176 <a name="prop-bytes-per-element"></a> 177 178 #### Uint8Array.prototype.BYTES_PER_ELEMENT 179 180 Number of bytes per view element. 181 182 <!-- eslint-disable stdlib/require-globals --> 183 184 ```javascript 185 var arr = new Uint8Array( 5 ); 186 var nbytes = arr.BYTES_PER_ELEMENT; 187 // returns 1 188 ``` 189 190 <a name="prop-length"></a> 191 192 #### Uint8Array.prototype.length 193 194 **Read-only** property which returns the number of view elements. 195 196 <!-- eslint-disable stdlib/require-globals --> 197 198 ```javascript 199 var arr = new Uint8Array( 5 ); 200 var len = arr.length; 201 // returns 5 202 ``` 203 204 * * * 205 206 ### Methods 207 208 <a name="static-method-from"></a> 209 210 #### Uint8Array.from( src\[, map\[, thisArg]] ) 211 212 Creates a new typed array from an array-like `object` or an iterable. 213 214 ```javascript 215 var arr = Uint8Array.from( [ 1, 2 ] ); 216 // returns <Uint8Array>[ 1, 2 ] 217 ``` 218 219 To invoke a function for each `src` value, provide a callback function. 220 221 ```javascript 222 function mapFcn( v ) { 223 return v * 2; 224 } 225 226 var arr = Uint8Array.from( [ 1, 2 ], mapFcn ); 227 // returns <Uint8Array>[ 2, 4 ] 228 ``` 229 230 A callback function is provided two arguments: 231 232 - `value`: source value 233 - `index`: source index 234 235 To set the callback execution context, provide a `thisArg`. 236 237 ```javascript 238 function mapFcn( v ) { 239 this.count += 1; 240 return v * 2; 241 } 242 243 var ctx = { 244 'count': 0 245 }; 246 247 var arr = Uint8Array.from( [ 1, 2 ], mapFcn, ctx ); 248 // returns <Uint8Array>[ 2, 4 ] 249 250 var n = ctx.count; 251 // returns 2 252 ``` 253 254 <a name="static-method-of"></a> 255 256 #### Uint8Array.of( element0\[, element1\[, ...elementN]] ) 257 258 Creates a new typed array from a variable number of arguments. 259 260 ```javascript 261 var arr = Uint8Array.of( 1, 2 ); 262 // returns <Uint8Array>[ 1, 2 ] 263 ``` 264 265 <a name="method-copy-within"></a> 266 267 #### Uint8Array.prototype.copyWithin( target, start\[, end] ) 268 269 Copies a sequence of elements within an array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. 270 271 <!-- eslint-disable stdlib/require-globals --> 272 273 ```javascript 274 var arr = new Uint8Array( [ 1, 2, 3, 4, 5 ] ); 275 276 // Copy the last two elements to the first two elements: 277 arr.copyWithin( 0, 3 ); 278 279 var v = arr[ 0 ]; 280 // returns 4 281 282 v = arr[ 1 ]; 283 // returns 5 284 ``` 285 286 By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. 287 288 <!-- eslint-disable stdlib/require-globals --> 289 290 ```javascript 291 var arr = new Uint8Array( [ 1, 2, 3, 4, 5 ] ); 292 293 // Copy the first two elements to the last two elements: 294 arr.copyWithin( 3, 0, 2 ); 295 296 var v = arr[ 3 ]; 297 // returns 1 298 299 v = arr[ 4 ]; 300 // returns 2 301 ``` 302 303 When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: 304 305 <!-- eslint-disable stdlib/require-globals --> 306 307 ```javascript 308 var arr = new Uint8Array( [ 1, 2, 3, 4, 5 ] ); 309 310 // Copy the first two elements to the last two elements: 311 arr.copyWithin( -2, -5, -3 ); 312 313 var v = arr[ 3 ]; 314 // returns 1 315 316 v = arr[ 4 ]; 317 // returns 2 318 ``` 319 320 <a name="method-entries"></a> 321 322 #### Uint8Array.prototype.entries() 323 324 Returns an iterator for iterating over array key-value pairs. 325 326 <!-- eslint-disable stdlib/require-globals --> 327 328 ```javascript 329 var arr = new Uint8Array( [ 1, 2 ] ); 330 331 // Create an iterator: 332 var it = arr.entries(); 333 334 // Iterate over key-value pairs... 335 var v = it.next().value; 336 // returns [ 0, 1 ] 337 338 v = it.next().value; 339 // returns [ 1, 2 ] 340 341 var bool = it.next().done; 342 // returns true 343 ``` 344 345 <a name="method-every"></a> 346 347 #### Uint8Array.prototype.every( predicate\[, thisArg] ) 348 349 Tests whether all array elements pass a test implemented by a `predicate` function. 350 351 <!-- eslint-disable stdlib/require-globals --> 352 353 ```javascript 354 function predicate( v ) { 355 return ( v <= 1 ); 356 } 357 358 var arr = new Uint8Array( [ 1, 2 ] ); 359 360 var bool = arr.every( predicate ); 361 // returns false 362 ``` 363 364 A `predicate` function is provided three arguments: 365 366 - `value`: array element 367 - `index`: array index 368 - `arr`: array on which the method is invoked 369 370 To set the callback execution context, provide a `thisArg`. 371 372 <!-- eslint-disable stdlib/require-globals --> 373 374 ```javascript 375 function predicate( v ) { 376 this.count += 1; 377 return ( v >= 1 ); 378 } 379 380 var ctx = { 381 'count': 0 382 }; 383 384 var arr = new Uint8Array( [ 1, 2 ] ); 385 386 var bool = arr.every( predicate, ctx ); 387 // returns true 388 389 var n = ctx.count; 390 // returns 2 391 ``` 392 393 <a name="method-fill"></a> 394 395 #### Uint8Array.prototype.fill( value\[, start\[, end]] ) 396 397 Fills an array from a `start` index to an `end` index (non-inclusive) with a provided `value`. 398 399 <!-- eslint-disable stdlib/require-globals --> 400 401 ```javascript 402 var arr = new Uint8Array( 2 ); 403 404 // Set all array elements to the same value: 405 arr.fill( 2 ); 406 407 var v = arr[ 0 ]; 408 // returns 2 409 410 v = arr[ 1 ]; 411 // returns 2 412 413 // Set all array elements starting from the first index to the same value: 414 arr.fill( 3, 1 ); 415 416 v = arr[ 0 ]; 417 // returns 2 418 419 v = arr[ 1 ]; 420 // returns 3 421 422 // Set all array elements, except the last element, to the same value: 423 arr.fill( 4, 0, arr.length-1 ); 424 425 v = arr[ 0 ]; 426 // returns 4 427 428 v = arr[ 1 ]; 429 // returns 3 430 ``` 431 432 When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. 433 434 <!-- eslint-disable stdlib/require-globals --> 435 436 ```javascript 437 var arr = new Uint8Array( 2 ); 438 439 // Set all array elements, except the last element, to the same value: 440 arr.fill( 2, -arr.length, -1 ); 441 442 var v = arr[ 0 ]; 443 // returns 2 444 445 v = arr[ 1 ]; 446 // returns 0 447 ``` 448 449 <a name="method-filter"></a> 450 451 #### Uint8Array.prototype.filter( predicate\[, thisArg] ) 452 453 Creates a new array (of the same data type as the host array) which includes those elements for which a `predicate` function returns a truthy value. 454 455 <!-- eslint-disable stdlib/require-globals --> 456 457 ```javascript 458 function predicate( v ) { 459 return ( v >= 2 ); 460 } 461 462 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 463 464 var arr2 = arr1.filter( predicate ); 465 // returns <Uint8Array>[ 2, 3 ] 466 ``` 467 468 If a `predicate` function does not return a truthy value for any array element, the method returns an empty array. 469 470 <!-- eslint-disable stdlib/require-globals --> 471 472 ```javascript 473 function predicate( v ) { 474 return ( v >= 10 ); 475 } 476 477 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 478 479 var arr2 = arr1.filter( predicate ); 480 // returns <Uint8Array>[] 481 ``` 482 483 A `predicate` function is provided three arguments: 484 485 - `value`: array element 486 - `index`: array index 487 - `arr`: array on which the method is invoked 488 489 To set the callback execution context, provide a `thisArg`. 490 491 <!-- eslint-disable stdlib/require-globals --> 492 493 ```javascript 494 function predicate( v ) { 495 this.count += 1; 496 return ( v >= 2 ); 497 } 498 499 var ctx = { 500 'count': 0 501 }; 502 503 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 504 505 var arr2 = arr1.filter( predicate, ctx ); 506 507 var n = ctx.count; 508 // returns 3 509 ``` 510 511 <a name="method-find"></a> 512 513 #### Uint8Array.prototype.find( predicate\[, thisArg] ) 514 515 Returns the first array element for which a provided `predicate` function returns a truthy value. 516 517 <!-- eslint-disable stdlib/require-globals --> 518 519 ```javascript 520 function predicate( v ) { 521 return ( v > 2 ); 522 } 523 524 var arr = new Uint8Array( [ 1, 2, 3 ] ); 525 526 var v = arr.find( predicate ); 527 // returns 3 528 ``` 529 530 If a `predicate` function does not return a truthy value for any array element, the method returns `undefined`. 531 532 <!-- eslint-disable stdlib/require-globals --> 533 534 ```javascript 535 function predicate( v ) { 536 return ( v < 1 ); 537 } 538 539 var arr = new Uint8Array( [ 1, 2, 3 ] ); 540 541 var v = arr.find( predicate ); 542 // returns undefined 543 ``` 544 545 A `predicate` function is provided three arguments: 546 547 - `value`: array element 548 - `index`: array index 549 - `arr`: array on which the method is invoked 550 551 To set the callback execution context, provide a `thisArg`. 552 553 <!-- eslint-disable stdlib/require-globals --> 554 555 ```javascript 556 function predicate( v ) { 557 this.count += 1; 558 return ( v > 2 ); 559 } 560 561 var ctx = { 562 'count': 0 563 }; 564 565 var arr = new Uint8Array( [ 1, 2, 3 ] ); 566 567 var v = arr.find( predicate, ctx ); 568 // returns 3 569 570 var n = ctx.count; 571 // returns 3 572 ``` 573 574 <a name="method-find-index"></a> 575 576 #### Uint8Array.prototype.findIndex( predicate\[, thisArg] ) 577 578 Returns the index of the first array element for which a provided `predicate` function returns a truthy value. 579 580 <!-- eslint-disable stdlib/require-globals --> 581 582 ```javascript 583 function predicate( v ) { 584 return ( v >= 3 ); 585 } 586 587 var arr = new Uint8Array( [ 1, 2, 3 ] ); 588 589 var idx = arr.findIndex( predicate ); 590 // returns 2 591 ``` 592 593 If a `predicate` function does not return a truthy value for any array element, the method returns `-1`. 594 595 <!-- eslint-disable stdlib/require-globals --> 596 597 ```javascript 598 function predicate( v ) { 599 return ( v < 1 ); 600 } 601 602 var arr = new Uint8Array( [ 1, 2, 3 ] ); 603 604 var idx = arr.findIndex( predicate ); 605 // returns -1 606 ``` 607 608 A `predicate` function is provided three arguments: 609 610 - `value`: array element 611 - `index`: array index 612 - `arr`: array on which the method is invoked 613 614 To set the callback execution context, provide a `thisArg`. 615 616 <!-- eslint-disable stdlib/require-globals --> 617 618 ```javascript 619 function predicate( v ) { 620 this.count += 1; 621 return ( v >= 3 ); 622 } 623 624 var ctx = { 625 'count': 0 626 }; 627 628 var arr = new Uint8Array( [ 1, 2, 3 ] ); 629 630 var idx = arr.findIndex( predicate, ctx ); 631 // returns 2 632 633 var n = ctx.count; 634 // returns 3 635 ``` 636 637 <a name="method-for-each"></a> 638 639 #### Uint8Array.prototype.forEach( fcn\[, thisArg] ) 640 641 Invokes a callback for each array element. 642 643 <!-- eslint-disable stdlib/require-globals --> 644 645 ```javascript 646 var arr = new Uint8Array( [ 1, 2, 3 ] ); 647 648 var str = ''; 649 650 function fcn( v, i ) { 651 str += i + ':' + v; 652 if ( i < arr.length-1 ) { 653 str += ' '; 654 } 655 } 656 657 arr.forEach( fcn ); 658 659 console.log( str ); 660 // => '0:1 1:2 2:3' 661 ``` 662 663 The callback is provided three arguments: 664 665 - `value`: array element 666 - `index`: array index 667 - `arr`: array on which the method is invoked 668 669 To set the callback execution context, provide a `thisArg`. 670 671 <!-- eslint-disable stdlib/require-globals --> 672 673 ```javascript 674 function fcn() { 675 this.count += 1; 676 } 677 678 var ctx = { 679 'count': 0 680 }; 681 682 var arr = new Uint8Array( [ 1, 2, 3 ] ); 683 684 arr.forEach( fcn, ctx ); 685 686 var n = ctx.count; 687 // returns 3 688 ``` 689 690 <a name="method-includes"></a> 691 692 #### Uint8Array.prototype.includes( searchElement\[, fromIndex] ) 693 694 Returns a `boolean` indicating whether an array includes a search element. 695 696 <!-- eslint-disable stdlib/require-globals --> 697 698 ```javascript 699 var arr = new Uint8Array( [ 1, 2, 3 ] ); 700 701 var bool = arr.includes( 3 ); 702 // returns true 703 704 bool = arr.includes( 0 ); 705 // returns false 706 ``` 707 708 By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. 709 710 <!-- eslint-disable stdlib/require-globals --> 711 712 ```javascript 713 var arr = new Uint8Array( [ 1, 2, 3 ] ); 714 715 var bool = arr.includes( 1, 1 ); 716 // returns false 717 ``` 718 719 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 720 721 <!-- eslint-disable stdlib/require-globals --> 722 723 ```javascript 724 var arr = new Uint8Array( [ 1, 2, 3 ] ); 725 726 bool = arr.includes( 1, -2 ); 727 // returns false 728 ``` 729 730 <a name="method-index-of"></a> 731 732 #### Uint8Array.prototype.indexOf( searchElement\[, fromIndex] ) 733 734 Returns the index of the first array element strictly equal to a search element. 735 736 <!-- eslint-disable stdlib/require-globals --> 737 738 ```javascript 739 var arr = new Uint8Array( [ 1, 2, 3 ] ); 740 741 var idx = arr.indexOf( 3 ); 742 // returns 2 743 744 idx = arr.indexOf( 0 ); 745 // returns -1 746 ``` 747 748 By default, the method searches the entire array (`fromIndex = 0`). To begin searching from a specific array index, provide a `fromIndex`. 749 750 <!-- eslint-disable stdlib/require-globals --> 751 752 ```javascript 753 var arr = new Uint8Array( [ 1, 2, 3 ] ); 754 755 var idx = arr.indexOf( 1, 1 ); 756 // returns -1 757 ``` 758 759 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 760 761 <!-- eslint-disable stdlib/require-globals --> 762 763 ```javascript 764 var arr = new Uint8Array( [ 1, 2, 3 ] ); 765 766 var idx = arr.indexOf( 1, -2 ); 767 // returns -1 768 ``` 769 770 <a name="method-join"></a> 771 772 #### Uint8Array.prototype.join( \[separator] ) 773 774 Serializes an array by joining all array elements as a string. 775 776 <!-- eslint-disable stdlib/require-globals --> 777 778 ```javascript 779 var arr = new Uint8Array( [ 1, 2, 3 ] ); 780 781 var str = arr.join(); 782 // returns '1,2,3' 783 ``` 784 785 By default, the method delineates array elements using a comma `,`. To specify a custom separator, provide a `separator` string. 786 787 <!-- eslint-disable stdlib/require-globals --> 788 789 ```javascript 790 var arr = new Uint8Array( [ 1, 2, 3 ] ); 791 792 var str = arr.join( '|' ); 793 // returns '1|2|3' 794 ``` 795 796 <a name="method-keys"></a> 797 798 #### Uint8Array.prototype.keys() 799 800 Returns an iterator for iterating over array keys. 801 802 <!-- eslint-disable stdlib/require-globals --> 803 804 ```javascript 805 var arr = new Uint8Array( [ 1, 2 ] ); 806 807 // Create an iterator: 808 var it = arr.keys(); 809 810 // Iterate over keys... 811 var v = it.next().value; 812 // returns 0 813 814 v = it.next().value; 815 // returns 1 816 817 var bool = it.next().done; 818 // returns true 819 ``` 820 821 <a name="method-last-index-of"></a> 822 823 #### Uint8Array.prototype.lastIndexOf( searchElement\[, fromIndex] ) 824 825 Returns the index of the last array element strictly equal to a search element, iterating from right to left. 826 827 <!-- eslint-disable stdlib/require-globals --> 828 829 ```javascript 830 var arr = new Uint8Array( [ 1, 0, 2, 0, 1 ] ); 831 832 var idx = arr.lastIndexOf( 0 ); 833 // returns 3 834 835 idx = arr.lastIndexOf( 3 ); 836 // returns -1 837 ``` 838 839 By default, the method searches the entire array (`fromIndex = -1`). To begin searching from a specific array index, provide a `fromIndex`. 840 841 <!-- eslint-disable stdlib/require-globals --> 842 843 ```javascript 844 var arr = new Uint8Array( [ 1, 0, 2, 0, 1 ] ); 845 846 var idx = arr.lastIndexOf( 0, 2 ); 847 // returns 1 848 ``` 849 850 When a `fromIndex` is negative, the starting index is resolved relative to the last array element. 851 852 <!-- eslint-disable stdlib/require-globals --> 853 854 ```javascript 855 var arr = new Uint8Array( [ 1, 0, 2, 0, 1 ] ); 856 857 var idx = arr.lastIndexOf( 0, -3 ); 858 // returns 1 859 ``` 860 861 <a name="method-map"></a> 862 863 #### Uint8Array.prototype.map( fcn\[, thisArg] ) 864 865 Maps each array element to an element in a new array having the same data type as the host array. 866 867 <!-- eslint-disable stdlib/require-globals --> 868 869 ```javascript 870 function fcn( v ) { 871 return v * 2; 872 } 873 874 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 875 876 var arr2 = arr1.map( fcn ); 877 // returns <Uint8Array>[ 2, 4, 6 ] 878 ``` 879 880 A callback is provided three arguments: 881 882 - `value`: array element 883 - `index`: array index 884 - `arr`: array on which the method is invoked 885 886 To set the callback execution context, provide a `thisArg`. 887 888 <!-- eslint-disable stdlib/require-globals --> 889 890 ```javascript 891 function fcn( v ) { 892 this.count += 1; 893 return v * 2; 894 } 895 896 var ctx = { 897 'count': 0 898 }; 899 900 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 901 902 var arr2 = arr1.map( fcn, ctx ); 903 904 var n = ctx.count; 905 // returns 3 906 ``` 907 908 <a name="method-reduce"></a> 909 910 #### Uint8Array.prototype.reduce( fcn\[, initialValue] ) 911 912 Applies a function against an accumulator and each element in an array and returns the accumulated result. 913 914 <!-- eslint-disable stdlib/require-globals --> 915 916 ```javascript 917 function fcn( acc, v ) { 918 return acc + ( v*v ); 919 } 920 921 var arr = new Uint8Array( [ 2, 1, 3 ] ); 922 923 var v = arr.reduce( fcn ); 924 // returns 12 925 ``` 926 927 If not provided an initial value, the method invokes a provided function with the first array element as the first argument and the second array element as the second argument. 928 929 If provided an initial value, the method invokes a provided function with the initial value as the first argument and the first array element as the second argument. 930 931 <!-- eslint-disable stdlib/require-globals --> 932 933 ```javascript 934 function fcn( acc, v ) { 935 return acc + ( v*v ); 936 } 937 938 var arr = new Uint8Array( [ 2, 1, 3 ] ); 939 940 var v = arr.reduce( fcn, 0 ); 941 // returns 14 942 ``` 943 944 A callback is provided four arguments: 945 946 - `acc`: accumulated result 947 - `value`: array element 948 - `index`: array index 949 - `arr`: array on which the method is invoked 950 951 <a name="method-reduce-right"></a> 952 953 #### Uint8Array.prototype.reduceRight( fcn\[, initialValue] ) 954 955 Applies a function against an accumulator and each element in an array and returns the accumulated result, iterating from right to left. 956 957 <!-- eslint-disable stdlib/require-globals --> 958 959 ```javascript 960 function fcn( acc, v ) { 961 return acc + ( v*v ); 962 } 963 964 var arr = new Uint8Array( [ 2, 1, 3 ] ); 965 966 var v = arr.reduceRight( fcn ); 967 // returns 8 968 ``` 969 970 If not provided an initial value, the method invokes a provided function with the last array element as the first argument and the second-to-last array element as the second argument. 971 972 If provided an initial value, the method invokes a provided function with the initial value as the first argument and the last array element as the second argument. 973 974 <!-- eslint-disable stdlib/require-globals --> 975 976 ```javascript 977 function fcn( acc, v ) { 978 return acc + ( v*v ); 979 } 980 981 var arr = new Uint8Array( [ 2, 1, 3 ] ); 982 983 var v = arr.reduce( fcn, 0 ); 984 // returns 14 985 ``` 986 987 A callback is provided four arguments: 988 989 - `acc`: accumulated result 990 - `value`: array element 991 - `index`: array index 992 - `arr`: array on which the method is invoked 993 994 <a name="method-reverse"></a> 995 996 #### Uint8Array.prototype.reverse() 997 998 Reverses an array **in-place** (thus mutating the array on which the method is invoked). 999 1000 <!-- eslint-disable stdlib/require-globals --> 1001 1002 ```javascript 1003 var arr = new Uint8Array( [ 2, 0, 3 ] ); 1004 1005 // Reverse the array: 1006 arr.reverse(); 1007 1008 var v = arr[ 0 ]; 1009 // returns 3 1010 1011 v = arr[ 1 ]; 1012 // returns 0 1013 1014 v = arr[ 2 ]; 1015 // returns 2 1016 ``` 1017 1018 <a name="method-set"></a> 1019 1020 #### Uint8Array.prototype.set( arr\[, offset] ) 1021 1022 Sets array elements. 1023 1024 <!-- eslint-disable stdlib/require-globals --> 1025 1026 ```javascript 1027 var arr = new Uint8Array( [ 1, 2, 3 ] ); 1028 // returns <Uint8Array>[ 1, 2, 3 ] 1029 1030 // Set the first two array elements: 1031 arr.set( [ 4, 5 ] ); 1032 1033 var v = arr[ 0 ]; 1034 // returns 4 1035 1036 v = arr[ 1 ]; 1037 // returns 5 1038 ``` 1039 1040 By default, the method starts writing values at the first array index. To specify an alternative index, provide an index `offset`. 1041 1042 <!-- eslint-disable stdlib/require-globals --> 1043 1044 ```javascript 1045 var arr = new Uint8Array( [ 1, 2, 3 ] ); 1046 // returns <Uint8Array>[ 1, 2, 3 ] 1047 1048 // Set the last two array elements: 1049 arr.set( [ 4, 5 ], 1 ); 1050 1051 var v = arr[ 1 ]; 1052 // returns 4 1053 1054 v = arr[ 2 ]; 1055 // returns 5 1056 ``` 1057 1058 <a name="method-slice"></a> 1059 1060 #### Uint8Array.prototype.slice( \[begin\[, end]] ) 1061 1062 Copies array elements to a new array with the same underlying data type as the host array. 1063 1064 <!-- eslint-disable stdlib/require-globals --> 1065 1066 ```javascript 1067 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1068 1069 var arr2 = arr1.slice(); 1070 1071 var bool = ( arr1 === arr2 ); 1072 // returns false 1073 1074 bool = ( arr1.buffer === arr2.buffer ); 1075 // returns false 1076 1077 var v = arr2[ 0 ]; 1078 // returns 1 1079 1080 v = arr2[ 1 ]; 1081 // returns 2 1082 1083 v = arr2[ 2 ]; 1084 // returns 3 1085 ``` 1086 1087 By default, the method copies elements beginning with the first array element. To specify an alternative array index at which to begin copying, provide a `begin` index (inclusive). 1088 1089 <!-- eslint-disable stdlib/require-globals --> 1090 1091 ```javascript 1092 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1093 1094 var arr2 = arr1.slice( 1 ); 1095 1096 var len = arr2.length; 1097 // returns 2 1098 1099 var v = arr2[ 0 ]; 1100 // returns 2 1101 1102 v = arr2[ 1 ]; 1103 // returns 3 1104 ``` 1105 1106 By default, the method copies all array elements after `begin`. To specify an alternative array index at which to end copying, provide an `end` index (exclusive). 1107 1108 <!-- eslint-disable stdlib/require-globals --> 1109 1110 ```javascript 1111 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1112 1113 var arr2 = arr1.slice( 0, 2 ); 1114 1115 var len = arr2.length; 1116 // returns 2 1117 1118 var v = arr2[ 0 ]; 1119 // returns 1 1120 1121 v = arr2[ 1 ]; 1122 // returns 2 1123 ``` 1124 1125 When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. 1126 1127 <!-- eslint-disable stdlib/require-globals --> 1128 1129 ```javascript 1130 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1131 1132 var arr2 = arr1.slice( -arr1.length, -1 ); 1133 1134 var len = arr2.length; 1135 // returns 2 1136 1137 var v = arr2[ 0 ]; 1138 // returns 1 1139 1140 v = arr2[ 1 ]; 1141 // returns 2 1142 ``` 1143 1144 <a name="method-some"></a> 1145 1146 #### Uint8Array.prototype.some( predicate\[, thisArg] ) 1147 1148 Tests whether at least one array element passes a test implemented by a `predicate` function. 1149 1150 <!-- eslint-disable stdlib/require-globals --> 1151 1152 ```javascript 1153 function predicate( v ) { 1154 return ( v >= 2 ); 1155 } 1156 1157 var arr = new Uint8Array( [ 1, 2 ] ); 1158 1159 var bool = arr.some( predicate ); 1160 // returns true 1161 ``` 1162 1163 A `predicate` function is provided three arguments: 1164 1165 - `value`: array element 1166 - `index`: array index 1167 - `arr`: array on which the method is invoked 1168 1169 To set the callback execution context, provide a `thisArg`. 1170 1171 <!-- eslint-disable stdlib/require-globals --> 1172 1173 ```javascript 1174 function predicate( v ) { 1175 this.count += 1; 1176 return ( v >= 2 ); 1177 } 1178 1179 var ctx = { 1180 'count': 0 1181 }; 1182 1183 var arr = new Uint8Array( [ 1, 1 ] ); 1184 1185 var bool = arr.some( predicate, ctx ); 1186 // returns false 1187 1188 var n = ctx.count; 1189 // returns 2 1190 ``` 1191 1192 <a name="method-sort"></a> 1193 1194 #### Uint8Array.prototype.sort( \[compareFunction] ) 1195 1196 Sorts an array **in-place** (thus mutating the array on which the method is invoked). 1197 1198 <!-- eslint-disable stdlib/require-globals --> 1199 1200 ```javascript 1201 var arr = new Uint8Array( [ 2, 3, 0 ] ); 1202 1203 // Sort the array (in ascending order): 1204 arr.sort(); 1205 1206 var v = arr[ 0 ]; 1207 // returns 0 1208 1209 v = arr[ 1 ]; 1210 // returns 2 1211 1212 v = arr[ 2 ]; 1213 // returns 3 1214 ``` 1215 1216 By default, the method sorts array elements in ascending order. To impose a custom order, provide a `compareFunction`. 1217 1218 <!-- eslint-disable stdlib/require-globals --> 1219 1220 ```javascript 1221 function descending( a, b ) { 1222 return b - a; 1223 } 1224 1225 var arr = new Uint8Array( [ 2, 3, 0 ] ); 1226 1227 // Sort the array (in descending order): 1228 arr.sort( descending ); 1229 1230 var v = arr[ 0 ]; 1231 // returns 3 1232 1233 v = arr[ 1 ]; 1234 // returns 2 1235 1236 v = arr[ 2 ]; 1237 // returns 0 1238 ``` 1239 1240 The comparison function is provided two array elements, `a` and `b`, per invocation, and its return value determines the sort order as follows: 1241 1242 - If the comparison function returns a value **less** than zero, then the method sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`). 1243 - If the comparison function returns a value **greater** than zero, then the method sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`). 1244 - If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged. 1245 1246 <a name="method-subarray"></a> 1247 1248 #### Uint8Array.prototype.subarray( \[begin\[, end]] ) 1249 1250 Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. 1251 1252 <!-- eslint-disable stdlib/require-globals --> 1253 1254 ```javascript 1255 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1256 1257 var arr2 = arr1.subarray(); 1258 // returns <Uint8Array>[ 1, 2, 3 ] 1259 1260 var bool = ( arr1.buffer === arr2.buffer ); 1261 // returns true 1262 ``` 1263 1264 By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). 1265 1266 <!-- eslint-disable stdlib/require-globals --> 1267 1268 ```javascript 1269 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1270 1271 var arr2 = arr1.subarray( 1 ); 1272 // returns <Uint8Array>[ 2, 3 ] 1273 1274 var bool = ( arr1.buffer === arr2.buffer ); 1275 // returns true 1276 ``` 1277 1278 By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). 1279 1280 <!-- eslint-disable stdlib/require-globals --> 1281 1282 ```javascript 1283 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1284 1285 var arr2 = arr1.subarray( 0, 2 ); 1286 // returns <Uint8Array>[ 1, 2 ] 1287 1288 var bool = ( arr1.buffer === arr2.buffer ); 1289 // returns true 1290 ``` 1291 1292 When a `begin` and/or `end` index is negative, the respective index is determined relative to the last array element. 1293 1294 <!-- eslint-disable stdlib/require-globals --> 1295 1296 ```javascript 1297 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1298 1299 var arr2 = arr1.subarray( -arr1.length, -1 ); 1300 // returns <Uint8Array>[ 1, 2 ] 1301 1302 var bool = ( arr1.buffer === arr2.buffer ); 1303 // returns true 1304 ``` 1305 1306 If the method is unable to resolve indices to a non-empty array subsequence, the method returns an empty typed array. 1307 1308 <!-- eslint-disable stdlib/require-globals --> 1309 1310 ```javascript 1311 var arr1 = new Uint8Array( [ 1, 2, 3 ] ); 1312 1313 var arr2 = arr1.subarray( 10, -1 ); 1314 // returns <Uint8Array>[] 1315 ``` 1316 1317 <a name="method-to-locale-string"></a> 1318 1319 #### Uint8Array.prototype.toLocaleString( \[locales\[, options]] ) 1320 1321 Serializes an array as a locale-specific `string`. 1322 1323 <!-- eslint-disable stdlib/require-globals --> 1324 1325 ```javascript 1326 var arr = new Uint8Array( [ 1, 2, 3 ] ); 1327 1328 var str = arr.toLocaleString(); 1329 // returns '1,2,3' 1330 ``` 1331 1332 <a name="method-to-string"></a> 1333 1334 #### Uint8Array.prototype.toString() 1335 1336 Serializes an array as a `string`. 1337 1338 <!-- eslint-disable stdlib/require-globals --> 1339 1340 ```javascript 1341 var arr = new Uint8Array( [ 1, 2, 3 ] ); 1342 1343 var str = arr.toString(); 1344 // returns '1,2,3' 1345 ``` 1346 1347 <a name="method-values"></a> 1348 1349 #### Uint8Array.prototype.values() 1350 1351 Returns an iterator for iterating over array elements. 1352 1353 <!-- eslint-disable stdlib/require-globals --> 1354 1355 ```javascript 1356 var arr = new Uint8Array( [ 1, 2 ] ); 1357 1358 // Create an iterator: 1359 var it = arr.values(); 1360 1361 // Iterate over array elements... 1362 var v = it.next().value; 1363 // returns 1 1364 1365 v = it.next().value; 1366 // returns 2 1367 1368 var bool = it.next().done; 1369 // returns true 1370 ``` 1371 1372 </section> 1373 1374 <!-- /.usage --> 1375 1376 * * * 1377 1378 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1379 1380 <section class="notes"> 1381 1382 </section> 1383 1384 <!-- /.notes --> 1385 1386 <!-- Package usage examples. --> 1387 1388 <section class="examples"> 1389 1390 ## Examples 1391 1392 <!-- eslint no-undef: "error" --> 1393 1394 ```javascript 1395 var randu = require( '@stdlib/random/base/randu' ); 1396 var round = require( '@stdlib/math/base/special/round' ); 1397 var ctor = require( '@stdlib/array/uint8' ); 1398 1399 var arr; 1400 var i; 1401 1402 arr = new ctor( 10 ); 1403 for ( i = 0; i < arr.length; i++ ) { 1404 arr[ i ] = round( randu()*100.0 ); 1405 } 1406 console.log( arr ); 1407 ``` 1408 1409 </section> 1410 1411 <!-- /.examples --> 1412 1413 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1414 1415 <section class="references"> 1416 1417 </section> 1418 1419 <!-- /.references --> 1420 1421 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 1422 1423 <section class="links"> 1424 1425 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 1426 1427 [@stdlib/array/buffer]: https://www.npmjs.com/package/@stdlib/array/tree/main/buffer 1428 1429 </section> 1430 1431 <!-- /.links -->