Publish node module

npm install

//> do changes ..

git add && git commit && git push
npm publish

use it in dependant projects ūüôā


> *ngFor="let id of ids">
Id is {{id}}

> *ngSwitchCase="'0'" [ngClass]="'one'">I am Even.
> *ngSwitchCase="'1'" [ngClass]="'two'">I am Odd.
> *ngSwitchDefault>Nothing Found.
/>

Advertisements

JS Utils

JWT Token

Intereptor to Refresh Token
Ex1: https://medium.com/@alexandrubereghici/angular-tutorial-implement-refresh-token-with-httpinterceptor-bfa27b966f57

Ex2: https://codeforgeek.com/2018/03/refresh-token-jwt-nodejs-authentication/
Ex3: https://gist.github.com/Toilal/8849bd63d53bd2df2dd4df92d3b12f26

Minimalist Example:
Ex1: http://jasonwatmore.com/post/2018/05/23/angular-6-jwt-authentication-example-tutorial

Remove html to prevent SQ Injection

RegEx Playground: https://regex101.com/
2 int followed by 4 caps: ^[A-Z]{2}[0-9]{4}$

str.replace(/<[^>]*>/g, ”)

 

var str = '
<h2>Hello</h2>
';
str.replace(/]*&gt;/g, '');

Sort array of objects by ID

var data = [1, 2, 3, 4, 5]
  .map(el => {
    return {
      id: el,
      name: 'alpha',
      title: 'Mr.',
      description: 'laziest objects',
      age: (el * 10)
    };
  });
data.sort(function(a, b){return b.id - a.year});

Date Diff

d2 = new Date(2019, 3, 18);
d1 = new Date(2018, 9, 18);

Date.diffInMonths = function(d1, d2) {
  return Math.round((d2 - d1) / (60 * 30 * 60 * 24 * 1000));
}

Date.DateDiff = {

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}

Date.DateDiff.inDays(d1,d2);

 

Javascript

Lempel‚ÄďZiv‚ÄďWelch (LZW) Compression in JS

// Decode LZW-encoded string
function lzwDecode(s) {
        var dict = {};
        var data = (s + "").split("");
        var currChar = data[0];
        var oldPhrase = currChar;
        var out = [currChar];
        var code = 256;
        var phrase;
        for (var i = 1; i < data.length; i++) {
            var currCode = data[i].charCodeAt(0);
            if (currCode < 256) {
                phrase = data[i];
            }
            else {
                phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
            }
            out.push(phrase);
            currChar = phrase.charAt(0);
            dict = oldPhrase + currChar;
            code++;
            oldPhrase = phrase;
        }
        return out.join("");
}

// Compress a string with LZW
function lzwEncode(s) {
        var dict = {};
        var data = (s + "").split("");
        var out = [];
        var currChar;
        var phrase = data[0];
        var code = 256;
        var i, l;
        for (i = 1, l = data.length; i < l; i++) {
            currChar = data[i];
            if (dict[phrase + currChar] != null) {
                phrase += currChar;
            }
            else {
                out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
                dict[phrase + currChar] = code;
                code++;
                phrase = currChar;
            }
        }
        out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
        for (i = 0, l = out.length; i < l; i++) {
            out[i] = String.fromCharCode(out[i]);
        }
        return out.join("");
}
Image

JSON

rename a prop

var obj = {creed: "monotheism"};
obj.faith = obj.creed;
delete obj.creed;

console.log(obj); // {faith: "monotheism"}

JS Var to JSON & vice-versa

function getObj() {
  var arrA = [22,43], arrB = [];
  return {
   ar1:arrA, 
   ar2:arrB
  };
}
var arr = getObj().ar1;
console.log(arr);

var arr1 = [1,2];
var arr2 = [3,4];

var obj = {a:arr1,b:arr2};

console.log(JSON.stringify(obj)); // {"a":[1,2],"b":[3,4]}
console.log(JSON.stringify(arr2)); // [3,4]

// adding top node to existing obj
var withPref = {'pref' : obj}

console.log(JSON.stringify(withPref)); // {"pref": {"a":[1,2], "b":[3,4]} }

var arr = (obj.a).concat(obj.b);
console.log(arr); // plain js array: [1, 2, 3, 4]

var jane = {"person":[{"id":121},{"title":"Jane"}]};

