typescript-flat-serializer
Version:
A typescript library to serialize/deserialize classes to/from string in a flat format. Supports inheritance, circular reference and more
17 lines (14 loc) • 31.4 kB
JavaScript
"use strict";
/*! *****************************************************************************
Copyright (C) Microsoft. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */var Reflect$1;!function(Reflect){!function(factory){var root="object"==typeof globalThis?globalThis:"object"==typeof global?global:"object"==typeof self?self:"object"==typeof this?this:function(){try{return Function("return this;")()}catch(_){}}()||function(){try{return(0,eval)("(function() { return this; })()")}catch(_){}}(),exporter=makeExporter(Reflect);function makeExporter(target,previous){return function(key,value){Object.defineProperty(target,key,{configurable:!0,writable:!0,value:value}),previous&&previous(key,value)}}void 0!==root.Reflect&&(exporter=makeExporter(root.Reflect,exporter)),function(exporter,root){var hasOwn=Object.prototype.hasOwnProperty,supportsSymbol="function"==typeof Symbol,toPrimitiveSymbol=supportsSymbol&&void 0!==Symbol.toPrimitive?Symbol.toPrimitive:"@@toPrimitive",iteratorSymbol=supportsSymbol&&void 0!==Symbol.iterator?Symbol.iterator:"@@iterator",supportsCreate="function"==typeof Object.create,supportsProto={__proto__:[]}instanceof Array,downLevel=!supportsCreate&&!supportsProto,HashMap={create:supportsCreate?function(){return MakeDictionary(Object.create(null))}:supportsProto?function(){return MakeDictionary({__proto__:null})}:function(){return MakeDictionary({})},has:downLevel?function(map,key){return hasOwn.call(map,key)}:function(map,key){return key in map},get:downLevel?function(map,key){return hasOwn.call(map,key)?map[key]:void 0}:function(map,key){return map[key]}},functionPrototype=Object.getPrototypeOf(Function),_Map="function"==typeof Map&&"function"==typeof Map.prototype.entries?Map:CreateMapPolyfill(),_Set="function"==typeof Set&&"function"==typeof Set.prototype.entries?Set:CreateSetPolyfill(),_WeakMap="function"==typeof WeakMap?WeakMap:CreateWeakMapPolyfill(),registrySymbol=supportsSymbol?Symbol.for("@reflect-metadata:registry"):void 0,metadataRegistry=GetOrCreateMetadataRegistry(),metadataProvider=CreateMetadataProvider(metadataRegistry);function decorate(decorators,target,propertyKey,attributes){if(IsUndefined(propertyKey)){if(!IsArray(decorators))throw new TypeError;if(!IsConstructor(target))throw new TypeError;return DecorateConstructor(decorators,target)}if(!IsArray(decorators))throw new TypeError;if(!IsObject(target))throw new TypeError;if(!IsObject(attributes)&&!IsUndefined(attributes)&&!IsNull(attributes))throw new TypeError;return IsNull(attributes)&&(attributes=void 0),DecorateProperty(decorators,target,propertyKey=ToPropertyKey(propertyKey),attributes)}function metadata(metadataKey,metadataValue){function decorator(target,propertyKey){if(!IsObject(target))throw new TypeError;if(!IsUndefined(propertyKey)&&!IsPropertyKey(propertyKey))throw new TypeError;OrdinaryDefineOwnMetadata(metadataKey,metadataValue,target,propertyKey)}return decorator}function defineMetadata(metadataKey,metadataValue,target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryDefineOwnMetadata(metadataKey,metadataValue,target,propertyKey)}function hasMetadata(metadataKey,target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryHasMetadata(metadataKey,target,propertyKey)}function hasOwnMetadata(metadataKey,target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryHasOwnMetadata(metadataKey,target,propertyKey)}function getMetadata(metadataKey,target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryGetMetadata(metadataKey,target,propertyKey)}function getOwnMetadata(metadataKey,target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryGetOwnMetadata(metadataKey,target,propertyKey)}function getMetadataKeys(target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryMetadataKeys(target,propertyKey)}function getOwnMetadataKeys(target,propertyKey){if(!IsObject(target))throw new TypeError;return IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),OrdinaryOwnMetadataKeys(target,propertyKey)}function deleteMetadata(metadataKey,target,propertyKey){if(!IsObject(target))throw new TypeError;if(IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey)),!IsObject(target))throw new TypeError;IsUndefined(propertyKey)||(propertyKey=ToPropertyKey(propertyKey));var provider=GetMetadataProvider(target,propertyKey,!1);return!IsUndefined(provider)&&provider.OrdinaryDeleteMetadata(metadataKey,target,propertyKey)}function DecorateConstructor(decorators,target){for(var i=decorators.length-1;i>=0;--i){var decorated=(0,decorators[i])(target);if(!IsUndefined(decorated)&&!IsNull(decorated)){if(!IsConstructor(decorated))throw new TypeError;target=decorated}}return target}function DecorateProperty(decorators,target,propertyKey,descriptor){for(var i=decorators.length-1;i>=0;--i){var decorated=(0,decorators[i])(target,propertyKey,descriptor);if(!IsUndefined(decorated)&&!IsNull(decorated)){if(!IsObject(decorated))throw new TypeError;descriptor=decorated}}return descriptor}function OrdinaryHasMetadata(MetadataKey,O,P){if(OrdinaryHasOwnMetadata(MetadataKey,O,P))return!0;var parent=OrdinaryGetPrototypeOf(O);return!IsNull(parent)&&OrdinaryHasMetadata(MetadataKey,parent,P)}function OrdinaryHasOwnMetadata(MetadataKey,O,P){var provider=GetMetadataProvider(O,P,!1);return!IsUndefined(provider)&&ToBoolean(provider.OrdinaryHasOwnMetadata(MetadataKey,O,P))}function OrdinaryGetMetadata(MetadataKey,O,P){if(OrdinaryHasOwnMetadata(MetadataKey,O,P))return OrdinaryGetOwnMetadata(MetadataKey,O,P);var parent=OrdinaryGetPrototypeOf(O);return IsNull(parent)?void 0:OrdinaryGetMetadata(MetadataKey,parent,P)}function OrdinaryGetOwnMetadata(MetadataKey,O,P){var provider=GetMetadataProvider(O,P,!1);if(!IsUndefined(provider))return provider.OrdinaryGetOwnMetadata(MetadataKey,O,P)}function OrdinaryDefineOwnMetadata(MetadataKey,MetadataValue,O,P){GetMetadataProvider(O,P,!0).OrdinaryDefineOwnMetadata(MetadataKey,MetadataValue,O,P)}function OrdinaryMetadataKeys(O,P){var ownKeys=OrdinaryOwnMetadataKeys(O,P),parent=OrdinaryGetPrototypeOf(O);if(null===parent)return ownKeys;var parentKeys=OrdinaryMetadataKeys(parent,P);if(parentKeys.length<=0)return ownKeys;if(ownKeys.length<=0)return parentKeys;for(var set=new _Set,keys=[],_i=0,ownKeys_1=ownKeys;_i<ownKeys_1.length;_i++){var key=ownKeys_1[_i];set.has(key)||(set.add(key),keys.push(key))}for(var _a=0,parentKeys_1=parentKeys;_a<parentKeys_1.length;_a++){key=parentKeys_1[_a];set.has(key)||(set.add(key),keys.push(key))}return keys}function OrdinaryOwnMetadataKeys(O,P){var provider=GetMetadataProvider(O,P,!1);return provider?provider.OrdinaryOwnMetadataKeys(O,P):[]}function Type(x){if(null===x)return 1;switch(typeof x){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return null===x?1:6;default:return 6}}function IsUndefined(x){return void 0===x}function IsNull(x){return null===x}function IsSymbol(x){return"symbol"==typeof x}function IsObject(x){return"object"==typeof x?null!==x:"function"==typeof x}function ToPrimitive(input,PreferredType){switch(Type(input)){case 0:case 1:case 2:case 3:case 4:case 5:return input}var hint=3===PreferredType?"string":5===PreferredType?"number":"default",exoticToPrim=GetMethod(input,toPrimitiveSymbol);if(void 0!==exoticToPrim){var result=exoticToPrim.call(input,hint);if(IsObject(result))throw new TypeError;return result}return OrdinaryToPrimitive(input,"default"===hint?"number":hint)}function OrdinaryToPrimitive(O,hint){if("string"===hint){var toString_1=O.toString;if(IsCallable(toString_1))if(!IsObject(result=toString_1.call(O)))return result;if(IsCallable(valueOf=O.valueOf))if(!IsObject(result=valueOf.call(O)))return result}else{var valueOf;if(IsCallable(valueOf=O.valueOf))if(!IsObject(result=valueOf.call(O)))return result;var result,toString_2=O.toString;if(IsCallable(toString_2))if(!IsObject(result=toString_2.call(O)))return result}throw new TypeError}function ToBoolean(argument){return!!argument}function ToString(argument){return""+argument}function ToPropertyKey(argument){var key=ToPrimitive(argument,3);return IsSymbol(key)?key:ToString(key)}function IsArray(argument){return Array.isArray?Array.isArray(argument):argument instanceof Object?argument instanceof Array:"[object Array]"===Object.prototype.toString.call(argument)}function IsCallable(argument){return"function"==typeof argument}function IsConstructor(argument){return"function"==typeof argument}function IsPropertyKey(argument){switch(Type(argument)){case 3:case 4:return!0;default:return!1}}function SameValueZero(x,y){return x===y||x!=x&&y!=y}function GetMethod(V,P){var func=V[P];if(null!=func){if(!IsCallable(func))throw new TypeError;return func}}function GetIterator(obj){var method=GetMethod(obj,iteratorSymbol);if(!IsCallable(method))throw new TypeError;var iterator=method.call(obj);if(!IsObject(iterator))throw new TypeError;return iterator}function IteratorValue(iterResult){return iterResult.value}function IteratorStep(iterator){var result=iterator.next();return!result.done&&result}function IteratorClose(iterator){var f=iterator.return;f&&f.call(iterator)}function OrdinaryGetPrototypeOf(O){var proto=Object.getPrototypeOf(O);if("function"!=typeof O||O===functionPrototype)return proto;if(proto!==functionPrototype)return proto;var prototype=O.prototype,prototypeProto=prototype&&Object.getPrototypeOf(prototype);if(null==prototypeProto||prototypeProto===Object.prototype)return proto;var constructor=prototypeProto.constructor;return"function"!=typeof constructor||constructor===O?proto:constructor}function CreateMetadataRegistry(){var fallback,first,second,rest;IsUndefined(registrySymbol)||void 0===root.Reflect||registrySymbol in root.Reflect||"function"!=typeof root.Reflect.defineMetadata||(fallback=CreateFallbackProvider(root.Reflect));var targetProviderMap=new _WeakMap,registry={registerProvider:registerProvider,getProvider:getProvider,setProvider:setProvider};return registry;function registerProvider(provider){if(!Object.isExtensible(registry))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case fallback===provider:break;case IsUndefined(first):first=provider;break;case first===provider:break;case IsUndefined(second):second=provider;break;case second===provider:break;default:void 0===rest&&(rest=new _Set),rest.add(provider)}}function getProviderNoCache(O,P){if(!IsUndefined(first)){if(first.isProviderFor(O,P))return first;if(!IsUndefined(second)){if(second.isProviderFor(O,P))return first;if(!IsUndefined(rest))for(var iterator=GetIterator(rest);;){var next=IteratorStep(iterator);if(!next)return;var provider=IteratorValue(next);if(provider.isProviderFor(O,P))return IteratorClose(iterator),provider}}}if(!IsUndefined(fallback)&&fallback.isProviderFor(O,P))return fallback}function getProvider(O,P){var provider,providerMap=targetProviderMap.get(O);return IsUndefined(providerMap)||(provider=providerMap.get(P)),IsUndefined(provider)?(IsUndefined(provider=getProviderNoCache(O,P))||(IsUndefined(providerMap)&&(providerMap=new _Map,targetProviderMap.set(O,providerMap)),providerMap.set(P,provider)),provider):provider}function hasProvider(provider){if(IsUndefined(provider))throw new TypeError;return first===provider||second===provider||!IsUndefined(rest)&&rest.has(provider)}function setProvider(O,P,provider){if(!hasProvider(provider))throw new Error("Metadata provider not registered.");var existingProvider=getProvider(O,P);if(existingProvider!==provider){if(!IsUndefined(existingProvider))return!1;var providerMap=targetProviderMap.get(O);IsUndefined(providerMap)&&(providerMap=new _Map,targetProviderMap.set(O,providerMap)),providerMap.set(P,provider)}return!0}}function GetOrCreateMetadataRegistry(){var metadataRegistry;return!IsUndefined(registrySymbol)&&IsObject(root.Reflect)&&Object.isExtensible(root.Reflect)&&(metadataRegistry=root.Reflect[registrySymbol]),IsUndefined(metadataRegistry)&&(metadataRegistry=CreateMetadataRegistry()),!IsUndefined(registrySymbol)&&IsObject(root.Reflect)&&Object.isExtensible(root.Reflect)&&Object.defineProperty(root.Reflect,registrySymbol,{enumerable:!1,configurable:!1,writable:!1,value:metadataRegistry}),metadataRegistry}function CreateMetadataProvider(registry){var metadata=new _WeakMap,provider={isProviderFor:function(O,P){var targetMetadata=metadata.get(O);return!IsUndefined(targetMetadata)&&targetMetadata.has(P)},OrdinaryDefineOwnMetadata:OrdinaryDefineOwnMetadata,OrdinaryHasOwnMetadata:OrdinaryHasOwnMetadata,OrdinaryGetOwnMetadata:OrdinaryGetOwnMetadata,OrdinaryOwnMetadataKeys:OrdinaryOwnMetadataKeys,OrdinaryDeleteMetadata:OrdinaryDeleteMetadata};return metadataRegistry.registerProvider(provider),provider;function GetOrCreateMetadataMap(O,P,Create){var targetMetadata=metadata.get(O),createdTargetMetadata=!1;if(IsUndefined(targetMetadata)){if(!Create)return;targetMetadata=new _Map,metadata.set(O,targetMetadata),createdTargetMetadata=!0}var metadataMap=targetMetadata.get(P);if(IsUndefined(metadataMap)){if(!Create)return;if(metadataMap=new _Map,targetMetadata.set(P,metadataMap),!registry.setProvider(O,P,provider))throw targetMetadata.delete(P),createdTargetMetadata&&metadata.delete(O),new Error("Wrong provider for target.")}return metadataMap}function OrdinaryHasOwnMetadata(MetadataKey,O,P){var metadataMap=GetOrCreateMetadataMap(O,P,!1);return!IsUndefined(metadataMap)&&ToBoolean(metadataMap.has(MetadataKey))}function OrdinaryGetOwnMetadata(MetadataKey,O,P){var metadataMap=GetOrCreateMetadataMap(O,P,!1);if(!IsUndefined(metadataMap))return metadataMap.get(MetadataKey)}function OrdinaryDefineOwnMetadata(MetadataKey,MetadataValue,O,P){GetOrCreateMetadataMap(O,P,!0).set(MetadataKey,MetadataValue)}function OrdinaryOwnMetadataKeys(O,P){var keys=[],metadataMap=GetOrCreateMetadataMap(O,P,!1);if(IsUndefined(metadataMap))return keys;for(var iterator=GetIterator(metadataMap.keys()),k=0;;){var next=IteratorStep(iterator);if(!next)return keys.length=k,keys;var nextValue=IteratorValue(next);try{keys[k]=nextValue}catch(e){try{IteratorClose(iterator)}finally{throw e}}k++}}function OrdinaryDeleteMetadata(MetadataKey,O,P){var metadataMap=GetOrCreateMetadataMap(O,P,!1);if(IsUndefined(metadataMap))return!1;if(!metadataMap.delete(MetadataKey))return!1;if(0===metadataMap.size){var targetMetadata=metadata.get(O);IsUndefined(targetMetadata)||(targetMetadata.delete(P),0===targetMetadata.size&&metadata.delete(targetMetadata))}return!0}}function CreateFallbackProvider(reflect){var defineMetadata=reflect.defineMetadata,hasOwnMetadata=reflect.hasOwnMetadata,getOwnMetadata=reflect.getOwnMetadata,getOwnMetadataKeys=reflect.getOwnMetadataKeys,deleteMetadata=reflect.deleteMetadata,metadataOwner=new _WeakMap;return{isProviderFor:function(O,P){var metadataPropertySet=metadataOwner.get(O);return IsUndefined(metadataPropertySet)?!!getOwnMetadataKeys(O,P).length&&(IsUndefined(metadataPropertySet)&&(metadataPropertySet=new _Set,metadataOwner.set(O,metadataPropertySet)),metadataPropertySet.add(P),!0):metadataPropertySet.has(P)},OrdinaryDefineOwnMetadata:defineMetadata,OrdinaryHasOwnMetadata:hasOwnMetadata,OrdinaryGetOwnMetadata:getOwnMetadata,OrdinaryOwnMetadataKeys:getOwnMetadataKeys,OrdinaryDeleteMetadata:deleteMetadata}}function GetMetadataProvider(O,P,Create){var registeredProvider=metadataRegistry.getProvider(O,P);if(!IsUndefined(registeredProvider))return registeredProvider;if(Create){if(metadataRegistry.setProvider(O,P,metadataProvider))return metadataProvider;throw new Error("Illegal state.")}}function CreateMapPolyfill(){var cacheSentinel={},arraySentinel=[],MapIterator=function(){function MapIterator(keys,values,selector){this._index=0,this._keys=keys,this._values=values,this._selector=selector}return MapIterator.prototype["@@iterator"]=function(){return this},MapIterator.prototype[iteratorSymbol]=function(){return this},MapIterator.prototype.next=function(){var index=this._index;if(index>=0&&index<this._keys.length){var result=this._selector(this._keys[index],this._values[index]);return index+1>=this._keys.length?(this._index=-1,this._keys=arraySentinel,this._values=arraySentinel):this._index++,{value:result,done:!1}}return{value:void 0,done:!0}},MapIterator.prototype.throw=function(error){throw this._index>=0&&(this._index=-1,this._keys=arraySentinel,this._values=arraySentinel),error},MapIterator.prototype.return=function(value){return this._index>=0&&(this._index=-1,this._keys=arraySentinel,this._values=arraySentinel),{value:value,done:!0}},MapIterator}();return function(){function Map(){this._keys=[],this._values=[],this._cacheKey=cacheSentinel,this._cacheIndex=-2}return Object.defineProperty(Map.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),Map.prototype.has=function(key){return this._find(key,!1)>=0},Map.prototype.get=function(key){var index=this._find(key,!1);return index>=0?this._values[index]:void 0},Map.prototype.set=function(key,value){var index=this._find(key,!0);return this._values[index]=value,this},Map.prototype.delete=function(key){var index=this._find(key,!1);if(index>=0){for(var size=this._keys.length,i=index+1;i<size;i++)this._keys[i-1]=this._keys[i],this._values[i-1]=this._values[i];return this._keys.length--,this._values.length--,SameValueZero(key,this._cacheKey)&&(this._cacheKey=cacheSentinel,this._cacheIndex=-2),!0}return!1},Map.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=cacheSentinel,this._cacheIndex=-2},Map.prototype.keys=function(){return new MapIterator(this._keys,this._values,getKey)},Map.prototype.values=function(){return new MapIterator(this._keys,this._values,getValue)},Map.prototype.entries=function(){return new MapIterator(this._keys,this._values,getEntry)},Map.prototype["@@iterator"]=function(){return this.entries()},Map.prototype[iteratorSymbol]=function(){return this.entries()},Map.prototype._find=function(key,insert){if(!SameValueZero(this._cacheKey,key)){this._cacheIndex=-1;for(var i=0;i<this._keys.length;i++)if(SameValueZero(this._keys[i],key)){this._cacheIndex=i;break}}return this._cacheIndex<0&&insert&&(this._cacheIndex=this._keys.length,this._keys.push(key),this._values.push(void 0)),this._cacheIndex},Map}();function getKey(key,_){return key}function getValue(_,value){return value}function getEntry(key,value){return[key,value]}}function CreateSetPolyfill(){return function(){function Set(){this._map=new _Map}return Object.defineProperty(Set.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),Set.prototype.has=function(value){return this._map.has(value)},Set.prototype.add=function(value){return this._map.set(value,value),this},Set.prototype.delete=function(value){return this._map.delete(value)},Set.prototype.clear=function(){this._map.clear()},Set.prototype.keys=function(){return this._map.keys()},Set.prototype.values=function(){return this._map.keys()},Set.prototype.entries=function(){return this._map.entries()},Set.prototype["@@iterator"]=function(){return this.keys()},Set.prototype[iteratorSymbol]=function(){return this.keys()},Set}()}function CreateWeakMapPolyfill(){var UUID_SIZE=16,keys=HashMap.create(),rootKey=CreateUniqueKey();return function(){function WeakMap(){this._key=CreateUniqueKey()}return WeakMap.prototype.has=function(target){var table=GetOrCreateWeakMapTable(target,!1);return void 0!==table&&HashMap.has(table,this._key)},WeakMap.prototype.get=function(target){var table=GetOrCreateWeakMapTable(target,!1);return void 0!==table?HashMap.get(table,this._key):void 0},WeakMap.prototype.set=function(target,value){return GetOrCreateWeakMapTable(target,!0)[this._key]=value,this},WeakMap.prototype.delete=function(target){var table=GetOrCreateWeakMapTable(target,!1);return void 0!==table&&delete table[this._key]},WeakMap.prototype.clear=function(){this._key=CreateUniqueKey()},WeakMap}();function CreateUniqueKey(){var key;do{key="@@WeakMap@@"+CreateUUID()}while(HashMap.has(keys,key));return keys[key]=!0,key}function GetOrCreateWeakMapTable(target,create){if(!hasOwn.call(target,rootKey)){if(!create)return;Object.defineProperty(target,rootKey,{value:HashMap.create()})}return target[rootKey]}function FillRandomBytes(buffer,size){for(var i=0;i<size;++i)buffer[i]=255*Math.random()|0;return buffer}function GenRandomBytes(size){return"function"==typeof Uint8Array?"undefined"!=typeof crypto?crypto.getRandomValues(new Uint8Array(size)):"undefined"!=typeof msCrypto?msCrypto.getRandomValues(new Uint8Array(size)):FillRandomBytes(new Uint8Array(size),size):FillRandomBytes(new Array(size),size)}function CreateUUID(){var data=GenRandomBytes(UUID_SIZE);data[6]=79&data[6]|64,data[8]=191&data[8]|128;for(var result="",offset=0;offset<UUID_SIZE;++offset){var byte=data[offset];4!==offset&&6!==offset&&8!==offset||(result+="-"),byte<16&&(result+="0"),result+=byte.toString(16).toLowerCase()}return result}}function MakeDictionary(obj){return obj.__=void 0,delete obj.__,obj}exporter("decorate",decorate),exporter("metadata",metadata),exporter("defineMetadata",defineMetadata),exporter("hasMetadata",hasMetadata),exporter("hasOwnMetadata",hasOwnMetadata),exporter("getMetadata",getMetadata),exporter("getOwnMetadata",getOwnMetadata),exporter("getMetadataKeys",getMetadataKeys),exporter("getOwnMetadataKeys",getOwnMetadataKeys),exporter("deleteMetadata",deleteMetadata)}(exporter,root),void 0===root.Reflect&&(root.Reflect=Reflect)}()}(Reflect$1||(Reflect$1={}));class Reflection{static apiMap="api:map:";static apiMapFlatObject=`${Reflection.apiMap}TSFlatObject`;static apiMapFlatProperty=`${Reflection.apiMap}TSFlatProperty`;static apiMapCollection=`${Reflection.apiMap}TSFlatCollection`;static getApiMapFlatPropertyKey(target,propertyKey){return`${Reflection.apiMapFlatProperty}.${target.constructor.name}.${propertyKey}`}static getApiMapFlatCollectionKey(target,propertyKey){return`${Reflection.apiMapCollection}.${target.constructor.name}.${propertyKey}`}static getFlatPropertyMetadata(target,propertyKey){if(!target)return;const key=Reflection.getApiMapFlatPropertyKey(target,propertyKey);return Reflect.getMetadata(key,target)}static getFlatCollectionMetadata(target,propertyKey){if(!target)return;const key=Reflection.getApiMapFlatCollectionKey(target,propertyKey);return Reflect.getMetadata(key,target)}static getFlatObjectMetadata(type){return type?Reflect.getMetadata(Reflection.apiMapFlatObject,type):void 0}static setFlatPropertyMetadata(value,target,propertyKey){if(!target)return;const key=Reflection.getApiMapFlatPropertyKey(target,propertyKey);Reflect.defineMetadata(key,value,target)}static setFlatCollectionMetadata(value,target,propertyKey){if(!target)return;const key=Reflection.getApiMapFlatCollectionKey(target,propertyKey);Reflect.defineMetadata(key,value,target)}static setFlatObject(value,target){target&&Reflect.defineMetadata(Reflection.apiMapFlatObject,value,target)}}const registeredTSFlatObjects=new Map;function registerTSFlatObject(target,options){const constructorParams=options?.constructorParams??[];Reflection.setFlatObject({constructorParams:constructorParams},target),registeredTSFlatObjects.set(target.name,target)}function copyBuffer(cur){return cur instanceof Buffer?Buffer.from(cur):new cur.constructor(cur.buffer.slice(),cur.byteOffset,cur.length)}function getId(index){return`#_${index}_#`}function isId(str){return null!=/^#_(\d+)_#$/gm.exec(str)}function addItem(value,input,positionsByObj){const index=input.push(value)-1;return positionsByObj.set(value,index),index}const ATTR_CLASS_NAME="__class__";function formatValue(value){return null!=value&&"object"==typeof value&®isteredTSFlatObjects.has(value.constructor.name)?(value[ATTR_CLASS_NAME]=value.constructor.name,value):value}function convertCollectionToArray(value,currentCollectionType){if(null==value)return value;switch(currentCollectionType){case"array":return value;case"set":return Array.from(value.values());case"map":return Array.from(value.entries());case"dictionary":{const array=[];for(const key of Object.keys(value))array.push({key:key,value:value[key]});return array}}}function convertArrayToOriginalCollectionType(value,currentCollectionType){if(null==value)return value;const array=value;switch(currentCollectionType){case"array":return value;case"set":return new Set(array);case"map":return new Map(array);case"dictionary":{const obj={};return array.forEach((item=>{obj[item.key]=item.value})),obj}}}function getFlatPropertyMetadata(obj,propertyKey){let propertyMetadata=null;do{propertyMetadata=Reflection.getFlatPropertyMetadata(obj.constructor.prototype,propertyKey),obj=Object.getPrototypeOf(obj)}while(null==propertyMetadata&&"Object"!=obj.constructor.name);return propertyMetadata}function getFlatCollectionMetadata(obj,propertyKey){let collectionMetadata=null;do{collectionMetadata=Reflection.getFlatCollectionMetadata(obj.constructor.prototype,propertyKey),obj=Object.getPrototypeOf(obj)}while(null==collectionMetadata&&"Object"!=obj.constructor.name);return collectionMetadata}function afterParse(obj,alreadyVisited=new Set){if(null!=obj)for(const key of Object.keys(obj)){if(null!=obj[key]&&"object"==typeof obj[key]){if(alreadyVisited.has(obj[key]))continue;alreadyVisited.add(obj[key]),afterParse(obj[key],alreadyVisited)}const propertyMetadata=getFlatPropertyMetadata(obj,key),collectionMetadata=getFlatCollectionMetadata(obj,key);null!=propertyMetadata&&propertyMetadata.afterParse&&(obj[key]=propertyMetadata.afterParse(obj[key])),null!=collectionMetadata&&(obj[key]=convertArrayToOriginalCollectionType(obj[key],collectionMetadata.collectionType))}}function registerTSFlatProperty(target,propertyName,options){Reflection.setFlatPropertyMetadata({beforeStringify:options?.beforeStringify,afterParse:options?.afterParse,ignore:options?.ignore},target,propertyName)}function registerTSFlatCollection(target,propertyName,options){Reflection.setFlatCollectionMetadata({collectionType:options.collectionType},target,propertyName)}exports.TSFlatCollection=options=>(target,key)=>{registerTSFlatCollection(target,key,options)},exports.TSFlatObject=options=>target=>{registerTSFlatObject(target,options)},exports.TSFlatProperty=options=>(target,key)=>{registerTSFlatProperty(target,key,options)},exports.parse=function(str){const array=JSON.parse(str);return"object"!=typeof array[0]||(array.forEach(((item,index)=>{if(null!=item&&"object"==typeof item){const className=item[ATTR_CLASS_NAME];if(null!=className&®isteredTSFlatObjects.has(className)){const prototype=registeredTSFlatObjects.get(className).prototype,tempObj=Object.create(prototype),metadata=Reflection.getFlatObjectMetadata(tempObj.constructor);array[index]=Object.assign(new tempObj.constructor(...metadata.constructorParams),item)}}})),array.forEach((item=>{if(null!=item&&"object"==typeof item)for(const key of Object.keys(item))if(isId(item[key])){const indexOnFlat=(id=item[key],parseInt(id.split("#_")[1],10));item[key]=array[indexOnFlat]}var id})),afterParse(array[0]),array.forEach((item=>{null!=item&&delete item[ATTR_CLASS_NAME]}))),array[0]},exports.registerTSFlat=function(objectRegistration,...items){if(registerTSFlatObject(objectRegistration.target,objectRegistration.options),items)for(const item of items)"collectionName"in item?registerTSFlatCollection(objectRegistration.target.prototype,item.collectionName,item.options):"propertyName"in item&®isterTSFlatProperty(objectRegistration.target.prototype,item.propertyName,item.options)},exports.registerTSFlatCollection=registerTSFlatCollection,exports.registerTSFlatObject=registerTSFlatObject,exports.registerTSFlatProperty=registerTSFlatProperty,exports.stringify=function(obj,options){const cloner=function(opts){const refs=[],refsNew=[];return function clone(o){if("object"!=typeof o||null===o)return o;if(opts?.customWayOfCloningObject?.has(o.constructor.prototype))return opts.customWayOfCloningObject.get(o.constructor.prototype)(o);if(o instanceof Date)return new Date(o);if(Array.isArray(o))return cloneArray(o,clone);if(o instanceof Map)return new Map(cloneArray(Array.from(o),clone));if(o instanceof Set)return new Set(cloneArray(Array.from(o),clone));const o2=Object.create(Object.getPrototypeOf(o));refs.push(o),refsNew.push(o2);for(const k in o){const propertyMetadata=getFlatPropertyMetadata(o,k);if(!1===Object.hasOwnProperty.call(o,k)||!0===propertyMetadata?.ignore)continue;const cur=o[k];if("object"!=typeof cur||null===cur)o2[k]=cur;else if(opts?.customWayOfCloningObject?.has(cur.constructor))o2[k]=opts.customWayOfCloningObject.get(cur.constructor)(cur);else if(cur instanceof Date)o2[k]=new Date(cur);else if(cur instanceof Map)o2[k]=new Map(cloneArray(Array.from(cur),clone));else if(cur instanceof Set)o2[k]=new Set(cloneArray(Array.from(cur),clone));else if(ArrayBuffer.isView(cur))o2[k]=copyBuffer(cur);else{const i=refs.indexOf(cur);o2[k]=-1!==i?refsNew[i]:clone(cur)}}return refs.pop(),refsNew.pop(),o2};function cloneArray(a,fn){const keys=Object.keys(a),a2=new Array(keys.length);for(let i=0;i<keys.length;i++){const k=keys[i],cur=a[k];if("object"!=typeof cur||null===cur)a2[k]=cur;else if(cur instanceof Date)a2[k]=new Date(cur);else if(ArrayBuffer.isView(cur))a2[k]=copyBuffer(cur);else{const index=refs.indexOf(cur);a2[k]=-1!==index?refsNew[index]:fn(cur)}}return a2}}(options?.rFDCOptions);obj=cloner(obj);const positionsByObj=new Map,inputArray=[],outputArray=[];let i=0,first=!0;for(addItem(obj,inputArray,positionsByObj);i<inputArray.length;)first=!0,outputArray.push(JSON.stringify(inputArray[i++],((key,value)=>{if(first){if(null!=value&&"object"==typeof value&&!(value instanceof Array))for(const childKey of Object.keys(value)){const propertyMetadata=getFlatPropertyMetadata(value,childKey),collectionMetadata=getFlatCollectionMetadata(value,childKey);null!=propertyMetadata&&propertyMetadata.beforeStringify&&(value[childKey]=propertyMetadata.beforeStringify(value[childKey])),null!=collectionMetadata&&(value[childKey]=convertCollectionToArray(value[childKey],collectionMetadata.collectionType))}return first=!1,formatValue(value)}return null==value?value:"object"==typeof value?positionsByObj.has(value)?getId(positionsByObj.get(value)):getId(addItem(formatValue(value),inputArray,positionsByObj)):formatValue(value)})));return`[${outputArray.join(",")}]`};
//# sourceMappingURL=index.js.map