8000 Add 'duck type check' for isByteBuffer, see #52. Alias calculateUTF8B… · ExodusMovement/bytebuffer.js@d1e46a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d1e46a8

Browse files
committed
Add 'duck type check' for isByteBuffer, see protobufjs#52. Alias calculateUTF8Bytes as calculateString
1 parent e5e94ad commit d1e46a8

15 files changed

+298
-188
lines changed

dist/ByteBufferAB.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@
179179
*/
180180
var ByteBufferPrototype = ByteBuffer.prototype;
181181

182+
/**
183+
* An indicator used to reliably determine if an object is a ByteBuffer or not.
184+
* @type {boolean}
185+
* @const
186+
* @expose
187+
* @private
188+
*/
189+
ByteBufferPrototype.__isByteBuffer__;
190+
191+
Object.defineProperty(ByteBuffer.prototype, "__isByteBuffer__", {
192+
value: true,
193+
enumerable: false,
194+
configurable: false
195+
});
196+
182197
// helpers
183198

184199
/**
@@ -295,7 +310,7 @@
295310
* @expose
296311
*/
297312
ByteBuffer.isByteBuffer = function(bb) {
298-
return (bb && bb instanceof ByteBuffer) === true;
313+
return (bb && bb["__isByteBuffer__"]) === true;
299314
};
300315
/**
301316
* Gets the backing buffer type.
@@ -369,7 +384,7 @@
369384
} else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
370385
bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
371386
bb.limit = buffer.length;
372-
for (i=0; i<buffer.length; ++i)
387+
for (var i=0; i<buffer.length; ++i)
373388
bb.view[i] = buffer[i];
374389
} else
375390
throw TypeError("Illegal buffer"); // Otherwise fail
@@ -2101,7 +2116,6 @@
21012116
/**
21022117
* Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's
21032118
* `length` property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
2104-
* @function
21052119
* @param {string} str String to calculate
21062120
* @returns {number} Number of UTF8 characters
21072121
* @expose
@@ -2112,7 +2126,6 @@
21122126

21132127
/**
21142128
* Calculates the number of UTF8 bytes of a string.
2115-
* @function
21162129
* @param {string} str String to calculate
21172130
* @returns {number} Number of UTF8 bytes
21182131
* @expose
@@ -2121,6 +2134,15 @@
21212134
return utfx.calculateUTF16asUTF8(stringSource(str))[1];
21222135
};
21232136

2137+
/**
2138+
* Calculates the number of UTF8 bytes of a string. This is an alias of {@link ByteBuffer.calculateUTF8Bytes}.
2139+
* @function
2140+
* @param {string} str String to calculate
2141+
* @returns {number} Number of UTF8 bytes
2142+
* @expose
2143+
*/
2144+
ByteBuffer.calculateString = ByteBuffer.calculateUTF8Bytes;
2145+
21242146
/**
21252147
* Reads an UTF8 encoded string.
21262148
* @param {number} length Number of characters or bytes to read.
@@ -2159,7 +2181,7 @@
21592181
return i < length && offset < this.limit ? this.view[offset++] : null;
21602182
}.bind(this), function(cp) {
21612183
++i; utfx.UTF8toUTF16(cp, sd);
2162-
}.bind(this));
2184+
});
21632185
if (i !== length)
21642186
throw RangeError("Illegal range: Truncated data, "+i+" == "+length);
21652187
if (relative) {

dist/ByteBufferAB.min.js

Lines changed: 84 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ByteBufferAB.min.js.gz

54 Bytes
Binary file not shown.

dist/ByteBufferAB.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/ByteBufferAB_DataView.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@
179179
*/
180180
var ByteBufferPrototype = ByteBuffer.prototype;
181181

182+
/**
183+
* An indicator used to reliably determine if an object is a ByteBuffer or not.
184+
* @type {boolean}
185+
* @const
186+
* @expose
187+
* @private
188+
*/
189+
ByteBufferPrototype.__isByteBuffer__;
190+
191+
Object.defineProperty(ByteBuffer.prototype, "__isByteBuffer__", {
192+
value: true,
193+
enumerable: false,
194+
configurable: false
195+
});
196+
182197
// helpers
183198

184199
/**
@@ -296,7 +311,7 @@
296311
* @expose
297312
*/
298313
ByteBuffer.isByteBuffer = function(bb) {
299-
return (bb && bb instanceof ByteBuffer) === true;
314+
return (bb && bb["__isByteBuffer__"]) === true;
300315
};
301316
/**
302317
* Gets the backing buffer type.
@@ -370,7 +385,7 @@
370385
} else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
371386
bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
372387
bb.limit = buffer.length;
373-
for (i=0; i<buffer.length; ++i)
388+
for (var i=0; i<buffer.length; ++i)
374389
bb.view.setUint8(i, buffer[i]);
375390
} else
376391
throw TypeError("Illegal buffer"); // Otherwise fail
@@ -1806,7 +1821,6 @@
18061821
/**
18071822
* Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's
18081823
* `length` property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
1809-
* @function
18101824
* @param {string} str String to calculate
18111825
* @returns {number} Number of UTF8 characters
18121826
* @expose
@@ -1817,7 +1831,6 @@
18171831

18181832
/**
18191833
* Calculates the number of UTF8 bytes of a string.
1820-
* @function
18211834
* @param {string} str String to calculate
18221835
* @returns {number} Number of UTF8 bytes
18231836
* @expose
@@ -1826,6 +1839,15 @@
18261839
return utfx.calculateUTF16asUTF8(stringSource(str))[1];
18271840
};
18281841

1842+
/**
1843+
* Calculates the number of UTF8 bytes of a string. This is an alias of {@link ByteBuffer.calculateUTF8Bytes}.
1844+
* @function
1845+
* @param {string} str String to calculate
1846+
* @returns {number} Number of UTF8 bytes
1847+
* @expose
1848+
*/
1849+
ByteBuffer.calculateString = ByteBuffer.calculateUTF8Bytes;
1850+
18291851
/**
18301852
* Reads an UTF8 encoded string.
18311853
* @param {number} length Number of characters or bytes to read.
@@ -1864,7 +1886,7 @@
18641886
return i < length && offset < this.limit ? this.view.getUint8(offset++) : null;
18651887
}.bind(this), function(cp) {
18661888
++i; utfx.UTF8toUTF16(cp, sd);
1867-
}.bind(this));
1889+
});
18681890
if (i !== length)
18691891
throw RangeError("Illegal range: Truncated data, "+i+" == "+length);
18701892
if (relative) {

0 commit comments

Comments
 (0)
0