realistic-structured-clone
Advanced tools
Comparing version 0.0.2 to 0.0.3
37
index.js
@@ -17,8 +17,6 @@ 'use strict'; | ||
function structuredClone(input, memory) { | ||
memory = memory !== undefined ? memory : []; | ||
memory = memory !== undefined ? memory : new Map(); | ||
for (var i = 0; i < memory.length; i++) { | ||
if (memory[i].source === input) { | ||
return memory[i].destination; | ||
} | ||
if (memory.has(input)) { | ||
return memory.get(input); | ||
} | ||
@@ -38,5 +36,13 @@ | ||
} else if (input instanceof RegExp) { | ||
output = new RegExp(input.source, "g".substr(0, Number(input.global)) + "i".substr(0, Number(input.ignoreCase)) + "m".substr(0, Number(input.multiline))); | ||
// Supposed to also handle Blob, FileList, ImageData, ImageBitmap, ArrayBuffer, and "object with a [[DataView]] internal slot", but fuck it | ||
output = new RegExp(input.source, input.flags); | ||
} else if (input instanceof ArrayBuffer) { | ||
output = input.slice(); | ||
} else if (ArrayBuffer.isView(input)) { | ||
var outputBuffer = structuredClone(input.buffer, memory); | ||
if (input instanceof DataView) { | ||
output = new DataView(outputBuffer, input.byteOffset, input.byteLength); | ||
} else { | ||
output = new input.constructor(outputBuffer, input.byteOffset, input.length); | ||
} | ||
// Supposed to also handle Blob, FileList, ImageData, ImageBitmap, but fuck it | ||
} else if (Array.isArray(input)) { | ||
@@ -58,11 +64,12 @@ output = new Array(input.length); | ||
memory.push({ | ||
source: input, | ||
destination: output | ||
}); | ||
memory.set(input, output); | ||
if (deepClone === 'map') { | ||
throw new DataCloneError('Map support not implemented yet'); | ||
input.forEach(function (v, k) { | ||
output.set(structuredClone(k, memory), structuredClone(v, memory)); | ||
}); | ||
} else if (deepClone === 'set') { | ||
throw new DataCloneError('Set support not implemented yet'); | ||
input.forEach(function (v) { | ||
output.add(structuredClone(v, memory)); | ||
}); | ||
} else if (deepClone === 'own') { | ||
@@ -81,2 +88,2 @@ for (var name in input) { | ||
module.exports = structuredClone; | ||
module.exports = structuredClone; |
{ | ||
"name": "realistic-structured-clone", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A pure JS implementation of the structured clone algorithm (or at least something pretty close to that)", | ||
@@ -30,5 +30,5 @@ "homepage": "https://github.com/dumbmatter/realistic-structured-clone", | ||
"devDependencies": { | ||
"eslint": "^0.20.0", | ||
"eslint": "^3.9.1", | ||
"mocha": "^2.2.4" | ||
} | ||
} |
@@ -50,3 +50,3 @@ # Realistic Structured Clone [![Build Status](https://travis-ci.org/dumbmatter/realistic-structured-clone.svg?branch=master)](https://travis-ci.org/dumbmatter/realistic-structured-clone) | ||
[The spec](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#internal-structured-cloning-algorithm) says it's supposed to handle Blob, FileList, ImageData, ImageBitmap, ArrayBuffer, objects with [[DataView]] internal slots, Map, and Set objects. But none of that is implemented yet, so passing an object containing any of those types of objects will result in an erroneous `DataCloneError`. | ||
[The spec](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#internal-structured-cloning-algorithm) says it's supposed to handle Blob, FileList, ImageData, and ImageBitmap objects. But none of that is implemented yet, so passing an object containing any of those types of objects will result in an erroneous `DataCloneError`. | ||
@@ -53,0 +53,0 @@ All other data types should work like described in the spec. Check the tests if you don't believe me, and please create an issue if you find a problem. |
@@ -6,2 +6,21 @@ 'use strict'; | ||
function assertSameEntries(xcontainer, ycontainer) { | ||
var x = xcontainer.entries(); | ||
var y = ycontainer.entries(); | ||
var xentry = x.next(); | ||
var yentry = y.next(); | ||
while (xentry.done === false) { | ||
assert.deepEqual(xentry.value[0], yentry.value[0]); | ||
assert.deepEqual(xentry.value[1], yentry.value[1]); | ||
xentry = x.next(); | ||
yentry = y.next(); | ||
} | ||
assert.equal(yentry.done, true); | ||
} | ||
function confirmContainerWorks(x) { | ||
var y = structuredClone(x); | ||
assertSameEntries(x, y); | ||
} | ||
describe('Valid Input', function () { | ||
@@ -14,5 +33,8 @@ var confirmWorks = function (x) { | ||
assert.equal(x.source, y.source); | ||
assert.equal(x.flags, y.flags); | ||
assert.equal(x.global, y.global); | ||
assert.equal(x.ignoreCase, y.ignoreCase); | ||
assert.equal(x.multiline, y.multiline); | ||
assert.equal(x.unicode, y.unicode); | ||
assert.equal(x.sticky, y.sticky); | ||
} else { | ||
@@ -40,7 +62,30 @@ assert.deepEqual(structuredClone(x), x); | ||
confirmWorks(/ab+c/i); | ||
confirmWorks(new RegExp('de+f', 'gm')); | ||
confirmWorks(new RegExp('gh.*i', 'yu')); | ||
}); | ||
it('ArrayBuffer', function () { | ||
var ab = new ArrayBuffer(5); | ||
var ab2 = structuredClone(ab); | ||
assertSameEntries(new Int8Array(ab), new Int8Array(ab2)); | ||
var shared = new ArrayBuffer(7); | ||
var obj = { | ||
wrapper1: new Uint8Array(shared), | ||
wrapper2: new Uint16Array(shared, 2, 2) | ||
}; | ||
obj.wrapper1[0] = 1; | ||
obj.wrapper2[1] = 0xffff; | ||
var obj2 = structuredClone(obj); | ||
assert(obj2.wrapper1.buffer === obj2.wrapper2.buffer); | ||
assertSameEntries(obj.wrapper1, obj2.wrapper1); | ||
confirmContainerWorks(new Int16Array(7)); | ||
confirmContainerWorks(new Int16Array(new ArrayBuffer(16), 2, 7)); | ||
confirmWorks(new DataView(new ArrayBuffer(16), 3, 13)); | ||
}); | ||
it('Array', function () { | ||
confirmWorks([1, 2, 5, 3]); | ||
confirmWorks(['a', 'g', 2, true, null]); | ||
confirmContainerWorks([1, 2, 5, 3]); | ||
confirmContainerWorks(['a', 'g', 2, true, null]); | ||
}); | ||
@@ -58,2 +103,12 @@ | ||
it('Map', function () { | ||
confirmContainerWorks(new Map([['a', 1], [{}, 2], [{}, 5], [0, 3]])); | ||
confirmContainerWorks(new Map()); | ||
}); | ||
it('Set', function () { | ||
confirmContainerWorks(new Set(['a', {}, {}, 0])); | ||
confirmContainerWorks(new Set()); | ||
}); | ||
it('Circular Reference', function () { | ||
@@ -60,0 +115,0 @@ var circular = []; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16845
203