How to identify which serialized format is this? - serialization

I'm trying to parse some CSV file that has some elements with this format (Below I show 2 examples)
{ :{a:{-} b:{c:{6} d:{-} } e:{-} f:{-} } }
{ a:{b:{c:{123} } } }
Below in vertical form only in order for you to visualize better.
{ : || {
{ || a:{
a:{-} || b:{
b:{ || c:{123}
c:{6} || }
d:{-} || }
} || }
e:{-} ||
f:{-} ||
} ||
} ||
At first sight I thought it was JSON format, but is not JSON. I checked the formats shown here but I didn't find something similar.
Is there a way to identify if it is some known format similar to JSON? Thank in advance.

Related

Vue: Refactoring a computed property

I have the following computed property that works as expected. I want it to return true if any of the 3 data properties are empty strings and want it return true if any of the strings are "n/a":
appNamesInvalid() {
if (!this.preferredAppName1 || !this.preferredAppName2 || !this.preferredAppName3) {
return true;
}
if (this.preferredAppName1.toLowerCase().includes('n/a') || this.preferredAppName2.toLowerCase().includes('n/a') || this.preferredAppName3.toLowerCase().includes('n/a')) {
return true;
}
return false;
},
I had originally tried to do this but this wasn't working as expected and I can't tell why:
appNamesInvalid() {
let appNames = [this.preferredAppName1, this.preferredAppName2, this.preferredAppName3];
appNames.forEach(appName => {
if (appName == "" || appName.toLowerCase().includes("n/a")) {
return true;
}
}
return false;
},
Is there a cleaner way of refactoring the working code in the first block?
Try using Array.prototype.some to return a boolean based on your specific criteria:
appNamesInvalid() {
let appNames = [this.preferredAppName1, this.preferredAppName2, this.preferredAppName3];
return appNames.some(appName => {
return appName === "" || appName.toLowerCase().includes("n/a");
}
},
That would return true if for any aoo either appName === "" or appName.toLowerCase().includes("n/a") is true.
Hopefully that helps!

How to use computedFrom with multiple properties in Aurelia

I have the following code:
#computedFrom(['id', 'isDiscontinued'])
get disableAdd(){
return this.id === '0' || this.isDiscontinued;
}
This throws the following error: aurelia-binding.js:2580 Uncaught TypeError: this.input.charCodeAt is not a function
This however works:
#computedFrom('id')
get disableAdd(){
return this.id === '0' || this.isDiscontinued;
}
But I need two computedFrom fields, what am I doing wrong?
#computedFrom('id', 'isDiscontinued')
get disableAdd(){
return this.id === '0' || this.isDiscontinued;
}
Didn't need []

i18next version 1.7.2 cannot read property 'status' of null error

I had a difficulty in using i18next to get i18n'ed versions of strings.
I am having cannot read property 'status' of null error on line 1381 of i18next-1.7.2.js
my json files are located in locales/translation-en.json,... etc.
My init code is like below:
i18n.init({ lng:"en" , resGetPath: "locales/__ns__-__lng__.json", ns:"translation"},
function(t) {
text.nodeValue = i18n.t("app.name");
....
});
Cannot read property 'length' of null (javascript) didn't apply for my case.
When I inspected the source code of i18next-1.7.2 line:1382
It seems that there is a missing null check in the code.
error is null and error.status is being checked.
So I added a simple null check.
Here is the change in the code:
Old code (from original i18next-1.7.2.js) :
if (error.status == 200) {
// file loaded but invalid json, stop waste time !
f.log('There is a typo in: ' + url);
} else if (error.status == 404) {
f.log('Does not exist: ' + url);
} else {
f.log(error.status + ' when loading ' + url);
}
done(error, {});
proposed code:
if (error == null){
done(error, {});
}else{
if (error.status == 200) {
// file loaded but invalid json, stop waste time !
f.log('There is a typo in: ' + url);
} else if (error.status == 404) {
f.log('Does not exist: ' + url);
} else {
f.log(error.status + ' when loading ' + url);
}
done(error, {});
}
When I changed the code like above, it didn't work.
When I included jquery.js file it worked.
Developer of i18next Jamuhl knows about the subject.

Format Textbox input for phone number MVC

I am simply using a #Html.TextBoxFor(m => m.PhoneNumber, new { id = "phoneNo")
I am using a regex to limit it to 10 numbers only.
Is there a way I can format the textbox to appear like (555) 444-3333 while they type, but in the model it will simply be passing the 10 numbers, like 5554443333? I meant to automatically create those brackets and - while also checking using regex if they entered 10 numbers?
You can do it with jquery as Matt said at his comment, stated at this question of the site:
Phone mask with jQuery and Masked Input Plugin
Or with plain javascript, as explained by xxx here with alternatives too:
Mask US phone number string with JavaScript
List of alternatives coded for a example input called "phone":
Example code with plain javaScript:
document.getElementById('phone').addEventListener('input', function (e) {
var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : '');
});
Example code with jQuery but without adding any new dependence():
$('#phone', '#example-form')
.keydown(function (e) {
var key = e.which || e.charCode || e.keyCode || 0;
$phone = $(this);
// Don't let them remove the starting '('
if ($phone.val().length === 1 && (key === 8 || key === 46)) {
$phone.val('(');
return false;
}
// Reset if they highlight and type over first char.
else if ($phone.val().charAt(0) !== '(') {
$phone.val('('+$phone.val());
}
// Auto-format- do not expose the mask as the user begins to type
if (key !== 8 && key !== 9) {
if ($phone.val().length === 4) {
$phone.val($phone.val() + ')');
}
if ($phone.val().length === 5) {
$phone.val($phone.val() + ' ');
}
if ($phone.val().length === 9) {
$phone.val($phone.val() + '-');
}
}
// Allow numeric (and tab, backspace, delete) keys only
return (key == 8 ||
key == 9 ||
key == 46 ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
.bind('focus click', function () {
$phone = $(this);
if ($phone.val().length === 0) {
$phone.val('(');
}
else {
var val = $phone.val();
$phone.val('').val(val); // Ensure cursor remains at the end
}
})
.blur(function () {
$phone = $(this);
if ($phone.val() === '(') {
$phone.val('');
}
});
Example code with jQuery using Masked Input Plugin:
$("#phone").mask("(99) 9999?9-9999");
$("#phone").on("blur", function() {
var last = $(this).val().substr( $(this).val().indexOf("-") + 1 );
if( last.length == 3 ) {
var move = $(this).val().substr( $(this).val().indexOf("-") - 1, 1 );
var lastfour = move + last;
var first = $(this).val().substr( 0, 9 );
$(this).val( first + '-' + lastfour );
}
});

Browser loading php script after submitting form using jQuery Form plugin

I'm trying to implement a form using the jQuery Form plugin. The form has three text fields and a file input and I am validating the form in the beforeSend callback. The problem is, whether the validation passes or not, the php script that handles the file upload gets loaded in the browser, which, obviously is not what I want to happen - I need to stay on the form's page.
You can take a look at the form and it's dependent files at http://www.eventidewebdesign.com/public/testUpload/. Indexing is on for that directory, so you can take a look at all of the related files. The form itself is on testUpload.php.
I'd appreciate it if someone could take a look at my code and help me figure out what's going on here.
Please write the following script instead of your, this will work.
<script>
$(document).ready( function() {
// Initialize and populate the datepicker
$('#sermonDate').datepicker();
var currentDate = new Date();
$("#sermonDate").datepicker('setDate',currentDate);
$("#sermonDate").datepicker('option',{ dateFormat: 'mm/dd/yy' });
/*
* Upload
*/
// Reset validation and progress elements
var formValid = true,
percentVal = '0%';
$('#uploadedFile, #sermonTitle, #speakerName, #sermonDate').removeClass('error');
$('#status, #required').empty().removeClass();
$('.statusBar').width(percentVal)
$('.percent').html(percentVal);
$('#frmSermonUpload').ajaxForm({
beforeSend: function() {
if (!ValidateUploadForm()) {
formValid = false;
console.log('validateuploadform returned false');
} else {
console.log('validateuploadform returned true');
formValid = true;
}
console.log('in beforeSend. formValid: ' + formValid);
if (!formValid) {
$('#uploadedFile').val('');
return false;
}
},
uploadProgress: function(event, position, total, percentComplete) {
console.log('in uploadProgress function. formValid: ' + formValid);
if (formValid) {
var percentVal = percentComplete + '%';
$('.statusBar').width(percentVal)
$('.percent').html(percentVal);
}
},
complete: function(xhr) {
console.log('in complete function. formValid: ' + formValid);
if (formValid) {
console.log('xhr.responseText: ' + xhr.responseText);
console.log('formValid: ' + formValid);
if (xhr.responseText === 'success') {
$('.statusBar').width('100%');
$('.percent').html('100%');
$('#status').html('Successfully uploaded the sermon.').addClass('successUpload');
// Clear the form
ClearForm();
} else if (xhr.responseText === 'fail') {
$('#status').html('There was a problem uploading the file. Try again.<br>If the problem persists, contact your system administrator.').addClass('errorUpload');
}
}
}
}); // End Upload Status Bar
});
function ValidateUploadForm() {
// Reset errors and clear message
$('#uploadedFile, #sermonTitle, #speakerName, #sermonDate').removeClass('error');
$('#required').empty();
var result = true;
title = $('#sermonTitle').val(),
speaker = $('#speakerName').val(),
date = $('#sermonDate').val(),
fileName = $('#uploadedFile').val();
extension = $('#uploadedFile').val().split('.').pop().toLowerCase();
//if (fileName !== '' && extension !== 'mp3') {
if ((fileName === '') || (extension !== 'mp3')) {
$('#uploadedFile').addClass('error');
$('#required').html('Only mp3 files are allowed!');
return false;
} else if (fileName === '') {
result = false;
} else if (title === '') {
$('#sermonTitle').addClass('error');
result = false;
} else if (speaker === '') {
$('#speakerName').addClass('error');
result = false;
} else if (date === '') {
$('#sermonDate').addClass('error');
result = false;
}
console.log('returning ' + result + ' from the validateuploadform function');
if (!result) { $('#required').html('All fields are required.'); }
return result;
}
function ClearForm() {
$('#uploadedFile, #sermonTitle, #sermonDate, #speakerName').val('').removeClass();
}
</script>