-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve error message when no wrapped value
- Loading branch information
Showing
6 changed files
with
85 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ArgumentError } from './errors'; | ||
|
||
export function wrapWithProxy<Value extends object | undefined>( | ||
valueCb: () => Value, | ||
): NonNullable<Value> { | ||
return new Proxy( | ||
{}, | ||
Object.fromEntries(([ | ||
'apply', 'construct', 'defineProperty', 'deleteProperty', 'getOwnPropertyDescriptor', | ||
'getPrototypeOf', 'isExtensible', 'ownKeys', 'preventExtensions', 'set', 'setPrototypeOf', | ||
'get', 'has', | ||
] as const).map((name) => [name, (t: {}, ...args: unknown[]) => { | ||
const target = valueCb(); | ||
if (target == null) throw new ArgumentError('wrapped value', 'defined', target); | ||
if (name === 'get' && args[0] === '_wrappedValue') return target; | ||
const res = (Reflect[name] as any)(target, ...args); | ||
return typeof res === 'function' && name === 'get' ? res.bind(target) : res; | ||
}])), | ||
) as NonNullable<Value>; | ||
} | ||
|
||
export function unwrapProxy<Value extends object>(value: Value): Value { | ||
return (value as { _wrappedValue?: Value })._wrappedValue ?? value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import { wrapWithProxy, unwrapProxy } from '../../src/utils/wrap-proxy'; | ||
import { ArgumentError } from '../../src'; | ||
|
||
describe('Utils', () => { | ||
describe('wrapWithProxy', () => { | ||
it('wraps value', () => { | ||
let t = { test: 'foo' }; | ||
const wrapped = wrapWithProxy(() => t); | ||
expect(wrapped).to.not.be.equal(t); | ||
expect(wrapped.test).to.be.equal('foo'); | ||
t.test = 'bar'; | ||
expect(wrapped.test).to.be.equal('bar'); | ||
t = { test: 'baz' }; | ||
expect(wrapped.test).to.be.equal('baz'); | ||
}); | ||
|
||
it('throws error if value undefined', () => { | ||
const wrapped = wrapWithProxy<{ test: string } | undefined>(() => undefined); | ||
expect(() => wrapped.test) | ||
.to.throw(ArgumentError, 'wrapped value should be defined, got undefined instead'); | ||
}); | ||
|
||
it('can call private method', () => { | ||
class Entity { | ||
readonly t = 5; | ||
|
||
#bar(): number { | ||
return this.t; | ||
} | ||
|
||
foo(): number { | ||
return this.#bar(); | ||
} | ||
} | ||
|
||
const entity = new Entity(); | ||
const wrapped = wrapWithProxy(() => entity); | ||
expect(wrapped.foo()).to.be.equal(5); | ||
}); | ||
}); | ||
|
||
describe('unwrapProxy', () => { | ||
const t = { test: 'foo' }; | ||
|
||
it('unwraps proxy to value', () => { | ||
const wrapped = wrapWithProxy(() => t); | ||
expect(unwrapProxy(wrapped)).to.be.equal(t); | ||
}); | ||
|
||
it('does nothing if not wrapped', () => { | ||
expect(unwrapProxy(t)).to.be.equal(t); | ||
}); | ||
}); | ||
}); |