JSFiddle - React, Tailwind, and code Playground
by Ralt
JavaScript
// ES6 WeakMaps Shim from https://gist.github.com/1269991
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint asi: true undef: true es5: true node: true devel: true
forin: false latedef: false */
/*global define: true */
if (typeof(WeakMap) === 'undefined') WeakMap = (function(global) {
"use strict";
function defineNamespace(object, namespace) {
/**
Utility function takes `object` and `namespace` and overrides `valueOf`
method of `object`, so that when called with a `namespace` argument,
`private` object associated with this `namespace` is returned. If argument
is different, `valueOf` falls back to original `valueOf` property.
**/
// Private inherits from `object`, so that `this.foo` will refer to the
// `object.foo`. Also, original `valueOf` is saved in order to be able to
// delegate to it when necessary.
var privates = Object.create(object), base = object.valueOf
Object.defineProperty(object, 'valueOf', { value: function valueOf(value) {
// If `this` or `namespace` is not associated with a `privates` being
// stored we fallback to original `valueOf`, otherwise we return privates.
return value != namespace || this != object ? base.apply(this, arguments)
: privates
}, configurable: true })
return privates
}
function Name() {
/**
Desugared implementation of private names proposal. API is different as
it's not possible to implement API proposed for harmony with in ES5. In
terms of provided functionality it supposed to be same.
http://wiki.ecmascript.org/doku.php?id=strawman:private_names
**/
var namespace = {}
return function name(object) {
var privates = object.valueOf(namespace)
return privates !== object ? privates : defineNamespace(object, namespace)
}
}
function guard(key) {
/**
Utility function to guard WeakMap methods from keys that are not
a non-null...