Skip to content

Instantly share code, notes, and snippets.

View VivienAdnot's full-sized avatar

Vivien Adnot VivienAdnot

View GitHub Profile
@VivienAdnot
VivienAdnot / axios-401-response-interceptor.js
Last active February 22, 2018 14:47 — forked from yajra/axios-401-response-interceptor.js
Axios 401 response interceptor.
// Add a 401 response interceptor
window.axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (401 === error.response.status) {
window.location = '/login';
}
});
@VivienAdnot
VivienAdnot / checkOrReject.js
Created February 8, 2018 16:35
Pattern getOrReject / checkOrReject
const checkOrReject = (preFetch, condition, errorMessage) => preFetch()
.then(data => (condition(data))
? Promise.resolve()
: Promise.reject(Boom.forbidden());
const doesUserOwnFeed = (feed, userId) => feed._user === userId;
const doesUserOwnFeedOrReject = (feed, _user) => checkOrReject(
() => doesUserOwnFeed(feed, userId),
'Feed not found'
@VivienAdnot
VivienAdnot / node-middleware.js
Created February 6, 2018 10:41
Show how to chain node middlewares and handle error
app.get('/user/:id', function (req, res, next) {
console.log('middleware 1 reached', req.params);
next();
}, function (req, res, next) {
const { id } = req.params;
console.log('middleware 2 reached');
const squareAsync = (nbr) => {
return new Promise((resolve) => {
// mock async call, to illustrate a heavy weight backend process which returns the square of a number
setTimeout(() => resolve(nbr * nbr), 200);
});
};
@VivienAdnot
VivienAdnot / bubbleSort.ts
Created May 24, 2017 10:59
bubble sort implementation in typescript
export function bubbleSort(array: number[]): number[] {
array = array.slice(); // creates a copy of the array
for(let i = 0; i < array.length; i++) {
for(let j = 0; j < array.length - 1; j++) {
if(array[j] > array[j + 1]) {
let swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
@VivienAdnot
VivienAdnot / Deferred.ts
Created May 12, 2017 13:05
Naive implementation of Deferred concept
import { Promise } from "es6-promise"
import { Thenable } from "es6-promise"
export class Deferred<T> {
public promise: Promise<T>;
public resolve: (value?: T | Thenable<T>) => void;
public reject: (error?: any) => void;
constructor() {
@VivienAdnot
VivienAdnot / mediastay.js
Last active April 18, 2017 13:28
mediastay.js
Playtem.init({
apiKey: "e048-4cdev",
container: document.getElementById("playtem-ad"),
modal: true,
userId: "WebTestUser1",
onAdAvailable: function() {
console.log("Page.onAdAvailable");
},
onAdClick: function() {
class FizzBuzz {
private min: number;
private max: number;
constructor(min:number, max:number) {
this.min = min;
this.max = max;
}
private containsDigit(currentNumber:number, digit: number) {
const gulp = require('gulp');
const argv = require('yargs').argv;
const merge = require('merge');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const connect = require('gulp-connect');
const paths = {
pages: ['./src/*.html'],
buildDir: "./build/dev"
@VivienAdnot
VivienAdnot / rx-poc.css
Created December 13, 2016 14:06
a proof concept to showcase the encapsulation of streams with rxjs
body {
font-family: sans-serif;
padding: 10px;
}
h2 {
font-weight: bold;
display: inline-block;
}
.refresh {
font-size: 80%;