Skip to content

Happy Holidays


Happy Holidays! Be wealthy healthy wise! See you next year 🙂

Free-Vector-Christmas-Pack

Advertisements

Pure JavaScript In-Memory Database


Recently I found Alasql.js – in-memory SQL database available both client and server side (Node.js). It is easy to use:

  • in browser
 <script src="alasql.js"></script>
  • in Node
var alasql = require('alasql');

Supports usual SQL statements, grouping, functions and aggregates. Allows usage of user-defined functions

alasql.userlib.DOUBLE = function(x){return x*2}; 
 db.exec('SELECT a, DOUBLE(a) AS b FROM test1');

Supports promises, …

 db.aexec('SELECT * FROM test').then(function(res){
  console.log(res);
 });

… parameters, compiled statements and transactions. Has own SQL to JavaScript parser and compiler resulting in nice performance. (tests are available here and here).

Also has limitations but authors state that you should succeed in selecting 1 000 000 records or 2 join two tables by 10 000 records in each.

As the version is 0.0.13 (6th Nov 2014) there are (and probably will be) some bugs. But I’ll keep an eye on this project.

Javascript FormData Object makes life easier


Discovering FormData object (supported by XMLHttpRequest level 2) was a relief for me adn I’d like to share it, hoping to be also useful for somebody else.

FormData object allows to easily construct a set of key/value pairs and to send them through XMLHttpRequest send() method to a server. Using it becomes the simplest and the fastest way, facilitating ajax transfer. But it has at least one disadvantage — data cannot be stringified.

Though FormData object has enough methods and properties one can start out from scratch very easily. Using jQuery to shorten the code it will look like:

$.ajax({
  url: "stash.php",
  type: "POST",
  data: new FormData(document.getElementsByTagName('form')[0] || null),
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});

The only code necessary is the constructor with a form element as input parameter. Same with Vanilla JS will be something like:

var request = new XMLHttpRequest();
request.open("POST", "submitform.php");
request.send( new FormData( document.getElementById( "myFormElement" ) ) );

that’s it. Simple, eh?

On the other hand one can create FormData from scratch, without having form element at all:

var formData = new FormData();
formData.append("username", "John Doe");
formData.append("accnum", 123);
var request = new XMLHttpRequest();
request.open("POST", "http://mysrv.com/process.php");
request.send(formData);

Be aware though that appending values will be converted immediately to string. So the above value of the accnum field will be the string “123”. Actually append() method expect one of the following types – Blob, File, or a string. Any other type if threated as a string.

Adding file is also easy. Having an <input type=”file”/> element we could use it like this:

// HTML file input user's choice...
formData.append("userfile", fileInputElement.files[0]);

As easy is also creating Blob

// JavaScript file-like object...
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});
formData.append("webmasterfile", blob);

A Blob is immutable, raw data type – a file-like object. It does not require to be data in a JavaScript-native format. As the File interface is based on Blob both can be appended directly to a FormData object, like this:

data.append("myfile", myBlobObj, "myfilename.txt");

The FormData can be used either to send HTML form data or independently constructed chunks of data. When transmitted and if the form’s encoding type were set to “multipart/form-data”, the same format is used as form’s submit() method would use .

Using FormData with XMLHttpRequest causes every request to be unique, bypassing the cache since the local cache is indexed by URL.

Happy coding

Javascript Array – join() vs toString() method


Javascript Array.toString()  overrides the method inherited from Object to represent array contents as a string. For the purpose the method joins array elements comma separated. Starting from ECMAScript 5 (JavaScript 1.8.5) toString() method can be applied to any object. When toString() is executed it calls the join() method if exists, or Object.prototype.toString() otherwise.

With that knowledge here is a reasonable question: when using arrays which one is better to use?

It turns out that Array.toString() applied on a string array serves the best.

Here is the (jsperf) test case with interesting results. Not only for the method but also for browser’s engines used – 70 ops/sec for IE11 vs almost 35 ops/sec for Firefox. Bellow are mine.

1_Chrome 2_Opera 3_IE11 4_Firefox

