Skip to main content

Posts

Showing posts with the label Javascript

App: Calculate your job experience or age in years

Usually, all of those who works have to put years of experience on their resume or curriculum vitae. But 90% people put it wrong when they convert their experience in years only. Although they know the exact number of months and years but the conversion, they do is wrong. This happens because there are 12 months while the digit after decimal would be 0-9, i.e., 10 digits. Which means that we have to represent the number of months in terms of year. So here I have provided a small gadget to calculate it. Just put the date when you had started working in the From Date field and put current date in the To Date field. You can also calculate your age as well with this tool by putting your date of birth in the From Date field and put current date in the To Date field. As an alternative, you can use the hassle-free and simple to use  Date Differentiator  micro webapp. Bookmark it so you may easily access it later.

JS: Insertion Sort

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. Here is a practical implementation of it.     

JS: Fibonacci sequence

The first 21 Fibonacci numbers F n for n = 0, 1, 2, ..., 20 are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765. The sequence can also be extended to negative index n using the re-arranged recurrence relation F n-2 = F n - F n-1 So here is a sample program to generate Fibonacci series or sequence.  

JS: Open on InAppBrowser for Cordova/Phonegap based mobile application

There is requirement when we want to load the external link inside our mobiapp. So to ease the process I have created a class that will help in achieving it. You just have to create an instance of it. That's it! var OpenOnInAppBrowser = function () { window.name = "projectname"; var iabRef = null; window.notifier = { flag: false, start: function() { if(!window.notifier.flag) { window.notifier.flag = true; navigator.notification.activityStart('Loading!', 'Please wait ...'); } }, stop: function() { window.notifier.flag = false; navigator.notification.activityStop(); } } var iabLoadStart = function (event) { console.log('OpenOnInAppBrowser -> iabLoadStart: ' + event.type + ' - ' + event.url); window.notifier.st...

JS: Minimalist Templator

This is an evolved version of the previous CTemplator classes that you can refer at Templator , Advanced Templator . Basically in this, the rarely used methods have been chopped-off and the tagging method with '@' has been deprecated because it is limited to only 10 items array. You can say, it's a loop hole earlier due to which if array items exceeds this limit then while replacing the tag initial tags overwrites the exceeded tags. So this new version came into picture; that have only 2 methods where 'fitIn' uses serial-number or index value while the other one 'fixIn' uses key value of the hash-map array. /* @file CTemplator.js @author Abhishek Kumar @email akbittu@gmail.com */ window.CTemplator = { fitIn: function (template, arglist) { var tag, output = template; for (var i = 0, len = arglist.length; i

JS: Advanced Templator

