├── .gitattributes ├── .stylelintrc.json ├── .gitignore ├── .npmignore ├── .editorconfig ├── inch.json ├── docs ├── CHANGELOG.md └── api.md ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── CONTRIBUTING.md └── workflows │ └── ci.yml ├── .eslintrc.js ├── .jsbeautifyrc ├── .config └── karma.conf.js ├── package.json ├── README.md ├── funcs.js ├── LICENSE └── test └── spec ├── funcs-node-spec.js └── funcs-web-spec.js /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | -------------------------------------------------------------------------------- /.stylelintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "stylelint-config-standard" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .temp 3 | node_modules 4 | bower_components 5 | npm-debug.log 6 | package-lock.json 7 | .nyc_output 8 | coverage 9 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | target 2 | .temp 3 | .nyc_output 4 | .config 5 | coverage 6 | bower_components 7 | .github 8 | test 9 | example 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | indent_style = space 10 | indent_size = 4 11 | 12 | [*.json] 13 | indent_size = 2 14 | -------------------------------------------------------------------------------- /inch.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": { 3 | "included": [ 4 | "*.js", 5 | "lib/**/*.js", 6 | "tasks/**/*.js" 7 | ], 8 | "excluded": [ 9 | "**/Gruntfile.js", 10 | "**/.eslintrc.js", 11 | "**/stylelint.config.js" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /docs/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | | Date | Version | Description | 2 | | ----------- | ------- | ----------- | 3 | | 2020-05-12 | v2.0.0 | Migrate to github actions, upgrade minimal node version and remove bower | 4 | | 2019-02-08 | v1.0.18 | Maintenance | 5 | | 2017-04-28 | v1.0.1 | Offical release | 6 | | 2017-04-28 | v0.0.6 | Docs | 7 | | 2017-04-28 | v0.0.5 | Rename package as funcs-js | 8 | | 2017-04-27 | v0.0.1 | Initial release | 9 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Feature Description 11 | 12 | 13 | ### Describe The Solution You'd Like 14 | 15 | 16 | ### Code Sample 17 | 18 | ```js 19 | // paste code here 20 | ``` 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Describe The Bug 11 | 12 | 13 | ### To Reproduce 14 | 15 | 16 | ### Error Stack 17 | 18 | ```console 19 | The error stack trace 20 | ``` 21 | 22 | ### Code Sample 23 | 24 | ```js 25 | // paste code here 26 | ``` 27 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'env': { 3 | 'browser': true, 4 | 'node': true, 5 | 'commonjs': true, 6 | 'es2021': true, 7 | 'mocha': true 8 | }, 9 | 'extends': 'eslint:recommended', 10 | 'parserOptions': { 11 | 'ecmaVersion': 13 12 | }, 13 | 'rules': { 14 | 'indent': [ 15 | 'error', 16 | 4 17 | ], 18 | 'linebreak-style': [ 19 | 'error', 20 | 'unix' 21 | ], 22 | 'quotes': [ 23 | 'error', 24 | 'single' 25 | ], 26 | 'semi': [ 27 | 'error', 28 | 'always' 29 | ] 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | 4 | ## Issues 5 | 6 | Found a bug? Got a question? Want some enhancement?
7 | First place to go is the repository issues section, and I'll try to help as much as possible. 8 | 9 | ## Pull Requests 10 | 11 | Fixed a bug or just want to provided additional functionality?
12 | Simply fork this repository, implement your changes and create a pull request.
13 | Few guidelines regarding pull requests: 14 | 15 | * This repository is integrated with github actions for continuous integration.
16 | 17 | Your pull request build must pass (the build will run automatically).
18 | You can run the following command locally to ensure the build will pass: 19 | 20 | ````sh 21 | npm test 22 | ```` 23 | 24 | * This library is using multiple code inspection tools to validate certain level of standards.
The configuration is part of the repository and you can set your favorite IDE using that configuration.
You can run the following command locally to ensure the code inspection passes: 25 | 26 | ````sh 27 | npm run lint 28 | ```` 29 | 30 | * There are many automatic unit tests as part of the library which provide full coverage of the functionality.
Any fix/enhancement must come with a set of tests to ensure it's working well. 31 | -------------------------------------------------------------------------------- /.jsbeautifyrc: -------------------------------------------------------------------------------- 1 | { 2 | "js": { 3 | "indent_size": 4, 4 | "indent_char": " ", 5 | "eol": "\n", 6 | "indent_level": 0, 7 | "indent_with_tabs": false, 8 | "preserve_newlines": true, 9 | "max_preserve_newlines": 2, 10 | "space_in_paren": false, 11 | "jslint_happy": true, 12 | "space_after_anon_function": true, 13 | "brace_style": "collapse", 14 | "break_chained_methods": false, 15 | "keep_array_indentation": true, 16 | "unescape_strings": false, 17 | "wrap_line_length": 0, 18 | "end_with_newline": true, 19 | "comma_first": false, 20 | "eval_code": false, 21 | "keep_function_indentation": false, 22 | "space_before_conditional": true, 23 | "good_stuff": true 24 | }, 25 | "css": { 26 | "indent_size": 2, 27 | "indent_char": " ", 28 | "indent_with_tabs": false, 29 | "eol": "\n", 30 | "end_with_newline": true, 31 | "selector_separator_newline": false, 32 | "newline_between_rules": true 33 | }, 34 | "html": { 35 | "indent_size": 4, 36 | "indent_char": " ", 37 | "indent_with_tabs": false, 38 | "eol": "\n", 39 | "end_with_newline": true, 40 | "preserve_newlines": true, 41 | "max_preserve_newlines": 2, 42 | "indent_inner_html": true, 43 | "brace_style": "collapse", 44 | "indent_scripts": "normal", 45 | "wrap_line_length": 0, 46 | "wrap_attributes": "auto", 47 | "wrap_attributes_indent_size": 4 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | env: 4 | CLICOLOR_FORCE: 1 5 | jobs: 6 | node-ci: 7 | name: Node CI 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: ['18.x'] 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v2 16 | - name: Install node.js 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - name: Install Dependencies 21 | run: npm install 22 | - name: Run CI 23 | run: npm test 24 | - name: Run Coverage 25 | run: npm run coverage 26 | - name: Coveralls 27 | uses: coverallsapp/github-action@master 28 | with: 29 | github-token: ${{ secrets.GITHUB_TOKEN }} 30 | path-to-lcov: './coverage/lcov.info' 31 | web-ci: 32 | name: Web CI 33 | runs-on: ubuntu-latest 34 | strategy: 35 | fail-fast: false 36 | env: 37 | CI_RUN_KARMA: true 38 | steps: 39 | - name: Checkout 40 | uses: actions/checkout@v2 41 | - name: Install Chrome 42 | uses: browser-actions/setup-chrome@latest 43 | - name: Install Dependencies 44 | run: npm install 45 | - name: Run CI 46 | run: npm test 47 | - name: Coveralls 48 | uses: coverallsapp/github-action@master 49 | with: 50 | github-token: ${{ secrets.GITHUB_TOKEN }} 51 | path-to-lcov: './coverage/lcov.info' 52 | -------------------------------------------------------------------------------- /.config/karma.conf.js: -------------------------------------------------------------------------------- 1 | /*global module: false, require: false */ 2 | 3 | module.exports = function (config) { 4 | 'use strict'; 5 | 6 | const mainJSFile = require('../package.json').main; 7 | 8 | config.set({ 9 | basePath: '../', 10 | frameworks: [ 11 | 'mocha', 12 | 'sinon-chai' 13 | ], 14 | port: 9876, 15 | logLevel: config.LOG_INFO, 16 | autoWatch: false, 17 | browsers: [ 18 | 'ChromiumHeadless' 19 | ], 20 | singleRun: false, 21 | reporters: [ 22 | 'progress', 23 | 'coverage' 24 | ], 25 | preprocessors: { 26 | [mainJSFile]: [ 27 | 'coverage' 28 | ] 29 | }, 30 | coverageReporter: { 31 | dir: 'coverage', 32 | reporters: [ 33 | { 34 | type: 'lcov', 35 | subdir: '.' 36 | } 37 | ], 38 | check: { 39 | global: { 40 | statements: 100, 41 | functions: 100, 42 | lines: 100, 43 | branches: 100 44 | } 45 | } 46 | }, 47 | customLaunchers: { 48 | ChromeHeadlessCI: { 49 | base: 'ChromeHeadless', 50 | flags: [ 51 | '--no-sandbox' 52 | ] 53 | } 54 | }, 55 | files: [ 56 | mainJSFile, 57 | 'test/spec/**/*web*.js' 58 | ] 59 | }); 60 | }; 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "funcs-js", 3 | "version": "2.0.0", 4 | "description": "Function wrappers for enhanced behavior.", 5 | "author": { 6 | "name": "Sagie Gur-Ari", 7 | "email": "sagiegurari@gmail.com" 8 | }, 9 | "license": "Apache-2.0", 10 | "homepage": "http://github.com/sagiegurari/funcs-js", 11 | "repository": { 12 | "type": "git", 13 | "url": "http://github.com/sagiegurari/funcs-js.git" 14 | }, 15 | "bugs": { 16 | "url": "http://github.com/sagiegurari/funcs-js/issues" 17 | }, 18 | "keywords": [ 19 | "function", 20 | "functions", 21 | "utilities" 22 | ], 23 | "main": "funcs.js", 24 | "directories": { 25 | "test": "test/spec", 26 | "example": "example" 27 | }, 28 | "scripts": { 29 | "clean": "rm -Rf ./.nyc_output ./coverage", 30 | "format": "js-beautify --config ./.jsbeautifyrc --file ./*.js ./test/**/*.js", 31 | "lint-js": "eslint ./*.js ./test/**/*.js", 32 | "lint-css": "stylelint --allow-empty-input ./docs/**/*.css", 33 | "lint": "npm run lint-js && npm run lint-css", 34 | "jstest": "mocha --exit ./test/spec/**/*node*.js && karma start --single-run", 35 | "coverage": "nyc --reporter=html --reporter=text --reporter=lcovonly --check-coverage=true mocha --exit ./test/spec/**/*node*.js", 36 | "docs": "jsdoc2md ./funcs.js > ./docs/api.md", 37 | "test": "npm run clean && npm run format && npm run lint && npm run docs && npm run jstest", 38 | "postpublish": "git fetch && git pull" 39 | }, 40 | "husky": { 41 | "hooks": { 42 | "pre-push": "npm run build" 43 | } 44 | }, 45 | "devDependencies": { 46 | "chai": "^4", 47 | "eslint": "^8", 48 | "js-beautify": "^1", 49 | "jsdoc-to-markdown": "^8", 50 | "karma": "^6", 51 | "karma-chrome-launcher": "^3", 52 | "karma-coverage": "^2", 53 | "karma-mocha": "^2", 54 | "karma-sinon-chai": "^2", 55 | "mocha": "^10", 56 | "nyc": "^15", 57 | "sinon": "^15", 58 | "sinon-chai": "^3", 59 | "stylelint": "^13", 60 | "stylelint-config-standard": "^22" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /docs/api.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## funcs : object 4 | Function wrappers and utilities for enhanced behavior. 5 | 6 | **Kind**: global namespace 7 | **Author**: Sagie Gur-Ari 8 | 9 | * [funcs](#funcs) : object 10 | * [.noop()](#funcs.noop) ⇒ undefined 11 | * [.isFunction([fn])](#funcs.isFunction) ⇒ Boolean 12 | * [.ensure([fn])](#funcs.ensure) ⇒ function 13 | * [.maxTimes(fn, times, [options])](#funcs.maxTimes) ⇒ function 14 | * [.once(fn, [options])](#funcs.once) ⇒ function 15 | * [.delay(fn, [delay], [options])](#funcs.delay) ⇒ function 16 | * [.async(fn, [options])](#funcs.async) ⇒ function 17 | 18 | 19 | 20 | ### funcs.noop() ⇒ undefined 21 | Empty function. 22 | 23 | **Returns**: undefined - Undefined 24 | **Access**: public 25 | 26 | 27 | ### funcs.isFunction([fn]) ⇒ Boolean 28 | Returns true if the provided argument is a function. 29 | 30 | **Returns**: Boolean - True if the provided argument is a function 31 | **Access**: public 32 | 33 | | Param | Type | Description | 34 | | --- | --- | --- | 35 | | [fn] | function | The function to check | 36 | 37 | **Example** 38 | ````js 39 | const isFn = funcs.isFunction(myFunction); 40 | 41 | funcs.isFunction(function () {}); //true 42 | funcs.isFunction(); //false 43 | funcs.isFunction(5); //false 44 | funcs.isFunction(true); //false 45 | ```` 46 | 47 | 48 | ### funcs.ensure([fn]) ⇒ function 49 | Ensures a return function.
50 | If a function is provided, it will be returned, otherwise a noop function will be returned. 51 | 52 | **Returns**: function - The original function if provided, or a noop 53 | **Access**: public 54 | 55 | | Param | Type | Description | 56 | | --- | --- | --- | 57 | | [fn] | function | The function to check | 58 | 59 | **Example** 60 | ````js 61 | const handler = funcs.ensure(maybeHandler); 62 | ```` 63 | 64 | 65 | ### funcs.maxTimes(fn, times, [options]) ⇒ function 66 | Wraps the provided function and ensures it is invoked no more than the provided amount.
67 | This function output can be chained with other funcs apis. 68 | 69 | **Returns**: function - The new wrapper function 70 | **Access**: public 71 | 72 | | Param | Type | Default | Description | 73 | | --- | --- | --- | --- | 74 | | fn | function | | The function to wrap | 75 | | times | Number | | The max times the provided function will be invoked | 76 | | [options] | Object | | see details | 77 | | [options.callbackStyle] | Boolean | false | If true, the provided function will only get the first 2 arguments (will improve runtime performance) | 78 | 79 | **Example** 80 | ````js 81 | const onlyOnceCallback = funcs.maxTimes(callback, 1); 82 | 83 | //can also chain multiple modifications (chained functions do not require original function as argument) 84 | const delayedMaxTimesCallback = funcs.maxTimes(callback, 5).delay(500); 85 | ```` 86 | 87 | 88 | ### funcs.once(fn, [options]) ⇒ function 89 | Ensures the provided function is invoked only once.
90 | This is the same as calling funcs.maxTimes(fn, 1)
91 | This function output can be chained with other funcs apis. 92 | 93 | **Returns**: function - The new wrapper function 94 | **Access**: public 95 | 96 | | Param | Type | Default | Description | 97 | | --- | --- | --- | --- | 98 | | fn | function | | The function to wrap | 99 | | [options] | Object | | see details | 100 | | [options.callbackStyle] | Boolean | false | If true, the provided function will only get the first 2 arguments (will improve runtime performance) | 101 | 102 | **Example** 103 | ````js 104 | const onlyOnceCallback = funcs.once(callback); 105 | 106 | //can also chain multiple modifications (chained functions do not require original function as argument) 107 | const asyncOnceCallback = funcs.once(callback).async(); 108 | ```` 109 | 110 | 111 | ### funcs.delay(fn, [delay], [options]) ⇒ function 112 | Trigger the actual function only after the provided delay.
113 | This function output can be chained with other funcs apis. 114 | 115 | **Returns**: function - The new wrapper function 116 | **Access**: public 117 | 118 | | Param | Type | Default | Description | 119 | | --- | --- | --- | --- | 120 | | fn | function | | The function to wrap | 121 | | [delay] | Number | 0 | The invocation delay in millies | 122 | | [options] | Object | | see details | 123 | | [options.callbackStyle] | Boolean | false | If true, the provided function will only get the first 2 arguments (will improve runtime performance) | 124 | 125 | **Example** 126 | ````js 127 | const delayedCallback = funcs.delay(callback, 500); 128 | 129 | //can also chain multiple modifications (chained functions do not require original function as argument) 130 | const delayedMaxTimesCallback = funcs.delay(callback, 500).maxTimes(5); 131 | ```` 132 | 133 | 134 | ### funcs.async(fn, [options]) ⇒ function 135 | Ensures the function is invoked only in the next cycle.
136 | This is the same as calling funcs.delay(fn, 0)
137 | This function output can be chained with other funcs apis. 138 | 139 | **Returns**: function - The new wrapper function 140 | **Access**: public 141 | 142 | | Param | Type | Default | Description | 143 | | --- | --- | --- | --- | 144 | | fn | function | | The function to wrap | 145 | | [options] | Object | | see details | 146 | | [options.callbackStyle] | Boolean | false | If true, the provided function will only get the first 2 arguments (will improve runtime performance) | 147 | 148 | **Example** 149 | ````js 150 | const asyncCallback = funcs.async(callback); 151 | 152 | //can also chain multiple modifications (chained functions do not require original function as argument) 153 | const asyncOnceCallback = funcs.async(callback).once(); 154 | ```` 155 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # funcs-js 2 | 3 | [![NPM Version](http://img.shields.io/npm/v/funcs-js.svg?style=flat)](https://www.npmjs.org/package/funcs-js) [![CI](https://github.com/sagiegurari/funcs-js/workflows/CI/badge.svg?branch=master)](https://github.com/sagiegurari/funcs-js/actions) [![Coverage Status](https://coveralls.io/repos/sagiegurari/funcs-js/badge.svg)](https://coveralls.io/r/sagiegurari/funcs-js) [![Known Vulnerabilities](https://snyk.io/test/github/sagiegurari/funcs-js/badge.svg)](https://snyk.io/test/github/sagiegurari/funcs-js) [![Inline docs](http://inch-ci.org/github/sagiegurari/funcs-js.svg?branch=master)](http://inch-ci.org/github/sagiegurari/funcs-js) [![License](https://img.shields.io/npm/l/funcs-js.svg?style=flat)](https://github.com/sagiegurari/funcs-js/blob/master/LICENSE) [![Total Downloads](https://img.shields.io/npm/dt/funcs-js.svg?style=flat)](https://www.npmjs.org/package/funcs-js) 4 | 5 | > Function wrappers for enhanced behavior. 6 | 7 | * [Overview](#overview) 8 | * [Usage](#usage) 9 | * [once](#usage-once) 10 | * [maxTimes](#usage-maxTimes) 11 | * [async](#usage-async) 12 | * [delay](#usage-delay) 13 | * [isFunction](#usage-isFunction) 14 | * [noop](#usage-noop) 15 | * [ensure](#usage-ensure) 16 | * [Installation](#installation) 17 | * [API Documentation](docs/api.md) 18 | * [Contributing](.github/CONTRIBUTING.md) 19 | * [Release History](#history) 20 | * [License](#license) 21 | 22 | 23 | ## Overview 24 | The funcs-js provides different utility functions which enable to wrap functions and modify their behavior. 25 | 26 | This library requires no external dependencies, and it is supported for both browser and node.js environments. 27 | 28 | 29 | ## Usage 30 | In order to use the library in browser, you first must add the relevant dependency: 31 | 32 | ```html 33 | 34 | ``` 35 | 36 | When using an AMD loader (such as RequireJS) or CommonJS type loader, the funcs object is not automatically defined on the window scope. 37 | 38 | For node.js, simply require the module as follows: 39 | 40 | ````js 41 | var funcs = require('funcs-js'); 42 | ```` 43 | 44 | 45 | 46 | ### 'funcs.once(fn, [options]) ⇒ function' 47 | Ensures the provided function is invoked only once.
48 | This is the same as calling funcs.maxTimes(fn, 1)
49 | This function output can be chained with other funcs apis. 50 | 51 | **Example** 52 | ````js 53 | const onlyOnceCallback = funcs.once(callback); 54 | 55 | //can also chain multiple modifications (chained functions do not require original function as argument) 56 | const asyncOnceCallback = funcs.once(callback).async(); 57 | ```` 58 | 59 | 60 | 61 | 62 | ### 'funcs.maxTimes(fn, times, [options]) ⇒ function' 63 | Wraps the provided function and ensures it is invoked no more than the provided amount.
64 | This function output can be chained with other funcs apis. 65 | 66 | **Example** 67 | ````js 68 | const onlyOnceCallback = funcs.maxTimes(callback, 1); 69 | 70 | //can also chain multiple modifications (chained functions do not require original function as argument) 71 | const delayedMaxTimesCallback = funcs.maxTimes(callback, 5).delay(500); 72 | ```` 73 | 74 | 75 | 76 | 77 | ### 'funcs.async(fn, [options]) ⇒ function' 78 | Ensures the function is invoked only in the next cycle.
79 | This is the same as calling funcs.delay(fn, 0)
80 | This function output can be chained with other funcs apis. 81 | 82 | **Example** 83 | ````js 84 | const asyncCallback = funcs.async(callback); 85 | 86 | //can also chain multiple modifications (chained functions do not require original function as argument) 87 | const asyncOnceCallback = funcs.async(callback).once(); 88 | ```` 89 | 90 | 91 | 92 | 93 | ### 'funcs.delay(fn, [delay], [options]) ⇒ function' 94 | Trigger the actual function only after the provided delay.
95 | This function output can be chained with other funcs apis. 96 | 97 | **Example** 98 | ````js 99 | const delayedCallback = funcs.delay(callback, 500); 100 | 101 | //can also chain multiple modifications (chained functions do not require original function as argument) 102 | const delayedMaxTimesCallback = funcs.delay(callback, 500).maxTimes(5); 103 | ```` 104 | 105 | 106 | 107 | 108 | ### 'funcs.isFunction([fn]) ⇒ Boolean' 109 | Returns true if the provided argument is a function. 110 | 111 | **Example** 112 | ````js 113 | const isFn = funcs.isFunction(myFunction); 114 | 115 | funcs.isFunction(function () {}); //true 116 | funcs.isFunction(); //false 117 | funcs.isFunction(5); //false 118 | funcs.isFunction(true); //false 119 | ```` 120 | 121 | 122 | 123 | 124 | ### 'funcs.noop() ⇒ undefined' 125 | Empty function. 126 | 127 | 128 | 129 | 130 | 131 | ### 'funcs.ensure([fn]) ⇒ function' 132 | Ensures a return function.
133 | If a function is provided, it will be returned, otherwise a noop function will be returned. 134 | 135 | **Example** 136 | ````js 137 | const handler = funcs.ensure(maybeHandler); 138 | ```` 139 | 140 | 141 | 142 | ## Installation 143 | Run npm install in your project as follows: 144 | 145 | ```sh 146 | npm install --save funcs-js 147 | ``` 148 | 149 | Or if you are using bower, you can install it as follows: 150 | 151 | ```sh 152 | bower install funcs-js --save 153 | ``` 154 | 155 | ## API Documentation 156 | See full docs at: [API Docs](docs/api.md) 157 | 158 | ## Contributing 159 | See [contributing guide](.github/CONTRIBUTING.md) 160 | 161 | 162 | ## Release History 163 | 164 | | Date | Version | Description | 165 | | ----------- | ------- | ----------- | 166 | | 2020-05-12 | v2.0.0 | Migrate to github actions, upgrade minimal node version and remove bower | 167 | | 2019-02-08 | v1.0.18 | Maintenance | 168 | | 2017-04-28 | v1.0.1 | Offical release | 169 | | 2017-04-28 | v0.0.6 | Docs | 170 | | 2017-04-28 | v0.0.5 | Rename package as funcs-js | 171 | | 2017-04-27 | v0.0.1 | Initial release | 172 | 173 | 174 | ## License 175 | Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license. 176 | -------------------------------------------------------------------------------- /funcs.js: -------------------------------------------------------------------------------- 1 | /*global define: false */ 2 | 3 | /** 4 | * Function wrappers and utilities for enhanced behavior. 5 | * 6 | * @name funcs 7 | * @namespace funcs 8 | * @author Sagie Gur-Ari 9 | */ 10 | 11 | /** 12 | * Initializes the funcs API. 13 | * 14 | * @function 15 | * @memberof! funcs 16 | * @alias funcs.initFuncs 17 | * @private 18 | * @param {Object} global - The root context (window/global/...) 19 | * @param {function} factory - Returns a new instance of the API 20 | * @returns {Object} New instance of the API 21 | */ 22 | (function initFuncs(global, factory) { 23 | 'use strict'; 24 | 25 | const funcs = factory(); 26 | 27 | /*istanbul ignore next*/ 28 | /** 29 | * Initializes the funcs API (only used for testing). 30 | * 31 | * @function 32 | * @memberof! funcs 33 | * @alias funcs.initFuncsFromContext 34 | * @private 35 | * @param {Object} context - The root context (window/global/...) 36 | * @returns {Object} New instance of the API 37 | */ 38 | funcs.initFuncsFromContext = function (context) { 39 | return initFuncs(context, factory); 40 | }; 41 | 42 | /*istanbul ignore next*/ 43 | if ((typeof define === 'function') && define.amd) { 44 | define(function defineLib() { 45 | return funcs; 46 | }); 47 | } else if ((typeof module === 'object') && module.exports) { 48 | module.exports = funcs; 49 | } else { 50 | global.funcs = funcs; 51 | } 52 | 53 | return funcs; 54 | }(this, function initFuncs() { 55 | 'use strict'; 56 | 57 | const funcs = {}; 58 | 59 | /** 60 | * Adds chaining support for the provided function. 61 | * 62 | * @function 63 | * @memberof! funcs 64 | * @alias funcs.addChaining 65 | * @private 66 | * @param {function} fn - Adds the funcs APIs to the provided function with this function as a context 67 | */ 68 | const addChaining = function (fn) { 69 | /** 70 | * Chain function. 71 | * 72 | * @function 73 | * @memberof! funcs 74 | * @alias funcs.maxTimesChain 75 | * @private 76 | * @param {Number} times - The max times the provided function will be invoked 77 | * @returns {function} The new wrapper function 78 | */ 79 | fn.maxTimes = function (times) { 80 | return funcs.maxTimes(fn, times); 81 | }; 82 | 83 | /** 84 | * Chain function. 85 | * 86 | * @function 87 | * @memberof! funcs 88 | * @alias funcs.onceChain 89 | * @private 90 | * @returns {function} The new wrapper function 91 | */ 92 | fn.once = function () { 93 | return funcs.once(fn); 94 | }; 95 | 96 | /** 97 | * Chain function. 98 | * 99 | * @function 100 | * @memberof! funcs 101 | * @alias funcs.delayChain 102 | * @private 103 | * @param {Number} delay - The invocation delay in millies 104 | * @returns {function} The new wrapper function 105 | */ 106 | fn.delay = function (delay) { 107 | return funcs.delay(fn, delay); 108 | }; 109 | 110 | /** 111 | * Chain function. 112 | * 113 | * @function 114 | * @memberof! funcs 115 | * @alias funcs.asyncChain 116 | * @private 117 | * @returns {function} The new wrapper function 118 | */ 119 | fn.async = function () { 120 | return funcs.async(fn); 121 | }; 122 | }; 123 | 124 | /** 125 | * Empty function. 126 | * 127 | * @function 128 | * @memberof! funcs 129 | * @alias funcs.noop 130 | * @public 131 | * @returns {undefined} Undefined 132 | */ 133 | funcs.noop = function () { 134 | return undefined; 135 | }; 136 | 137 | /** 138 | * Returns true if the provided argument is a function. 139 | * 140 | * @function 141 | * @memberof! funcs 142 | * @alias funcs.isFunction 143 | * @public 144 | * @param {function} [fn] - The function to check 145 | * @returns {Boolean} True if the provided argument is a function 146 | * @example 147 | * ````js 148 | * const isFn = funcs.isFunction(myFunction); 149 | * 150 | * funcs.isFunction(function () {}); //true 151 | * funcs.isFunction(); //false 152 | * funcs.isFunction(5); //false 153 | * funcs.isFunction(true); //false 154 | * ```` 155 | */ 156 | funcs.isFunction = function (fn) { 157 | return (fn && (typeof fn === 'function')) || false; 158 | }; 159 | 160 | /** 161 | * Ensures a return function.
162 | * If a function is provided, it will be returned, otherwise a noop function will be returned. 163 | * 164 | * @function 165 | * @memberof! funcs 166 | * @alias funcs.ensure 167 | * @public 168 | * @param {function} [fn] - The function to check 169 | * @returns {function} The original function if provided, or a noop 170 | * @example 171 | * ````js 172 | * const handler = funcs.ensure(maybeHandler); 173 | * ```` 174 | */ 175 | funcs.ensure = function (fn) { 176 | if (!this.isFunction(fn)) { 177 | return funcs.noop; 178 | } 179 | 180 | return fn; 181 | }; 182 | 183 | /** 184 | * Wraps the provided function and ensures it is invoked no more than the provided amount.
185 | * This function output can be chained with other funcs apis. 186 | * 187 | * @function 188 | * @memberof! funcs 189 | * @alias funcs.maxTimes 190 | * @public 191 | * @param {function} fn - The function to wrap 192 | * @param {Number} times - The max times the provided function will be invoked 193 | * @param {Object} [options] - see details 194 | * @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance) 195 | * @returns {function} The new wrapper function 196 | * @example 197 | * ````js 198 | * const onlyOnceCallback = funcs.maxTimes(callback, 1); 199 | * 200 | * //can also chain multiple modifications (chained functions do not require original function as argument) 201 | * const delayedMaxTimesCallback = funcs.maxTimes(callback, 5).delay(500); 202 | * ```` 203 | */ 204 | funcs.maxTimes = function (fn, times, options) { 205 | if ((!this.isFunction(fn)) || (!times) || (typeof times !== 'number') || (times < 0)) { 206 | return this.noop; 207 | } 208 | 209 | let callbackStyle = false; 210 | if (options && options.callbackStyle) { 211 | callbackStyle = true; 212 | } 213 | 214 | let counter = 0; 215 | 216 | const fnMaxTimesWrapper = function (arg1, arg2) { 217 | if (counter < times) { 218 | counter++; 219 | 220 | if (callbackStyle) { 221 | return fn(arg1, arg2); 222 | } 223 | 224 | if (!arguments.length) { 225 | return fn(); 226 | } 227 | 228 | return fn.apply(null, arguments); 229 | } 230 | }; 231 | 232 | //add chaining support 233 | addChaining(fnMaxTimesWrapper); 234 | 235 | return fnMaxTimesWrapper; 236 | }; 237 | 238 | /** 239 | * Ensures the provided function is invoked only once.
240 | * This is the same as calling funcs.maxTimes(fn, 1)
241 | * This function output can be chained with other funcs apis. 242 | * 243 | * @function 244 | * @memberof! funcs 245 | * @alias funcs.once 246 | * @public 247 | * @param {function} fn - The function to wrap 248 | * @param {Object} [options] - see details 249 | * @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance) 250 | * @returns {function} The new wrapper function 251 | * @example 252 | * ````js 253 | * const onlyOnceCallback = funcs.once(callback); 254 | * 255 | * //can also chain multiple modifications (chained functions do not require original function as argument) 256 | * const asyncOnceCallback = funcs.once(callback).async(); 257 | * ```` 258 | */ 259 | funcs.once = function (fn, options) { 260 | return this.maxTimes(fn, 1, options); 261 | }; 262 | 263 | /** 264 | * Trigger the actual function only after the provided delay.
265 | * This function output can be chained with other funcs apis. 266 | * 267 | * @function 268 | * @memberof! funcs 269 | * @alias funcs.delay 270 | * @public 271 | * @param {function} fn - The function to wrap 272 | * @param {Number} [delay=0] - The invocation delay in millies 273 | * @param {Object} [options] - see details 274 | * @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance) 275 | * @returns {function} The new wrapper function 276 | * @example 277 | * ````js 278 | * const delayedCallback = funcs.delay(callback, 500); 279 | * 280 | * //can also chain multiple modifications (chained functions do not require original function as argument) 281 | * const delayedMaxTimesCallback = funcs.delay(callback, 500).maxTimes(5); 282 | * ```` 283 | */ 284 | funcs.delay = function (fn, delay, options) { 285 | if (!this.isFunction(fn)) { 286 | return this.noop; 287 | } 288 | 289 | //if delay not provided, but options were provided 290 | if (delay && (typeof delay === 'object')) { 291 | options = delay; 292 | delay = 0; 293 | } 294 | 295 | delay = delay || 0; 296 | 297 | if (delay < 0) { 298 | return fn; 299 | } 300 | 301 | let callbackStyle = false; 302 | if (options && options.callbackStyle) { 303 | callbackStyle = true; 304 | } 305 | 306 | const fnDelayWrapper = function (arg1, arg2) { 307 | const fnArguments = arguments; 308 | 309 | setTimeout(function postDelay() { 310 | if (callbackStyle) { 311 | fn(arg1, arg2); 312 | } else if (!fnArguments.length) { 313 | fn(); 314 | } else { 315 | fn.apply(null, fnArguments); 316 | } 317 | }, delay); 318 | }; 319 | 320 | //add chaining support 321 | addChaining(fnDelayWrapper); 322 | 323 | return fnDelayWrapper; 324 | }; 325 | 326 | /** 327 | * Ensures the function is invoked only in the next cycle.
328 | * This is the same as calling funcs.delay(fn, 0)
329 | * This function output can be chained with other funcs apis. 330 | * 331 | * @function 332 | * @memberof! funcs 333 | * @alias funcs.async 334 | * @public 335 | * @param {function} fn - The function to wrap 336 | * @param {Object} [options] - see details 337 | * @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance) 338 | * @returns {function} The new wrapper function 339 | * @example 340 | * ````js 341 | * const asyncCallback = funcs.async(callback); 342 | * 343 | * //can also chain multiple modifications (chained functions do not require original function as argument) 344 | * const asyncOnceCallback = funcs.async(callback).once(); 345 | * ```` 346 | */ 347 | funcs.async = function (fn, options) { 348 | return this.delay(fn, 0, options); 349 | }; 350 | 351 | return funcs; 352 | })); 353 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /test/spec/funcs-node-spec.js: -------------------------------------------------------------------------------- 1 | /*jslint node: true, browser: false*/ 2 | 'use strict'; 3 | 4 | /*eslint no-implicit-globals: 'off', strict: ['error', 'global']*/ 5 | /*jshint node: true, browser: false*/ 6 | 7 | const chai = require('chai'); 8 | const assert = chai.assert; 9 | const funcs = require('../../funcs'); 10 | 11 | describe('funcs', function () { 12 | const createCounterFn = function (validateInput, callbackStyle) { 13 | let counter = 0; 14 | 15 | const fn = function (arg1, arg2, arg3) { 16 | if (validateInput) { 17 | assert.strictEqual(arg1, 1); 18 | if (callbackStyle) { 19 | if (typeof arg2 !== 'function') { 20 | assert.strictEqual(arg2, 'test'); 21 | } 22 | assert.isUndefined(arg3); 23 | } else { 24 | assert.strictEqual(arg2, 'test'); 25 | assert.strictEqual(arg3, false); 26 | } 27 | } 28 | 29 | counter++; 30 | 31 | const cb = arguments[arguments.length - 1]; 32 | if (cb && (typeof cb === 'function')) { 33 | cb(counter); 34 | } 35 | 36 | return counter; 37 | }; 38 | 39 | fn.getCounter = function () { 40 | return counter; 41 | }; 42 | 43 | return fn; 44 | }; 45 | 46 | describe('noop', function () { 47 | it('simple', function () { 48 | assert.isFunction(funcs.noop); 49 | 50 | assert.isUndefined(funcs.noop()); 51 | }); 52 | }); 53 | 54 | describe('isFunction', function () { 55 | it('no input', function () { 56 | assert.isFunction(funcs.isFunction); 57 | 58 | assert.isFalse(funcs.isFunction()); 59 | }); 60 | 61 | it('null', function () { 62 | assert.isFunction(funcs.isFunction); 63 | 64 | assert.isFalse(funcs.isFunction(null)); 65 | }); 66 | 67 | it('string', function () { 68 | assert.isFunction(funcs.isFunction); 69 | 70 | assert.isFalse(funcs.isFunction('test')); 71 | }); 72 | 73 | it('number', function () { 74 | assert.isFunction(funcs.isFunction); 75 | 76 | assert.isFalse(funcs.isFunction(5)); 77 | }); 78 | 79 | it('true', function () { 80 | assert.isFunction(funcs.isFunction); 81 | 82 | assert.isFalse(funcs.isFunction(true)); 83 | }); 84 | 85 | it('false', function () { 86 | assert.isFunction(funcs.isFunction); 87 | 88 | assert.isFalse(funcs.isFunction(false)); 89 | }); 90 | 91 | it('object', function () { 92 | assert.isFunction(funcs.isFunction); 93 | 94 | assert.isFalse(funcs.isFunction({})); 95 | }); 96 | 97 | it('function', function () { 98 | assert.isFunction(funcs.isFunction); 99 | 100 | assert.isTrue(funcs.isFunction(function () { 101 | return true; 102 | })); 103 | }); 104 | }); 105 | 106 | describe('ensure', function () { 107 | it('no input', function () { 108 | assert.isFunction(funcs.ensure); 109 | 110 | const output = funcs.ensure(); 111 | assert.isTrue(output === funcs.noop); 112 | }); 113 | 114 | it('null', function () { 115 | assert.isFunction(funcs.ensure); 116 | 117 | const output = funcs.ensure(null); 118 | assert.isTrue(output === funcs.noop); 119 | }); 120 | 121 | it('string', function () { 122 | assert.isFunction(funcs.ensure); 123 | 124 | const output = funcs.ensure('test'); 125 | assert.isTrue(output === funcs.noop); 126 | }); 127 | 128 | it('number', function () { 129 | assert.isFunction(funcs.ensure); 130 | 131 | const output = funcs.ensure(5); 132 | assert.isTrue(output === funcs.noop); 133 | }); 134 | 135 | it('true', function () { 136 | assert.isFunction(funcs.ensure); 137 | 138 | const output = funcs.ensure(true); 139 | assert.isTrue(output === funcs.noop); 140 | }); 141 | 142 | it('false', function () { 143 | assert.isFunction(funcs.ensure); 144 | 145 | const output = funcs.ensure(false); 146 | assert.isTrue(output === funcs.noop); 147 | }); 148 | 149 | it('object', function () { 150 | assert.isFunction(funcs.ensure); 151 | 152 | const output = funcs.ensure({}); 153 | assert.isTrue(output === funcs.noop); 154 | }); 155 | 156 | it('function', function () { 157 | assert.isFunction(funcs.ensure); 158 | 159 | const testFn = function () { 160 | return true; 161 | }; 162 | 163 | const output = funcs.ensure(testFn); 164 | assert.isTrue(output === testFn); 165 | }); 166 | }); 167 | 168 | describe('maxTimes', function () { 169 | it('no function', function () { 170 | assert.isFunction(funcs.maxTimes); 171 | 172 | const output = funcs.maxTimes(10, 5); 173 | assert.isTrue(output === funcs.noop); 174 | }); 175 | 176 | it('times not a number', function () { 177 | assert.isFunction(funcs.maxTimes); 178 | 179 | const fn = createCounterFn(); 180 | const output = funcs.maxTimes(fn, {}); 181 | assert.isTrue(output === funcs.noop); 182 | }); 183 | 184 | it('times negative', function () { 185 | assert.isFunction(funcs.maxTimes); 186 | 187 | const fn = createCounterFn(); 188 | const output = funcs.maxTimes(fn, -1); 189 | assert.isTrue(output === funcs.noop); 190 | }); 191 | 192 | it('times is 0', function () { 193 | assert.isFunction(funcs.maxTimes); 194 | 195 | const fn = createCounterFn(); 196 | const output = funcs.maxTimes(fn, 0); 197 | assert.strictEqual(fn.getCounter(), 0); 198 | assert.isUndefined(output()); 199 | assert.strictEqual(fn.getCounter(), 0); 200 | fn(); 201 | assert.strictEqual(fn.getCounter(), 1); 202 | }); 203 | 204 | it('times is positive', function () { 205 | assert.isFunction(funcs.maxTimes); 206 | 207 | const times = 5; 208 | const fn = createCounterFn(); 209 | const output = funcs.maxTimes(fn, times); 210 | assert.strictEqual(fn.getCounter(), 0); 211 | for (let index = 0; index < times; index++) { 212 | assert.strictEqual(output(), (index + 1)); 213 | } 214 | assert.isUndefined(output()); 215 | assert.strictEqual(fn.getCounter(), times); 216 | fn(); 217 | assert.strictEqual(fn.getCounter(), times + 1); 218 | }); 219 | 220 | it('validate input proxy', function () { 221 | assert.isFunction(funcs.maxTimes); 222 | 223 | const fn = createCounterFn(true); 224 | const output = funcs.maxTimes(fn, 1); 225 | assert.strictEqual(fn.getCounter(), 0); 226 | assert.strictEqual(output(1, 'test', false), 1); 227 | assert.isUndefined(output(1, 'test', false)); 228 | assert.strictEqual(fn.getCounter(), 1); 229 | fn(1, 'test', false); 230 | assert.strictEqual(fn.getCounter(), 2); 231 | }); 232 | 233 | it('no args', function () { 234 | assert.isFunction(funcs.maxTimes); 235 | 236 | const times = 5; 237 | let counter = 0; 238 | const output = funcs.maxTimes(function () { 239 | counter++; 240 | assert.strictEqual(arguments.length, 0); 241 | }, times); 242 | for (let index = 0; index < (times + 5); index++) { 243 | output(); 244 | } 245 | 246 | assert.strictEqual(counter, times); 247 | }); 248 | 249 | it('callback style', function () { 250 | assert.isFunction(funcs.maxTimes); 251 | 252 | const fn = createCounterFn(true, true); 253 | const output = funcs.maxTimes(fn, 1, { 254 | callbackStyle: true 255 | }); 256 | assert.strictEqual(fn.getCounter(), 0); 257 | assert.strictEqual(output(1, 'test', 'bad'), 1); 258 | assert.isUndefined(output(1, 'test', 'bad')); 259 | assert.strictEqual(fn.getCounter(), 1); 260 | fn(1, 'test'); 261 | assert.strictEqual(fn.getCounter(), 2); 262 | }); 263 | 264 | it('chaining', function (done) { 265 | assert.isFunction(funcs.maxTimes); 266 | 267 | const fn = createCounterFn(); 268 | const output = funcs.maxTimes(fn, 1).async(); 269 | assert.strictEqual(fn.getCounter(), 0); 270 | const result = output(function (counter) { 271 | assert.strictEqual(counter, 1); 272 | 273 | output(function () { 274 | assert.fail(); 275 | }); 276 | 277 | setTimeout(done, 10); 278 | }); 279 | assert.isUndefined(result); 280 | }); 281 | }); 282 | 283 | describe('once', function () { 284 | it('no function', function () { 285 | assert.isFunction(funcs.once); 286 | 287 | const output = funcs.once(10); 288 | assert.isTrue(output === funcs.noop); 289 | }); 290 | 291 | it('once', function () { 292 | assert.isFunction(funcs.once); 293 | 294 | const fn = createCounterFn(); 295 | const output = funcs.once(fn); 296 | assert.strictEqual(fn.getCounter(), 0); 297 | assert.strictEqual(output(), 1); 298 | assert.isUndefined(output()); 299 | assert.strictEqual(fn.getCounter(), 1); 300 | fn(); 301 | assert.strictEqual(fn.getCounter(), 2); 302 | }); 303 | 304 | it('validate input proxy', function () { 305 | assert.isFunction(funcs.once); 306 | 307 | const fn = createCounterFn(true); 308 | const output = funcs.once(fn); 309 | assert.strictEqual(fn.getCounter(), 0); 310 | assert.strictEqual(output(1, 'test', false), 1); 311 | assert.isUndefined(output(1, 'test', false)); 312 | assert.strictEqual(fn.getCounter(), 1); 313 | fn(1, 'test', false); 314 | assert.strictEqual(fn.getCounter(), 2); 315 | }); 316 | 317 | it('no args', function () { 318 | assert.isFunction(funcs.once); 319 | 320 | let counter = 0; 321 | const output = funcs.once(function () { 322 | counter++; 323 | assert.strictEqual(arguments.length, 0); 324 | }); 325 | output(); 326 | output(); 327 | output(); 328 | 329 | assert.strictEqual(counter, 1); 330 | }); 331 | 332 | it('callback style', function () { 333 | assert.isFunction(funcs.once); 334 | 335 | const fn = createCounterFn(true, true); 336 | const output = funcs.once(fn, { 337 | callbackStyle: true 338 | }); 339 | assert.strictEqual(fn.getCounter(), 0); 340 | assert.strictEqual(output(1, 'test', 'bad'), 1); 341 | assert.isUndefined(output(1, 'test', 'bad')); 342 | assert.strictEqual(fn.getCounter(), 1); 343 | fn(1, 'test'); 344 | assert.strictEqual(fn.getCounter(), 2); 345 | }); 346 | 347 | it('chaining', function (done) { 348 | assert.isFunction(funcs.once); 349 | 350 | const fn = createCounterFn(); 351 | const output = funcs.once(fn).delay(0); 352 | assert.strictEqual(fn.getCounter(), 0); 353 | const result = output(function (counter) { 354 | assert.strictEqual(counter, 1); 355 | 356 | output(function () { 357 | assert.fail(); 358 | }); 359 | 360 | setTimeout(done, 10); 361 | }); 362 | assert.isUndefined(result); 363 | }); 364 | }); 365 | 366 | describe('delay', function () { 367 | it('no function', function () { 368 | assert.isFunction(funcs.delay); 369 | 370 | const output = funcs.delay(10, 10); 371 | assert.isTrue(output === funcs.noop); 372 | }); 373 | 374 | it('delay not provided', function (done) { 375 | assert.isFunction(funcs.delay); 376 | 377 | const fn = createCounterFn(); 378 | const output = funcs.delay(fn); 379 | assert.isFalse(output === fn); 380 | output(function (counter) { 381 | assert.strictEqual(counter, 1); 382 | 383 | done(); 384 | }); 385 | }); 386 | 387 | it('negative delay', function () { 388 | assert.isFunction(funcs.delay); 389 | 390 | const fn = createCounterFn(); 391 | const output = funcs.delay(fn, -1); 392 | assert.isTrue(output === fn); 393 | }); 394 | 395 | it('delay is 0', function (done) { 396 | assert.isFunction(funcs.delay); 397 | 398 | const fn = createCounterFn(); 399 | const output = funcs.delay(fn, 0); 400 | assert.isFalse(output === fn); 401 | output(function (counter) { 402 | assert.strictEqual(counter, 1); 403 | assert.strictEqual(fn.getCounter(), 1); 404 | 405 | done(); 406 | }); 407 | }); 408 | 409 | it('delay is positive', function (done) { 410 | assert.isFunction(funcs.delay); 411 | 412 | const delay = 20; 413 | const fn = createCounterFn(); 414 | const output = funcs.delay(fn, delay); 415 | assert.isFalse(output === fn); 416 | const startTime = Date.now(); 417 | output(function (counter) { 418 | assert.strictEqual(counter, 1); 419 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 420 | assert.strictEqual(fn.getCounter(), 1); 421 | 422 | done(); 423 | }); 424 | }); 425 | 426 | it('validate input proxy', function (done) { 427 | assert.isFunction(funcs.delay); 428 | 429 | const delay = 20; 430 | const fn = createCounterFn(true); 431 | const output = funcs.delay(fn, delay); 432 | assert.isFalse(output === fn); 433 | const startTime = Date.now(); 434 | output(1, 'test', false, function (counter) { 435 | assert.strictEqual(counter, 1); 436 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 437 | assert.strictEqual(fn.getCounter(), 1); 438 | 439 | done(); 440 | }); 441 | }); 442 | 443 | it('no args', function (done) { 444 | assert.isFunction(funcs.delay); 445 | 446 | const fn = createCounterFn(true, true); 447 | const output = funcs.delay(function () { 448 | assert.strictEqual(arguments.length, 0); 449 | 450 | done(); 451 | }, 0); 452 | assert.isFalse(output === fn); 453 | 454 | output(); 455 | }); 456 | 457 | it('callback style', function (done) { 458 | assert.isFunction(funcs.delay); 459 | 460 | const delay = 20; 461 | const fn = createCounterFn(true, true); 462 | const output = funcs.delay(fn, delay, { 463 | callbackStyle: true 464 | }); 465 | assert.isFalse(output === fn); 466 | const startTime = Date.now(); 467 | output(1, function (counter) { 468 | assert.strictEqual(counter, 1); 469 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 470 | assert.strictEqual(fn.getCounter(), 1); 471 | 472 | done(); 473 | }, 'bad'); 474 | }); 475 | 476 | it('options provided without delay', function (done) { 477 | assert.isFunction(funcs.delay); 478 | 479 | const fn = createCounterFn(true, true); 480 | const output = funcs.delay(fn, { 481 | callbackStyle: true 482 | }); 483 | assert.isFalse(output === fn); 484 | output(1, function (counter) { 485 | assert.strictEqual(counter, 1); 486 | assert.strictEqual(fn.getCounter(), 1); 487 | 488 | done(); 489 | }, 'bad'); 490 | }); 491 | 492 | it('chaining', function (done) { 493 | assert.isFunction(funcs.delay); 494 | 495 | const delay = 20; 496 | const fn = createCounterFn(); 497 | const output = funcs.delay(fn, delay).once(); 498 | assert.strictEqual(fn.getCounter(), 0); 499 | const startTime = Date.now(); 500 | output(function (counter) { 501 | assert.strictEqual(counter, 1); 502 | assert.isTrue((Date.now() - startTime) >= delay); 503 | assert.strictEqual(fn.getCounter(), 1); 504 | 505 | output(function () { 506 | assert.fail(); 507 | }); 508 | 509 | setTimeout(done, delay + 50); 510 | }); 511 | }); 512 | }); 513 | 514 | describe('async', function () { 515 | it('no function', function () { 516 | assert.isFunction(funcs.async); 517 | 518 | const output = funcs.async(10); 519 | assert.isTrue(output === funcs.noop); 520 | }); 521 | 522 | it('async', function (done) { 523 | assert.isFunction(funcs.async); 524 | 525 | const fn = createCounterFn(); 526 | const output = funcs.async(fn); 527 | assert.isFalse(output === fn); 528 | let invoked = false; 529 | output(function (counter) { 530 | invoked = true; 531 | assert.strictEqual(counter, 1); 532 | assert.strictEqual(fn.getCounter(), 1); 533 | 534 | done(); 535 | }); 536 | 537 | assert.isFalse(invoked); 538 | }); 539 | 540 | it('validate input proxy', function (done) { 541 | assert.isFunction(funcs.async); 542 | 543 | const fn = createCounterFn(true); 544 | const output = funcs.async(fn); 545 | assert.isFalse(output === fn); 546 | let invoked = false; 547 | output(1, 'test', false, function (counter) { 548 | invoked = true; 549 | assert.strictEqual(counter, 1); 550 | assert.strictEqual(fn.getCounter(), 1); 551 | 552 | done(); 553 | }); 554 | 555 | assert.isFalse(invoked); 556 | }); 557 | 558 | it('no args', function (done) { 559 | assert.isFunction(funcs.async); 560 | 561 | const fn = createCounterFn(true, true); 562 | const output = funcs.async(function () { 563 | assert.strictEqual(arguments.length, 0); 564 | 565 | done(); 566 | }); 567 | assert.isFalse(output === fn); 568 | 569 | output(); 570 | }); 571 | 572 | it('callback style', function (done) { 573 | assert.isFunction(funcs.async); 574 | 575 | const fn = createCounterFn(true, true); 576 | const output = funcs.async(fn, { 577 | callbackStyle: true 578 | }); 579 | assert.isFalse(output === fn); 580 | let invoked = false; 581 | output(1, function (counter) { 582 | invoked = true; 583 | assert.strictEqual(counter, 1); 584 | assert.strictEqual(fn.getCounter(), 1); 585 | 586 | done(); 587 | }, 'bad'); 588 | 589 | assert.isFalse(invoked); 590 | }); 591 | 592 | it('chaining', function (done) { 593 | assert.isFunction(funcs.async); 594 | 595 | const fn = createCounterFn(); 596 | const output = funcs.async(fn).maxTimes(1); 597 | assert.strictEqual(fn.getCounter(), 0); 598 | let invoked = false; 599 | output(function (counter) { 600 | invoked = true; 601 | assert.strictEqual(counter, 1); 602 | assert.strictEqual(fn.getCounter(), 1); 603 | 604 | output(function () { 605 | assert.fail(); 606 | }); 607 | 608 | setTimeout(done, 50); 609 | }); 610 | 611 | assert.isFalse(invoked); 612 | }); 613 | }); 614 | }); 615 | -------------------------------------------------------------------------------- /test/spec/funcs-web-spec.js: -------------------------------------------------------------------------------- 1 | /*global assert: false */ 2 | 3 | describe('funcs', function () { 4 | 'use strict'; 5 | 6 | const createCounterFn = function (validateInput, callbackStyle) { 7 | let counter = 0; 8 | 9 | const fn = function (arg1, arg2, arg3) { 10 | if (validateInput) { 11 | assert.strictEqual(arg1, 1); 12 | if (callbackStyle) { 13 | if (typeof arg2 !== 'function') { 14 | assert.strictEqual(arg2, 'test'); 15 | } 16 | assert.isUndefined(arg3); 17 | } else { 18 | assert.strictEqual(arg2, 'test'); 19 | assert.strictEqual(arg3, false); 20 | } 21 | } 22 | 23 | counter++; 24 | 25 | const cb = arguments[arguments.length - 1]; 26 | if (cb && (typeof cb === 'function')) { 27 | cb(counter); 28 | } 29 | 30 | return counter; 31 | }; 32 | 33 | fn.getCounter = function () { 34 | return counter; 35 | }; 36 | 37 | return fn; 38 | }; 39 | 40 | describe('init', function () { 41 | it('window', function () { 42 | assert.isObject(window.funcs); 43 | assert.isFunction(window.funcs.initFuncsFromContext); 44 | assert.isFunction(window.funcs.async); 45 | }); 46 | 47 | it('global', function () { 48 | const global = {}; 49 | 50 | window.funcs.initFuncsFromContext(global); 51 | 52 | assert.isObject(global.funcs); 53 | assert.isFunction(global.funcs.async); 54 | }); 55 | 56 | it('define', function () { 57 | const global = {}; 58 | 59 | window.define = function (factory) { 60 | const funcs = factory(); 61 | 62 | assert.isObject(funcs); 63 | assert.isFunction(funcs.async); 64 | 65 | delete window.define; 66 | }; 67 | window.define.amd = true; 68 | 69 | window.funcs.initFuncsFromContext(global); 70 | }); 71 | 72 | it('module', function () { 73 | const global = {}; 74 | 75 | window.module = { 76 | exports: {} 77 | }; 78 | 79 | window.funcs.initFuncsFromContext(global); 80 | 81 | assert.isFunction(window.module.exports.async); 82 | 83 | delete window.module; 84 | }); 85 | }); 86 | 87 | describe('noop', function () { 88 | it('simple', function () { 89 | const funcs = window.funcs; 90 | assert.isDefined(funcs); 91 | 92 | assert.isFunction(funcs.noop); 93 | 94 | assert.isUndefined(funcs.noop()); 95 | }); 96 | }); 97 | 98 | describe('isFunction', function () { 99 | it('no input', function () { 100 | const funcs = window.funcs; 101 | assert.isDefined(funcs); 102 | 103 | assert.isFunction(funcs.isFunction); 104 | 105 | assert.isFalse(funcs.isFunction()); 106 | }); 107 | 108 | it('null', function () { 109 | const funcs = window.funcs; 110 | assert.isDefined(funcs); 111 | 112 | assert.isFunction(funcs.isFunction); 113 | 114 | assert.isFalse(funcs.isFunction(null)); 115 | }); 116 | 117 | it('string', function () { 118 | const funcs = window.funcs; 119 | assert.isDefined(funcs); 120 | 121 | assert.isFunction(funcs.isFunction); 122 | 123 | assert.isFalse(funcs.isFunction('test')); 124 | }); 125 | 126 | it('number', function () { 127 | const funcs = window.funcs; 128 | assert.isDefined(funcs); 129 | 130 | assert.isFunction(funcs.isFunction); 131 | 132 | assert.isFalse(funcs.isFunction(5)); 133 | }); 134 | 135 | it('true', function () { 136 | const funcs = window.funcs; 137 | assert.isDefined(funcs); 138 | 139 | assert.isFunction(funcs.isFunction); 140 | 141 | assert.isFalse(funcs.isFunction(true)); 142 | }); 143 | 144 | it('false', function () { 145 | const funcs = window.funcs; 146 | assert.isDefined(funcs); 147 | 148 | assert.isFunction(funcs.isFunction); 149 | 150 | assert.isFalse(funcs.isFunction(false)); 151 | }); 152 | 153 | it('object', function () { 154 | const funcs = window.funcs; 155 | assert.isDefined(funcs); 156 | 157 | assert.isFunction(funcs.isFunction); 158 | 159 | assert.isFalse(funcs.isFunction({})); 160 | }); 161 | 162 | it('function', function () { 163 | const funcs = window.funcs; 164 | assert.isDefined(funcs); 165 | 166 | assert.isFunction(funcs.isFunction); 167 | 168 | assert.isTrue(funcs.isFunction(function () { 169 | return true; 170 | })); 171 | }); 172 | }); 173 | 174 | describe('ensure', function () { 175 | it('no input', function () { 176 | const funcs = window.funcs; 177 | assert.isDefined(funcs); 178 | 179 | assert.isFunction(funcs.ensure); 180 | 181 | const output = funcs.ensure(); 182 | assert.isTrue(output === funcs.noop); 183 | }); 184 | 185 | it('null', function () { 186 | const funcs = window.funcs; 187 | assert.isDefined(funcs); 188 | 189 | assert.isFunction(funcs.ensure); 190 | 191 | const output = funcs.ensure(null); 192 | assert.isTrue(output === funcs.noop); 193 | }); 194 | 195 | it('string', function () { 196 | const funcs = window.funcs; 197 | assert.isDefined(funcs); 198 | 199 | assert.isFunction(funcs.ensure); 200 | 201 | const output = funcs.ensure('test'); 202 | assert.isTrue(output === funcs.noop); 203 | }); 204 | 205 | it('number', function () { 206 | const funcs = window.funcs; 207 | assert.isDefined(funcs); 208 | 209 | assert.isFunction(funcs.ensure); 210 | 211 | const output = funcs.ensure(5); 212 | assert.isTrue(output === funcs.noop); 213 | }); 214 | 215 | it('true', function () { 216 | const funcs = window.funcs; 217 | assert.isDefined(funcs); 218 | 219 | assert.isFunction(funcs.ensure); 220 | 221 | const output = funcs.ensure(true); 222 | assert.isTrue(output === funcs.noop); 223 | }); 224 | 225 | it('false', function () { 226 | const funcs = window.funcs; 227 | assert.isDefined(funcs); 228 | 229 | assert.isFunction(funcs.ensure); 230 | 231 | const output = funcs.ensure(false); 232 | assert.isTrue(output === funcs.noop); 233 | }); 234 | 235 | it('object', function () { 236 | const funcs = window.funcs; 237 | assert.isDefined(funcs); 238 | 239 | assert.isFunction(funcs.ensure); 240 | 241 | const output = funcs.ensure({}); 242 | assert.isTrue(output === funcs.noop); 243 | }); 244 | 245 | it('function', function () { 246 | const funcs = window.funcs; 247 | assert.isDefined(funcs); 248 | 249 | assert.isFunction(funcs.ensure); 250 | 251 | const testFn = function () { 252 | return true; 253 | }; 254 | 255 | const output = funcs.ensure(testFn); 256 | assert.isTrue(output === testFn); 257 | }); 258 | }); 259 | 260 | describe('maxTimes', function () { 261 | it('no function', function () { 262 | const funcs = window.funcs; 263 | assert.isDefined(funcs); 264 | 265 | assert.isFunction(funcs.maxTimes); 266 | 267 | const output = funcs.maxTimes(10, 5); 268 | assert.isTrue(output === funcs.noop); 269 | }); 270 | 271 | it('times not a number', function () { 272 | const funcs = window.funcs; 273 | assert.isDefined(funcs); 274 | 275 | assert.isFunction(funcs.maxTimes); 276 | 277 | const fn = createCounterFn(); 278 | const output = funcs.maxTimes(fn, {}); 279 | assert.isTrue(output === funcs.noop); 280 | }); 281 | 282 | it('times negative', function () { 283 | const funcs = window.funcs; 284 | assert.isDefined(funcs); 285 | 286 | assert.isFunction(funcs.maxTimes); 287 | 288 | const fn = createCounterFn(); 289 | const output = funcs.maxTimes(fn, -1); 290 | assert.isTrue(output === funcs.noop); 291 | }); 292 | 293 | it('times is 0', function () { 294 | const funcs = window.funcs; 295 | assert.isDefined(funcs); 296 | 297 | assert.isFunction(funcs.maxTimes); 298 | 299 | const fn = createCounterFn(); 300 | const output = funcs.maxTimes(fn, 0); 301 | assert.strictEqual(fn.getCounter(), 0); 302 | assert.isUndefined(output()); 303 | assert.strictEqual(fn.getCounter(), 0); 304 | fn(); 305 | assert.strictEqual(fn.getCounter(), 1); 306 | }); 307 | 308 | it('times is positive', function () { 309 | const funcs = window.funcs; 310 | assert.isDefined(funcs); 311 | 312 | assert.isFunction(funcs.maxTimes); 313 | 314 | const times = 5; 315 | const fn = createCounterFn(); 316 | const output = funcs.maxTimes(fn, times); 317 | assert.strictEqual(fn.getCounter(), 0); 318 | for (let index = 0; index < times; index++) { 319 | assert.strictEqual(output(), (index + 1)); 320 | } 321 | assert.isUndefined(output()); 322 | assert.strictEqual(fn.getCounter(), times); 323 | fn(); 324 | assert.strictEqual(fn.getCounter(), times + 1); 325 | }); 326 | 327 | it('validate input proxy', function () { 328 | const funcs = window.funcs; 329 | assert.isDefined(funcs); 330 | 331 | assert.isFunction(funcs.maxTimes); 332 | 333 | const fn = createCounterFn(true); 334 | const output = funcs.maxTimes(fn, 1); 335 | assert.strictEqual(fn.getCounter(), 0); 336 | assert.strictEqual(output(1, 'test', false), 1); 337 | assert.isUndefined(output(1, 'test', false)); 338 | assert.strictEqual(fn.getCounter(), 1); 339 | fn(1, 'test', false); 340 | assert.strictEqual(fn.getCounter(), 2); 341 | }); 342 | 343 | it('no args', function () { 344 | const funcs = window.funcs; 345 | assert.isDefined(funcs); 346 | 347 | assert.isFunction(funcs.maxTimes); 348 | 349 | const times = 5; 350 | let counter = 0; 351 | const output = funcs.maxTimes(function () { 352 | counter++; 353 | assert.strictEqual(arguments.length, 0); 354 | }, times); 355 | for (let index = 0; index < (times + 5); index++) { 356 | output(); 357 | } 358 | 359 | assert.strictEqual(counter, times); 360 | }); 361 | 362 | it('callback style', function () { 363 | const funcs = window.funcs; 364 | assert.isDefined(funcs); 365 | 366 | assert.isFunction(funcs.maxTimes); 367 | 368 | const fn = createCounterFn(true, true); 369 | const output = funcs.maxTimes(fn, 1, { 370 | callbackStyle: true 371 | }); 372 | assert.strictEqual(fn.getCounter(), 0); 373 | assert.strictEqual(output(1, 'test', 'bad'), 1); 374 | assert.isUndefined(output(1, 'test', 'bad')); 375 | assert.strictEqual(fn.getCounter(), 1); 376 | fn(1, 'test'); 377 | assert.strictEqual(fn.getCounter(), 2); 378 | }); 379 | 380 | it('chaining', function (done) { 381 | const funcs = window.funcs; 382 | assert.isDefined(funcs); 383 | 384 | assert.isFunction(funcs.maxTimes); 385 | 386 | const fn = createCounterFn(); 387 | const output = funcs.maxTimes(fn, 1).async(); 388 | assert.strictEqual(fn.getCounter(), 0); 389 | const result = output(function (counter) { 390 | assert.strictEqual(counter, 1); 391 | 392 | output(function () { 393 | assert.fail(); 394 | }); 395 | 396 | setTimeout(done, 10); 397 | }); 398 | assert.isUndefined(result); 399 | }); 400 | }); 401 | 402 | describe('once', function () { 403 | it('no function', function () { 404 | const funcs = window.funcs; 405 | assert.isDefined(funcs); 406 | 407 | assert.isFunction(funcs.once); 408 | 409 | const output = funcs.once(10); 410 | assert.isTrue(output === funcs.noop); 411 | }); 412 | 413 | it('once', function () { 414 | const funcs = window.funcs; 415 | assert.isDefined(funcs); 416 | 417 | assert.isFunction(funcs.once); 418 | 419 | const fn = createCounterFn(); 420 | const output = funcs.once(fn); 421 | assert.strictEqual(fn.getCounter(), 0); 422 | assert.strictEqual(output(), 1); 423 | assert.isUndefined(output()); 424 | assert.strictEqual(fn.getCounter(), 1); 425 | fn(); 426 | assert.strictEqual(fn.getCounter(), 2); 427 | }); 428 | 429 | it('validate input proxy', function () { 430 | const funcs = window.funcs; 431 | assert.isDefined(funcs); 432 | 433 | assert.isFunction(funcs.once); 434 | 435 | const fn = createCounterFn(true); 436 | const output = funcs.once(fn); 437 | assert.strictEqual(fn.getCounter(), 0); 438 | assert.strictEqual(output(1, 'test', false), 1); 439 | assert.isUndefined(output(1, 'test', false)); 440 | assert.strictEqual(fn.getCounter(), 1); 441 | fn(1, 'test', false); 442 | assert.strictEqual(fn.getCounter(), 2); 443 | }); 444 | 445 | it('no args', function () { 446 | const funcs = window.funcs; 447 | assert.isDefined(funcs); 448 | 449 | assert.isFunction(funcs.once); 450 | 451 | let counter = 0; 452 | const output = funcs.once(function () { 453 | counter++; 454 | assert.strictEqual(arguments.length, 0); 455 | }); 456 | output(); 457 | output(); 458 | output(); 459 | 460 | assert.strictEqual(counter, 1); 461 | }); 462 | 463 | it('callback style', function () { 464 | const funcs = window.funcs; 465 | assert.isDefined(funcs); 466 | 467 | assert.isFunction(funcs.once); 468 | 469 | const fn = createCounterFn(true, true); 470 | const output = funcs.once(fn, { 471 | callbackStyle: true 472 | }); 473 | assert.strictEqual(fn.getCounter(), 0); 474 | assert.strictEqual(output(1, 'test', 'bad'), 1); 475 | assert.isUndefined(output(1, 'test', 'bad')); 476 | assert.strictEqual(fn.getCounter(), 1); 477 | fn(1, 'test'); 478 | assert.strictEqual(fn.getCounter(), 2); 479 | }); 480 | 481 | it('chaining', function (done) { 482 | const funcs = window.funcs; 483 | assert.isDefined(funcs); 484 | 485 | assert.isFunction(funcs.once); 486 | 487 | const fn = createCounterFn(); 488 | const output = funcs.once(fn).delay(0); 489 | assert.strictEqual(fn.getCounter(), 0); 490 | const result = output(function (counter) { 491 | assert.strictEqual(counter, 1); 492 | 493 | output(function () { 494 | assert.fail(); 495 | }); 496 | 497 | setTimeout(done, 10); 498 | }); 499 | assert.isUndefined(result); 500 | }); 501 | }); 502 | 503 | describe('delay', function () { 504 | it('no function', function () { 505 | const funcs = window.funcs; 506 | assert.isDefined(funcs); 507 | 508 | assert.isFunction(funcs.delay); 509 | 510 | const output = funcs.delay(10, 10); 511 | assert.isTrue(output === funcs.noop); 512 | }); 513 | 514 | it('delay not provided', function (done) { 515 | const funcs = window.funcs; 516 | assert.isDefined(funcs); 517 | 518 | assert.isFunction(funcs.delay); 519 | 520 | const fn = createCounterFn(); 521 | const output = funcs.delay(fn); 522 | assert.isFalse(output === fn); 523 | output(function (counter) { 524 | assert.strictEqual(counter, 1); 525 | 526 | done(); 527 | }); 528 | }); 529 | 530 | it('negative delay', function () { 531 | const funcs = window.funcs; 532 | assert.isDefined(funcs); 533 | 534 | assert.isFunction(funcs.delay); 535 | 536 | const fn = createCounterFn(); 537 | const output = funcs.delay(fn, -1); 538 | assert.isTrue(output === fn); 539 | }); 540 | 541 | it('delay is 0', function (done) { 542 | const funcs = window.funcs; 543 | assert.isDefined(funcs); 544 | 545 | assert.isFunction(funcs.delay); 546 | 547 | const fn = createCounterFn(); 548 | const output = funcs.delay(fn, 0); 549 | assert.isFalse(output === fn); 550 | output(function (counter) { 551 | assert.strictEqual(counter, 1); 552 | assert.strictEqual(fn.getCounter(), 1); 553 | 554 | done(); 555 | }); 556 | }); 557 | 558 | it('delay is positive', function (done) { 559 | const funcs = window.funcs; 560 | assert.isDefined(funcs); 561 | 562 | assert.isFunction(funcs.delay); 563 | 564 | const delay = 20; 565 | const fn = createCounterFn(); 566 | const output = funcs.delay(fn, delay); 567 | assert.isFalse(output === fn); 568 | const startTime = Date.now(); 569 | output(function (counter) { 570 | assert.strictEqual(counter, 1); 571 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 572 | assert.strictEqual(fn.getCounter(), 1); 573 | 574 | done(); 575 | }); 576 | }); 577 | 578 | it('validate input proxy', function (done) { 579 | const funcs = window.funcs; 580 | assert.isDefined(funcs); 581 | 582 | assert.isFunction(funcs.delay); 583 | 584 | const delay = 20; 585 | const fn = createCounterFn(true); 586 | const output = funcs.delay(fn, delay); 587 | assert.isFalse(output === fn); 588 | const startTime = Date.now(); 589 | output(1, 'test', false, function (counter) { 590 | assert.strictEqual(counter, 1); 591 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 592 | assert.strictEqual(fn.getCounter(), 1); 593 | 594 | done(); 595 | }); 596 | }); 597 | 598 | it('no args', function (done) { 599 | const funcs = window.funcs; 600 | assert.isDefined(funcs); 601 | 602 | assert.isFunction(funcs.delay); 603 | 604 | const fn = createCounterFn(true, true); 605 | const output = funcs.delay(function () { 606 | assert.strictEqual(arguments.length, 0); 607 | 608 | done(); 609 | }, 0); 610 | assert.isFalse(output === fn); 611 | 612 | output(); 613 | }); 614 | 615 | it('callback style', function (done) { 616 | const funcs = window.funcs; 617 | assert.isDefined(funcs); 618 | 619 | assert.isFunction(funcs.delay); 620 | 621 | const delay = 20; 622 | const fn = createCounterFn(true, true); 623 | const output = funcs.delay(fn, delay, { 624 | callbackStyle: true 625 | }); 626 | assert.isFalse(output === fn); 627 | const startTime = Date.now(); 628 | output(1, function (counter) { 629 | assert.strictEqual(counter, 1); 630 | assert.isTrue((Date.now() - startTime) >= (delay / 2)); 631 | assert.strictEqual(fn.getCounter(), 1); 632 | 633 | done(); 634 | }, 'bad'); 635 | }); 636 | 637 | it('options provided without delay', function (done) { 638 | const funcs = window.funcs; 639 | assert.isDefined(funcs); 640 | 641 | assert.isFunction(funcs.delay); 642 | 643 | const fn = createCounterFn(true, true); 644 | const output = funcs.delay(fn, { 645 | callbackStyle: true 646 | }); 647 | assert.isFalse(output === fn); 648 | output(1, function (counter) { 649 | assert.strictEqual(counter, 1); 650 | assert.strictEqual(fn.getCounter(), 1); 651 | 652 | done(); 653 | }, 'bad'); 654 | }); 655 | 656 | it('chaining', function (done) { 657 | const funcs = window.funcs; 658 | assert.isDefined(funcs); 659 | 660 | assert.isFunction(funcs.delay); 661 | 662 | const delay = 20; 663 | const fn = createCounterFn(); 664 | const output = funcs.delay(fn, delay).once(); 665 | assert.strictEqual(fn.getCounter(), 0); 666 | const startTime = Date.now(); 667 | output(function (counter) { 668 | assert.strictEqual(counter, 1); 669 | assert.isTrue((Date.now() - startTime) >= delay); 670 | assert.strictEqual(fn.getCounter(), 1); 671 | 672 | output(function () { 673 | assert.fail(); 674 | }); 675 | 676 | setTimeout(done, delay + 50); 677 | }); 678 | }); 679 | }); 680 | 681 | describe('async', function () { 682 | it('no function', function () { 683 | const funcs = window.funcs; 684 | assert.isDefined(funcs); 685 | 686 | assert.isFunction(funcs.async); 687 | 688 | const output = funcs.async(10); 689 | assert.isTrue(output === funcs.noop); 690 | }); 691 | 692 | it('async', function (done) { 693 | const funcs = window.funcs; 694 | assert.isDefined(funcs); 695 | 696 | assert.isFunction(funcs.async); 697 | 698 | const fn = createCounterFn(); 699 | const output = funcs.async(fn); 700 | assert.isFalse(output === fn); 701 | let invoked = false; 702 | output(function (counter) { 703 | invoked = true; 704 | assert.strictEqual(counter, 1); 705 | assert.strictEqual(fn.getCounter(), 1); 706 | 707 | done(); 708 | }); 709 | 710 | assert.isFalse(invoked); 711 | }); 712 | 713 | it('validate input proxy', function (done) { 714 | const funcs = window.funcs; 715 | assert.isDefined(funcs); 716 | 717 | assert.isFunction(funcs.async); 718 | 719 | const fn = createCounterFn(true); 720 | const output = funcs.async(fn); 721 | assert.isFalse(output === fn); 722 | let invoked = false; 723 | output(1, 'test', false, function (counter) { 724 | invoked = true; 725 | assert.strictEqual(counter, 1); 726 | assert.strictEqual(fn.getCounter(), 1); 727 | 728 | done(); 729 | }); 730 | 731 | assert.isFalse(invoked); 732 | }); 733 | 734 | it('no args', function (done) { 735 | const funcs = window.funcs; 736 | assert.isDefined(funcs); 737 | 738 | assert.isFunction(funcs.async); 739 | 740 | const fn = createCounterFn(true, true); 741 | const output = funcs.async(function () { 742 | assert.strictEqual(arguments.length, 0); 743 | 744 | done(); 745 | }); 746 | assert.isFalse(output === fn); 747 | 748 | output(); 749 | }); 750 | 751 | it('callback style', function (done) { 752 | const funcs = window.funcs; 753 | assert.isDefined(funcs); 754 | 755 | assert.isFunction(funcs.async); 756 | 757 | const fn = createCounterFn(true, true); 758 | const output = funcs.async(fn, { 759 | callbackStyle: true 760 | }); 761 | assert.isFalse(output === fn); 762 | let invoked = false; 763 | output(1, function (counter) { 764 | invoked = true; 765 | assert.strictEqual(counter, 1); 766 | assert.strictEqual(fn.getCounter(), 1); 767 | 768 | done(); 769 | }, 'bad'); 770 | 771 | assert.isFalse(invoked); 772 | }); 773 | 774 | it('chaining', function (done) { 775 | const funcs = window.funcs; 776 | assert.isDefined(funcs); 777 | 778 | assert.isFunction(funcs.async); 779 | 780 | const fn = createCounterFn(); 781 | const output = funcs.async(fn).maxTimes(1); 782 | assert.strictEqual(fn.getCounter(), 0); 783 | let invoked = false; 784 | output(function (counter) { 785 | invoked = true; 786 | assert.strictEqual(counter, 1); 787 | assert.strictEqual(fn.getCounter(), 1); 788 | 789 | output(function () { 790 | assert.fail(); 791 | }); 792 | 793 | setTimeout(done, 50); 794 | }); 795 | 796 | assert.isFalse(invoked); 797 | }); 798 | }); 799 | }); 800 | --------------------------------------------------------------------------------