Skip to content

Instantly share code, notes, and snippets.

View VivienAdnot's full-sized avatar

Vivien Adnot VivienAdnot

View GitHub Profile
@VivienAdnot
VivienAdnot / jsonp.js
Created March 19, 2015 16:53
vanilla js : module jsonp with timeout support
//Lightweight example (with support for onSuccess and onTimeout). You need to pass callback name within URL if you need it.
//At GitHub: https://github.com/sobstel/jsonp.js/blob/master/jsonp.js
var $jsonp = (function(){
var that = {};
that.send = function(src, options) {
var callback_name = options.callbackName || 'callback',
on_success = options.onSuccess || function(){},
on_timeout = options.onTimeout || function(){},
@VivienAdnot
VivienAdnot / Controller.cs
Last active August 29, 2015 14:27
How to support etags in asp.net mvc (http caching)
public ActionResult Test304(string input)
{
var requestedETag = Request.Headers["If-None-Match"];
var responseETag = LookupEtagFromInput(input); // lookup or generate etag however you want
if (requestedETag == responseETag)
return new HttpStatusCodeResult(HttpStatusCode.NotModified);
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetETag(responseETag);
return GetResponse(input); // do whatever work you need to obtain the result
objectToQueryString: function(obj) {
var result = [];
for (var prefix in obj) {
add(prefix, obj[prefix]);
}
return result.join("&").replace(/%20/g, "+");
function add(key, value) {
@VivienAdnot
VivienAdnot / collections-utils.js
Last active October 13, 2015 12:24
a set ot utils to manipulate collections
// merge an array into another
function mergeArray(source, destination) {
[].push.apply(destination, source);
return destination;
}
// Is value a function?
function isFunction(x) {
return "function" == typeof x;
}
@VivienAdnot
VivienAdnot / checkValidateObject.js
Created October 30, 2015 13:57
To ensure that an objects matches the expected structure
var target = {
prop1: "123",
prop2: "456",
prop3: {
A: "1",
B: "2"
}
};
var schema = [
@VivienAdnot
VivienAdnot / stream.js
Created January 15, 2016 09:20
stream head tail
function node(head, tail) {
return [head, tail];
};
function head(stream) {
return stream[0];
};
function tail(stream) {
var tail = stream[stream.length - 1];
@VivienAdnot
VivienAdnot / extendObject.js
Last active March 21, 2016 11:21
A vanilla js function to extend an object A with the properties of another object B. It creates the property or overrides the existing one. Works recursively.
var extendObject = function (destination, source) {
var objectSignature = Object.prototype.toString.call({});
var isPropertyAnObject = function (property) {
return Object.prototype.toString.call(property) === objectSignature;
};
for (var property in source) {
// http://brianflove.com/2013/09/05/javascripts-hasownproperty-method/
if(source.hasOwnProperty(property)) {
@VivienAdnot
VivienAdnot / Gruntfile.js
Created April 21, 2016 14:25
A gruntfile example with livereload
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {
@VivienAdnot
VivienAdnot / once.js
Created July 5, 2016 15:43
call the function once, the other times it won't be called
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
@VivienAdnot
VivienAdnot / Promises - fetch => sum ES5.js
Last active August 31, 2016 09:37
Play with promises: call several endpoints and aggregate the result
var data = [{
isActive: "1",
logins: 2000,
clicks: 1000
}, {
isActive: "1",
logins: 3000,
clicks: 1000
}, {
isActive: "1",