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
316 lines (310 loc) • 16.4 kB
JavaScript
(function () {
console.log('\n' +
' ---- Decipher mehod concept and notions : -----\n' +
'\n' +
'Definitions and notions used below:\n' +
'\n' +
'parent object pO. Object or array having subobject(s) ciphered\n' +
'\n' +
' pO={..., o:{},...} or\n' +
' pO=[..., o, ...]\n' +
'\n' +
'object - entity containing cipher (cipher pair or cipher object)\n' +
'\n' +
'pO & o subordination keeps after deciphering\n' +
'\n' +
'Deciphering converts\n' +
'\n' +
'cipher pair (keyRe,v1ReV2) or cipher object {keyRe:v1ReV2}\n' +
'\n' +
'into RegExp objects integrated in appropriate structure\n' +
'(variable, object(or array), parent object (or array)\n' +
'\n' +
'cipher separator string RE\n' +
'\n' +
' RE="RE"+rndmN (1)\n' +
'\n' +
'rndmN - random part; string consisting only of N random digits (0-9)\n' +
'\n' +
'keyRe - key-cipher string composed of string key and string RE\n' +
'\n' +
' keyRe=key+RE (2)\n' +
'\n' +
'v1ReV2 - RegExp value ciphering string. Three parts string:\n' +
' v1ReV2=v1+RE+v2, (3)\n' +
'where RE string is used as separator\n' +
'to select v1 and v2 strings (body string and modifier flags string) determining\n' +
'regular expression object by following way -\n' +
'\n' +
' rE= new RegExp(body,modifier) or\n' +
' rE= new RegExp(v1,v2) (4)\n' +
'\n' +
'keyRe string and RE separator string are used for ciphering Regular expression\n' +
'being property of object or element of array object\n' +
'\n' +
'object property case:\n' +
' o={key:RegExp} is ciphered as o={keyRe:v1Rev2} (5.1)\n' +
'or in opposite direction\n' +
' o={keyRe:v1Rev2} is deciphered in o={key:RegExp} (5)\n' +
'\n' +
' o={...,keyRe:v1ReV2,..} -> ={...,key:new RegExp(v1,v2)}, ...} (6)\n' +
'\n' +
'For ciphering naked regular expression as value of variable or array\'s element\n' +
'cipher object is used with key="". That is cipher pair is (RE,v1+RE+v2) or\n' +
'cipher object {RE:v1ReV2}={"\'RE\'+rndmN":v1+\'RE\'+rndmN+v2}\n' +
'\n' +
'RE is used instead of keyRE in key string part.\n' +
'\n' +
'So for array\n' +
'\n' +
' a=[...,{RE:v1ReV2},...] is deciphered into a=[...,rE,...], (7)\n' +
'\n' +
' where rE=new RegExp(v1,v2) (8)\n' +
'\n' +
'variable\n' +
'\n' +
' v={RE:v1ReV2} is deciphered into v=rE (9)\n' +
'\n' +
'subproperty o\n' +
'\n' +
' pO={...,o:{RE:v1ReV2},...} is converted in pO={...,o:rE,...} (10)\n' +
'\n' +
'at the same time if key is used\n' +
'\n' +
' pO={...,o:{keyRe:v1ReV2},...} is converted in pO={...,o:{key:rE},...} (11)\n' +
'\n' +
'for array element as an object:\n' +
'\n' +
' pO=[...,{keyRe:v1ReV2},...] -> pO=[...,{key:rE},...] (12)\n' +
'\n' +
'in the case of arrays key could have digital form key=n - interger, what in this case\n' +
'would mean index number of deciphered result.\n' +
'For example if we have cipher {nRE:v1ReV2} as element with index ind of array pO\n' +
'\n' +
' ind <-index of cipher\n' +
' pO=[...,...,{nRe:v1ReV2},...] is converted into pO=[...,rE,...,...] (13)\n' +
' index of rE element -> n\n' +
'\n' +
'cipher Objects are removed from resulting deciphered objects or arrays.\n' +
'\n' +
'Remark 1.\n' +
'nRE form of key-code used for ciphering objects is deciphered in \'n\' key letter string\n' +
'expressing digit(s) for ex. o={...,"23RE123":v1+\'RE123\'+v2},...} will be deciphered in\n' +
' o={...,"23":new RegExp(v1,v2),...}\n' +
'\n' +
'Remark 2.\n' +
'RE form of keyRe could be used for ciphering of object property only if object has\n' +
'single property, i.e.\n' +
'\n' +
' o={"RE12":v1+"RE12"+v2} (14)\n' +
'\n' +
'is pemissible and is converted into o=new RegExp(v1,v2) (14.1)\n' +
' but\n' +
' o={a:\'a\',...,"RE12":v1+"RE12"+v2, ...} is prohibited (15)\n' +
'\n' +
' and will throw Error!\n' +
'\n' +
'\n' +
'\n' +
'\n' +
'\n' +
'The table below uses some abbreviations for shortness.\n' +
'\n' +
'abbreviations:\n' +
'k - key\n' +
'R - RE\n' +
'vRv2 - v1REv2\n' +
'pO - parent object\n' +
'rE - new RegExp(v1,v2)\n' +
'o - object containing cipher -\n' +
' or cipher pair (key,value) as object property with string value\n' +
' {...,kR:vRv2,..}\n' +
' or cipher object with single property {kR:vRv2}\n' +
'v - variable\n' +
'a - array\n' +
'\n' +
'\n' +
'different keys nomination:\n' +
'\n' +
'kR - keyRE - key ciphered = key+"RE"+rndmN\n' +
'nR - nRe - the same as kR but key part consists of digits only\n' +
'R - RE - when key part ="" or undefined\n' +
'\n' +
'Table. Algorithm explanation. Ciphered entities and their decipherings:\n' +
'\n' +
'entity | ciphered | deciphering |equation| comments \n' +
' | and actions\n' +
'variable:\n' +
' v={R:vRv2} | v=rE (T1) (rE= new RegExp(v,v2); )\n' +
'object:\n' +
' o={kR:vRv2} | o={k:rE}; (T2)\n' +
' | 1. delete o[kR]\n' +
' | 2. o[k]=rE before assignment checks if o already has\n' +
' | property k as instance of RegExp equivalent\n' +
' | to rE (see equation (3) item 2. ). If this is\n' +
' | the case nothing new is inserted.\n' +
' |\n' +
' o={...,kR:vRv2, ... } | o={..., k:rE, ...}; (T3)\n' +
' | 1. delete o[kR];\n' +
' | 2.\n' +
' | if(!o.hasOwnPrperty(k)||\n' +
' | o[k] instanceof RegExp!==true\n' +
' | || o[k]!==rE){ // (*)!! new RegExp(a,b)!== new RegExp(a,b)\n' +
' | o[k]=rE; // (o[k].source !== rE.source ||\n' +
' | } // regstr.migV(o[k])!==regstr.migV(rE));\n' +
' |\n' +
' o={...,nR:vRv2, ... } | o={...,"n":rE, ...}; (T4) property name consists of digits only\n' +
' | 1. delete o[nR];\n' +
' | 2. o["n"]=rE; !! peculiarities similar to (3) 2. !!\n' +
' |\n' +
' o={..., R:vRv2, ... } | !! PROHIBITED !! (X) only variable form (1) is possible\n' +
' | or for object containing ciphered pair\n' +
' | (Object.keys(o).length===1)===true\n' +
'or | like in the case when o is\n' +
'subObject of pO |\n' +
' | (Object.keys(o).length==1) ===true\n' +
' pO={...,o:{R:vRv2},...} | pO={ ... ,o:rE, ... } (T5) which is similar to case (T1)\n' +
' |\n' +
'Array is pO(parent |\n' +
' object): |\n' +
' ind | ind element of pO with index ind\n' +
' pO=[... ,{kR:vRv2},...] | pO=[...,{k:rE}, ... ] (T6)\n' +
' | 1. pO[ind]={k:rE};\n' +
' ind | index == ind\n' +
' pO=[... ,{nR:vRv2},...] | pO=[...,..,rE,...] (T7) new element of pO with index n\n' +
' | n index == n\n' +
' |\n' +
' | if((pO[n] instanceof RegExp)!==true) // has RegExp element with index n\n' +
' | ||( value equivalent to rE)!==true){\n' +
' | 1. pO.splice(n,0,rE);\n' +
' | }else{\n' +
' | then nothing is added and\n' +
' | 2. if(ind<n){\n' +
' | pO.splice(ind,1); // removes cipher element from array\n' +
' | }else{\n' +
' | pO.splice(ind+1,1);\n' +
' | }\n' +
' | }\n' +
' ind | ind\n' +
' pO=[... ,{R:vRv2},...] | pO=[... ,rE, ... ] (T8)\n' +
'\n' +
'\n');
/* 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\n. Deciphered RegExp will be:
* new RegExp(v1,v2);
*/
var decipheringScheme = '\n\n' +
' * ---- Reconversion - deciphering scheme: ---- *\n' +
' Procedure verifies objects or arrays looking for "ciphered"\n' +
' properties or elements\n' +
' The Mark of \'cipher\' presence:\n' +
' - for Object\'s property:\n' +
' property name has form keyRE=key+RE,\n' +
' property value is string of the model: v1ReV2=v1+RE+v2,\n' +
' where key - is the name of RegExp property ciphered with\n' +
' value=new RegExp(v1,v2)\n' +
'\n' +
' "Ciphered" means that it is a "cipher-object" or "cipher"\n' +
' cipher={keyRE:vReV2} coded RegExp object property\n' +
' {...,key:new RegExp(v1,v2),...}\n' +
' or single property object={keyRE:vReV2}\n' +
'\n' +
' A.Ciphered properties of object;\n' +
' B.Element(s) of array "ciphered" in the form of single property object\n' +
' {keyRE:v1ReV2} or {nRe:v1Rev2} (see <Digital key practice> paragraph\n' +
' regarding nRe form of key;\n' +
' C.Variable being RegExp ciphered by means of cipher object wihtout key, i.e.\n' +
' Cipher without key has format {RE:vReV2}, where RE=\'RE\'+rndmN,\n' +
' rndmN - string of N random digits. Deciphered RegExp will be:\n' +
' new RegExp(v1,v2)\n';
console.log(decipheringScheme);
var digitalKeys = ' --- "Digital keys practice": ---\n\n' +
' The notion "Digital key" means property name consisting only of digital\n' +
' characters. For ex.: \'1\',\'12\', \'234\' ... something conforms regular\n' +
' expression /^\\d+$/.\n' +
' Let\'s use short variable form <n> as notation of a Digital key.\n' +
' Here is an example of digital key in single property object -\n' +
' {"n":new RegExp(v1,v2)} with value as RegExp object.\n' +
' Decipher converts cipher object into RegExp.\n' +
' Deciphering procedure uses single property digital key object as\n' +
' Intermediary in conversion. Cipher object like {nRe:v1ReV2} transforms\n' +
' into {n:new RegExp(v1,v2}, is stored in Depo and than converts into\n' +
' RegExp object. Depo keeps key-value pairs ordered by digital keys values.\n' +
' to fulfill final conversion.\n' +
' \n' +
' Decipher having found digital key single property object as an array\'s\n' +
' element will transfer it into an element with value new RegExp(v1,v2)\n' +
' and index n.\n' +
' i index n index\n' +
' pO=[...,{nRe:v1Rev1},... ] ==> [ ..., new RegExp,...]\n' +
' n could not be equal to i, but typically is when deciphering is reverse\n' +
' procedure of previous ciphering:\n' +
' [...,rE,...] ->cipher-> [...,{iRe:v1ReV2},..] ->decipher -> [...,rE,..]\n' +
' Due to some reasons parent array in ciphered form could have few\n' +
' cipher objects with\n' +
' - equal digital keys and equal/not equal regExp values\n' +
' - or has one object with key,value pairs as separate properties\n' +
' Decipher follows convention:\n' +
' 1.index number got from digital key prevails indices of existed elements,\n' +
' but, if parent array already has element with RegExp value and index\n' +
' coinciding that determined by digital key, new element is not added.\n' +
' 2.few cipher objects having equal digital keys combined in common array\n' +
' who itself becomes an element of parent array with index equal to digital\n' +
' key value, i.e. [..,{k1:rE1},{k1:rE2,...,{k1:rE3}] converts into ->\n' +
' [..., [rE1,rE2,rE3],...],where subarray with rE-s is an element of \n' +
' parent array and has index k1.\n' +
' 3.When cipher object being element of parent array has few cipher pairs\n' +
' properties [...{k1:re1,k2:re2,k3:re3},...] it will be converted into\n' +
' [...,re1,...,re2,re3], where elements re1,re2,re3 have indices k1,k2,k3\n' +
' appropriately.\n';
console.log(digitalKeys);
}());
var remarkRegardingDeletPropertyOfObject =
' * ---- this.property = null vs. delete this.property ---- *\n\n' +
' Google Style Guide \n' +
' https://google.github.io/styleguide/javascriptguide.xml#JSDoc_Tag_Reference \n' +
' prefferes \n' +
' this.property = null; \n' +
' to\n' +
' delete this.property; \n' +
' At the same time in regStr handler we use delete property; in the\n' +
' following cases:\n' +
' A. While deciphring cipher pair keyRe=vRev2 into property ]n' +
' {...,key:new RegExp(v,v2),..}\n' +
' keyRe property is not pertinent to original object so to leave it means\n' +
' changing initial object. \n' +
' @toDo To consider is it worthwhile the loss of productivity based on\n' +
' this supposition ( keeping original original shape )?\n' +
'\n' +
' B. The unCycle package uses same logic when uid property is deleted\n' +
' presuming that it\'s new one and was absent in original object\n' +
'\n' +
' C. While transforming cipher object being parent array element and \n' +
' and having the form { nRe:vRev2} into parent array\'s element with index\n' +
' n and value new RegExp(v,v2) -> [...,...,new RegExp(v,v2),...]\n' +
'\n' +
' Reason used: using o[p]null instead of delete o[p] save time but change\n' +
' the original which could don\'t like for public user. For internal use or\n' +
' external puclic user would accept appearance of new propertis like\n' +
' <uid> or <keyRe> ... it is possible to make extra code. But in the case\n' +
' of parent array extra element in the form of cipher object could be harmful\n';