JavaScript: Answer To Tricky Interview Questions


This question is listed on June 22, 2014 @ JS: Tricky Interview Questions under part -2: intermediate

Q: If i have a var str = ‘hello world’, how could i get str.reverse() return ‘dlrow olleh’?

Introduced in ECMAScript 5, string could be treated as an array-like object, where individual characters correspond to a numerical index:

return 'cat'[1]; // returns "a"

JavaScript automatically converts primitives (string literals, i.e. quoted, and strings in a non-constructor context i.e., without using the new keyword) to String objects, so we can always apply String object methods.

Analyzing the task probably the first think we’ll notice (at least I did) is the word “reverse” which is an Array method. Starting from here we’ll need one String object method to convert string to an array, the reverse itself, and another Array object method to convert array back to string.

If we had to split string into words I hope that every javascript developer is aware of the existence of String.prototype.split() method.

Definition reads: Splits a String object into an array of strings by separating the string into substrings.
The split method returns a new array. It accept two parameters: split([separator][, limit]). What we are looking for is: “If separator is an empty string, string is converted to an array of characters“.

So let’s apply it:

'hello world'.split('');
> ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]

So far, so good. Next we apply array.reverse() method which “… reverses an array in place. The first array element becomes the last and the last becomes the first.

'hello world'.split('').reverse();
> ["d", "l", "r", "o", "w", " ", "o", "l", "l", "e", "h"]

Last one is again well known – how to combine char array into string. Do you know it?

Good for you!
Yes, it is “The join() method joins all elements of an array into a string.” But be aware that “If [separator is] omitted, the array elements are separated with a comma.” So as a separator we’ll use empty string:

'hello world'.split('').reverse().join('');
 > "dlrow olleh"

Done! Inline.

I hope now you’re ready to answer that question 😉

Could not load file or assembly CefSharp.dll


Trying the C#  System.Windows.Forms.WebBrowser control alternative CefSharp I’ve faced a runtime problem. Nevertheless compilation was successful Windows Forms application crashed on start with error:

Could not load file or assembly “CefSharp.WinForms.dll”

I found a lot of discussions on the topic where some said there is no problem with running application using CefSharp control, while others constantly failing to start it.

It was very teasing getting this error again and again, referencing different versions of CefSharp.dll and CefSharp.WinForms.dll.

The decision

turns out to be very trivial: adding msvcp110.dll and msvcr110.dll into runtime directory wiped out the error. Hope this will help to others too.

HowTo Fix Easily Oracle SQL Developer MSVCR71.DLL Not Found Error


I’ve downloaded latest Windows 32-bit – zip file includes the JDK1.6.0_35 version (currently 3.2.2) of SQL Developer (Oracle). Happily unzipped it in a folder (F:\sqldeveloper in my case) and started sqldeveloper.exe immediately. And immediately got MSVCR71.DLL Not Found Error (images 1 & 2).

MSVCR71.DLL Not Found Error

image 1: MSVCR71.DLL Not Found Error

MSVCR71.DLL Not Found Error

image 2: MSVCR71.DLL Not Found Error

Of course MSVCR71.DLL was missing in my Windows 8 PC. I found this article, describing the error and giving a solution. But I’m not a fan of registry poking, so I’ve asked myself whether there is another way to achieve the same result.

First in my mind came the PATH environment variable. So I’ve opened my Control Panel -> System (image 3) and clicked on Advances system settings. (image 4)

Control Panel

image 3: Control Panel

Advanced Settings

image 4: Advanced Settings

System Properties dialog was brought with Advanced tab selected. Path is in another dialog, which is opened by Environment Variables button click. (image 5)

System Properties

image 5: System Properties

In the lower box (System variables – image 6) is located Path variable (perhaps you’ll need to scroll down a little bit.) Use Edit button and add at the end of Variable value field (lower one) the path to you jdk-jre-bin folder.

Environment Variables

image 6: Environment Variables

Click consecutively all OK buttons to close everything opened and fire sqldeveloper.exe. Now it should work properly.

Path value

image 7: Path value

jre location

image 8: jre location