Skip to content

Commit

Permalink
switched conrol prop to symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
karmaniverous committed Oct 16, 2024
1 parent 7693d85 commit a675cf6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/controlledProxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import { controlledProxy } from './controlledProxy';
import { control, controlledProxy } from './controlledProxy';

const sym = Symbol('symProp');

Expand Down Expand Up @@ -214,7 +214,7 @@ describe('controlledProxy', function () {
target,
});

expect(proxy.controls).to.deep.equal({ foo: true, bar: false });
expect(proxy[control]).to.deep.equal({ foo: true, bar: false });
});

it('should prevent adding new properties to controls', function () {
Expand All @@ -237,7 +237,7 @@ describe('controlledProxy', function () {

expect(proxy.foo('test')).to.equal('foo: test');

proxy.controls.foo = false;
proxy[control].foo = false;
expect(proxy.foo('test')).to.be.undefined;
});

Expand All @@ -251,7 +251,7 @@ describe('controlledProxy', function () {

expect(proxy[sym]).to.be.undefined;

proxy.controls[sym] = true;
proxy[control][sym] = true;
expect(proxy[sym]).to.equal('symbol value');
});

Expand Down
6 changes: 4 additions & 2 deletions src/controlledProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface ControlledProxyOptions<
defaultProxyFunction?: ControlledMethod;
}

export const control = Symbol('control');

export function controlledProxy<
Properties extends PropertyKey,
Target extends ControlledPartial<Properties>,
Expand All @@ -28,7 +30,7 @@ export function controlledProxy<
return new Proxy(target, {
get(targetObj, prop, receiver): unknown {
// if property is control property, return it
if (prop === 'controls') return sealedControls;
if (prop === control) return sealedControls;
const value = Reflect.get(targetObj, prop, receiver);

return prop in sealedControls // controlled?
Expand All @@ -47,5 +49,5 @@ export function controlledProxy<
? Reflect.set(targetObj, prop, value, receiver) // !controlled | enabled
: false;
},
}) as Target & { controls: Record<Properties, boolean> };
}) as Target & { [control]: Record<Properties, boolean> };
}

0 comments on commit a675cf6

Please sign in to comment.