method-missing
Advanced tools
Comparing version 1.1.4 to 1.2.0
14
index.js
@@ -1,1 +0,13 @@ | ||
module.exports = require('./lib/method-missing'); | ||
/*! | ||
* Method Missing. | ||
* | ||
* Application entry file. | ||
* @author Jarrad Seers <jarrad@seers.me> | ||
* @created 29/03/2017 NZDT | ||
*/ | ||
/** | ||
* Module exports. | ||
*/ | ||
module.exports = require('./lib/method-missing'); |
@@ -9,7 +9,5 @@ /*! | ||
'use strict'; | ||
/** | ||
* Method Missing Error. | ||
* | ||
* | ||
* @class MethodMissingError | ||
@@ -23,4 +21,4 @@ * @extends {Error} | ||
* Creates an instance of MethodMissingError. | ||
* @param {any} msg | ||
* | ||
* @param {any} msg | ||
* | ||
* @memberOf MethodMissingError | ||
@@ -31,3 +29,3 @@ */ | ||
super(msg); | ||
this.name = this.constructor.name | ||
this.name = this.constructor.name; | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -34,0 +32,0 @@ } |
@@ -9,4 +9,2 @@ /*! | ||
'use strict'; | ||
/** | ||
@@ -20,3 +18,3 @@ * Module dependencies. | ||
* MethodMissing Class | ||
* | ||
* | ||
* @class MethodMissing | ||
@@ -29,10 +27,9 @@ */ | ||
* Creates an instance of MethodMissing. | ||
* @param {any} method | ||
* | ||
* @param {any} method | ||
* | ||
* @memberOf MethodMissing | ||
*/ | ||
constructor(method) { | ||
method = method || '__call'; | ||
return proxy(this, method); | ||
return proxy(this, method || '__call'); | ||
} | ||
@@ -42,8 +39,8 @@ | ||
* Static method handler. | ||
* | ||
* | ||
* @static | ||
* @param {any} that | ||
* @param {any} method | ||
* @returns | ||
* | ||
* @param {any} that | ||
* @param {any} method | ||
* @returns | ||
* | ||
* @memberOf MethodMissing | ||
@@ -53,4 +50,3 @@ */ | ||
static static(that, method) { | ||
method = method || '__call'; | ||
return proxy(that, method); | ||
return proxy(that, method || '__call'); | ||
} | ||
@@ -64,2 +60,2 @@ | ||
module.exports = MethodMissing; | ||
module.exports = MethodMissing; |
@@ -9,4 +9,2 @@ /*! | ||
'use strict'; | ||
/** | ||
@@ -20,26 +18,26 @@ * Module dependencies. | ||
* Main proxy handler. | ||
* | ||
* @param {object} Class | ||
* @param {string} method | ||
* | ||
* @param {object} Class | ||
* @param {string} method | ||
*/ | ||
function proxy(Class, method) { | ||
return new Proxy(Class, { | ||
get: function (obj, prop) { | ||
get(obj, prop) { | ||
if (Reflect.has(obj, prop)) { | ||
return Reflect.get(obj, prop); | ||
} else if (typeof method === 'function') { | ||
return function() { | ||
method.call(this, prop, arguments); | ||
} | ||
return function methodMissing(...args) { | ||
method.call(this, prop, args); | ||
}; | ||
} else if (Reflect.has(obj, method)) { | ||
return function() { | ||
obj[method].call(this, prop, arguments); | ||
} | ||
} else { | ||
throw new MethodMissingError( | ||
`${method}, use method '__call(method, args)' in your class to catch.` | ||
); | ||
return function methodMissing(...args) { | ||
obj[method].call(this, prop, args); | ||
}; | ||
} | ||
} | ||
const err = `${method}, use method '__call(method, args)' in your class to catch.`; | ||
throw new MethodMissingError(err); | ||
}, | ||
}); | ||
@@ -46,0 +44,0 @@ } |
{ | ||
"name": "method-missing", | ||
"version": "1.1.4", | ||
"engines" : { | ||
"node" : ">=6.0.0" | ||
"version": "1.2.0", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
@@ -14,3 +14,3 @@ "engineStrict": true, | ||
"scripts": { | ||
"test": "node test" | ||
"test": "eslint ./lib/*.js && node test" | ||
}, | ||
@@ -33,3 +33,8 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/jarradseers/method-missing#readme" | ||
"homepage": "https://github.com/jarradseers/method-missing#readme", | ||
"devDependencies": { | ||
"eslint": "^3.18.0", | ||
"eslint-config-airbnb-base": "^11.1.2", | ||
"eslint-plugin-import": "^2.2.0" | ||
} | ||
} |
@@ -24,6 +24,2 @@ # Method Missing Class | ||
constructor() { | ||
super(); | ||
} | ||
__call(name, args) { | ||
@@ -37,3 +33,3 @@ console.log(`The method '${name}' was called with:`, args); | ||
simple.nonExistent('Hello!'); | ||
// The method 'nonExistent' was called with: { '0': 'Hello!' } | ||
// The method 'nonExistent' was called with: [ 'Hello!' ] | ||
``` | ||
@@ -53,4 +49,6 @@ | ||
Simple = MethodMissing.static(Simple); | ||
// Call the static method. | ||
Simple.nonExistentStatic(1, 2, 3); | ||
// The method 'nonExistentStatic' was called with: {0: 1, 1: 2, 2: 3} | ||
// The method 'nonExistentStatic' was called with: [ 1, 2, 3 ] | ||
``` | ||
@@ -63,6 +61,2 @@ | ||
constructor() { | ||
super(); | ||
} | ||
iExist(str) { | ||
@@ -89,5 +83,5 @@ console.log(`I do exist ${str}.`); | ||
Simple.nonExistentStatic('hey'); | ||
// The method 'nonExistent' was called with: { '0': 'hello' } | ||
// The method 'nonExistent' was called with: [ 'hello' ] | ||
// I do exist world. | ||
// The method 'nonExistentStatic' was called with: { '0': 'hey' } | ||
// The method 'nonExistentStatic' was called with: [ 'hey' ] | ||
``` | ||
@@ -102,3 +96,3 @@ Used on an object: | ||
}, (name, args) => { | ||
console.log(`Sorry, method '${name}' doesn't exist.`); | ||
console.log(`Sorry, method '${name}' doesn't exist.`, args); | ||
}); | ||
@@ -110,3 +104,3 @@ | ||
// hey there | ||
// Sorry, method 'two' doesn't exist. | ||
// Sorry, method 'two' doesn't exist. [] | ||
``` | ||
@@ -132,3 +126,3 @@ | ||
Changing the `__call` method | ||
Changing the `__call` method (if you must, just be careful with this). | ||
@@ -158,26 +152,6 @@ ```js | ||
test.nonExistentStatic('world'); | ||
// The method 'nonExistentStatic' was called with: { '0': 'hey' } | ||
// The method 'nonExistent' was called with: { '0': 'hello' } | ||
// The method 'nonExistentStatic' was called with: { '0': 'world' } | ||
// The method 'nonExistent' was called with: [ 'hello' ] | ||
// The method 'nonExistentStatic' was called with: [ 'world' ] | ||
``` | ||
Modifying the arguments to be an array: | ||
```js | ||
class Args extends MethodMissing { | ||
constructor() { | ||
super(); | ||
} | ||
__call(name, [...args]) { | ||
console.log(`The method '${name}' was called with:`, args); | ||
} | ||
} | ||
new Args().say('hello', 'world!'); | ||
// The method 'say' was called with: [ 'hello', 'world!' ] | ||
``` | ||
## Tests | ||
@@ -184,0 +158,0 @@ |
@@ -9,17 +9,12 @@ /*! | ||
// TODO : Write some real unit tests ;) | ||
'use strict'; | ||
const MethodMissing = require('../'); | ||
class Simple extends MethodMissing { | ||
constructor() { | ||
super(); | ||
} | ||
iExist(str) { | ||
console.log(`I do exist ${str}.`); | ||
return this; | ||
} | ||
__call(name, args) { | ||
console.log(`The method '${name}' was called with:`, args); | ||
return this; | ||
} | ||
@@ -49,2 +44,3 @@ static __call(name, args) { | ||
console.log(`The method '${name}' was called with:`, args); | ||
return this; | ||
} | ||
@@ -67,7 +63,5 @@ static missing(name, args) { | ||
class RealSimple extends MethodMissing { | ||
constructor() { | ||
super(); | ||
} | ||
__call(name, [...args]) { | ||
console.log(`The method '${name}' was called with:`, args); | ||
return this; | ||
} | ||
@@ -80,7 +74,5 @@ } | ||
class Args extends MethodMissing { | ||
constructor() { | ||
super(); | ||
} | ||
__call(name, [...args]) { | ||
console.log(`The method '${name}' was called with:`, args); | ||
return this; | ||
} | ||
@@ -93,6 +85,6 @@ } | ||
const object = MethodMissing.static({ | ||
one: function() { | ||
one () { | ||
console.log('hey there'); | ||
} | ||
}, (name, args) => { | ||
}, | ||
}, (name) => { | ||
console.log(`Sorry, method '${name}' doesn't exist.`); | ||
@@ -99,0 +91,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18374
10
464
3
158
1