Archive for Javascript

Simple NodeJS Crypto and AS3Crypto Equivalent

There is very little documentation. It took me two days to get NodeJS Crypto and AS3Crypto to spit out the same results. The encryption algorithm I chose may not be the best, and there is no IV, but atleast I can get Node and AS3 communicating properly with encryption. I’ll find something better like ‘aes192’ but ‘des-ecb’ is working fine and is what I’ll stick with for now as I get my project up and running. Googling for this is hard, hopefully this will help someone out there. Please leave a comment if it helped you so I know I’m not wasting my time posting all these code bits up thx!

Key = 12345678
Text = Hello World
Encrypted = 01Jk6AecyqldguNoHIO7dw==
Decrypted = Hello World

AngularJS “Ago” Coffee Filter

Just made a jam-packed and ugly AngularJS filter that converts a date to an “ago” string. So if you entered a javascript date it will convert it to “10 days ago” or “23 minutes ago”. Short hand, and in-line function, yes it’s messy, but interesting!

ng = angular.module "myApp", []
ng = ng.filter "ago", () -> (dt) -> 
  if not dt then return ''
  agofn = (val,unit)-> return val + ' '+unit+(if val>0 then 's')+' ago'
  msecs = (new Date()).getTime()-(new Date(dt)).getTime() 
  days = parseInt(msecs/(24*60*60*1000))
  if days>0 then return agofn days, 'day'
  hours = parseInt(msecs/(60*60*1000))
  if hours>0 then return agofn hours, 'hour'
  mins = parseInt(msecs/(60*1000))  
  if mins>0 then return agofn mins, 'minute'
  secs = parseInt(msecs/1000)
  return agofn secs,'second'

Here’s how you would use it:

I ran {{myDate | ago}} ...

This is what it will output:

I ran 45 minutes ago ...

Here is my GIST for the same code.

AngularJS Email Filter in CoffeeScript

AngularJS 1.2 is a nice and simple framework to create web applications. One hurdle I just went thru is to create a filter to convert an email to a clickable link. To use this you will need to include “angular-sanitize.js” to get your html approved else it’s considered unsafe.

Include the sanitize JS file in your main HTML:

<script src="vendor/angular/1.2.0-rc.2/angular-sanitize.js"></script>

Then in CoffeeScript add it to your app’s module:

ng = angular.module "myApp", ['ngSanitize']

Then add this filter to your config code:

ng = ng.filter "email", () -> 
   (text) -> 
      '<a href="mailto:'+text+'">'+text+'</a>'

Finally use it in anywhere in your HTML as an “ng-bind-html”:

<span ng-bind-html=" | email"></span>

My First GitHub, the ANIME Stack!

Do you like Angular, NodeJS, Iced Coffee Script, MongoDB and Express!?? Add to that Grunt and Heroku support. Well if you happen to like all that, and like all that together, then I have the perfect solution for you! Introducing the ANIME stack, as opposed to the MEAN stack. It has all the things I like. Can some other programmer like all those at the same time? I’m not sure it’s possible. This entire stack will probably be quickly obsoleted with months but here is my first contribution to GitHub!

Why Iced Coffee Script? – It’s basically the same as Coffee Script but it has super easy support for synchronous operations such as AWAIT and DEFER.

Why Angular? – It’s separates visuals into templates and leaves most code out. It’s also useful for SPAs (Single Page Applications) which is my next side project.

Take a look, get some ideas and find some use of it. Enjoy!

Click here to view the ANIME stack!