import { ErrorConstant } from '@remirror/core-constants' ; import { invariant } from './core-errors' ; import { isArray , isObject } from './core-helpers' ; remarks export function freeze < Target extends object > ( target , options = { } ) { if ( process . env . NODE_ENV === 'production' ) { return target ; } invariant ( isObject ( target ) || isArray ( target ) , { message : '`freeze` only supports objects and arrays.' , code : ErrorConstant . CORE_HELPERS , } ) ; return new Proxy ( target , { get : ( target , prop , receiver ) => { invariant ( prop in target || ! options . requireKeys , { message : `The prop: '${ prop . toString ( ) }' you are trying to access does not yet exist on the target.` , } ) ; return Reflect . get ( target , prop , receiver ) ; } , set : ( _ , prop ) => { invariant ( false , { message : `It seems you're trying to set the value of the property (${ String ( prop ) }) on a frozen object. For your protection this object does not allow direct mutation.` , code : ErrorConstant . MUTATION , } ) ; } , } ) ; } interface FreezeOptions { default requireKeys ? : boolean ; }