Skip to content

Instantly share code, notes, and snippets.

View nfroidure's full-sized avatar
🌻
Getting greener

Nicolas Froidure nfroidure

🌻
Getting greener
View GitHub Profile
@nfroidure
nfroidure / gist:3608274
Created September 3, 2012 10:06
Convert a number to an amount
Number.prototype.toAmount = function() {
var parts=(this+'').split('.');
parts[0]=(parts[0]?parts[0]:'0');
parts[1]=(parts[1]?parts[1].substring(0,2):'00');
parts[1]+=(parts[1].length==1?'0':'');
return parts[0]+'.'+parts[1];
}
var price=12.112;
console.log(price.toAmount()); // Outputs 12.11
@nfroidure
nfroidure / gist:3727624
Created September 15, 2012 12:43
Migration
#! /bin/sh
# Init
cd /
if [ "$1" != "Y" ]; then
echo "$0 1:sure(Y/N)"
else
while read orgid database
do
@nfroidure
nfroidure / gist:4663042
Last active December 11, 2015 21:29
How to handle errors on chaining APIs
// Chaining API for URIBuilder
var uri=new URLBuilder().parse('bad_url_format').addQueryParam('param','value').setPort('443').toString();
// How to handle the bad url parse error ?
// Don't want to throw exception
// Can't return null
// Chainable API for URIBuilder
var uri = new URLBuilder(logErrorHandler)
.parse('bad_url_format')
.addQueryParam('param','value')
.setPort('443')
.toString();
function logErrorHandler(err, url_object){
if('parse_error'===err.type)
@nfroidure
nfroidure / gist:4692419
Last active December 12, 2015 01:38
RegExp inconsistency ?
var str="<h2>Blahblah</h2><p>Blahblah Blahblah Blahblah.</p><h2>Blahblah</h2><p style=\"text-align: center;\"> <img alt=\"Blahblah\" src=\"http://www.example.com/images/blah.jpg\" /></p><p>BlahblahBlahblah Blahblah Blahblah.</p><h2>Blahblah</h2><p>Blahblah</p>";
var regExp=new RegExp('<([^>]+)"([^>]*)>','mg');
var pattern='<$1$2>';
while(regExp.test(str))
str = str.replace(regExp, pattern);
console.log(str);
// outputs
// <h2>Blahblah</h2><p>Blahblah Blahblah Blahblah.</p><h2>Blahblah</h2><p style=text-align: center;> <img alt="Blahblah" src=http://www.example.com/images/blah.jpg /></p><p>BlahblahBlahblah Blahblah Blahblah.</p><h2>Blahblah</h2><p>Blahblah</p>
// Notice the " in the img alt attribute
@nfroidure
nfroidure / gist:4967142
Created February 16, 2013 14:31
Fun arguments limit : Tested in Google Chrome
(function () {}).apply(this,new Array(1000000))
// RangeError: Maximum call stack size exceeded
@nfroidure
nfroidure / gist:5005563
Last active December 14, 2015 01:19
Fun Javascript behavior
if(function test(a){console.log(a)}) { console.log(test('test')) }
// ReferenceError : test is not defined
var test;
if(test = (function(a){console.log(a)})) { console.log(test('test')) }
// test
@nfroidure
nfroidure / gist:5111769
Created March 7, 2013 21:01
Rest input for love :)
# Json
{
"truc":"bidule",
"truc2":"bidule2",
"truc3":"bidule3"
"tab":
[
"machin",
"bidule",
"truc"
@nfroidure
nfroidure / gist:5118171
Last active December 14, 2015 16:49 — forked from bloodyowl/gist:5118132
{
"name" : "FranceJS"
, "version" : "0.0.1"
, "dependencies" : {
"jslovers" : "*",
"parisjs" : "*",
"toulousejs" : "*",
"lyonjs" : "*",
"lillejs" : "*"
}
@nfroidure
nfroidure / gist:5253315
Last active December 15, 2015 11:29
Plans on the moon for Javascript
var test = Object.create([].prototype);
test.[]=function(index) {
console.log("Reading index "+index);
return Array.[].call(this,index);
}