Skip to content

Instantly share code, notes, and snippets.

@leifoolsen
leifoolsen / namespacing-strategies.js
Last active August 29, 2015 14:17
Various Namespacing strategies in JavaScript
// Classic Module Pattern
// See e.g: https://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
var V0 = (function() {
'use strict';
var self = this,
i = 0,
privateFunction = function () {
@leifoolsen
leifoolsen / timer.js
Last active August 29, 2015 14:17
Timer with callback in JavaScript
// See e.g: http://www.sitepoint.com/creating-accurate-timers-in-javascript/
// http://stackoverflow.com/questions/3969475/javascript-pause-settimeout
Timer = function(duration, resolution, delegates) {
'use strict';
duration || (duration = 1000);
resolution || (resolution = 20);
delegates || (delegates = {});
delegates.onStart || (delegates.onStart = function(){} );
@leifoolsen
leifoolsen / throttle.js
Last active August 29, 2015 14:17
Throttling is a means by which we can limit the number of times a function can be called in a given period.
// Throttling is a means by which we can limit the number of times a function can be called in a given period.
// Throttling is good for reducing mousemove events to a lesser, manageable rate, for instance.
// See e.g: https://remysharp.com/2010/07/21/throttling-function-calls
function throttle(fn, delay, options) {
'use strict';
delay || (delay = 250);
options || (options = {});
var timeout,
@leifoolsen
leifoolsen / debounce.js
Last active August 28, 2016 12:57
Debounce limits the rate at which a function can fire.
// Debounce limits the rate at which a function can fire.
// Debouncing ensures that exactly one signal is sent for an event that may be happening several times
// See e.g: http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
// http://davidwalsh.name/javascript-debounce-function
function debounce(fn, treshold, options) {
'use strict';
treshold || (treshold = 250);
options || (options = {});
@leifoolsen
leifoolsen / ReflectionUtils.java
Created March 22, 2015 20:34
Some reflection utils + SneakyThrow
package com.github.leifoolsen.reflection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
@leifoolsen
leifoolsen / typedetection.js
Created March 24, 2015 13:32
Functions for working with type detection.
['Undefined', 'Null', 'Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Array', 'Object'].forEach(
function(name) {
var is = 'is' + name;
if(!window.is)
window[is] = function(obj) {
return ({}).toString.call(obj) === '[object ' + name + ']';
};
});
(function() {
@leifoolsen
leifoolsen / stringextensions.js
Last active August 29, 2015 14:17
Extensions to the standard javascript String object
/*
* Author : leif.o.olsen
* Description : Extensions to the standard javascript String object
*/
(function () {
var methods = {
/**
* Check if a string contains a newline character
* @return {boolean} true if a newline character ocurs in the string
@leifoolsen
leifoolsen / once.js
Created March 24, 2015 14:53
Function in javascript that can be called only once
// Function in javascript that can be called only once
// See e.g : http://davidwalsh.name/javascript-once
// http://stackoverflow.com/questions/12713564/function-in-javascript-that-can-be-called-only-once
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
@leifoolsen
leifoolsen / jax-rs-list of-entities.java
Last active October 23, 2023 18:38
JAX-RS: Return a list of entities in a response
@GET
public Response allBooks() {
UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder().clone();
// Given a list of books
List<Book> books = Lists.newArrayList(new Book("1234567890"), new Book("0123456789"));
// Convert to GenericEntity and return in response
GenericEntity<List<Book>> entities = new GenericEntity<List<Book>>(books){};
@leifoolsen
leifoolsen / remove-duplicate.java
Last active August 29, 2015 14:18
Guava Splitter: Compare strings and remove identical part of string. Split query string into a name-value Map.
String s1 = http://localhost:8080/api;
String s2 = http://localhost:8080/api/books/search/any?offset=5&limit=5;
// Remove identical part from s2
List<String> l = Lists.newArrayList(Splitter.on(s1).omitEmptyStrings().trimResults().split(s2));
String s3 = l.get(0); // => /books/search/any?offset=5&limit=5
// Split query string into name value
URI uri = URI.create(s3);