From 75229c1ab50e01aaab42f740ffd602607c2061f3 Mon Sep 17 00:00:00 2001 From: Galeel Bhasha Satthar Date: Mon, 30 Oct 2017 21:52:21 +0530 Subject: [PATCH] Simplified the usage by self invoking the function when it isrequired or imported --- CustomEvent.js | 27 ++++++++++++--------------- README.md | 10 +++++++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/CustomEvent.js b/CustomEvent.js index 47a6d74..728d592 100644 --- a/CustomEvent.js +++ b/CustomEvent.js @@ -1,18 +1,15 @@ -function CustomEvent ( event, params ) { - params = params || { - bubbles: false, - cancelable: false, - detail: undefined, - }; - var evt = document.createEvent( 'CustomEvent' ); - evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); - return evt; -} +(function() { + if (typeof window === "undefined") return false; + if (typeof window.CustomEvent === "function") return false; + + function CustomEvent ( event, params ) { + params = params || { bubbles: false, cancelable: false, detail: undefined }; + var evt = document.createEvent( 'CustomEvent' ); + evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); + return evt; + } -module.exports = function ( undefined ) { - if ( typeof window.CustomEvent === 'function' ) { - return; - }; CustomEvent.prototype = window.Event.prototype; + window.CustomEvent = CustomEvent; -}; +})(); diff --git a/README.md b/README.md index bf07134..889de91 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,15 @@ npm install --save-dev jspolyfill-custom-event # Usage ```js -require( 'jspolyfill-custom-event' )(); +require('jspolyfill-custom-event'); +``` + +or + +```js +import('jspolyfill-custom-event'); ``` -**Note**: Make sure to add the extra set of parenthesis at the end to fire the `function` that -registers the creation of the polyfill. # Syntax