|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>JSDoc: Source: BinaryArray.js</title> |
| 6 | + |
| 7 | + <script src="scripts/prettify/prettify.js"> </script> |
| 8 | + <script src="scripts/prettify/lang-css.js"> </script> |
| 9 | + <!--[if lt IE 9]> |
| 10 | + <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> |
| 11 | + <![endif]--> |
| 12 | + <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> |
| 13 | + <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> |
| 14 | +</head> |
| 15 | + |
| 16 | +<body> |
| 17 | + |
| 18 | +<div id="main"> |
| 19 | + |
| 20 | + <h1 class="page-title">Source: BinaryArray.js</h1> |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + <section> |
| 28 | + <article> |
| 29 | + <pre class="prettyprint source linenums"><code>/** Class for returning array members from {@link Binary} objects */ |
| 30 | +class BinaryArrayBase { |
| 31 | + // @member |
| 32 | + type |
| 33 | + // @member |
| 34 | + dv |
| 35 | + // @member |
| 36 | + offset |
| 37 | + // @member |
| 38 | + length |
| 39 | + // @member |
| 40 | + bytes |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new customized array |
| 44 | + * @param {DataView} dv - The DataView handling the buffer where the data lives |
| 45 | + * @param {object} type - The type of the array members from {@link Types} |
| 46 | + * @param {number} offset - The offset of the first member of the array into the buffer |
| 47 | + * @param {number} length - The length of the array |
| 48 | + */ |
| 49 | + constructor (dv, type, offset, length) { |
| 50 | + this.type = type |
| 51 | + this.dv = dv |
| 52 | + this.offset = offset |
| 53 | + this.length = length |
| 54 | + this.bytes = length * type.bytes |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Proxy array methods using this iterator |
| 59 | + * @param {function} fn - The function to apply on the array elements |
| 60 | + * @return {array} - The new generated array (not bound to original values) |
| 61 | + * @method |
| 62 | + */ |
| 63 | + map = (fn) => Array.from(this, fn) |
| 64 | + // reduce = (...args) => Array.prototype.reduce.call([...this], ...args); |
| 65 | + |
| 66 | + /** |
| 67 | + * Transform this array into a JSON string |
| 68 | + * @return {string} - The JSON string representing this array |
| 69 | + * @method |
| 70 | + */ |
| 71 | + toJSON = () => JSON.parse(JSON.stringify(this.map())); |
| 72 | + |
| 73 | + /** |
| 74 | + * Make a generator iterator |
| 75 | + * @method |
| 76 | + * @generator |
| 77 | + * @function iterator |
| 78 | + * @yield {any} - Each of this array elements of type {@link Types} |
| 79 | + * @name iterator |
| 80 | + */ |
| 81 | + * [Symbol.iterator] () { |
| 82 | + // Deconstruct to optimize and ease reading |
| 83 | + const { |
| 84 | + length, |
| 85 | + dv, |
| 86 | + offset, |
| 87 | + type: { get, bytes } |
| 88 | + } = this |
| 89 | + |
| 90 | + // Use a new index for each iterator. This makes multiple |
| 91 | + // iterations over the iterable safe for non-trivial cases, |
| 92 | + // such as use of break or nested looping over the same iterable. |
| 93 | + for (let index = 0; index < length; index++) { |
| 94 | + yield get(dv, offset + bytes * index) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * A Proxy handler for the {@link BinaryArray} class to allow accessing its elements |
| 101 | + * @enum |
| 102 | + */ |
| 103 | +const BinaryArrayHandler = { |
| 104 | + /** |
| 105 | + * Getter for the elements of the handled {@link BinaryArray} |
| 106 | + * @param {BinaryArray} target - The handled {@link BinaryArray} instance |
| 107 | + * @param {string} prop - The property to return (only handled when prop is a string representing a number) |
| 108 | + * @return {any} - The element at {@link prop} position, or a reflected value from {@link target} |
| 109 | + */ |
| 110 | + get (target, prop) { |
| 111 | + // Very inefficient way |
| 112 | + // Need to: |
| 113 | + // - Override Array internals, but are private |
| 114 | + // - Override `[]` operator, but it's not possible |
| 115 | + if (prop === '0' || (typeof prop === 'string' && Number(prop) > 0)) { |
| 116 | + // Destructure to optimize |
| 117 | + const { |
| 118 | + dv, |
| 119 | + offset, |
| 120 | + type: { get, bytes } |
| 121 | + } = target |
| 122 | + return get(dv, offset + bytes * Number(prop)) |
| 123 | + } |
| 124 | + |
| 125 | + // Return original value |
| 126 | + return Reflect.get(target, prop) |
| 127 | + }, |
| 128 | + |
| 129 | + /** |
| 130 | + * Setter for the elements of the handled {@link BinaryArray} |
| 131 | + * @param {BinaryArray} target - The handled {@link BinaryArray} instance |
| 132 | + * @param {string} prop - The property to set (only handled when prop is a string representing a number) |
| 133 | + * @param {any} value - The value to assign to the {@link prop}'th element |
| 134 | + * @return {boolean} - If {@link prop} is numericalish, true (as needed for JS setters), else the return value from the {@link target} reflected setter |
| 135 | + */ |
| 136 | + set (target, prop, value) { |
| 137 | + if (prop === '0' || (typeof prop === 'string' && Number(prop) > 0)) { |
| 138 | + // Destructure to optimize |
| 139 | + const { |
| 140 | + dv, |
| 141 | + offset, |
| 142 | + type: { set, bytes } |
| 143 | + } = target |
| 144 | + set(dv, offset + bytes * Number(prop), value) |
| 145 | + return true |
| 146 | + } |
| 147 | + return Reflect.set(target, prop, value) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// #TODO: BUG: Argument Spread Operator not working |
| 152 | +// well when packing with webpack |
| 153 | +/** |
| 154 | + * Proxy creator for {@link BinaryArrayBase} |
| 155 | + * @param {DataView} dv - The DataView handling the buffer where the data lives |
| 156 | + * @param {object} type - The type of the array members from {@link Types} |
| 157 | + * @param {number} offset - The offset before the first member of the array |
| 158 | + * @param {number} length - The length of the array |
| 159 | + * @return {Proxy} - The proxy to {@link BinaryArrayBase} with {@link BinaryArrayHandler} as proxy handler |
| 160 | + */ |
| 161 | +const BinaryArray = (dv, type, offset, length) => { |
| 162 | + return new Proxy( |
| 163 | + new BinaryArrayBase(dv, type, offset, length), |
| 164 | + BinaryArrayHandler |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +export default BinaryArray |
| 169 | +</code></pre> |
| 170 | + </article> |
| 171 | + </section> |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +</div> |
| 177 | + |
| 178 | +<nav> |
| 179 | + <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Binary.html">Binary</a></li><li><a href="BinaryArrayBase.html">BinaryArrayBase</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BinaryArray">BinaryArray</a></li><li><a href="global.html#BinaryArrayHandler">BinaryArrayHandler</a></li><li><a href="global.html#Types">Types</a></li><li><a href="global.html#binary">binary</a></li><li><a href="global.html">binarySize</a></li><li><a href="global.html#decorator">decorator</a></li><li><a href="global.html#iterator">iterator</a></li><li><a href="global.html#propertyDescriptor">propertyDescriptor</a></li><li><a href="global.html#propertyDescriptorGetter">propertyDescriptorGetter</a></li><li><a href="global.html#propertyDescriptorSetter">propertyDescriptorSetter</a></li><li><a href="global.html#withBinary">withBinary</a></li></ul> |
| 180 | +</nav> |
| 181 | + |
| 182 | +<br class="clear"> |
| 183 | + |
| 184 | +<footer> |
| 185 | + Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.3</a> on Wed Jun 12 2024 07:48:28 GMT+0000 (Coordinated Universal Time) |
| 186 | +</footer> |
| 187 | + |
| 188 | +<script> prettyPrint(); </script> |
| 189 | +<script src="scripts/linenumber.js"> </script> |
| 190 | +</body> |
| 191 | +</html> |
0 commit comments