commit 96edcb311db894f41d8674c012193e4fb474a39a Author: David Kebler Date: Sat Feb 16 11:47:55 2019 -0800 pulled from uci-utils into it's own repository, updated dependency diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..49bac18 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,37 @@ +module.exports = { + "ecmaFeatures": { + "modules": true, + "spread" : true, + "restParams" : true + }, + // "plugins": [ + // "unicorn" + // ], + "env": { + "es6": true, + "node": true, + "mocha": true + }, + "parserOptions": { + "ecmaVersion": 2017, + "sourceType": "module" + }, + "extends": "eslint:recommended", + "rules": { + "indent": [ + "error", + 2 + ], + // "unicorn/no-array-instanceof": "error", + "no-console": 0, + "semi": ["error", "never"], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e61051f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/coverage/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..a4114e0 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +tests/ +test/ +*.test.js +testing/ +node_modulescd diff --git a/package.json b/package.json new file mode 100644 index 0000000..bf9fee1 --- /dev/null +++ b/package.json @@ -0,0 +1,41 @@ +{ + "name": "@uci-utils/bind-funcs", + "version": "0.2.3", + "description": "Function to bind all functions to a context in an array or object", + "main": "src/bind-functions.js", + "scripts": { + "test": "./node_modules/.bin/mocha -r esm --timeout 30000", + "testd": "UCI_ENV=dev ./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000' || exit 0", + "testdd": "UCI_LOG_LEVEL='trace' npm run testd", + "testde": "UCI_LOG_LEVEL='warn' npm run testd", + "testl": "UCI_ENV=pro UCI_LOG_PATH=./test/test.log 0 npm run test || exit 0" + }, + "author": "David Kebler", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/uCOMmandIt/uci-utils.git" + }, + "keywords": [ + "node.js", + "communication", + "serial", + "utilities", + "helpers" + ], + "bugs": { + "url": "https://github.com/uCOMmandIt/uci-utils/issues" + }, + "homepage": "https://github.com/uCOMmandIt/uci-utils#readme", + "dependencies": { + "@uci-utils/type": "^0.2.0", + "clone": "^2.1.0", + "traverse": "^0.6.6" + }, + "devDependencies": { + "chai": "^4.2.0", + "esm": "^3.2.4", + "mocha": "^5.2.0", + "nodemon": "^1.18.10" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d5ebdc2 --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +### Bind Functions +#### a uCOMmandIt Utiltiy Function diff --git a/src/bind-functions.js b/src/bind-functions.js new file mode 100644 index 0000000..c0b32c2 --- /dev/null +++ b/src/bind-functions.js @@ -0,0 +1,28 @@ +import traverse from 'traverse' +import _ from '@uci-utils/type' +import clone from 'clone' + +function bindFuncs (pfuncs,site) { + let funcs = clone(pfuncs) + if (!site) site=this + if(_.isArray(funcs)) { + funcs.forEach(function (entry,index) { + if(_.isFunction(entry)) funcs[index] = entry.bind(site) + }) + return funcs + } + if(_.isPlainObject(funcs)) { + traverse(funcs).forEach(function (func) { + // console.log(this.path) + if(_.isFunction(func)) { + // console.log('function',this.path) + this.update(func.bind(site)) + } + }) + return funcs + } + throw('bindFuncs: must be array or plain object of functions') +} + +export default bindFuncs +export { bindFuncs } diff --git a/test/bind-funcs.test.js b/test/bind-funcs.test.js new file mode 100644 index 0000000..e0b7680 --- /dev/null +++ b/test/bind-funcs.test.js @@ -0,0 +1,26 @@ +let date = new Date(Date.now()) +console.log('run:', date.getMinutes(), ':', date.getSeconds()) + +import { expect } from 'chai' +import { bindFuncs } from '../src/bind-functions' + +describe('Bind Functions Utility - ', function () { + + it('Should bind context to function in array', function () { + let context = {prop:'test string'} + let array = [1,'2',{test:true},function test() {return this.prop}] + let boundArray = bindFuncs(array,context) + expect(boundArray[3]()).to.equal('test string') + expect(boundArray[1]).to.equal('2') + }) + + it('Should bind context to function deep in object', function () { + let context = {prop:'obj test string'} + let obj = {prop:true,prop2:'test',prop3:{test:'string unchanged',prop4:function test() {return this.prop}}} + let boundObject = bindFuncs(obj,context) + expect(boundObject.prop3.prop4()).to.equal('obj test string') + expect(boundObject.prop3.test).to.equal('string unchanged') + }) + + +})