Skip to content

Commit

Permalink
copy URLs and URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandabler committed Jan 5, 2019
1 parent 8debf0c commit 651caf6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/dubl.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ const duplicateArrayBuffer = arrBuff => new ArrayBuffer(arrBuff.length)
const duplicateDataView = dv =>
new DataView(dv.buffer, dv.byteOffset, dv.byteLength)

/**
* Returns a copy of an object able to be built by a serializable string.
*
* This is a higher order function that takes a construcutor and
* returns a function which will actually make the copy of an object.
* The one distinction is that the `param` must be serializable to a
* string and the constructor be able to create a copy from that string.
*
* @param {Object} constructor
* @returns {function}
*/
const duplicateSerializable = constructor => param => new constructor(param.toString())

const duplicate = {
[types.STRING]: identity,
[types.NUMBER]: identity,
Expand Down Expand Up @@ -180,7 +193,9 @@ const duplicate = {
[types.GENERATOR]: identity,
[types.GENERATORFUNC]: duplicateFunction,
[types.WASM]: identity,
[types.ASYNCFUNC]: duplicateFunction
[types.ASYNCFUNC]: duplicateFunction,
[types.URL]: duplicateSerializable(URL),
[types.URLPARAMS]: duplicateSerializable(URLSearchParams)
}

module.exports = {
Expand All @@ -196,5 +211,6 @@ module.exports = {
duplicateTypedArray,
duplicateArrayBuffer,
duplicateDataView,
duplicateSerializable,
duplicate
};
24 changes: 23 additions & 1 deletion test/test-dubl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
duplicatePromise,
duplicateTypedArray,
duplicateArrayBuffer,
duplicateDataView
duplicateDataView,
duplicateSerializable
} = require("../src/dubl");

const expect = chai.expect;
Expand Down Expand Up @@ -492,5 +493,26 @@ describe("facsimile.js", function() {
checkObjects(result, input, shouldDup);
});
});

describe("duplicateSerializable()", function() {
const duplicateURL = duplicateSerializable(URL);
const duplicateURLParams = duplicateSerializable(URLSearchParams);

it('Should duplicate a URL', function() {
const input = new URL('https://ryan:dabler@en.myurl.com:2200/dabler?test=a&b=type#hash')
const result = duplicateURL(input);

expect(result).to.not.equal(input);
expect(result.toString()).to.equal(input.toString());
});

it('Should duplicate URLSearchParams', function() {
const input = new URLSearchParams('test=a&b=type')
const result = duplicateURLParams(input);

expect(result).to.not.equal(input);
expect(result.toString()).to.equal(input.toString());
});
});
});

0 comments on commit 651caf6

Please sign in to comment.