Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should return a Derivable', () => {
const px = createForObject('Hello');
const der = px.$derive(v => v);
expect(isDerivable(der)).to.be.true;
});
function deepUnwrap(obj: any): any {
if (isDerivable(obj)) {
return obj.get();
}
if (Array.isArray(obj)) {
return obj.map(deepUnwrap);
}
if (utils.isPlainObject(obj)) {
const result = {};
for (const key of Object.keys(obj)) {
result[key] = deepUnwrap(obj[key]);
}
return result;
}
return obj;
}
export function struct(obj: any) {
if (isDerivable(obj)) {
return obj;
}
if (!Array.isArray(obj) && !utils.isPlainObject(obj)) {
throw new Error('"struct" only accepts Derivables, plain Objects and Arrays');
}
return derive(deepUnwrap, obj);
}
return key => {
if (!isDerivable(key)) {
const cacheItem = cache.get(key);
if (cacheItem) {
return cacheItem[CACHED_PROXY];
}
}
return lens(descriptor, key);
};
}