├── CHANGELOG.md ├── .gitignore ├── LICENSE ├── .jshintrc ├── tasks ├── clean.js ├── changelog.js ├── bytesize.js ├── copy.js ├── markdox.js ├── uglify.js ├── watch.js ├── jscs.js ├── mocha.js ├── bump.js ├── jshint.js ├── copyright.js └── buildcontrol.js ├── .travis.yml ├── src ├── .jshintrc └── function.js ├── bower.json ├── README.md ├── tests ├── bower.json ├── index.html └── spec │ └── function.js ├── .jscsrc ├── Gruntfile.js └── package.json /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /build 3 | /docs 4 | .DS_Store 5 | /tests/bower_components/ 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This Source Code Form is subject to the terms of the Mozilla Public 2 | License, v. 2.0. If a copy of the MPL was not distributed with this 3 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 | 5 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": "vars", 11 | "boss": true, 12 | "eqnull": true, 13 | "node": true 14 | } 15 | -------------------------------------------------------------------------------- /tasks/clean.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('clean', { 9 | build: ['build', 'docs'] 10 | }); 11 | }; 12 | -------------------------------------------------------------------------------- /tasks/changelog.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('changelog', { 9 | options: { 10 | from: 'source-<%= pkgReadOnly.version %>' 11 | } 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /tasks/bytesize.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('bytesize', { 9 | all: { 10 | src: ['build/function.js', 'build/function.min.js'] 11 | } 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /tasks/copy.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('copy', { 9 | dist: { 10 | src: 'src/function.js', 11 | dest: 'build/function.js' 12 | } 13 | }); 14 | }; 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: node_modules 3 | 4 | sudo: false 5 | 6 | notifications: 7 | email: 8 | recipients: 9 | - shane@shanetomlinson.com 10 | on_success: change 11 | on_failure: always 12 | 13 | node_js: 14 | - 0.10.25 15 | 16 | before_install: 17 | - phantomjs --version 18 | 19 | install: 20 | - travis_retry npm install --silent 21 | - npm run-script setup-bower 22 | 23 | script: 24 | - npm test 25 | 26 | -------------------------------------------------------------------------------- /src/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": false, 7 | "noarg": true, 8 | "node": false, 9 | "sub": true, 10 | "undef": true, 11 | "devel": true, 12 | "unused": "vars", 13 | "boss": true, 14 | "eqnull": true, 15 | "browser": true, 16 | "globals": { 17 | "define": false, 18 | "require": false, 19 | "module": false 20 | }, 21 | "globalstrict": true 22 | } 23 | -------------------------------------------------------------------------------- /tasks/markdox.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('markdox', { 9 | dist: { 10 | files: [ 11 | { src: 'src/function.js', dest: 'docs/api.md' } 12 | ] 13 | } 14 | }); 15 | }; 16 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-utils", 3 | "version": "0.0.0", 4 | "description": "Some function helpers not available in ES*", 5 | "author": "Shane Tomlinson ", 6 | "main": "function-utils.js", 7 | "moduleType": [ 8 | "amd", 9 | "globals", 10 | "node" 11 | ], 12 | "keywords": [ 13 | ], 14 | "license": "MPL 2.0", 15 | "ignore": [ 16 | "**/.*", 17 | "node_modules", 18 | "bower_components", 19 | "test", 20 | "tests" 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # function-utils 2 | 3 | Some basic utilities for working with functions that are not available in JavaScript. 4 | 5 | ## API: 6 | 7 | ## Author: 8 | * Shane Tomlinson 9 | * shane@shanetomlinson.com 10 | * stomlinson@mozilla.com 11 | * set117@yahoo.com 12 | * https://shanetomlinson.com 13 | * https://github.com/shane-tomlinson 14 | * @shane_tomlinson 15 | 16 | ## Get involved: 17 | 18 | ## License: 19 | This software is available under version 2.0 of the MPL: 20 | 21 | https://www.mozilla.org/MPL/ 22 | 23 | -------------------------------------------------------------------------------- /tasks/uglify.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('uglify', { 9 | options: { 10 | sourceMap: true 11 | }, 12 | dist: { 13 | files: { 14 | 'build/function.min.js': ['build/function.js'] 15 | } 16 | } 17 | }); 18 | }; 19 | -------------------------------------------------------------------------------- /tasks/watch.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('watch', { 9 | dev: { 10 | options: { 11 | atBegin: true 12 | }, 13 | files: ['Gruntfile.js', 'src/**/*.js', 'tests/**/*.js'], 14 | tasks: ['build', 'mocha'] 15 | } 16 | }); 17 | }; 18 | -------------------------------------------------------------------------------- /tasks/jscs.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('jscs', { 9 | src: [ 10 | '**/*.js', 11 | '!node_modules/**', 12 | '!build/**', 13 | '!docs/**', 14 | '!tests/**' 15 | ], 16 | options: { 17 | config: '.jscsrc' 18 | } 19 | }); 20 | }; 21 | -------------------------------------------------------------------------------- /tasks/mocha.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('mocha', { 9 | test: { 10 | src: ['tests/index.html'], 11 | options: { 12 | run: true, 13 | log: true, 14 | logErrors: true, 15 | timeout: 20000 16 | } 17 | } 18 | }); 19 | }; 20 | -------------------------------------------------------------------------------- /tasks/bump.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('bump', { 9 | options: { 10 | files: ['package.json', 'bower.json'], 11 | push: false, 12 | createTag: false, 13 | commitFiles: ['-a'], 14 | commitMessage: 'Start of release %VERSION%' 15 | } 16 | }); 17 | }; 18 | -------------------------------------------------------------------------------- /tasks/jshint.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('jshint', { 9 | config: { 10 | options: {jshintrc: '.jshintrc'}, 11 | src: ['Gruntfile.js', 'tasks/*.js', 'config/**/*.js', 'node/**/*.js'] 12 | }, 13 | app: { 14 | options: {jshintrc: 'src/.jshintrc'}, 15 | src: ['src/*.js', 'src/lib/**/*'] 16 | } 17 | }); 18 | }; 19 | -------------------------------------------------------------------------------- /tests/bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-utils-tests", 3 | "version": "0.0.0", 4 | "authors": [ 5 | "Shane Tomlinson " 6 | ], 7 | "description": "The tests for function-utils library", 8 | "moduleType": [ 9 | "amd", 10 | "es6" 11 | ], 12 | "license": "MPL 2.0", 13 | "private": true, 14 | "ignore": [ 15 | "**/.*", 16 | "node_modules", 17 | "bower_components", 18 | "test", 19 | "tests" 20 | ], 21 | "dependencies": { 22 | "chai": "1.10.0", 23 | "mocha": "2.1.0", 24 | "sinon": "http://sinonjs.org/releases/sinon-1.12.2.js" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tasks/copyright.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('copyright', { 9 | app: { 10 | options: { 11 | pattern: /This Source Code Form is subject to the terms of the Mozilla Public/ 12 | }, 13 | src: [ 14 | '**/*.js', 15 | '!tests/bower_components/**', 16 | '!build/**', 17 | '!node_modules/**', 18 | '!docs/**' 19 | ] 20 | } 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /tasks/buildcontrol.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | 'use strict'; 7 | 8 | grunt.config('buildcontrol', { 9 | options: { 10 | commit: true, 11 | push: true, 12 | remote: 'git@github.com:shane-tomlinson/function-utils.git' 13 | }, 14 | release: { 15 | options: { 16 | branch: 'release', 17 | dir: 'build', 18 | tag: '<%= pkg.version %>' 19 | } 20 | }, 21 | docs: { 22 | options: { 23 | branch: 'gh-pages', 24 | dir: 'docs', 25 | tag: 'docs-<%= pkg.version %>' 26 | } 27 | } 28 | }); 29 | }; 30 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | function-utils Unit Tests 22 | 23 | 24 | 25 | 26 |
27 | 28 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "disallowKeywords": ["with", "eval"], 3 | "disallowKeywordsOnNewLine": ["else"], 4 | "requireSpaceBeforeBinaryOperators": ["?", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], 5 | "disallowMultipleLineStrings": true, 6 | "requireSpaceAfterBinaryOperators": ["?", "/", "*", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], 7 | "disallowSpaceAfterObjectKeys": true, 8 | "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-"], 9 | "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], 10 | "maximumLineLength": 420, 11 | "requireCapitalizedConstructors": true, 12 | "requireCurlyBraces": ["if", "else", "for", "while", "do"], 13 | "requireLineFeedAtFileEnd": true, 14 | "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return"], 15 | "validateIndentation": 2, 16 | "validateLineBreaks": "LF", 17 | "validateQuoteMarks": true, 18 | "validateJSDoc": { 19 | "checkParamNames": true, 20 | "checkRedundantParams": true, 21 | "requireParamTypes": true 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/spec/function.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | var assert = chai.assert; 6 | 7 | describe('function-utils', function () { 8 | describe('delay', function () { 9 | it('delays the invocation of a function', function (done) { 10 | var arg; 11 | function callLater(_arg) { 12 | arg = _arg; 13 | } 14 | 15 | var delayBy10 = FunctionUtils.delay(callLater, 10); 16 | delayBy10('an argument'); 17 | assert.isUndefined(arg); 18 | 19 | setTimeout(function() { 20 | assert.equal(arg, 'an argument'); 21 | done(); 22 | }, 15); 23 | }); 24 | }); 25 | 26 | describe('partial', function () { 27 | it('partially applies a function', function () { 28 | function adder(a, b) { 29 | return a + b; 30 | } 31 | 32 | var add10 = FunctionUtils.partial(adder, 10); 33 | var result = add10(9); 34 | assert.equal(result, 19); 35 | }); 36 | }); 37 | }); 38 | 39 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (grunt) { 6 | // load all grunt tasks matching the `grunt-*` pattern 7 | require('load-grunt-tasks')(grunt); 8 | 9 | var pkg = grunt.file.readJSON('package.json'); 10 | 11 | grunt.initConfig({ 12 | pkg: pkg, 13 | pkgReadOnly: pkg 14 | }); 15 | 16 | // load local Grunt tasks 17 | grunt.loadTasks('tasks'); 18 | 19 | grunt.registerTask('build', 20 | 'Build compressed resources', 21 | ['clean', 'lint', 'copy', 'uglify', 'bytesize']); 22 | 23 | grunt.registerTask('test', 24 | 'Run tests', 25 | ['mocha']); 26 | 27 | grunt.registerTask('lint', 28 | 'Alias for jshint and jscs tasks', 29 | ['jshint', 'jscs']); 30 | 31 | grunt.registerTask('default', 32 | ['build']); 33 | 34 | grunt.registerTask('doc', 35 | ['markdox']); 36 | 37 | grunt.registerTask('release', 38 | ['build', 'bump-only', 'changelog', 'bump-commit', 'doc', 'buildcontrol']); 39 | }; 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-utils", 3 | "version": "0.0.0", 4 | "description": "Some function helpers not available in ES*", 5 | "scripts": { 6 | "start": "grunt", 7 | "test": "grunt test", 8 | "setup": "npm install && npm run-script setup-bower", 9 | "setup-bower": "cd tests && bower install && cd ..", 10 | "contributors": "git shortlog -s | cut -c8- | sort -f > CONTRIBUTORS.md" 11 | }, 12 | "directories": { 13 | "test": "tests" 14 | }, 15 | "author": "Shane Tomlinson ", 16 | "license": "MPL 2.0", 17 | "devDependencies": { 18 | "bower": "1.3.12", 19 | "grunt": "0.4.5", 20 | "grunt-build-control": "git://github.com/robwierzbowski/grunt-build-control#274952", 21 | "grunt-bump": "0.0.16", 22 | "grunt-bytesize": "0.1.1", 23 | "grunt-cli": "0.1.13", 24 | "grunt-contrib-clean": "0.6.0", 25 | "grunt-contrib-copy": "0.7.0", 26 | "grunt-contrib-jshint": "0.10.0", 27 | "grunt-contrib-uglify": "0.7.0", 28 | "grunt-contrib-watch": "0.6.1", 29 | "grunt-conventional-changelog": "1.1.0", 30 | "grunt-copyright": "0.1.0", 31 | "grunt-jscs": "1.1.0", 32 | "grunt-markdox": "1.2.1", 33 | "grunt-mocha": "0.4.11", 34 | "load-grunt-tasks": "2.0.0" 35 | }, 36 | "dependencies": {} 37 | } 38 | -------------------------------------------------------------------------------- /src/function.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /** 6 | * Some utilities for working with function that are not available 7 | * in JavaScript by default. 8 | * 9 | * @method FunctionUtils 10 | */ 11 | 12 | // if the module has no dependencies, the above pattern can be simplified to 13 | (function (root, factory) { 14 | 'use strict'; 15 | 16 | if (typeof define === 'function' && define.amd) { 17 | // AMD. Register as an anonymous module. 18 | define([], factory); 19 | } else if (typeof exports === 'object') { 20 | // Node. Does not work with strict CommonJS, but 21 | // only CommonJS-like environments that support module.exports, 22 | // like Node. 23 | module.exports = factory(); 24 | } else { 25 | // Browser globals (root is window) 26 | root.FunctionUtils = factory(); 27 | } 28 | }(this, function () { //jshint ignore: line 29 | 'use strict'; 30 | 31 | /** 32 | * Delay the invocation of a method. Does not return the return value of 33 | * the delayed method. 34 | * 35 | * @method delay 36 | * @param {Function} method 37 | * Method to call 38 | * @param {Number} delayMS 39 | * Milliseconds to delay, default is 0 40 | * @returns {Function} 41 | * A new function to call in place of `method` 42 | */ 43 | function delay(method, delayMS) { 44 | return function () { 45 | var self = this; 46 | var args = [].slice.call(arguments, 0); 47 | setTimeout(function () { 48 | method.apply(self, args); 49 | }, delayMS || 0); 50 | }; 51 | } 52 | 53 | /** 54 | * Partially apply a function by filling in any number of its arguments, 55 | * without changing its dynamic this value. A close cousin of 56 | * Function.prototype.bind. 57 | * 58 | * @example 59 | * function add(a, b) { 60 | * return a + b; 61 | * } 62 | * 63 | * var add1 = partial(add, 1); 64 | * var result = add1(9); 65 | * // result is 10 66 | * 67 | * @method partial 68 | * @param {Function} method 69 | * method to pass arguments to. 70 | * @returns {Function} 71 | */ 72 | function partial(method) { 73 | var args = [].slice.call(arguments, 1); 74 | return function () { 75 | args = args.concat([].slice.call(arguments, 0)); 76 | return method.apply(this, args); 77 | }; 78 | } 79 | 80 | return { 81 | delay: delay, 82 | partial: partial 83 | }; 84 | })); 85 | --------------------------------------------------------------------------------