Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View rauschma's full-sized avatar

Axel Rauschmayer rauschma

View GitHub Profile
@rauschma
rauschma / amd_vs_cjs.js
Created November 15, 2012 18:10
Modules: AMD versus CJS
//---------------------
// AMD
define(["foo", "bar", "baz"], function (foo, bar, baz) {
return {
func: function (...) { ... }
};
});
//---------------------
@rauschma
rauschma / morse.js
Created December 16, 2012 02:45
Question: which combinations (with a maximum length of 4) are unused in international Morse code?
// Question: which combinations with a maximum length of 4 are unused in international Morse code?
// http://en.wikipedia.org/wiki/Morse_code
var morseCodes = {
'.-': 'A',
'-...': 'B',
'-.-.': 'C',
'-..': 'D',
'.': 'E',
'..-.': 'F',
@rauschma
rauschma / question.md
Last active April 8, 2018 02:25
JavaScript libraries and tools: what are the bare essentials?

Question: If a newcomer to JavaScript asked you for essential libraries and tools, what would you tell them? You don’t want to overwhelm them with too many suggestions!

Focus: language-related functionality (as opposed to browser-related functionality).

  • Libraries: including, say, Underscore.js and promises libraries, but excluding jQuery et al.
  • Tools: package managers, build tools, unit test tools, etc.
    • Less important: editors, IDEs. Rationale: it’s fairly obvious that you need them. There are other tools that people might not even know that they need.
// Proof of concept only!
function isInstance(value, Type) {
if (typeof Type.hasInstance === 'function') {
return Type.hasInstance(value);
} else {
return value instanceof Type;
}
}
isInstance.Primitive = {
Primitive
PrimitiveBoolean
PrimitiveNumber
PrimitiveString
ValueObject
(Future: user-defined value classes)
ReferenceObject
(Objects that are not an instance of Object)
Object
(Subclasses of Object)
@rauschma
rauschma / execAll.js
Last active April 7, 2019 12:09
A version of RegExp.prototype.exec() that returns an iterable
console.log(extractQuotedTextES5('“a” and “b” or “c”')); // [ 'a', 'b', 'c' ]
// If exec() is invoked on a regular expression whose flag /g is set
// then the regular expression is abused as an iterator:
// Its property `lastIndex` tracks how far along the iteration is
// and must be reset. It also means that the regular expression can’t be frozen.
var regexES5 = /“(.*?)”/g;
function extractQuotedTextES5(str) {
regexES5.lastIndex = 0; // to be safe
var results = [];
@rauschma
rauschma / munich.md
Last active April 8, 2018 01:32
A few tips for tourists in Munich
/**
* Create an array with `len` elements.
*
* @param [initFunc] Optional: a function that returns
* the elements with which to fill the array.
* If the function is omitted, all elements are `undefined`.
* `initFunc` receives a single parameter: the index of an element.
*/
function initArray(len, initFunc) {
if (typeof initFunc !== 'function') {
@rauschma
rauschma / newOperator.js
Created December 11, 2013 23:32
This is roughly how one would implement the `new` operator in JavaScript.
function newOperator(Constr, args) {
var inst = Object.create(Constr.prototype);
var result = Constr.apply(inst, args);
if (typeof result === 'object' && result !== null) {
return result;
}
return inst;
}
@rauschma
rauschma / global.js
Created December 14, 2013 15:14
Cross-platform way of referring to the global object.
(function (glob) {
// glob points to global object
}(typeof window !== 'undefined' ? window : global));