Fix typo in the ArrayBuffer#slice mock for node.

This commit is contained in:
John-David Dalton
2014-07-21 23:10:40 -07:00
parent 13669313d4
commit 653aab2358
2 changed files with 50 additions and 46 deletions

View File

@@ -113,38 +113,39 @@
document.createDocumentFragment = function() {}; document.createDocumentFragment = function() {};
setProperty(window, '_ArrayBuffer', window.ArrayBuffer); setProperty(window, '_ArrayBuffer', window.ArrayBuffer);
if (window.ArrayBuffer && window.Uint8Array && !new ArrayBuffer(0).slice) { if (window.ArrayBuffer && window.Uint8Array) {
ArrayBuffer = (function(_ArrayBuffer) { ArrayBuffer = (function(_ArrayBuffer) {
function ArrayBuffer(byteLength) {
var buffer = new _ArrayBuffer(byteLength);
if (!byteLength) {
setProperty(buffer, 'slice', buffer.slice ? null : bufferSlice);
}
return buffer;
}
function bufferSlice() {
var newBuffer = new _ArrayBuffer(this.byteLength),
view = new Uint8Array(newBuffer);
view.set(new Uint8Array(this));
return newBuffer;
}
function constant(value) { function constant(value) {
return function() { return function() {
return value; return value;
}; };
} }
function ArrayBuffer(byteLength) {
var buffer = new _ArrayBuffer(byteLength);
buffer.slice = function() {
var newBuffer = new _ArrayBuffer(this.byteLength),
view = new Uint8Array(newBuffer);
view.set(new Uint8Array(this));
return newBuffer;
};
setProperty(buffer.slice, 'toString', sliceToString);
return buffer;
}
var reToString = /toString/g, var reToString = /toString/g,
nativeString = Function.prototype.toString.call(toString), nativeString = Function.prototype.toString.call(toString),
bufferToString = constant(nativeString.replace(reToString, 'ArrayBuffer')), bufferToString = constant(nativeString.replace(reToString, 'ArrayBuffer')),
sliceToString = constant(nativeString.replace(reToString, 'slice')); sliceToString = constant(nativeString.replace(reToString, 'slice'));
setProperty(ArrayBuffer, 'toString', bufferToString); setProperty(ArrayBuffer, 'toString', bufferToString);
setProperty(bufferSlice, 'toString', sliceToString);
return ArrayBuffer; return ArrayBuffer;
}(_ArrayBuffer)); }(_ArrayBuffer));
} }
setProperty(window, '_Float64Array', window.Float64Array); setProperty(window, '_Float64Array', window.Float64Array);
if (window._ArrayBuffer == window.ArrayBuffer && window._Float64Array) { if (!window._Float64Array) {
Float64Array = function() {};
} else {
Float64Array = window.Uint8Array; Float64Array = window.Uint8Array;
} }
setProperty(window, 'WinRTError', Error); setProperty(window, 'WinRTError', Error);

View File

@@ -37,7 +37,8 @@
push = arrayProto.push, push = arrayProto.push,
slice = arrayProto.slice, slice = arrayProto.slice,
system = root.system, system = root.system,
toString = objectProto.toString; toString = objectProto.toString,
Uint8Array = root.Uint8Array;
/** The file path of the Lo-Dash file to test */ /** The file path of the Lo-Dash file to test */
var filePath = (function() { var filePath = (function() {
@@ -383,34 +384,34 @@
setProperty(stringProto, 'contains', _contains ? _.noop : Boolean); setProperty(stringProto, 'contains', _contains ? _.noop : Boolean);
var _ArrayBuffer = ArrayBuffer; var _ArrayBuffer = ArrayBuffer;
if (Uint8Array && new ArrayBuffer(0).slice) { setProperty(root, 'ArrayBuffer', (function() {
setProperty(root, 'ArrayBuffer', (function() { function ArrayBuffer(byteLength) {
function ArrayBuffer(byteLength) { var buffer = new _ArrayBuffer(byteLength);
var buffer = new _ArrayBuffer(byteLength); if (!byteLength) {
buffer.slice = function() { setProperty(buffer, 'slice', buffer.slice ? null : bufferSlice);
var newBuffer = new _ArrayBuffer(this.byteLength),
view = new Uint8Array(newBuffer);
view.set(new Uint8Array(this));
return newBuffer;
};
setProperty(buffer.slice, 'toString', sliceToString);
return buffer;
} }
var reToString = /toString/g, return buffer;
nativeString = _fnToString.call(toString), }
bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')), function bufferSlice() {
sliceToString = _.constant(nativeString.replace(reToString, 'slice')); var newBuffer = new _ArrayBuffer(this.byteLength),
view = new Uint8Array(newBuffer);
view.set(new Uint8Array(this));
return newBuffer;
}
var reToString = /toString/g,
nativeString = _fnToString.call(toString),
bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')),
sliceToString = _.constant(nativeString.replace(reToString, 'slice'));
setProperty(ArrayBuffer, 'toString', bufferToString);
setProperty(bufferSlice, 'toString', sliceToString);
return ArrayBuffer;
}()));
setProperty(ArrayBuffer, 'toString', bufferToString);
return ArrayBuffer;
}()));
}
var _Float64Array = root.Float64Array; var _Float64Array = root.Float64Array;
if (_ArrayBuffer == root.ArrayBuffer && _Float64Array) { if (!_Float64Array) {
setProperty(root, 'Float64Array', _.noop); setProperty(root, 'Float64Array', Uint8Array);
} else {
setProperty(root, 'Float64Array', root.Uint8Array);
} }
// fake `WinRTError` // fake `WinRTError`
setProperty(root, 'WinRTError', Error); setProperty(root, 'WinRTError', Error);
@@ -451,6 +452,8 @@
} }
if (_ArrayBuffer) { if (_ArrayBuffer) {
setProperty(root, 'ArrayBuffer', _ArrayBuffer); setProperty(root, 'ArrayBuffer', _ArrayBuffer);
} else {
delete root.ArrayBuffer;
} }
if (_Float64Array) { if (_Float64Array) {
setProperty(root, 'Float64Array', _Float64Array); setProperty(root, 'Float64Array', _Float64Array);
@@ -651,9 +654,9 @@
} }
strictEqual(actual, true, message('_.contains', 'String#contains')); strictEqual(actual, true, message('_.contains', 'String#contains'));
if (root.ArrayBuffer) { if (ArrayBuffer) {
try { try {
var buffer = new ArrayBuffer(8); var buffer = new ArrayBuffer(12);
actual = lodashBizarro.clone(buffer); actual = lodashBizarro.clone(buffer);
} catch(e) { } catch(e) {
actual = null; actual = null;
@@ -664,9 +667,9 @@
else { else {
skipTest(2); skipTest(2);
} }
if (root.ArrayBuffer && root.Uint8Array) { if (ArrayBuffer && Uint8Array) {
try { try {
var array = new Uint8Array(new ArrayBuffer(8)); var array = new Uint8Array(new ArrayBuffer(12));
actual = lodashBizarro.cloneDeep(array); actual = lodashBizarro.cloneDeep(array);
} catch(e) { } catch(e) {
actual = null; actual = null;