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

365 lines (342 loc) 11.4 kB
(function () { /**\n* test.js */ var regstr = require('./regstr').regStr; console.log('\n\nTests begins\n\n'); var tar = regstr.testRE[1]; console.log('test Array of [[key,value]] string pairs=\n'); console.log(tar); console.log('\n'); for (var i = 0; i < tar.length; i++) { var to = regstr.nDigs(tar[i][0]); var re = to.re; var n = to.n; var key = to.k1; var regO = regstr.regUp(tar[i][0], tar[i][1]); console.log('\nn=%s , re=%s , key= %s, regO={key: %s, value: %s };', n, re, key, regO.key, regO.value); } var oTest = function () { return [ ['something not RegExp', 2], ['simple RegExp object', /^something\t.+/mg], ['RegExp property of object', { re1: /abc(1|2)/g }], ['RegExp element of an array', [1, 'a', /abc(1|2)/g, 3]], ['RegExp property of subobject', { a: 'a', o: { re2: /abc(1|2)/g }, arr: [1, 'a', /abc(51|a2)[ ]$/gi, 3], o1: { re3: /^something\t.+/ } } ] ]; }; var oTst = oTest(); var _oTst=oTest(); var test; /*-------------- var m=0; oTst.forEach((e,i) => {if (e[0].length>m) {m=e[0].length}}); console.log("List of testing Entities:"); oTst.forEach( e => { var k=m-e[0].length; var f = ()=>{var s=" ";return ()=> s=s+" "};f1=f();for(i=0; i<=k; i++){var ss=f1()}; console.log( e[0] + ss +":",e[1]); }); */ require('./showTestList.js').showTestEntities(oTst); // ------------- for (var it = 0; it < oTst.length; it++) { console.log('\nCase %s: Testing Entity is <%s>;\n TestEntity.toString()= %s \n TestingEntity:', it, oTst[it][0], oTst[it][1].toString(), _oTst[it][1]); console.log('\nResult of streger(testVar):\n'); test = regstr.streger(oTst[it][1]); console.log('*** Input object Actual look: oTst[' + it + '][1]='); console.log(_oTst[it][1]); console.log('\nValue test returned by test=regstr.streger( oTest[' + it + '][1] )=\n'); console.log(test); } console.log('\nNow let\'s test reconvertion from ciphered key,value pairs'); /* Reconversion - deciphering scheme: * Procedure varifies 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 * vlaue=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 wihtout 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 varifies 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' + ' vlaue=new RegExp(v1,v2)\n' + '\n' + ' - for element of array - the element value is cipher object with\n' + ' "digital keys"( when in keyRe=key+\'RE\', key consists of digitals only).\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 fillO = function (o) { var ob = {}; var key, val; for (var i = 0; i < o.length; i++) { key = o[i][0]; val = o[i][1]; ob[key] = val; } return ob; }; var fillAr = function (o) { var ob = [], inO; for (var i = 0; i < o.length; i++) { inO = fillO([o[i]]); ob.push(inO); } return ob; }; var ot = regstr.testRE[1]; var ot1 = regstr.testRE[2]; // inpO1 var inpO1 = fillO(ot.slice(0)); var inpO2 = fillAr(ot.slice(0)); var inpO3 = fillO(ot1.slice(0)); var inpO4 = fillAr(ot1.slice(0)); var oT = [ ['**Variable', { RE123: 'bbbRE123mg' }], ['**Single property object', fillO([ot[0]])], ['**Object with ciphered properties', inpO1], ['**Array with ciphered elements', inpO2], ['**Object with ciphered digital keys elements', inpO3], ['**Array with ciphered digital keys elements', inpO4] ]; var oTT = { a: 'a', b: 2, arr: [1, 2], otO: oT[1][1], otA: [oT[3][1][0]], oA: [3, 4, oT[2][1]], oOA: { c: 'c', d: 6, oAA: oT[3][1], oOO: oT[2][1] }, otA1: [oT[5][1][0]], otA2: [oT[4][1]], oA1: [3, 4, oT[4][1]], oOA1: { c: 'c', d: 6, oAA: oT[4][1], oOO: oT[5][1] } }; oT.push(['**Complex object', oTT]); var ot_c = JSON.parse(JSON.stringify(ot)); var oT_c = JSON.parse(JSON.stringify(oT)); var oTT_c = JSON.parse(JSON.stringify(oTT)); console.log('Test objects:'); console.log('ot:'); console.log(ot); console.log('oT:'); console.log(oT); console.log('oTT:'); console.log(oTT); console.log('\nReconversion results:'); for (var ii = 0; ii < oT.length; ii++) { console.log(oT[ii][0] + ':'); console.log('ciphered:'); console.log(oT[ii][1]); console.log('deciphered:'); console.log(regstr.reger(oT[ii][1])); } /* return { ot:ot_c, oT:oT_c, oTT:oTT_c }; */ console.log('\n\nTest has finished. Wellcome!'); }()); exports.test = (function () { /**\n* test.js */ var regstr = require('./regstr').regStr; // console.log('\n\nTests begins\n\n'); var tar = regstr.testRE[1]; // console.log('test Array of [[key,values]] string pairs=\n'); // console.log(tar); // console.log('\n'); for (var i = 0; i < tar.length; i++) { var to = regstr.nDigs(tar[i][0]); var re = to.re; var n = to.n; var key = to.k1; var regO = regstr.regUp(tar[i][0], tar[i][1]); // console.log('\nn=%s , re=%s , key= %s, regO={key: %s, value: %s };', // n,re,key,regO.key,regO.value); } // console.log('\nTest of new options for versions ">=0.2" '); var oTest = function () { return [ ['something not RegExp', 2], ['simple RegExp object', /^something\t.+/mg], ['RegExp property of object', { re1: /abc(1|2)/g }], ['RegExp element of an array', [1, 'a', /abc(1|2)/g, 3]], ['RegExp property of subobject', { a: 'a', o: { re2: /abc(1|2)/g }, arr: [1, 'a', /abc(51|a2)[ ]$/gi, 3], o1: { re3: /^something\t.+/ } } ] ]; }; var oTst = oTest(); var test; for (var it = 0; it < oTst.length; it++) { test = regstr.streger(oTst[it][1]); } /* Reconversion - deciphering scheme: * Procedure varifies 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 * vlaue=new RegExp(v1,v2)\n * "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} * C.Variable being RegExp ciphered by means of cipher object witout key, i.e. * Cipher without key has format {RE:vReV2}, where RE=\'RE\'+rndmN, * rndmN - string of N random digits\n'); * */ var fillO = function (o) { var ob = {}; var key, val; for (var i = 0; i < o.length; i++) { key = o[i][0]; val = o[i][1]; ob[key] = val; } return ob; }; var fillAr = function (o) { var ob = [], inO; for (var i = 0; i < o.length; i++) { inO = fillO([o[i]]); ob.push(inO); } return ob; }; var ot = regstr.testRE[1]; var ot1 = regstr.testRE[2]; // inpO1 var inpO1 = fillO(ot.slice(0)); var inpO2 = fillAr(ot.slice(0)); var inpO3 = fillO(ot1.slice(0)); var inpO4 = fillAr(ot1.slice(0)); var oT = [ ['**Variable', { RE123: 'bbbRE123mg' }], ['**Single property object', fillO([ot[0]])], ['**Object with ciphered properties', inpO1], ['**Array with ciphered elements', inpO2], ['**Object with ciphered digital keys elements', inpO3], ['**Array with ciphered digital keys elements', inpO4] ]; var oTT = { a: 'a', b: 2, arr: [1, 2], otO: oT[1][1], otA: [oT[3][1][0]], oA: [3, 4, oT[2][1]], oOA: { c: 'c', d: 6, oAA: oT[3][1], oOO: oT[2][1] }, otA1: [oT[5][1][0]], otA2: [oT[4][1]], oA1: [3, 4, oT[4][1]], oOA1: { c: 'c', d: 6, oAA: oT[4][1], oOO: oT[5][1] } }; oT.push(['**Complex object', oTT]); var ot_c = JSON.parse(JSON.stringify(ot)); var oT_c = JSON.parse(JSON.stringify(oT)); var oTT_c = JSON.parse(JSON.stringify(oTT)); console.log('\n\nTo get some explanation type: <npm run explain>\n' + 'or from inside node type require("./docs/deciphering_explain.js");'); return { oTest: oTst, ot: ot_c, oT: oT_c, oTT: oTT_c, clone: function (o) { return JSON.parse(JSON.stringify(o)); } }; }());