Skip to content

Instantly share code, notes, and snippets.

View dmnsgn's full-sized avatar

Damien Seguin dmnsgn

View GitHub Profile
@dmnsgn
dmnsgn / es6-constructor.js
Created October 6, 2014 16:27
Douglas Crockford's constructor as explained in his NordicJS 2014 talk.
function constructor(spec) {
let {member} = spec,
{other} = other_constructor(spec),
method = function () {
// member, other, method, spec...
};
return Object.freeze({
method,
other
@dmnsgn
dmnsgn / NoClassSingleton.js
Last active April 16, 2016 17:35
ES6 singleton pattern: using objects
// http://www.2ality.com/2011/04/singleton-pattern-in-javascript-not.html
const NoClassSingleton = {
_instance: null,
get instance() {
if (!this._instance) {
this._instance = {
singletonMethod() {
return 'singletonMethod';
},
@dmnsgn
dmnsgn / SingletonNoInstance.js
Created April 16, 2016 17:46
ES6 singleton pattern: prevent the creation of an instance
// Implementation 4: prevent the creation of an instance
class SingletonNoInstance {
constructor(enforcer) {
throw new Error('Cannot construct singleton');
}
static singletonMethod() {
return 'singletonMethod';
}
@dmnsgn
dmnsgn / listen_say_voices.sh
Created March 28, 2017 13:35
Listen to all voices available by the `say` command on macOS
for voice in `say -v '?' | awk '{print $1}'`; do say -v "$voice" "Hi `whoami` my name is ${voice}"; done
// Disclaimer: this is not stable nor properly tested
// Not sure about the security implications of hijacking the fetch request either
// https://gist.github.com/pilwon/ff55634a29bb4456e0dd
const ES_MODULE_IMPORT_REGEX = /\bimport\s+(?:.+\s+from\s+)?[\'"]([^"\']+)["\']/g;
const packages = new Map().set(
"lodash/clamp",
"https://cdn.jsdelivr.net/npm/lodash-es@4.17.8/clamp.js"
);
import clamp from "lodash/clamp";
const num = Infinity;
const answer = clamp(num, 0, 42);
console.log(answer);
export default answer;
<!DOCTYPE html>
<html>
<head>
<title>ES modules in the browser - almost - now | Package name maps</title>
</head>
<body>
<!-- Declare the package map -->
<script type="packagemap">
<!DOCTYPE html>
<html>
<head>
<title>ES modules in the browser - almost - now | Service worker</title>
</head>
<body>
<script>
// Register
<!DOCTYPE html>
<html>
<head>
<title>ES modules in the browser - almost - now | unpkg getLibs</title>
<!-- Include the getlibs endpoint -->
<script src="https://unpkg.com/getlibs"></script>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>ES modules in the browser - almost - now | Basics: script type module</title>
</head>
<body>
<!-- Use a type set to "module" with a fallback -->
<script type="module" src="app.js"></script>