refactor(utils): improve function names

Many of the utility functions had slightly misleading names or no
names at all. This commit renames the functions with misleading names
and adds names to functions that were missing one.
This commit is contained in:
Grant Snodgrass 2016-12-10 16:14:38 -05:00
parent 78f1808735
commit 96f958c814
16 changed files with 39 additions and 39 deletions

View file

@ -59,7 +59,7 @@ var call = Function.prototype.call,
* @api public
*/
module.exports = function (ctx, name, method, chainingBehavior) {
module.exports = function addChainableMethod(ctx, name, method, chainingBehavior) {
if (typeof chainingBehavior !== 'function') {
chainingBehavior = function () { };
}
@ -76,13 +76,13 @@ module.exports = function (ctx, name, method, chainingBehavior) {
ctx.__methods[name] = chainableBehavior;
Object.defineProperty(ctx, name,
{ get: function () {
{ get: function chainableMethodGetter() {
chainableBehavior.chainingBehavior.call(this);
var assert = function assert() {
var chainableMethodWrapper = function () {
var old_ssfi = flag(this, 'ssfi');
if (old_ssfi)
flag(this, 'ssfi', assert);
flag(this, 'ssfi', chainableMethodWrapper);
var result = chainableBehavior.method.apply(this, arguments);
if (result !== undefined) {
@ -94,12 +94,12 @@ module.exports = function (ctx, name, method, chainingBehavior) {
return newAssertion;
};
addLengthGuard(assert, name, true);
addLengthGuard(chainableMethodWrapper, name, true);
// Use `__proto__` if available
if (hasProtoSupport) {
// Inherit all properties from the object by replacing the `Function` prototype
var prototype = assert.__proto__ = Object.create(this);
var prototype = chainableMethodWrapper.__proto__ = Object.create(this);
// Restore the `call` and `apply` methods from `Function`
prototype.call = call;
prototype.apply = apply;
@ -110,13 +110,13 @@ module.exports = function (ctx, name, method, chainingBehavior) {
asserterNames.forEach(function (asserterName) {
if (!excludeNames.test(asserterName)) {
var pd = Object.getOwnPropertyDescriptor(ctx, asserterName);
Object.defineProperty(assert, asserterName, pd);
Object.defineProperty(chainableMethodWrapper, asserterName, pd);
}
});
}
transferFlags(this, assert);
return proxify(assert);
transferFlags(this, chainableMethodWrapper);
return proxify(chainableMethodWrapper);
}
, configurable: true
});

View file

@ -36,12 +36,12 @@ var transferFlags = require('./transferFlags');
* @api public
*/
module.exports = function (ctx, name, method) {
var fn = function () {
module.exports = function addMethod(ctx, name, method) {
var methodWrapper = function () {
var keep_ssfi = flag(this, 'keep_ssfi');
var old_ssfi = flag(this, 'ssfi');
if (!keep_ssfi && old_ssfi)
flag(this, 'ssfi', fn);
flag(this, 'ssfi', methodWrapper);
var result = method.apply(this, arguments);
if (result !== undefined)
@ -52,6 +52,6 @@ module.exports = function (ctx, name, method) {
return newAssertion;
};
addLengthGuard(fn, name, false);
ctx[name] = proxify(fn, name);
addLengthGuard(methodWrapper, name, false);
ctx[name] = proxify(methodWrapper, name);
};

View file

@ -34,15 +34,15 @@ var transferFlags = require('./transferFlags');
* @api public
*/
module.exports = function (ctx, name, getter) {
module.exports = function addProperty(ctx, name, getter) {
getter = getter === undefined ? new Function() : getter;
Object.defineProperty(ctx, name,
{ get: function addProperty() {
{ get: function propertyGetter() {
var keep_ssfi = flag(this, 'keep_ssfi');
var old_ssfi = flag(this, 'ssfi');
if (!keep_ssfi && old_ssfi)
flag(this, 'ssfi', addProperty);
flag(this, 'ssfi', propertyGetter);
var result = getter.call(this);
if (result !== undefined)

View file

@ -26,6 +26,6 @@ var inspect = require('./inspect');
* @api public
*/
module.exports = function (a, b) {
module.exports = function compareByInspect(a, b) {
return inspect(a) < inspect(b) ? -1 : 1;
};

View file

@ -22,7 +22,7 @@ var AssertionError = require('assertion-error');
var flag = require('./flag');
var type = require('type-detect');
module.exports = function (obj, types) {
module.exports = function expectTypes(obj, types) {
obj = flag(obj, 'object');
types = types.map(function (t) { return t.toLowerCase(); });
types.sort();

View file

@ -23,7 +23,7 @@
* @api private
*/
module.exports = function (obj, key, value) {
module.exports = function flag(obj, key, value) {
var flags = obj.__flags || (obj.__flags = Object.create(null));
if (arguments.length === 3) {
flags[key] = value;

View file

@ -15,6 +15,6 @@
* @name getActual
*/
module.exports = function (obj, args) {
module.exports = function getActual(obj, args) {
return args.length > 4 ? args[4] : obj._obj;
};

View file

@ -32,7 +32,7 @@ var flag = require('./flag')
* @api public
*/
module.exports = function (obj, args) {
module.exports = function getMessage(obj, args) {
var negate = flag(obj, 'negate')
, val = flag(obj, 'object')
, expected = args[3]

View file

@ -17,7 +17,7 @@ var config = require('../config');
* @name isProxyEnabled
*/
module.exports = function isProxyEnabled () {
module.exports = function isProxyEnabled() {
return config.useProxy &&
typeof Proxy !== 'undefined' &&
typeof Reflect !== 'undefined';

View file

@ -24,7 +24,7 @@ var config = require('../config');
* @api public
*/
module.exports = function (obj) {
module.exports = function objDisplay(obj) {
var str = inspect(obj)
, type = Object.prototype.toString.call(obj);

View file

@ -40,11 +40,11 @@ var transferFlags = require('./transferFlags');
* @api public
*/
module.exports = function (ctx, name, method, chainingBehavior) {
module.exports = function overwriteChainableMethod(ctx, name, method, chainingBehavior) {
var chainableBehavior = ctx.__methods[name];
var _chainingBehavior = chainableBehavior.chainingBehavior;
chainableBehavior.chainingBehavior = function () {
chainableBehavior.chainingBehavior = function overwritingChainableMethodGetter() {
var result = chainingBehavior(_chainingBehavior).call(this);
if (result !== undefined) {
return result;
@ -56,7 +56,7 @@ module.exports = function (ctx, name, method, chainingBehavior) {
};
var _method = chainableBehavior.method;
chainableBehavior.method = function () {
chainableBehavior.method = function overwritingChainableMethodWrapper() {
var result = method(_method).apply(this, arguments);
if (result !== undefined) {
return result;

View file

@ -44,7 +44,7 @@ var transferFlags = require('./transferFlags');
* @api public
*/
module.exports = function (ctx, name, method) {
module.exports = function overwriteMethod(ctx, name, method) {
var _method = ctx[name]
, _super = function () {
throw new Error(name + ' is not a function');
@ -53,11 +53,11 @@ module.exports = function (ctx, name, method) {
if (_method && 'function' === typeof _method)
_super = _method;
var fn = function () {
var overwritingMethodWrapper = function () {
var keep_ssfi = flag(this, 'keep_ssfi');
var old_ssfi = flag(this, 'ssfi');
if (!keep_ssfi && old_ssfi)
flag(this, 'ssfi', fn);
flag(this, 'ssfi', overwritingMethodWrapper);
flag(this, 'keep_ssfi', true);
var result = method(_super).apply(this, arguments);
@ -72,6 +72,6 @@ module.exports = function (ctx, name, method) {
return newAssertion;
}
addLengthGuard(fn, name, false);
ctx[name] = proxify(fn, name);
addLengthGuard(overwritingMethodWrapper, name, false);
ctx[name] = proxify(overwritingMethodWrapper, name);
};

View file

@ -42,7 +42,7 @@ var transferFlags = require('./transferFlags');
* @api public
*/
module.exports = function (ctx, name, getter) {
module.exports = function overwriteProperty(ctx, name, getter) {
var _get = Object.getOwnPropertyDescriptor(ctx, name)
, _super = function () {};
@ -50,11 +50,11 @@ module.exports = function (ctx, name, getter) {
_super = _get.get
Object.defineProperty(ctx, name,
{ get: function overwriteProperty() {
{ get: function overwritingPropertyGetter() {
var keep_ssfi = flag(this, 'keep_ssfi');
var old_ssfi = flag(this, 'ssfi');
if (!keep_ssfi && old_ssfi)
flag(this, 'ssfi', overwriteProperty);
flag(this, 'ssfi', overwritingPropertyGetter);
flag(this, 'keep_ssfi', true);
var result = getter(_super).call(this);

View file

@ -27,11 +27,11 @@ var isProxyEnabled = require('./isProxyEnabled');
* @name proxify
*/
module.exports = function proxify (obj, nonChainableMethodName) {
module.exports = function proxify(obj, nonChainableMethodName) {
if (!isProxyEnabled()) return obj;
return new Proxy(obj, {
get: function getProperty (target, property) {
get: function proxyGetter(target, property) {
// This check is here because we should not throw errors on Symbol properties
// such as `Symbol.toStringTag`.
// The values for which an error should be thrown can be configured using

View file

@ -21,7 +21,7 @@ var flag = require('./flag');
* @name test
*/
module.exports = function (obj, args) {
module.exports = function test(obj, args) {
var negate = flag(obj, 'negate')
, expr = args[0];
return negate ? !expr : expr;

View file

@ -27,7 +27,7 @@
* @api private
*/
module.exports = function (assertion, object, includeAll) {
module.exports = function transferFlags(assertion, object, includeAll) {
var flags = assertion.__flags || (assertion.__flags = Object.create(null));
if (!object.__flags) {