Skip to content

Commit

Permalink
eliminate realm in favor of object class
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Sep 26, 2022
1 parent 54f5b54 commit 36d83f4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 191 deletions.
113 changes: 19 additions & 94 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hash.update('foo bar');

const SIMPLE_TYPES: PlainObject = {
boolean: true,
error: new Error('boom'),
error: new TypeError('boom'),
fn() {
return 'foo';
},
Expand Down Expand Up @@ -53,7 +53,7 @@ const COMPLEX_TYPES: PlainObject = {
})('foo', 'bar', 'baz'),
array: ['foo', { bar: 'baz' }],
arrayBuffer: new ArrayBuffer(8),
buffer: new Buffer('this is a test buffer'),
buffer: Buffer.from('this is a test buffer'),
customPrototype: Object.create({
method() {
return 'foo';
Expand All @@ -62,22 +62,22 @@ const COMPLEX_TYPES: PlainObject = {
}),
dataView: new DataView(new ArrayBuffer(16)),
date: new Date(),
float32Array: new Float32Array([12, 15]),
float64Array: new Float64Array([12, 15]),
float32Array: new Float32Array([1, 2]),
float64Array: new Float64Array([3, 4]),
hash,
int8Array: new Int8Array([12, 15]),
int16Array: new Int16Array([12, 15]),
int32Array: new Int32Array([12, 15]),
int8Array: new Int8Array([5, 6]),
int16Array: new Int16Array([7, 8]),
int32Array: new Int32Array([9, 10]),
map: new Map().set('foo', { bar: { baz: 'quz' } }),
object: { foo: { bar: 'baz' } },
regexp: /foo/,
set: new Set().add('foo').add({ bar: { baz: 'quz' } }),
// Disabling, as jest fails intermittently with blob construction.
// blob: new Blob(['<a id="a">hey!</a>'], {type : 'text/html'}),
uint8Array: new Uint8Array([12, 15]),
uint8ClampedArray: new Uint8ClampedArray([12, 15]),
uint16Array: new Uint16Array([12, 15]),
uint32Array: new Uint32Array([12, 15]),
uint8Array: new Uint8Array([11, 12]),
uint8ClampedArray: new Uint8ClampedArray([13, 14]),
uint16Array: new Uint16Array([15, 16]),
uint32Array: new Uint32Array([17, 18]),
};

Object.defineProperties(COMPLEX_TYPES, {
Expand Down Expand Up @@ -168,8 +168,8 @@ describe('copy', () => {
const properties = [].concat(
Object.keys(SIMPLE_TYPES),
Object.getOwnPropertySymbols(SIMPLE_TYPES).filter((symbol) =>
Object.prototype.propertyIsEnumerable.call(SIMPLE_TYPES, symbol),
),
Object.prototype.propertyIsEnumerable.call(SIMPLE_TYPES, symbol)
)
);

properties.forEach((property: string | symbol) => {
Expand All @@ -192,7 +192,7 @@ describe('copy', () => {
const properties = [
...Object.keys(COMPLEX_TYPES),
...Object.getOwnPropertySymbols(COMPLEX_TYPES).filter((symbol) =>
Object.prototype.propertyIsEnumerable.call(COMPLEX_TYPES, symbol),
Object.prototype.propertyIsEnumerable.call(COMPLEX_TYPES, symbol)
),
];

Expand All @@ -202,7 +202,7 @@ describe('copy', () => {
expect({ ...result[property] }).toEqual({ ...COMPLEX_TYPES[property] });
} else if (property === 'customPrototype') {
expect(Object.getPrototypeOf(result[property])).toBe(
Object.getPrototypeOf(COMPLEX_TYPES[property]),
Object.getPrototypeOf(COMPLEX_TYPES[property])
);
expect(result[property]).toEqual(COMPLEX_TYPES[property]);
} else {
Expand All @@ -226,42 +226,6 @@ describe('copy', () => {
expect(result).toEqual(SPECIAL_TYPES);
});

it('will handle when buffers are not supported', () => {
const cleanComplexTypes = Object.keys(COMPLEX_TYPES).reduce(
(types: PlainObject, key) => {
if (
key !== 'arguments' &&
key !== 'arrayBuffer' &&
key !== 'buffer' &&
key !== 'dataView' &&
!/float(.*)Array/.test(key) &&
!/int(.*)Array/.test(key)
) {
types[key] = COMPLEX_TYPES[key];
}

return types;
},
{},
);

const realm = {
Date: global.Date,
Error: global.Error,
Map: global.Map,
RegExp: global.RegExp,
Set: global.Set,
Blob: global.Blob,
WeakMap: global.WeakMap,
WeakSet: global.WeakSet,
} as typeof globalThis;

const result = copy(cleanComplexTypes, { realm });

expect(result).not.toBe(cleanComplexTypes);
expect(result).toEqual(cleanComplexTypes);
});

it('will copy referenced objects', () => {
const reusedObject = {
name: 'I like trains!',
Expand Down Expand Up @@ -347,7 +311,7 @@ describe('copyStrict', () => {

const properties = [].concat(
Object.getOwnPropertyNames(SIMPLE_TYPES),
Object.getOwnPropertySymbols(SIMPLE_TYPES),
Object.getOwnPropertySymbols(SIMPLE_TYPES)
);

properties.forEach((property: string | symbol) => {
Expand All @@ -374,7 +338,7 @@ describe('copyStrict', () => {

const properties = [].concat(
Object.getOwnPropertyNames(complexTypes),
Object.getOwnPropertySymbols(complexTypes),
Object.getOwnPropertySymbols(complexTypes)
);

properties.forEach((property: string | symbol) => {
Expand All @@ -388,10 +352,10 @@ describe('copyStrict', () => {
});
} else if (property === 'customPrototype') {
expect(Object.getPrototypeOf(result[property])).toBe(
Object.getPrototypeOf(COMPLEX_TYPES[property]),
Object.getPrototypeOf(COMPLEX_TYPES[property])
);
expect(Object.getPrototypeOf(result2[property])).toBe(
Object.getPrototypeOf(COMPLEX_TYPES[property]),
Object.getPrototypeOf(COMPLEX_TYPES[property])
);

expect(result[property]).toEqual(COMPLEX_TYPES[property]);
Expand Down Expand Up @@ -427,45 +391,6 @@ describe('copyStrict', () => {
expect(result2).toEqual(SPECIAL_TYPES);
});

it('will handle when buffers are not supported', () => {
const cleanComplexTypes = Object.keys(COMPLEX_TYPES).reduce(
(types: PlainObject, key) => {
if (
key !== 'arguments' &&
key !== 'arrayBuffer' &&
key !== 'buffer' &&
key !== 'dataView' &&
!/float(.*)Array/.test(key) &&
!/int(.*)Array/.test(key)
) {
types[key] = COMPLEX_TYPES[key];
}

return types;
},
{},
);

const realm = {
Date: global.Date,
Error: global.Error,
Map: global.Map,
RegExp: global.RegExp,
Set: global.Set,
WeakMap: global.WeakMap,
WeakSet: global.WeakSet,
} as typeof globalThis;

const result = copy(cleanComplexTypes, { isStrict: true, realm });
const result2 = copyStrict(cleanComplexTypes, { realm });

expect(result).not.toBe(cleanComplexTypes);
expect(result2).not.toBe(cleanComplexTypes);

expect(result).toEqual(cleanComplexTypes);
expect(result2).toEqual(cleanComplexTypes);
});

it('will copy referenced objects', () => {
const reusedObject = {
name: 'I like trains!',
Expand Down
44 changes: 16 additions & 28 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ describe('createCache', () => {
describe('getCleanClone', () => {
it('will return a pure object when there is no constructor', () => {
const object = Object.create(null);
const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual(object);
Expand All @@ -71,9 +70,7 @@ describe('getCleanClone', () => {

object.__proto__ = null;

const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual(object);
Expand All @@ -83,9 +80,8 @@ describe('getCleanClone', () => {

it('will return an empty POJO when the object passed is a POJO', () => {
const object = { foo: 'bar' };
const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual({});
Expand All @@ -100,9 +96,7 @@ describe('getCleanClone', () => {

object.foo = 'bar';

const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual({});
Expand All @@ -112,9 +106,8 @@ describe('getCleanClone', () => {

it('will return an empty object with the given constructor when it is a global constructor', () => {
const object = new Map();
const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual(new Map());
Expand All @@ -134,9 +127,8 @@ describe('getCleanClone', () => {
}

const object = new Foo('bar');
const realm = global;

const result = utils.getCleanClone(object, realm);
const result = utils.getCleanClone(object);

expect(result).not.toBe(object);
expect(result).toEqual(Object.create(Foo.prototype));
Expand All @@ -160,11 +152,10 @@ describe('getObjectCloneLoose', () => {
foo: 'bar',
[symbol]: 'blah',
};
const realm = global;
const handleCopy = jest.fn().mockImplementation((arg) => arg);
const cache = utils.createCache();

const result = utils.getObjectCloneLoose(object, realm, handleCopy, cache);
const result = utils.getObjectCloneLoose(object, handleCopy, cache);

Object.getOwnPropertySymbols = original;

Expand All @@ -174,7 +165,7 @@ describe('getObjectCloneLoose', () => {
clone[key] = object[key as keyof typeof object];

return clone;
}, {}),
}, {})
);

expect(handleCopy).toHaveBeenCalledTimes(Object.keys(object).length);
Expand All @@ -185,17 +176,16 @@ describe('getObjectCloneLoose', () => {
bar: { baz: 'quz' },
[Symbol('quz')]: 'blah',
};
const realm = global;
const handleCopy = jest.fn().mockImplementation((arg) => arg);
const cache = utils.createCache();

const result = utils.getObjectCloneLoose(object, realm, handleCopy, cache);
const result = utils.getObjectCloneLoose(object, handleCopy, cache);

expect(result).not.toBe(object);
expect(result).toEqual(object);

expect(handleCopy).toHaveBeenCalledTimes(
Object.keys(object).length + Object.getOwnPropertySymbols(object).length,
Object.keys(object).length + Object.getOwnPropertySymbols(object).length
);
});
});
Expand All @@ -222,11 +212,10 @@ describe('getObjectCloneStrict', () => {
value: 'blah',
});

const realm = global;
const handleCopy = jest.fn().mockImplementation((arg) => arg);
const cache = utils.createCache();

const result = utils.getObjectCloneStrict(object, realm, handleCopy, cache);
const result = utils.getObjectCloneStrict(object, handleCopy, cache);

Object.getOwnPropertySymbols = original;

Expand All @@ -238,12 +227,12 @@ describe('getObjectCloneStrict', () => {

return clone;
},
{},
),
{}
)
);

expect(handleCopy).toHaveBeenCalledTimes(
Object.getOwnPropertyNames(object).length,
Object.getOwnPropertyNames(object).length
);
});

Expand All @@ -261,18 +250,17 @@ describe('getObjectCloneStrict', () => {
value: 'blah',
});

const realm = global;
const handleCopy = jest.fn().mockImplementation((arg) => arg);
const cache = utils.createCache();

const result = utils.getObjectCloneStrict(object, realm, handleCopy, cache);
const result = utils.getObjectCloneStrict(object, handleCopy, cache);

expect(result).not.toBe(object);
expect(result).toEqual(object);

expect(handleCopy).toHaveBeenCalledTimes(
Object.getOwnPropertyNames(object).length +
Object.getOwnPropertySymbols(object).length,
Object.getOwnPropertySymbols(object).length
);
});
});
Expand Down
Loading

0 comments on commit 36d83f4

Please sign in to comment.