├── .gitignore ├── .npmignore ├── .npmrc ├── README.md ├── cjs ├── index.js └── package.json ├── es.js ├── esm └── index.js ├── index.js ├── package.json ├── rollup ├── es.config.js └── index.config.js └── test ├── index.html ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | coverage/ 4 | node_modules/ 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | .eslintrc.json 4 | .travis.yml 5 | coverage/ 6 | node_modules/ 7 | rollup/ 8 | test/ 9 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @ungap/once 2 | 3 | A [Function.prototype.once](https://github.com/tc39/proposal-function-once#readme) polyfill. 4 | 5 | ```js 6 | import '@ungap/once'; 7 | // require('@ungap/once'); 8 | 9 | const log = console.log.once().bind(console); 10 | 11 | log(1); // logs 1 12 | log(1); // does nothing 13 | ``` 14 | -------------------------------------------------------------------------------- /cjs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*! (c) Andrea Giammarchi - ISC */ 3 | 4 | const {apply, construct} = Reflect; 5 | 6 | Function.prototype.once || Object.defineProperty( 7 | Function.prototype, 8 | 'once', 9 | { 10 | writable: true, 11 | configurable: true, 12 | value() { 13 | let fn = this, execute = true, returned; 14 | return function once() { 15 | if (execute) { 16 | execute = false; 17 | returned = this instanceof once ? 18 | construct(fn, arguments) : 19 | apply(fn, this, arguments); 20 | } 21 | return returned; 22 | }; 23 | } 24 | } 25 | ); 26 | -------------------------------------------------------------------------------- /cjs/package.json: -------------------------------------------------------------------------------- 1 | {"type":"commonjs"} -------------------------------------------------------------------------------- /es.js: -------------------------------------------------------------------------------- 1 | !function(){"use strict"; 2 | /*! (c) Andrea Giammarchi - ISC */const{apply:t,construct:e}=Reflect;Function.prototype.once||Object.defineProperty(Function.prototype,"once",{writable:!0,configurable:!0,value(){let n,o=this,c=!0;return function i(){return c&&(c=!1,n=this instanceof i?e(o,arguments):t(o,this,arguments)),n}}})}(); 3 | -------------------------------------------------------------------------------- /esm/index.js: -------------------------------------------------------------------------------- 1 | /*! (c) Andrea Giammarchi - ISC */ 2 | 3 | const {apply, construct} = Reflect; 4 | 5 | Function.prototype.once || Object.defineProperty( 6 | Function.prototype, 7 | 'once', 8 | { 9 | writable: true, 10 | configurable: true, 11 | value() { 12 | let fn = this, execute = true, returned; 13 | return function once() { 14 | if (execute) { 15 | execute = false; 16 | returned = this instanceof once ? 17 | construct(fn, arguments) : 18 | apply(fn, this, arguments); 19 | } 20 | return returned; 21 | }; 22 | } 23 | } 24 | ); 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 'use strict'; 3 | 4 | /*! (c) Andrea Giammarchi - ISC */ 5 | 6 | const {apply, construct} = Reflect; 7 | 8 | Function.prototype.once || Object.defineProperty( 9 | Function.prototype, 10 | 'once', 11 | { 12 | writable: true, 13 | configurable: true, 14 | value() { 15 | let fn = this, execute = true, returned; 16 | return function once() { 17 | if (execute) { 18 | execute = false; 19 | returned = this instanceof once ? 20 | construct(fn, arguments) : 21 | apply(fn, this, arguments); 22 | } 23 | return returned; 24 | }; 25 | } 26 | } 27 | ); 28 | 29 | })(); 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@ungap/once", 3 | "version": "0.1.2", 4 | "description": "Function.prototype.once polyfill", 5 | "main": "./cjs/index.js", 6 | "scripts": { 7 | "build": "npm run cjs && npm run rollup:es && npm run rollup:index && npm run test", 8 | "cjs": "ascjs esm cjs", 9 | "rollup:es": "rollup --config rollup/es.config.js && sed -i.bck 's/^var /self./' es.js && rm -rf es.js.bck", 10 | "rollup:index": "rollup --config rollup/index.config.js", 11 | "coveralls": "c8 report --reporter=text-lcov | coveralls", 12 | "test": "c8 node test/index.js" 13 | }, 14 | "keywords": [ 15 | "once", 16 | "polyfill", 17 | "function" 18 | ], 19 | "author": "Andrea Giammarchi", 20 | "license": "ISC", 21 | "devDependencies": { 22 | "@rollup/plugin-node-resolve": "^13.1.3", 23 | "ascjs": "^5.0.1", 24 | "c8": "^7.11.0", 25 | "coveralls": "^3.1.1", 26 | "rollup": "^2.70.1", 27 | "rollup-plugin-terser": "^7.0.2" 28 | }, 29 | "module": "./esm/index.js", 30 | "type": "module", 31 | "exports": { 32 | ".": { 33 | "import": "./esm/index.js", 34 | "default": "./cjs/index.js" 35 | }, 36 | "./package.json": "./package.json" 37 | }, 38 | "unpkg": "es.js", 39 | "repository": { 40 | "type": "git", 41 | "url": "git+https://github.com/ungap/once.git" 42 | }, 43 | "bugs": { 44 | "url": "https://github.com/ungap/once/issues" 45 | }, 46 | "homepage": "https://github.com/ungap/once#readme" 47 | } 48 | -------------------------------------------------------------------------------- /rollup/es.config.js: -------------------------------------------------------------------------------- 1 | import {nodeResolve} from '@rollup/plugin-node-resolve'; 2 | import {terser} from 'rollup-plugin-terser'; 3 | 4 | export default { 5 | input: './esm/index.js', 6 | plugins: [ 7 | 8 | nodeResolve(), 9 | terser() 10 | ], 11 | 12 | output: { 13 | esModule: false, 14 | exports: 'named', 15 | file: './es.js', 16 | format: 'iife', 17 | name: 'once' 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /rollup/index.config.js: -------------------------------------------------------------------------------- 1 | import {nodeResolve} from '@rollup/plugin-node-resolve'; 2 | 3 | export default { 4 | input: './esm/index.js', 5 | plugins: [ 6 | 7 | nodeResolve() 8 | ], 9 | 10 | output: { 11 | esModule: false, 12 | exports: 'named', 13 | file: './index.js', 14 | format: 'iife', 15 | name: 'once' 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |