Skip to content

Instantly share code, notes, and snippets.

View tkissing's full-sized avatar

Timo Kissing tkissing

  • Orange County, CA
  • 14:30 (UTC -07:00)
View GitHub Profile
function A() {
function privateA() {}
function privateB() {}
function publicA() {
// now this.publicB() here, just publicB() - safes typing and even if someone overrides publicB we still use the implementation from here
// of course that latter might not be desired, in which case you'd write this.publicB() here
return publicB() ? privateA() : privateB();
}
this.publicA = publicA;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>
@tkissing
tkissing / gist:e5fa4908150c82d73131
Last active February 12, 2019 03:41
Singleton JavaScript modules that are easy to test! (Hint: Avoid object literals)
// using AMD syntax here to avoid confusion about scope and file boundaries,
// but the concept translates 1:1 to CommonJS (node/io) modules
define('badSingleton', function(require, exports, module) {
var dependency = require('dependency');
var privateState;
module.exports = {
foo: function() {
@tkissing
tkissing / gist:b4944c285c761efeff9e
Created March 10, 2015 21:10
setInterval wrapper that returns "stop" method.
function startInterval(func, freq) {
var id = setInterval(func, freq);
return function() {
if (id) {
clearInterval(id);
id = null;
return true;
}
return false;
}
@tkissing
tkissing / from-yesterday
Created March 26, 2015 16:27
npm registry weirdness
cat node_modules/chai-as-promised/package.json | grep version
"version": "4.3.2",
@tkissing
tkissing / gist:786bd8381fd5688bb9c9
Created March 31, 2015 08:41
Careful when using .then(successHandler, errorHandler) - it might not do what you mean to do!
(function() {
p = Promise.resolve(3);
p.then(function() { throw Error('e'); }, console.warn.bind(console, 'You will not see this one'));
p.then(function() { throw Error('e'); }).then(null, console.warn.bind(console, 'You will see this one'));
}());
define(function(require) {
var ko = require('knockout');
function observeEvent(pubsub, options) {
var o = ko.observable();
Object.keys(options).forEach(function(evt) {
var value = options[evt];
pubsub.on(evt, function() {
// promise code
function add5 (x) {
return x + 5
}
function add5Promise (x) {
return Promise.resolve(x).then(function (x) {
return add5(x)
})
}
@tkissing
tkissing / 01-directory-structure.md
Last active June 28, 2016 03:36 — forked from tracker1/01-directory-structure.md
Anatomy of a JavaScript/Node project.

Directory structure for JavaScript/Node Projects

While the following structure is not an absolute requirement or enforced by the tools, it is a recommendation based mostly on what the JavaScript and in particular Node community at large have been following by convention.

Beyond a suggested structure, no tooling recommendations, or sub-module structure is outlined here.

Directories

  • src/ is for the code you write manually independent of the module format
  • lib/ is for modules compiled from src/ into a format compatible with standard require in the node versions indicated by engines in your package.json (UMD, CommonJS)
  • dist/ is for modules or scripts compiled from src/ into formats not compatible with standard require in the node versions indicated by engines in your package.json (AMD, browser globals/IIFE)
@tkissing
tkissing / new-promise-reject.js
Created December 9, 2016 21:43
Using throw inside new Promise()
new Promise((resolve, reject) => {
resolve(someArr.map(e => {
if (e.foo) {
reject(Error('Found a foo!'));
// the .map will continue to loop
// even resolve will still be called although it won't have any more effect
}
return e.bar;
}));
});