UNPKG

regstr

Version:

JSON.stringify objects with RegExp properties and then JSON.parse json string resulted back into original objects. Converts RegExp object to be serializable - into pair of strings (key,value). Could be used for RegExp being bilaterally stringified and ge

258 lines (197 loc) 10.9 kB
node ./docs/deciphering_explain.js ---- Decipher method concept and notions : ----- Definitions and notions used below: parent object pO. Object or array having subobject(s) ciphered pO={..., o:{},...} or pO=[..., o, ...] object - entity containing cipher (cipher pair or cipher object) pO & o subordination keeps after deciphering Deciphering converts cipher pair (keyRe,v1ReV2) or cipher object {keyRe:v1ReV2} into RegExp objects integrated in appropriate structure (variable, object(or array), parent object (or array) cipher separator string RE RE="RE"+rndmN (1) rndmN - random part; string consisting only of N random digits (0-9) keyRe - key-cipher string composed of string key and string RE keyRe=key+RE (2) v1ReV2 - RegExp value ciphering string. Three parts string: v1ReV2=v1+RE+v2, (3) where RE string is used as separator to select v1 and v2 strings (body string and modifier strig) determining regular expression object by following way - rE= new RegExp(body,modifier) or rE= new RegExp(v1,v2) (4) keyRe string and RE separator string are used for ciphering Regular expression being property of object or element of array object object property case: o={key:RegExp} is ciphered as o={keyRe:v1Rev2} (5.1) or in opposite direction o={keyRe:v1Rev2} is deciphered in o={key:RegExp} (5) o={...,keyRe:v1ReV2,..} -> ={...,key:new RegExp(v1,v2)}, ...} (6) For ciphering naked regular expression as value of variable or array's element cipher object is used with key="". That is cipher pair is (RE,v1+RE+v2) or cipher object {RE:v1ReV2}={"'RE'+rndmN":v1+'RE'+rndmN+v2} RE is used instead of keyRE in key string part. So for array a=[...,{RE:v1ReV2},...] is deciphered into a=[...,rE,...], (7) where rE=new RegExp(v1,v2) (8) variable v={RE:v1ReV2} is deciphered into v=rE (9) subproperty o pO={...,o:{RE:v1ReV2},...} is converted in pO={...,o:rE,...} (10) at the same time if key is used pO={...,o:{keyRe:v1ReV2},...} is converted in pO={...,o:{key:rE},...} (11) for array element as an object: pO=[...,{keyRe:v1ReV2},...] -> pO=[...,{key:rE},...] (12) in the case of arrays key could have digital form key=n - interger, what in this case would mean index number of deciphered result. For example if we have cipher {nRE:v1ReV2} as element with index ind of array pO ind <-index of cipher pO=[...,...,{nRe:v1ReV2},...] is converted into pO=[...,rE,...,...] (13) index of rE element -> n cipher Objects are removed from resulting deciphered objects or arrays. Remark 1. nRE form of key-code used for ciphering objects is deciphered in 'n' key letter string expressing digit(s) for ex. o={...,"23RE123":v1+'RE123'+v2},...} will be deciphered in o={...,"23":new RegExp(v1,v2),...} Remark 2. RE form of keyRe could be used for ciphering of object property only if object has single property, i.e. o={"RE12":v1+"RE12"+v2} (14) is permissible and is converted into o=new RegExp(v1,v2) (14.1) but o={a:'a',...,"RE12":v1+"RE12"+v2, ...} is prohibited (15) and will throw Error! The table below uses some abbreviations for shortness. abbreviations: k - key R - RE vRv2 - v1REv2 pO - parent object rE - new RegExp(v1,v2) o - object containing cipher - or cipher pair (key,value) as object property with string value {...,kR:vRv2,..} or cipher object with single property {kR:vRv2} v - variable a - array different keys nomination: kR - keyRE - key ciphered = key+"RE"+rndmN nR - nRe - the same as kR but key part consists of digits only R - RE - when key part ="" or undefined Table. Algorithm explanation. Ciphered entities and their decipherings: entity | ciphered | deciphering |equation| comments | and actions variable: v={R:vRv2} | v=rE (T1) (rE= new RegExp(v,v2); ) object: o={kR:vRv2} | o={k:rE}; (T2) | 1. delete o[kR] | 2. o[k]=rE before assignment checks if o already has | property k as instance of RegExp equivalent | to rE (see equation (3) item 2. ). If this is | the case nothing new is inserted. | o={...,kR:vRv2, ... } | o={..., k:rE, ...}; (T3) | 1. delete o[kR]; | 2. | if(!o.hasOwnPrperty(k)|| | o[k] instanceof RegExp!==true | || o[k]!==rE){ // (*)!! new RegExp(a,b)!== new RegExp(a,b) | o[k]=rE; // (o[k].source !== rE.source || | } // regstr.migV(o[k])!==regstr.migV(rE)); | o={...,nR:vRv2, ... } | o={...,"n":rE, ...}; (T4) property name consists of digits only | 1. delete o[nR]; | 2. o["n"]=rE; !! peculiarities similar to (3) 2. !! | o={..., R:vRv2, ... } | !! PROHIBITED !! (X) only variable form (1) is possible | or for object containing ciphered pair | (Object.keys(o).length===1)===true or | like in the case when o is subObject of pO | | (Object.keys(o).length==1) ===true pO={...,o:{R:vRv2},...} | pO={ ... ,o:rE, ... } (T5) which is similar to case (T1) | Array is pO(parent | object): | ind | ind element of pO with index ind pO=[... ,{kR:vRv2},...] | pO=[...,{k:rE}, ... ] (T6) | 1. pO[ind]={k:rE}; ind | index == ind pO=[... ,{nR:vRv2},...] | pO=[...,..,rE,...] (T7) new element of pO with index n | n index == n | | if((pO[n] instanceof RegExp)!==true) // has RegExp element with index n | ||( value equivalent to rE)!==true){ | 1. pO.splice(n,0,rE); | }else{ | then nothing is added and | 2. if(ind<n){ | pO.splice(ind,1); // removes cipher element from array | }else{ | pO.splice(ind+1,1); | } | } ind | ind pO=[... ,{R:vRv2},...] | pO=[... ,rE, ... ] (T8) * ---- Reconversion - deciphering scheme: ---- * Procedure verifies objects or arrays looking for "ciphered" properties or elements The Mark of 'cipher' presence: - for Object's property: property name has form keyRE=key+RE, property value is string of the model: v1ReV2=v1+RE+v2, where key - is the name of RegExp property ciphered with value=new RegExp(v1,v2) "Ciphered" means that it is a "cipher-object" or "cipher" cipher={keyRE:vReV2} coded RegExp object property {...,key:new RegExp(v1,v2),...} or single property object={keyRE:vReV2} A.Ciphered properties of object; B.Element(s) of array "ciphered" in the form of single property object {keyRE:v1ReV2} or {nRe:v1Rev2} (see <Digital key practice> paragraph regarding nRe form of key; C.Variable being RegExp ciphered by means of cipher object without key, i.e. Cipher without key has format {RE:vReV2}, where RE='RE'+rndmN, rndmN - string of N random digits. Deciphered RegExp will be: new RegExp(v1,v2) --- "Digital keys practice": --- The notion "Digital key" means property name consisting only of digital characters. For ex.: '1','12', '234' ... something conforms regular expression /^d+$/. Let's use short variable form <n> as notation of a Digital key. Here is an example of digital key in single property object - {"n":new RegExp(v1,v2)} with value as RegExp object. Decipher converts cipher object into RegExp. Deciphering procedure uses single property digital key object as Intermediary in conversion. Cipher object like {nRe:v1ReV2} transforms into {n:new RegExp(v1,v2}, is stored in Depo and than converts into RegExp object. Depo keeps key-value pairs ordered by digital keys values. to fulfill final conversion. Decipher having found digital key single property object as an array's element will transfer it into an element with value new RegExp(v1,v2) and index n. i index n index pO=[...,{nRe:v1Rev1},... ] ==> [ ..., new RegExp,...] n could not be equal to i, but typically is when deciphering is reverse procedure of previous ciphering: [...,rE,...] ->cipher-> [...,{iRe:v1ReV2},..] ->decipher -> [...,rE,..] Due to some reasons parent array in ciphered form could have few cipher objects with - equal digital keys and equal/not equal regExp values - or has one object with key,value pairs as separate properties Decipher follows convention: 1.index number got from digital key prevails indices of existed elements, but, if parent array already has element with RegExp value and index coinciding that determined by digital key, new element is not added. 2.few cipher objects having equal digital keys combined in common array who itself becomes an element of parent array with index equal to digital key value, i.e. [..,{k1:rE1},{k1:rE2,...,{k1:rE3}] converts into -> [..., [rE1,rE2,rE3],...],where sub-array with rE-s is an element of parent array and has index k1. 3.When cipher object being element of parent array has few cipher pairs properties [...{k1:re1,k2:re2,k3:re3},...] it will be converted into [...,re1,...,re2,re3], where elements re1,re2,re3 have indices k1,k2,k3 appropriately.