pulled from uci-utils into it's own repository, updated dependency
commit
96edcb311d
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
/node_modules/
|
||||
/coverage/
|
|
@ -0,0 +1,5 @@
|
|||
tests/
|
||||
test/
|
||||
*.test.js
|
||||
testing/
|
||||
node_modulescd
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
### Bind Functions
|
||||
#### a uCOMmandIt Utiltiy Function
|
|
@ -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 }
|
|
@ -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')
|
||||
})
|
||||
|
||||
|
||||
})
|
Loading…
Reference in New Issue