Here is the advanced version of template manipulating class CTemplator in javascript. The basic version can replace just one key-tag in the provided template per item in hastable. While this advanced class having the method CTemplator.fitIn4() can update any occurrence of the key-tags in the provided template and replaces them with the related item in the hashtable. That means, say a template have multiple tag of name then all of them will get replaced by single item in the hashtable. /* @file CTemplator.js @author Abhishek Kumar @email akbittu@gmail.com */ var CTemplator = { fitIn1: function (template, hashtable) { var key, tag, output = template; for (key in hashtable) { tag = "[" + key + "]"; output = output.replace(tag, hashtable[key]); } return output; }, fitIn2: function (template, arglist) { var output = template; for (var i = 0; i < arglist.length; i++) { ...

JS: Javascript Class/File Dependency Handler

Here is the dependency class based on singleton design pattern in javascript. This class can handle all the javascript file dependencies from inside the script. That means you don't need to include the file in the script tag under html head. In fact, you can use it just like import/include/require statements in other language. It's usage is very simple. You just have to declare the class object which would be global since it is a singleton class, then call the import method to include the javascript file on which current file or method is dependent; then listen for the success event on which you can use that imported class/file method as specified. /* @file CDependency.js @author Abhishek Kumar @email abhishek.kumar@intigate.com */ function CDependency() { if (arguments.callee._singletonInstance) { return arguments.callee._singletonInstance; } arguments.callee._singletonInstance = this; var list = []; // jquery based method var jqloa...

JS: Phonegap/Cordova integration with JqueryMobile

Here is the standard way of loading and detecting Cordova & JqueryMobile framework. /* @author Abhishek Kumar @email akbittu@gmail.com */ var app = { deviceReadyDeferred, jqmReadyDeferred, initialize: function() { this.deviceReadyDeferred = $.Deferred(); this.jqmReadyDeferred = $.Deferred(); this.bindEvents(); }, bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); $(document).one("mobileinit", this.onJQMReady); $.when(this.deviceReadyDeferred, this.jqmReadyDeferred).then(this.onCompleteReady); }, onDeviceReady: function() { app.deviceReadyDeferred.resolve(); }, onJQMReady: function() { app.jqmReadyDeferred.resolve(); }, onCompleteReady: function() { main(); } };

JS: Singleton based Cache

Here is the cache class based on singleton design pattern in javascript. There are 2 alternatives present in here. /* @file CCache.js @author Abhishek Kumar @email akbittu@gmail.com */ // Singleton Class - Alternate#1 var CCache = (function () { var instance; function init() { var cache = {}; return { getCacheItem: function (itemId) { return cache[itemId]; }, setCacheItem: function (itemId, itemVal) { cache[itemId] = itemVal; } }; }; return { getInstance: function () { if (!instance) { instance = init(); } return instance; } }; })(); // Usage var oCache = CCache.getInstance(); oCache.setCacheItem('dataHolder', 'a new item value'); config.log(oCache.getCacheItem('dataHolder')); // Singleton Class - Alternate#2 function CCache() { if (arguments.callee._sing...

JS: Templator

Here is the template manipulating class in javascript. There are 2 methods present in this class for doing the same thing but in 2 different ways. Based on requirement or comfort-level, the particular method can be chosen. /* @file CTemplator.js @author Abhishek Kumar @email akbittu@gmail.com */ // Class var CTemplator = { fitIn1: function (template, hashtable) { var key, tag, output = template; for (key in hashtable) { tag = "[" + key + "]"; output = output.replace(tag, hashtable[key]); } return output }, fitIn2: function (template, arglist) { var output = template; for (var i = 0; i < arglist.length; i++) { output = output.replace(new RegExp('@' + i, 'g'), arglist[i]); } return output; } } // Usage CTemplator.fitIn1('<a href="#[Link]" title="[Title]">[Content]</a>', { Link:'...

JS: Communication Gateway

There is always a need of a single point of contact in every communication matter in real life. In the same way, in coding also when this technique is applied, it makes the communication easier to handle and to track the information flow. So here is an implementation of the same real life scenario in javascript. This technique can be extended or implemented in any language wherever there is a need of controlled communication. It's working is very straight forward. All the request calls are pushed into the queue. Queue works on First-In-First-Out (FIFO) logic. So as the requests get processed, it would be removed from the queue and the next request would be sent fro processing. This works automatically until the request queue become empty. /* @file communicator.js @author Abhishek Kumar @email akbittu@gmail.com */ // Basic function Communicator() { var requests = []; this.request = function (url, data, listener) { var package = { url: url, ...

JS: Get base URL with level-back feature

At many times we need to extract the root of a multi-level path URL. So here is the code to do that. /* @author Abhishek Kumar @usage getBaseUrl(n); where n = number of ../ @example For URL → http://localhost/workshop/showcase/shell/data/AKI/app/index.html Result of getBaseUrl() → http://localhost/workshop/showcase/shell/data/AKI/app/ getBaseUrl(3) → http://localhost/workshop/showcase/shell/ */ function getBaseUrl(levelback) { var basePath = window.location.pathname; basePath = basePath.substring(0, basePath.lastIndexOf('/')); var list = basePath.split('/'); for (var i = 0; i < levelback && list.length > 0; i++) { list.pop(); } return window.location.protocol + '//' + window.location.host + list.join('/') + '/'; }

JS: The complete code example of Crypto.js (DES)

For one of the project I was trying to use crypto.js but I found that the Quick-start Guide have some deficiency in terms of library usage. So I am writing it here as a useful note for memory recap. <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></script> <script> var encrypted = CryptoJS.DES.encrypt("The secret message", "secret_key"); var e_msg = encrypted.toString(); console.log(e_msg); var decrypted = CryptoJS.DES.decrypt(e_msg, "secret_key"); var d_msg = decrypted.toString(CryptoJS.enc.Utf8); console.log(d_msg); </script>