All About JSON Manipulation



  
    
	
	html,body {
		margin: 5px 0px 0px 5px;
		padding: 10px 0px 0px 0px;
	}
	span {
		border-radius: 3px;
		background: #220;
		color: #fff;
		padding: 10px;
	}
	
  

  
    <div class="content"></div>
    

	
	$( "div.content" ).html("<span>Hello Again! I will exapnd</span>");
	$( "div.content span" ).append("&nbsp; &gt; ABC" );
	
	var arry1 = [];
	var temp = {
		"gpc": [
          {
            "top": "Top1",
            "gmp": [
              {"name": "1. Basic Acc Srv"},
              {"name": "jquery"},
              {"name": "json"}
            ]
          },
			    {
            "top": "Top2",
            "gmp": [
              {"name": "1. Basic Acc Srv"},
              {"name": "ember"},
              {"name": "Top1"}
            ]
          }
		]
	};

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key &amp;&amp; obj[i] == val || i == key &amp;&amp; val == '') { //
            objects.push(obj);
        } else if (obj[i] == val &amp;&amp; key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(obj);
            }
        }
    }
    return objects;
}

function getValues(obj, key) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getValues(obj[i], key));
        } else if (i == key) {
            objects.push(obj[i]);
        }
    }
    return objects;
}

function getKeys(obj, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
			objects = objects.concat(getKeys(obj[i], val));
        } else if (obj[i] == val) {
			objects.push(i);
        }
    }
    return objects;
}


// then to get the JSON string
if(arry1.length === 0) {
  // console.log("empty arry");
} else {
  var strJSON = JSON.stringify({gpc: arry1});
  // console.log('New::: \n'+strJSON );
}

// console.log('Org::: \n'+JSON.stringify(temp));
var obj1 = getObjects(temp,'top','Top1');
var obj2 = getObjects(temp,'top','Top2');

var main = obj1.concat(obj2);

console.log(JSON.stringify(main));
	
  



adding a parent node

Stack Answer

Concat

Pen

See the Pen parse json by Hamzeen H (@hamzeen) on CodePen.

https://production-assets.codepen.io/assets/embed/ei.js

JSFiddle

http://jsfiddle.net/hamzeen/dw3q67md/

Back with Flock-The Social Web Browser

Guys Lonely Coder is back after a week of heavy work with a freaking project for Cutting Edge which is the most exiting event of IIT every year, Where the Final years of IIT enterprise their talent on stage to the public. And this is where all the work that they put over the years at IIT get exposed to the industry what we are really capable of being, students of University of Westminster. This time for this ever so  fascinating event, second years are also invited to take part due to lack of projects & gues what Lonely Coder grabbed it with both hands.

The project of us APES which stands for Automated Police Entry System. It’s¬† another entreating project I have¬†come across after the long lasting nights working out on ¬†Successlands another devastating project at Archmage which took me months to work on and believe me still it is incomplete. You can have a look at it via,

SuccessLands.com

Today I get a chance to explore a few breathtaking features of my new browser,¬†should say a social web browser to be precise. Namely it’s Flock. Believe me this post is actually written & published right from the Flock browser how that sounds for a blogger… I’m sure already the guys must be pumped up to figure out how this¬†actually works. It’s really easy as giving your particular blog’s username & password, don’t worry what ever the sort of blogger you are it supports all the famous blogs like blogger, wordpress in my case, xanga, LiveJournal,¬†TypePad¬†and many more.

This is just another single amazing aspect of Flock. You got many more than this , to start  with one might wonder if it another version of firefox if you were firefox browser over the years. Yes it looks very similar to firefox and also uses the same technology for fast browsing.

Going through few of the other embarasing features of Flock,

  • Favorites management
  • Feed reader
  • Media minibar
  • Photo Uploader
  • Account Handling
  • Again Flock’s in-built Photo uploader also¬†provides support to uploading media to most of your media sites which includes PICZO, Flickr, YOUTUBE & Photo Bucket. Never worry¬†with signins just goto your browser and get access to most of your favourite sites. Nothing to type in your addressbar unless you wanna¬†discover a new site.

    With all these wonderfull integerations it provides one with instantaneous access to most of their¬†favourite sites. But then again security wise it’s really dangerous to leave your account informations in the browser so in that case Flock becomes an extremely confidential and a private browser.

    If you are interested in trying out Flock visit,
    Flock.com

    Blogged with Flock

    T20 World Cup -Where the Drama Begins

    Yesterday (2nd day of ICC twenty-twenty inaugural World Cup) Zimbabwe met The three times consecutive world cup winners Australia in another thrilling encounter.

    The minors(Zimbabwe) played good positive cricket wright from the beginning were paid of for their effort in the field and aussies were all over the place with Sky Scrapers most of them got caught in the deep. The drama continued and Aussies were restricted down to 138 for 9 wickets.

    Later in the evening Zimbabwe chased it down to welcome Aussies to ICC T20 WorldCup with a Shock Treatment.

    Earlier KIWI’s (New Zealand) marked their arrival to the WorldCup with a comprehensive win over Kenya.

    And Pakistan too marked their first victory in the T20 World Cup win with a much fought win over Scotland after they were restricted by the underdogs (Scotland) for a mere 171 for the loss of 9 wickets.

    Many Cricket Critics are still reluctant to express their view over this new innovation by the ICC and it will remain so up untill the end of this fascinating tournament as the Drama continues.