Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Giammarchi committed Nov 23, 2018
0 parents commit e3b0f20
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules/
coverage/
package-lock.json
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage/*
node_modules/*
test/*
_config.yml
.DS_Store
.gitignore
.travis.yml
package-lock.json
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- stable
git:
depth: 1
branches:
only:
- master
after_success:
- "npm run coveralls"
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CustomEvent

[![Build Status](https://travis-ci.com/ungap/custom-event.svg?branch=master)](https://travis-ci.com/ungap/custom-event) [![Coverage Status](https://coveralls.io/repos/github/ungap/custom-event/badge.svg?branch=master)](https://coveralls.io/github/ungap/custom-event?branch=master) ![WebReflection status](https://offline.report/status/webreflection.svg)

An [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) polyfill for legacy browsers.

* CDN via https://unpkg.com/@ungap/custom-event
* ESM via `import CustomEvent from '@ungap/custom-event'`
* CJS via `const CustomEvent = require('@ungap/custom-event')`

Compatible down to IE9, works well with ES5 shim upfront in IE8 (and maybe lower too).

[Live test](https://ungap.github.io/custom-event/test/)
18 changes: 18 additions & 0 deletions cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*! (c) Andrea Giammarchi - ISC */
var self = this || /* istanbul ignore next */ {};
try { self.CustomEvent = CustomEvent; }
catch (CustomEvent) {
self.CustomEvent = function CustomEvent(type, init) {
if (!init)
init = {};
var e = document.createEvent('Event');
var bubbles = !!init.bubbles;
var cancelable = !!init.cancelable;
e.initEvent(type, bubbles, cancelable);
e.bubbles = bubbles;
e.cancelable = cancelable;
e.detail = init.detail;
return e;
};
}
module.exports = self.CustomEvent;
18 changes: 18 additions & 0 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*! (c) Andrea Giammarchi - ISC */
var self = this || /* istanbul ignore next */ {};
try { self.CustomEvent = CustomEvent; }
catch (CustomEvent) {
self.CustomEvent = function CustomEvent(type, init) {
if (!init)
init = {};
var e = document.createEvent('Event');
var bubbles = !!init.bubbles;
var cancelable = !!init.cancelable;
e.initEvent(type, bubbles, cancelable);
e.bubbles = bubbles;
e.cancelable = cancelable;
e.detail = init.detail;
return e;
};
}
export default self.CustomEvent;
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*! (c) Andrea Giammarchi - ISC */
var self = this || /* istanbul ignore next */ {};
try { self.CustomEvent = CustomEvent; }
catch (CustomEvent) {
self.CustomEvent = function CustomEvent(type, init) {
if (!init)
init = {};
var e = document.createEvent('Event');
var bubbles = !!init.bubbles;
var cancelable = !!init.cancelable;
e.initEvent(type, bubbles, cancelable);
e.bubbles = bubbles;
e.cancelable = cancelable;
e.detail = init.detail;
return e;
};
}
2 changes: 2 additions & 0 deletions min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@ungap/custom-event",
"version": "0.0.0",
"description": "A CustomEvent polyfill for legacy browsers.",
"main": "cjs/index.js",
"module": "esm/index.js",
"unpkg": "min.js",
"scripts": {
"build": "npm run cjs && npm run esm && npm run min && npm run test && npm run size",
"cjs": "cp index.js cjs/ && echo 'module.exports = self.CustomEvent;' >> cjs/index.js",
"esm": "cp index.js esm/ && echo 'export default self.CustomEvent;' >> esm/index.js",
"min": "uglifyjs index.js --support-ie8 --comments=/^!/ -c -m -o min.js",
"size": "cat index.js | wc -c && cat min.js | wc -c && gzip -c9 min.js | wc -c && cat min.js | brotli | wc -c",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"test": "istanbul cover test/index.js"
},
"keywords": [
"CustomEvent",
"polyfill",
"legacy",
"ungap"
],
"author": "Andrea Giammarchi",
"license": "ISC",
"devDependencies": {
"coveralls": "^3.0.2",
"istanbul": "^0.4.5",
"uglify-js": "^2.8.29"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ungap/custom-event.git"
},
"bugs": {
"url": "https://github.com/ungap/custom-event/issues"
},
"homepage": "https://github.com/ungap/custom-event#readme"
}
33 changes: 33 additions & 0 deletions test/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function loadScript(src, then) {
var s = document.createElement('script');
var after = true;
s.type = 'text/javascript';
s.onload = onload;
s.onreadystatechange = function () {
if (/loaded|complete/.test(s.readyState))
setTimeout(onload);
};
s.src = src;
document.documentElement.appendChild(s);
function onload() {
if (after) {
after = false;
then();
}
}
}

function loadTest(what, message) {
requiring(what);
loadScript('./index.js?_=' + Math.random(), function () {
alert(message || 'OK');
});
}

function requiring(what) {
function require() { return what; }
require.cache = {};
require.resolve = require;
window.require = require;
window.global = window;
}
34 changes: 34 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ungap WeakMap</title>
<!--[if lt IE 9]>
<script src="https://unpkg.com/ie8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js">
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-sham.min.js"></script>
<![endif]-->
<script src="global.js"></script>
<script type="module">
import CE from '../esm/index.js';
window.CE = CE;
</script>
<script>
this.onload = function () {
if (!this.CE)
loadScript('../min.js', function () {
loadTest(CustomEvent, 'CustomEvent!');
});
else {
console.assert(CE === CustomEvent);
loadTest(CustomEvent, 'CustomEvent!');
}
};
</script>
</head>
<body>

</body>
</html>
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
if (typeof document === 'undefined')
global.document = {
createEvent: function () {
return {initEvent: function (type, init) {
this.type = type;
if (!init) init = {};
this.bubbles = !!init.bubbles;
this.cancelable = !!init.cancelable;
}};
}
};

var CustomEvent = require('../cjs');
test();

function test() {

var a = new CustomEvent('a');
console.assert(a.type === 'a');
console.assert(!a.cancelable && !a.bubbles);

var b = new CustomEvent('b', {bubbles: true, cancelable: true});
console.assert(b.type === 'b');
console.assert(b.cancelable && b.bubbles);

var c = new CustomEvent('c', {bubbles: true});
console.assert(c.type === 'c');
console.assert(!c.cancelable && c.bubbles);

var d = new CustomEvent('d', {cancelable: true});
console.assert(d.type === 'd');
console.assert(d.cancelable && !d.bubbles);

var e = new CustomEvent('e', {bubbles: false, cancelable: false});
console.assert(e.type === 'e');
console.assert(!e.cancelable && !e.bubbles);

var f = new CustomEvent('f', {detail: 123});
console.assert(f.type === 'f');
console.assert(f.detail === 123);
}

0 comments on commit e3b0f20

Please sign in to comment.