Monday, November 12, 2012

heredoc in javascript

Javascript doesn't have built-in  "heredoc" operator or feature. So, if we need it, we have to implement it by ourself. Here is one of possible way to implement heredoc-style functionality.

1. To prevent javascript from checking syntax and issuing errors we create desired string as a comment within a function body:

var foo = function(){/*
blah blah blah
*/};  

2. Use built-in "toString()" function to extract this string:

var bar = foo.toString();

But toString() returns everything, not only function body:
"function (){/*blah blah blah/*}"
So, we need to extract only the part we need. I can suggest 2 possible ways:

a) foo.toString().slice(14, -3);
b) foo.toString().split(/\n/).slice(1, -1).join('\n');

if you select the second version, you must start and end your string with new line:
function (){/*
blah
blah blah
*/}

Generic functions:
1)
function heredoc(fn) {
return fn.toString().slice(14, -3);
}

2)
function heredoc(fn) {

return fn.toString().split(/\n/).slice(1, -1).join('\n');
}


There is no difference in performance between these two solutions. I prefer the first one as it looks  more simple, and short strings can be written in one line. But it matter of test.

Usage:

var foo = function(){/*
blah
blah blah

*/}; 


var bar = heredoc(foo);



console.log(bar); // blah 
                            blah blah

------------------------------------------------------------------------------------

Enjoy.
var zzz = function(){/*
var xxx
 = 
 
var zzz = function(){/*
var xxx
 = 
 'aaa';*/};var zzz = function(){/*
var xxx
 = 
 'aaa';*/};

Tuesday, August 21, 2012

Convert Javascript Object with Functions to String and String to Object

A lot of javascript developers want to serialize object and save it on server or local storage, in order to take this string later and convert it back into an object. It's not a big deal if your object has only member variables. You simply use javascript native JSON object to serialize/parse  your custom object.
But it doesn't work if your object has member function. Native JSON ignores functions. Recently I made a brief search and wasn't able to find acceptable way to manage this problem. So, I spent some time on development a very small plugin, which can help many developers to create simple and elegant applications. All plugin is just 20 lines of code, so I show it right here

----------------------------------------------------------------------------
var JSONfn;
if (!JSONfn) {
    JSONfn = {};
}
(function () {
JSONfn.stringify = function(obj) {
return JSON.stringify(obj,function(key, value){
return (typeof value === 'function' ) ? value.toString() : value;
});
}
JSONfn.parse = function(str) {
return JSON.parse(str,function(key, value){
if(typeof value != 'string') return value;
return ( value.substring(0,8) == 'function') ? eval('('+value+')') : value;
});
}
}());
------------------------------------------------------------------------------------------


       var str = JSONfn.stringify(obj);
var obj = JSONfn.parse(str);

See live demo at http://www.eslinstructor.net/jsonfn/

Enjoy!



Sunday, February 19, 2012

pretty-data

pretty-data is a small, simple and powerfull nodejs plugin to beautify ( pretty print ) or minify XML, JSON or CSS text.
This script also has client-side version known as vkBeautify


see demo at:   http://www.eslinstructor.net/pretty-data/

get source from:  https://github.com/vkiryukhin/pretty-data

Sunday, January 22, 2012

vkBeautify - XML, JSON and CSS Beautifier

http://www.eslinstructor.net/vkbeautify/

vkBeautify is a small, simple and powerfull javascript plugin to beautify XML, JSON or CSS text.
This script works either as a client-side javascript or as a nodeJS plagin.

Consider to use it if you:
  • create  XML, JSON or CSS  text manually at client side
  • create  XML, JSON or CSS  with tool, but let user to check or edit the result manually
  • get  XML, JSON or CSS  data from web service and check or edit it before or after processing the data
  • other cases when you need to create, edit or check content of an  XML, JSON or CSS  file.
This plugin:
  1. less then 2k if minified
  2. has only one function
  3. takes less then 5 mSec to process 50K text
    ( Celeron 2.2 GHz, 2.0GB, any of 3 major browsers)