Variable Changing Value On It's Own Javascript - javascript

I have formatted a string to be used within a putItem call using DynamoDB SDK. This Long string (item I would like to add has lots of information) is stored in a variable and I should be able to use it within the Item {} part of dynamoDdb.putItem() .
However, when the variable is actually processed extra characters are added in causing the formatting to be incorrect for how an Item {} should look.
See below:
itemToAdd = JSON.stringify(marshalled2, null,2)
newString = itemToAdd.slice(4, itemToAdd.length-1)
correct = newString.replaceAll(" ", '');
correct2 = correct.replaceAll("\n", "")
//console.log(newString)
var params = {
TableName: "Music",
Item: {
correct2
}
}
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
My issue is that the string correct2 has no slashes or \n in it as I have removed them prior however when the string correct2 is used with the params object extra black slashes have been added into the string messing up the formatting.
An example of the string contained in correct2 would be as follows:
"AlbumData":{"S":"2B5C1828-6077-4ED1-89AD-13602A7AC08D"},"label":{"S":"EvolutionMediaMusic"},"Album":{"S":"BleakDrama"},"albumCode":{"S":"EMM101"},"releaseDate":{"S":"21/06/2016"},"description":{"S":"Contemporarynoircrimedrama:coldandforebodingcueswithtwistedsyntheticatmospheresandpulsingrhythms."},"credits":{"S":""},"tracks":{"L":[{"M":{"id":{"S":"984CA56C-3FA8-4F49-9C9E-3F96C237EE7E"},"trackNo":{"S":"1"},"albumCode":{"S":"EMM101"},"albumName":{"S":"BleakDrama"},"lengthOfTrack":{"S":"02:30"}, //more fields would go here
If I was to copy the text above and insert it into where the variable is there is no issue with the code. Using the javascript debug console the variable correct2 is set to the above however
params.Item = \"AlbumData\":{\"S\":\"2B5C1828-6077-4ED1-89AD-13602A7AC08D\"},\"label\":{\"S\":\"EvolutionMediaMusic\"},\"Album\":{\"S\":\"BleakDrama\"}, // and the patten repeats
What is causing the difference between correct2 and params.Item? I would expect them to be the same.

Dude, its ok, just relax. Its called escaping
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences
https://www.geeksforgeeks.org/how-to-use-escape-characters-to-correctly-log-quotes-in-a-string-using-javascript/

Related

Is there a way to use filterByFormula in Airtable to filter by a specific field value by date without it defaulting to the first entry in the table?

I am trying to filter by date to get one specific record, of which the Name field should = 8/01/2022. (I used the Name field in Airtable to place the dates. I set the field type to date.)
The issue I'm having is that although it seems to work fine, it basically ignores the specification for the date and instead returns the first value in the table.
This is what I have for getting the data from airtable.
let isoDate = new Date("08/01/2022").toISOString().slice(0, 10);
const base = new Airtable({ apiKey: apiKey }).base(baseID);
base("tabledata")
.select({
filterByFormula: `"DATESTR({Name})='${isoDate}'"`,
view: "Grid view",
})
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function (record) {
let newEl = {
date: record.get("Name"),
game: record.get("games"),
};
setData(newEl);
});
try {
fetchNextPage();
} catch {
return;
}
},
function done(err) {
if (err) {
console.error(err);
return;
}
}
);
and this is the record that is retrieved:
{date: '2022-07-29', game: Array(6)}
date: "2022-07-29"
game: Array(6)
'2022-07-29' is the name of the first field in my table.
when I print the ISOString at any point I get 8/01/2022.
It turned out the issue was with this line
filterByFormula: `"DATESTR({Name})='${isoDate}'"`
I ended up changing the data type in airtable to short text since it doesn't really matter when it comes to the way I'm using it. So DATESTR was no longer needed, but I was still dealing with the issue no matter what I did.
Here is my new isoDate variable declaration:
let isoDate = new Date().toLocaleDateString();
It took me a while before I realized that it would make sense to try this line and see if it would work: "{Name})='8/01/2022'"
And it did! So clearly the issue was with the isoDate string.
After searching for a while I found this setup which worked:
filterByFormula: `{Name} = "${isoDate}"`
The main difference is the double quotes around the variable and no extra quotes around the full string.

javascript fix json with regex

Hi All I want to fix my json
I got strings without " and I need to bring them back somehow
I have found a regex that find some of the texts but still its not returning the right result
var newString2 = j.replace(/[a-zA-Z0-9#.,_+]*[^:][\w]/g, function(x){
return '"'+x+'"';
});
my json
{
User_Story_UI_24:
{
Env:Staging,
UserName:9ascf#gmail.com,
Password: User#SDF45sdfg,
Supplier:Xxxxx,
SupplierWebSite:www.xxxx.com,
SupplierPhone:+111 2223334440,
SupplierAddress:sss asf21, asf asf, saf
}
}
the epcepted result
{
"User_Story_UI_24":
{
"Env":"Staging",
"UserName":"9ascf#gmail.com",
"Password": "User#SDF45sdfg",
"Supplier":"Xxxxx",
"SupplierWebSite":"www.xxxx.com",
"SupplierPhone":"+111 2223334440",
"SupplierAddress":"sss asf21, asf asf, saf"
}
}
I've used a regex that detects the lines in your regex (assuming it's in the same format as in your example).
Then it uses a function that receives the matching groups as its parameters and creates two quoted strings accordingly.
The only change I made to your input was adding a comma after the last property, to keep all the lines in the same format:
const input = `{
User_Story_UI_24:
{
Env:Staging,
UserName:9ascf#gmail.com,
Password: User#SDF45sdfg,
Supplier:Xxxxx,
SupplierWebSite:www.xxxx.com,
SupplierPhone:+111 2223334440,
SupplierAddress:sss asf21, asf asf, saf,
}
}`;
let ans = input.replace(/(\w+?): ?([+\w #.,]+),/g, (row, prop, value) => {
return `"${prop}": "${value}",`;
});
console.log(ans);

REST Firebase request via qwest

i have a realtime database from firebase with 9386 datasets in it (but that might change in the future)
thats why i want to know whats the "key" of the last event.
thats how i'm currently trying to get it to know: (with npm module qwest)
let order = "\u0022\u0024key\u0022";
let dbUrl = "https://example.firebaseio.com/users.json?limitToLast=1&orderBy=";
let end;
qwest.get(dbUrl + order)
.then(function(xhr, response) {
console.log(response);
end = response[9386]["username"];
console.log(end);
});
first question: why do i have to escape the quotation marks and the dollarsign?
second question: how can i get the "key" of the last item i'm checking for in the json (limitToLast=1).
json response looks like this:
{"9386":
{
"fromListA":"1",
"fromListB":"0",
"id":"9939",
"lastChecked":"2019-05-09 03:18:05",
"userid":"123456789",
"username":"username"
}
}
and i want to get the "9386" in a variabel.
Since you chose to use " to limit the dbUrl string, you can't use " inside that string's value without escaping it. A simpler way to define the string is to use ' to delimit it:
let dbUrl = 'https://example.firebaseio.com/users.json?limitToLast=1&orderBy="$key"';
To get the key of the object, use something like:
Object.keys(response)[0]
var response = {"9386":
{
"fromListA":"1",
"fromListB":"0",
"id":"9939",
"lastChecked":"2019-05-09 03:18:05",
"userid":"123456789",
"username":"username"
}
}
console.log(Object.keys(response)[0]);

How to maintain character encoding of database query

Within a Firefox extension, I am opening a database file and displaying the database contents. However, Firefox is showing some characters incorrectly, presumably due to an encoding issue. I tried opening the database file in SQLite Database Browser and the name column displayed correctly.
How can I properly handle this text string so that it outputs characters as I intended (e.g., Caché rather than Caché)?
Code snippet which reproduces this problem:
var StorageService = Cc["#mozilla.org/storage/service;1"]
.getService(Ci.mozIStorageService);
_Conn = StorageService.openDatabase(file);
var stmt = _Conn.createStatement("Select name from data");
var RunQuery = function () {
return {
arr: [],
handleResult: function (aResultSet) {
var row = aResultSet.getNextRow();
alert([row.getResultByName("name"), 'Cach\u00E9']);
},
handleError: function (aError) {},
handleCompletion: function (aReason) {}
};
};
stmt.executeAsync(RunQuery());
Output: Caché,Caché
Intended Output: Caché,Caché
Assuming IDX_NAME is the index of column name, then
alert([row.getUTF8String(IDX_NAME), 'Cach\u00E9']);
will give you the intended result.
This seems to work:
var converter = Cc["#mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
alert([converter.ConvertToUnicode(row.getString("name")), 'Cach\u00E9']);

Backbone.js - Filter a Collection based on an Array containing multiple keywords

I'm using Backbone.js/Underscore.js to render a HTML table which filters as you type into a textbox. In this case it's a basic telephone directory.
The content for the table comes from a Collection populated by a JSON file.
A basic example of the JSON file is below:
[{
"Name":"Sales and Services",
"Department":"Small Business",
"Extension":"45446",
},
{
"Name":"Technical Support",
"Department":"Small Business",
"Extension":"18800",
},
{
"Name":"Research and Development",
"Department":"Mid Market",
"Extension":"75752",
}]
I convert the text box value to lower case and then pass it's value along with the Collection to this function, I then assign the returned value to a new Collection and use that to re-render the page.
filterTable = function(collection, filterValue) {
var filteredCollection;
if (filterValue === "") {
return collection.toJSON();
}
return filteredCollection = collection.filter(function(data) {
return _.some(_.values(data.toJSON()), function(value) {
value = (!isNaN(value) ? value.toString() : value.toLowerCase());
return value.indexOf(filterValue) >= 0;
});
});
};
The trouble is that the function is literal. To find the "Sales and Services" department from my example I'd have to type exactly that, or maybe just "Sales" or "Services". I couldn't type "sal serv" and still find it which is what I want to be able to do.
I've already written some javascript that seems pretty reliable at dividing up the text into an array of Words (now updated to code in use).
toWords = function(text) {
text = text.toLowerCase();
text = text.replace(/[^A-Za-z_0-9#.]/g, ' ');
text = text.replace(/[\s]+/g, ' ').replace(/\s\s*$/, '');
text = text.split(new RegExp("\\s+"));
var newsplit = [];
for (var index in text) {
if (text[index]) {
newsplit.push(text[index]);
};
};
text = newsplit;
return text;
};
I want to loop through each word in the "split" array and check to see if each word exists in one of the key/values. As long as all words exist then it would pass the truth iterator and get added to the Collection and rendered in the table.
So in my example if I typed "sal serv" it would find that both of those strings exist within the Name of the first item and it would be returned.
However if I typed "sales business" this would not be returned as although both the values do appear in that item, the same two words do not exist in the Name section.
I'm just not sure how to write this in Backbone/Underscore, or even if this is the best way to do it. I looked at the documentation and wasn't sure what function would be easiest.
I hope this makes sense. I'm a little new to Javascript and I realise I've dived into the deep-end but learning is the fun part ;-)
I can provide more code or maybe a JSFiddle if needed.
Using underscore's any and all make this relatively easy. Here's the gist of it:
var toWords = function(text) {
//Do any fancy cleanup and split to words
//I'm just doing a simple split by spaces.
return text.toLowerCase().split(/\s+/);
};
var partialMatch = function(original, fragment) {
//get the words of each input string
var origWords = toWords(original + ""), //force to string
fragWords = toWords(fragment);
//if all words in the fragment match any of the original words,
//returns true, otherwise false
return _.all(fragWords, function(frag) {
return _.any(origWords, function(orig) {
return orig && orig.indexOf(frag) >= 0;
});
});
};
//here's your original filterTable function slightly simplified
var filterTable = function(collection, filterValue) {
if (filterValue === "") {
return collection.toJSON();
}
return collection.filter(function(data) {
return _.some(_.values(data.toJSON()), function(value) {
return partialMatch(value, filterValue);
});
});
};
Note: This method is computationally pretty inefficient, as it involves first looping over all the items in the collection, then all the fields of each item, then all words in that item value. In addition there are a few nested functions declared inside loops, so the memory footprint is not optimal. If you have a small set of data, that should be OK, but if needed, there's a number of optimizations that can be done. I might come back later and edit this a bit, if I have time.
/code samples not tested

Categories