I'm trying to save a HUGE json string in my localStorage but for some reason sometimes it saves it and sometimes not, I thought I should compress it so I took an LZW implementation in js from one of the threads in stackoverflow.
The problem is when I try to localStorage.setItem() the compressed string it gives me an error "Invalid argument", any idea why or what should I do?
Edit:
this is the code I'm using:
// LZW-compress a string
function lzw_encode(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase=currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i=0; i<out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}
// Decompress an LZW-encoded string
function lzw_decode(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}
this is the code that calls the compressing algorithm and saves it in LS
LOG("GetMerchantList(): Done");
var SITESVAR = unescape(data)
localStorage.setItem("MYSITES", lzw_encode(SITESVAR)); //this throws error
IWT.BuildMerchantList(SITESVAR);
Related
Using app scripts I'm trying to extract all the email addresses from email messages and put them in an array. From my console.log messages, I'm getting stuck because it looks like instead of an array I just get a string. i'm not too familiar with javascript. Any help would be great. I'm looking for an array of email address. The methods of message.get() return a string. I want to split out the email address and create a single, unified array.
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
}
function getGmailEmails(){
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads();
var fullArray = [];
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++){
var message = messages[j];
if (message.isUnread()){
fullArray.push(extractDetails(message));
}
}
}
console.log("FullArray:"+fullArray);
for(var i=0; i<fullArray.length; i++){
console.log("printing array " + i + ": "+fullArray[i])
}
}
function extractDetails(message){
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var ccEmails = message.getCc();
var replyEmail = message.getReplyTo();
var toEmail = message.getTo();
var emailArray = []
var senderArray = senderDetails.split(',');
var ccArray = ccEmails.split(',');
var replyArray = replyEmail.split(',');
var toArray = toEmail.split(',');
for (var i =0 ; i<toArray.length; i++){
console.log("toArray Loop"+ i + " : "+ toArray[i]);
emailArray.push([toArray[i]]);
}
for (var i =0 ; i<ccArray.length; i++){
console.log("ccArray Loop"+ i + " : "+ ccArray[i]);
emailArray.push([ccArray[i]]);
}
console.log("Email Array: "+ emailArray);
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, ccEmails,replyEmail,toEmail,emailArray]);
return emailArray;
}
I think the problem is just the console output. If you change console.log("Email Array: "+ emailArray); to console.log("Email Array: ", emailArray);, then it shows an array of arrays. You could simplify your extract method as follows:
function extractDetails(message) {
/* ... */
var senderDetails = message.getFrom();
var ccEmails = message.getCc();
var replyEmails = message.getReplyTo();
var toEmails = message.getTo();
let emailArray = [senderDetails, ccEmails, replyEmails, toEmails].reduce(
(array, string) => {
//remove names (like "Name <name#company.com>") and filter empty values
let emails = string
.split(/\s*,\s*/)
.map(e => e.replace(/.*?([^#<\s]+#[^#\s>]+).*?/g, "$1"))
.filter(Boolean);
if(emails.length > 0)
return array.concat(emails)
return array
}, []);
/* ... */
}
i try with Node Red to build an query to send multiple values to an influxDB from a loop with this code:
var inputArray = msg.payload;
var lenInputArray =inputArray.length;
var modbusStartRegister = 14000;
var sendString = "";
var msg93 ={};
for (i = 0; i < lenInputArray; i++) {
var actRegister = modbusStartRegister +i;
var actValue = inputArray[i];
if ( i >=1){
sendString = sendString + " ,"
}
sendString = sendString +"{register: " + actRegister +"," +"value: " + actValue +"}";
if ( i ==(lenInputArray-1)){
sendString = sendString + "]"
}
}
msg93.payload = sendString;
return msg93
But the insert in the influxDB is one line it looks at them interpreted as an complete string. How can I build or convert the string that the DB accept them as individual entry? Thanks for the help
This is because you are building a string, node an object.
You can build the array object on the fly like this:
var inputArray = msg.payload;
var lenInputArray =inputArray.length;
var modbusStartRegister = 14000;
var payload = [];
var msg93 ={};
for (i = 0; i < lenInputArray; i++) {
var temp = {};
temp.register = modbusStartRegister +i;
temp.value = inputArray[i];
payload.push(temp);
}
msg93.payload = payload;
return msg93
I wrote the code that gets data from a device.
The form of the data is csv. Below is the data value.
1,1.635946,1.636609,1.640240,1.636091
2,1.642825,1.640267,1.639013,1.636568
3,1.636835,1.636022,1.637664,1.637144
4,1.641332,1.641166,1.637950,1.640760
5,1.636041,1.637437,1.640702,1.633678
But I want the data in json format. So I tried using an online converter and got the following values:
[
{
"1": 2,
"1.635946": 1.642825,
"1.636609": 1.640267,
"1.640240": 1.639013,
"1.636091": 1.636568
},
{
"1": 3,
"1.635946": 1.636835,
"1.636609": 1.636022,
"1.640240": 1.637664,
"1.636091": 1.637144
}
]
What parts of my code should I modify if I want to get these values?
Below is my code.
var Timer;
var i = 0 ;
setTimeout(function(){
Timer = setInterval(function(){
port.write(meascommand+'\n');
i++;
if(i==5){
clearInterval(Timer);
}
},5000);
},1000);
port.on('data',function(devicevalue){
arrayvalue = devicevalue.toString();
eachvalue = arrayvalue.split(';');
var results = [];
var index = i ;
var ch0value = eachvalue[0] ;
var ch1value = eachvalue[1] ;
var ch2value = eachvalue[2] ;
var ch3value = eachvalue[3] ;
results[0] = index ;
results[1] = ch0value ;
results[2] = ch1value ;
results[3] = ch2value ;
results[4] = ch3value ;
console.log(results);
fs.appendFile(file,results+'\r\n',function(err){
if(err)
console.log(err);
});
});
};
function processFiles(files) {
var file = files[0];
var reader = new FileReader();
reader.onload = function (e) {
var output = document.getElementById("fileOutput");
var texto = e.target.result;
csvJSON(texto);
};
reader.readAsText(file);
}
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers;
for (var i = 0; i < lines.length; i++) {
headers = lines[i].split("\n");
}
var cont = 0;
for (var i = 0; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split("\n");
for (var j = 0; j < headers.length; j++) {
obj[cont] = currentline[j];
}
cont++;
result.push(obj);
}
return JSON.stringify(result); //JSON
}
Try to use below code. Credits go to: https://gist.github.com/iwek/7154578
NOTE: split(","). If lines contains , snippet won't work. But thats not the case in your data, as far as I can see.
function csvJSON(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}
I would like to encrypt URL GET parameters that all links will be something like below.
Encrypted in html:
home.htm?ecryptParam=aajsbjabsjvyhasyayasy
Actual:
home.htm?fName=samir&lName=vora
At the same time, in controller class it will have the decrypted value automatically and I can retrieve it from request. e.g.:
Link:
home.htm?ecrypt=SKJIIU86iuGkJGkJFHGBVBVn
Controller class:
request.getParameter("fName"); will return samir
Samir, If you passing value as encrypt format in url and you will retrieve using request param as encrypt format, and you will need to decrypt those values for that please refer this link "http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html". It might help.
While you really should handle security backend, simple text obfuscation can be easily achived in JavaScript.
Here is an example:
//Encrypter class
var stringEncrypter = (function() {
function stringEncrypter() {}
stringEncrypter.encrypt = function(str) {
str = str.toString();
var returnCode = [];
for (var strIndex = 0; strIndex < str.length; strIndex++) {
var element = str[strIndex];
//Push with bitwise offset (or remove the bitwise offset here and in decrypt)
returnCode.push(element.charCodeAt(0) << this.off);
}
//return a joined string
return returnCode.join(this.splitter);
};
stringEncrypter.decrypt = function(str) {
var list = str.split(this.splitter);
var returnCode = "";
for (var strIndex = 0; strIndex < list.length; strIndex++) {
var element = list[strIndex];
//Push with bitwise offset (or remove the bitwise offset here and in encrypt)
returnCode += String.fromCharCode(parseInt(element) >> this.off);
}
return returnCode;
};
stringEncrypter.encryptUrl = function(url) {
if (url.substr(url.indexOf("?") >= 0)) {
var str = url.substr(url.indexOf("?") + 1);
if (str.lastIndexOf("#") >= 0) {
str = str.substr(0, str.lastIndexOf("#"));
}
var params = str.split("&");
for (var paramIndex = 0; paramIndex < params.length; paramIndex++) {
var param = params[paramIndex].split("=");
param[0] = this.encrypt(param[0]);
param[1] = this.encrypt(param[1]);
params[paramIndex] = param.join("=");
}
url = url.substr(0, url.indexOf("?") + 1) + params.join("&") +
(url.lastIndexOf("#") >= 0 ? url.substr(url.lastIndexOf("#")) : "");
}
return url;
};
stringEncrypter.decryptUrl = function(url) {
if (url.substr(url.indexOf("?") >= 0)) {
var str = url.substr(url.indexOf("?") + 1);
if (str.lastIndexOf("#") >= 0) {
str = str.substr(0, str.lastIndexOf("#"));
}
var params = str.split("&");
for (var paramIndex = 0; paramIndex < params.length; paramIndex++) {
var param = params[paramIndex].split("=");
param[0] = this.decrypt(param[0]);
param[1] = this.decrypt(param[1]);
params[paramIndex] = param.join("=");
}
url = url.substr(0, url.indexOf("?") + 1) + params.join("&") +
(url.lastIndexOf("#") >= 0 ? url.substr(url.lastIndexOf("#")) : "");
}
return url;
};
return stringEncrypter;
}());
//Bitwise offset. Completely optional
stringEncrypter.off = 2;
stringEncrypter.splitter = "|";
//Encrypt a string
console.log(stringEncrypter.encrypt("Testing123"));
//Decrypt a string
console.log(stringEncrypter.decrypt(stringEncrypter.encrypt("Testing123")));
//Decrypt a url
console.log(stringEncrypter.encryptUrl("www.test.dk?test=45"));
//Encrypt a url
console.log(stringEncrypter.decryptUrl(stringEncrypter.encryptUrl("www.test.dk?test=45")));
//Decrypt a url with #
console.log(stringEncrypter.encryptUrl("www.test.dk?test=45#title1"));
//Encrypt a url with #
console.log(stringEncrypter.decryptUrl(stringEncrypter.encryptUrl("www.test.dk?test=45#title1")));
Is this possible to do, or how do I do it?
arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"
arrayElement.JSONkey = "value1" // Can I do this or how would I?
So arrayElement.JSONkey is the same as arrayElement.ident.Lesson.Value
arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"
deepRef(arrayElement, JSONkey, "value1");
function deepRef(ref, key, value) {
var segments = key.split("."),
n = segments.length;
for (var i=0, skey; i<n; i++) {
skey = segments[i];
if (i < n - 1) {
ref[skey] = {};
ref = ref[skey];
} else {
ref[skey] = value;
}
}
}