README.md (4440B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 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 # DataView 22 23 > [Constructor][mdn-dataview] which returns a data view representing a provided array buffer. 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 DataView = require( '@stdlib/array/dataview' ); 41 ``` 42 43 #### DataView( buffer\[, byteOffset\[, byteLength]] ) 44 45 Returns a [`DataView`][mdn-dataview] representing a provided array buffer. 46 47 <!-- eslint-disable stdlib/require-globals --> 48 49 ```javascript 50 var ArrayBuffer = require( '@stdlib/array/buffer' ); 51 52 var buf = new ArrayBuffer( 5 ); 53 // returns <ArrayBuffer> 54 55 var dv = new DataView( buf ); 56 // returns <DataView> 57 ``` 58 59 * * * 60 61 ### Properties 62 63 #### DataView.prototype.buffer 64 65 **Read-only** property which returns the underlying array buffer. 66 67 <!-- eslint-disable stdlib/require-globals --> 68 69 ```javascript 70 var ArrayBuffer = require( '@stdlib/array/buffer' ); 71 72 var buf1 = new ArrayBuffer( 5 ); 73 var dv = new DataView( buf1 ); 74 75 var buf2 = dv.buffer; 76 // returns <ArrayBuffer> 77 78 var bool = ( buf1 === buf2 ); 79 // returns true 80 ``` 81 82 #### DataView.prototype.byteLength 83 84 **Read-only** property which returns the length (in bytes) of the [`DataView`][mdn-dataview]. 85 86 <!-- eslint-disable stdlib/require-globals --> 87 88 ```javascript 89 var ArrayBuffer = require( '@stdlib/array/buffer' ); 90 91 var buf = new ArrayBuffer( 5 ); 92 var dv = new DataView( buf ); 93 94 var byteLength = dv.byteLength; 95 // returns 5 96 ``` 97 98 #### DataView.prototype.byteOffset 99 100 **Read-only** property which returns the number of bytes from the [`DataView`][mdn-dataview] to the start of the underlying array buffer. 101 102 <!-- eslint-disable stdlib/require-globals --> 103 104 ```javascript 105 var ArrayBuffer = require( '@stdlib/array/buffer' ); 106 107 var buf = new ArrayBuffer( 5 ); 108 var dv = new DataView( buf, 3 ); 109 110 var byteOffset = dv.byteOffset; 111 // returns 3 112 ``` 113 114 * * * 115 116 ### Methods 117 118 TODO: document methods 119 120 </section> 121 122 <!-- /.usage --> 123 124 * * * 125 126 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 127 128 <section class="notes"> 129 130 </section> 131 132 <!-- /.notes --> 133 134 <!-- Package usage examples. --> 135 136 <section class="examples"> 137 138 ## Examples 139 140 <!-- eslint no-undef: "error" --> 141 142 ```javascript 143 var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); 144 var toBinaryString = require( '@stdlib/number/uint8/base/to-binary-string' ); 145 var randu = require( '@stdlib/random/base/randu' ); 146 var Uint8Array = require( '@stdlib/array/uint8' ); 147 var ArrayBuffer = require( '@stdlib/array/buffer' ); 148 var DataView = require( '@stdlib/array/dataview' ); 149 150 // Create a new ArrayBuffer: 151 var buf = new ArrayBuffer( 64 ); 152 153 // Create a new DataView: 154 var dv = new DataView( buf ); 155 156 // Set values in the view: 157 var i; 158 for ( i = 0; i < dv.byteLength/8; i++ ) { 159 dv.setFloat64( i*8, randu()*100.0, IS_LITTLE_ENDIAN ); 160 } 161 162 // Create a "bytes" view of the underlying array buffer: 163 var bytes = new Uint8Array( dv.buffer ); 164 165 // Print the bytes: 166 for ( i = 0; i < bytes.length; i++ ) { 167 console.log( 'byte %d: %s', i, toBinaryString( bytes[ i ] ) ); 168 } 169 ``` 170 171 </section> 172 173 <!-- /.examples --> 174 175 <!-- 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. --> 176 177 <section class="references"> 178 179 </section> 180 181 <!-- /.references --> 182 183 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 184 185 <section class="links"> 186 187 [mdn-dataview]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView 188 189 </section> 190 191 <!-- /.links -->