Skip to content
This repository was archived by the owner on Nov 4, 2020. It is now read-only.

Commit cd23439

Browse files
committed
Use modules
1 parent 32ddfb9 commit cd23439

18 files changed

+276
-232
lines changed

lib/assertion-error.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
* Copyright(c) 2013-2016 Denis Bardadym <[email protected]>
55
* MIT Licensed
66
*/
7-
8-
var util = require('./util');
7+
import { merge, format, functionName } from './util';
98

109
/**
1110
* should AssertionError
@@ -14,8 +13,8 @@ var util = require('./util');
1413
* @memberOf should
1514
* @static
1615
*/
17-
var AssertionError = function AssertionError(options) {
18-
util.merge(this, options);
16+
export default function AssertionError(options) {
17+
merge(this, options);
1918

2019
if (!options.message) {
2120
Object.defineProperty(this, 'message', {
@@ -42,7 +41,7 @@ var AssertionError = function AssertionError(options) {
4241

4342
if (this.stackStartFunction) {
4443
// try to strip useless frames
45-
var fn_name = util.functionName(this.stackStartFunction);
44+
var fn_name = functionName(this.stackStartFunction);
4645
var idx = out.indexOf('\n' + fn_name);
4746
if (idx >= 0) {
4847
// once we have located the function frame
@@ -55,7 +54,7 @@ var AssertionError = function AssertionError(options) {
5554
this.stack = out;
5655
}
5756
}
58-
};
57+
}
5958

6059

6160
var indent = ' ';
@@ -79,8 +78,8 @@ AssertionError.prototype = Object.create(Error.prototype, {
7978
if (!this.operator && this.previous) {
8079
return this.previous.message;
8180
}
82-
var actual = util.format(this.actual);
83-
var expected = 'expected' in this ? ' ' + util.format(this.expected) : '';
81+
var actual = format(this.actual);
82+
var expected = 'expected' in this ? ' ' + format(this.expected) : '';
8483
var details = 'details' in this && this.details ? ' (' + this.details + ')' : '';
8584

8685
var previous = this.previous ? '\n' + indentLines(this.previous.message) : '';
@@ -89,5 +88,3 @@ AssertionError.prototype = Object.create(Error.prototype, {
8988
}
9089
}
9190
});
92-
93-
module.exports = AssertionError;

lib/assertion.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* MIT Licensed
66
*/
77

8-
var AssertionError = require('./assertion-error');
8+
import AssertionError from './assertion-error';
99

1010
/**
1111
* should Assertion
@@ -14,7 +14,7 @@ var AssertionError = require('./assertion-error');
1414
* @memberOf should
1515
* @static
1616
*/
17-
function Assertion(obj) {
17+
export function Assertion(obj) {
1818
this.obj = obj;
1919

2020
this.anyOne = false;
@@ -101,7 +101,7 @@ Assertion.prototype = {
101101
*
102102
* @param {Promise} obj
103103
*/
104-
function PromisedAssertion(/* obj */) {
104+
export function PromisedAssertion(/* obj */) {
105105
Assertion.apply(this, arguments);
106106
}
107107

@@ -242,7 +242,9 @@ Assertion.addChain = function(name, onCall) {
242242
*/
243243
Assertion.alias = function(from, to) {
244244
var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
245-
if (!desc) throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
245+
if (!desc) {
246+
throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
247+
}
246248
Object.defineProperty(Assertion.prototype, to, desc);
247249

248250
var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from);
@@ -273,6 +275,3 @@ Assertion.addChain('not', function() {
273275
Assertion.addChain('any', function() {
274276
this.anyOne = true;
275277
});
276-
277-
module.exports = Assertion;
278-
module.exports.PromisedAssertion = PromisedAssertion;

lib/config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
* MIT Licensed
66
*/
77

8-
var Formatter = require('should-format').Formatter;
8+
import format from 'should-format';
99

1010
var config = {
11-
checkProtoEql: false,
12-
1311
getFormatter: function(opts) {
14-
return new Formatter(opts || config);
12+
return new format.Formatter(opts || config);
1513
}
1614
};
1715

18-
module.exports = config;
16+
export default config;

lib/ext/_assert.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
// when used in node, this will actually load the util module we depend on
2828
// versus loading the builtin util module as happens otherwise
2929
// this is a bug in node module loading as far as I am concerned
30-
var Assertion = require('./../assertion');
30+
import { Assertion } from './../assertion';
3131

32-
var _deepEqual = require('should-equal');
32+
import _deepEqual from 'should-equal';
3333

3434
var pSlice = Array.prototype.slice;
3535

3636
// 1. The assert module provides functions that throw
3737
// AssertionError's when particular conditions are not met. The
3838
// assert module must conform to the following interface.
3939

40-
var assert = module.exports = ok;
40+
var assert = ok;
41+
export default assert;
4142

4243
// 3. All of the following functions must throw an AssertionError
4344
// when a corresponding condition is not met, with a message that
@@ -84,7 +85,9 @@ assert.fail = fail;
8485
* @param {string} [message]
8586
*/
8687
function ok(value, message) {
87-
if (!value) fail(value, true, message, '==', assert.ok);
88+
if (!value) {
89+
fail(value, true, message, '==', assert.ok);
90+
}
8891
}
8992
assert.ok = ok;
9093

@@ -102,7 +105,9 @@ assert.ok = ok;
102105
* @param {string} [message]
103106
*/
104107
assert.equal = function equal(actual, expected, message) {
105-
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
108+
if (actual != expected) {
109+
fail(actual, expected, message, '==', assert.equal);
110+
}
106111
};
107112

108113
// 6. The non-equality assertion tests for whether two objects are not equal
@@ -136,7 +141,7 @@ assert.notEqual = function notEqual(actual, expected, message) {
136141
* @param {string} [message]
137142
*/
138143
assert.deepEqual = function deepEqual(actual, expected, message) {
139-
if (!_deepEqual(actual, expected).result) {
144+
if (_deepEqual(actual, expected).length !== 0) {
140145
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
141146
}
142147
};

lib/ext/assert.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* MIT Licensed
66
*/
77

8-
var util = require('../util');
9-
var assert = require('./_assert');
10-
var AssertionError = require('../assertion-error');
8+
import { merge, format } from '../util';
9+
import assert from './_assert';
10+
import AssertionError from '../assertion-error';
1111

12-
module.exports = function(should) {
13-
var i = should.format;
12+
export default function(should) {
13+
var i = format;
1414

1515
/*
1616
* Expose assert to should
@@ -21,7 +21,7 @@ module.exports = function(should) {
2121
* should.equal(foo.bar, undefined);
2222
*
2323
*/
24-
util.merge(should, assert);
24+
merge(should, assert);
2525

2626
/**
2727
* Assert _obj_ exists, with optional message.
@@ -68,4 +68,4 @@ module.exports = function(should) {
6868
});
6969
}
7070
};
71-
};
71+
}

lib/ext/bool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* MIT Licensed
66
*/
77

8-
module.exports = function(should, Assertion) {
8+
export default function(should, Assertion) {
99
/**
1010
* Assert given object is exactly `true`.
1111
*
@@ -67,4 +67,4 @@ module.exports = function(should, Assertion) {
6767

6868
this.assert(this.obj);
6969
});
70-
};
70+
}

lib/ext/chain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* MIT Licensed
66
*/
77

8-
module.exports = function(should, Assertion) {
8+
export default function(should, Assertion) {
99
/**
1010
* Simple chaining. It actually do nothing.
1111
*
@@ -28,4 +28,4 @@ module.exports = function(should, Assertion) {
2828
['an', 'of', 'a', 'and', 'be', 'has', 'have', 'with', 'is', 'which', 'the', 'it'].forEach(function(name) {
2929
Assertion.addChain(name);
3030
});
31-
};
31+
}

lib/ext/contain.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* MIT Licensed
66
*/
77

8-
var util = require('../util');
9-
var eql = require('should-equal');
8+
import { format, isIndexable, forEach, some, isEmptyObject, length } from '../util';
9+
import eql from 'should-equal';
1010

11-
module.exports = function(should, Assertion) {
12-
var i = should.format;
11+
export default function(should, Assertion) {
12+
var i = format;
1313

1414
/**
1515
* Assert that given object contain something that equal to `other`. It uses `should-equal` for equality checks.
@@ -44,9 +44,9 @@ module.exports = function(should, Assertion) {
4444

4545
if (typeof obj == 'string') {
4646
this.assert(obj.indexOf(String(other)) >= 0);
47-
} else if (util.isIndexable(obj)) {
48-
this.assert(util.some(obj, function(v) {
49-
return eql(v, other).result;
47+
} else if (isIndexable(obj)) {
48+
this.assert(some(obj, function(v) {
49+
return eql(v, other).length === 0;
5050
}));
5151
} else {
5252
this.have.properties(other);
@@ -78,8 +78,8 @@ module.exports = function(should, Assertion) {
7878
var obj = this.obj;
7979
if (typeof obj == 'string') {// expect other to be string
8080
this.is.equal(String(other));
81-
} else if (util.isIndexable(obj) && util.isIndexable(other)) {
82-
for (var objIdx = 0, otherIdx = 0, objLength = util.length(obj), otherLength = util.length(other); objIdx < objLength && otherIdx < otherLength; objIdx++) {
81+
} else if (isIndexable(obj) && isIndexable(other)) {
82+
for (var objIdx = 0, otherIdx = 0, objLength = length(obj), otherLength = length(other); objIdx < objLength && otherIdx < otherLength; objIdx++) {
8383
try {
8484
should(obj[objIdx]).containDeepOrdered(other[otherIdx]);
8585
otherIdx++;
@@ -93,12 +93,12 @@ module.exports = function(should, Assertion) {
9393

9494
this.assert(otherIdx === otherLength);
9595
} else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
96-
util.forEach(other, function(value, key) {
96+
forEach(other, function(value, key) {
9797
should(obj[key]).containDeepOrdered(value);
9898
});
9999

100100
// if both objects is empty means we finish traversing - and we need to compare for hidden values
101-
if (util.isEmptyObject(other)) {
101+
if (isEmptyObject(other)) {
102102
this.eql(other);
103103
}
104104
} else {
@@ -124,11 +124,13 @@ module.exports = function(should, Assertion) {
124124
var obj = this.obj;
125125
if (typeof obj == 'string') {// expect other to be string
126126
this.is.equal(String(other));
127-
} else if (util.isIndexable(obj) && util.isIndexable(other)) {
127+
} else if (isIndexable(obj) && isIndexable(other)) {
128128
var usedKeys = {};
129-
util.forEach(other, function(otherItem) {
130-
this.assert(util.some(obj, function(item, index) {
131-
if (index in usedKeys) return false;
129+
forEach(other, function(otherItem) {
130+
this.assert(some(obj, function(item, index) {
131+
if (index in usedKeys) {
132+
return false;
133+
}
132134

133135
try {
134136
should(item).containDeep(otherItem);
@@ -143,17 +145,17 @@ module.exports = function(should, Assertion) {
143145
}));
144146
}, this);
145147
} else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
146-
util.forEach(other, function(value, key) {
148+
forEach(other, function(value, key) {
147149
should(obj[key]).containDeep(value);
148150
});
149151

150152
// if both objects is empty means we finish traversing - and we need to compare for hidden values
151-
if (util.isEmptyObject(other)) {
153+
if (isEmptyObject(other)) {
152154
this.eql(other);
153155
}
154156
} else {
155157
this.eql(other);
156158
}
157159
});
158160

159-
};
161+
}

lib/ext/eql.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* MIT Licensed
66
*/
77

8-
var eql = require('should-equal');
9-
var type = require('should-type');
10-
var util = require('../util');
8+
import eql from 'should-equal';
9+
import getType from 'should-type';
10+
import { formatProp, format, forEach } from '../util';
1111

1212
function formatEqlResult(r, a, b) {
13-
return ((r.path.length > 0 ? 'at ' + r.path.map(util.formatProp).join(' -> ') : '') +
14-
(r.a === a ? '' : ', A has ' + util.format(r.a)) +
15-
(r.b === b ? '' : ' and B has ' + util.format(r.b)) +
13+
return ((r.path.length > 0 ? 'at ' + r.path.map(formatProp).join(' -> ') : '') +
14+
(r.a === a ? '' : ', A has ' + format(r.a)) +
15+
(r.b === b ? '' : ' and B has ' + format(r.b)) +
1616
(r.showReason ? ' because ' + r.reason : '')).trim();
1717
}
1818

19-
module.exports = function(should, Assertion) {
19+
export default function(should, Assertion) {
2020

2121
/**
2222
* Deep object equality comparison. For full spec see [`should-equal tests`](https://github.com/shouldjs/equal/blob/master/test.js).
@@ -40,13 +40,15 @@ module.exports = function(should, Assertion) {
4040
*/
4141
Assertion.add('eql', function(val, description) {
4242
this.params = {operator: 'to equal', expected: val, message: description};
43+
var obj = this.obj;
44+
var fails = eql(this.obj, val, should.config);
45+
this.params.details = fails.map(function(fail) {
46+
return formatEqlResult(fail, obj, val);
47+
}).join(', ');
4348

44-
var result = eql(this.obj, val, should.config);
45-
this.params.details = result.result ? '' : formatEqlResult(result, this.obj, val);
49+
this.params.showDiff = eql(getType(obj), getType(val)).length === 0;
4650

47-
this.params.showDiff = eql(type(this.obj), type(val)).result;
48-
49-
this.assert(result.result);
51+
this.assert(fails.length === 0);
5052
});
5153

5254
/**
@@ -68,7 +70,7 @@ module.exports = function(should, Assertion) {
6870
Assertion.add('equal', function(val, description) {
6971
this.params = {operator: 'to be', expected: val, message: description};
7072

71-
this.params.showDiff = eql(type(this.obj), type(val)).result;
73+
this.params.showDiff = eql(getType(this.obj), getType(val)).length === 0;
7274

7375
this.assert(val === this.obj);
7476
});
@@ -89,7 +91,7 @@ module.exports = function(should, Assertion) {
8991
var obj = this.obj;
9092
var found = false;
9193

92-
util.forEach(vals, function(val) {
94+
forEach(vals, function(val) {
9395
try {
9496
should(val)[method](obj);
9597
found = true;
@@ -134,4 +136,4 @@ module.exports = function(should, Assertion) {
134136
*/
135137
addOneOf('oneOf', 'to be one of', 'eql');
136138

137-
};
139+
}

0 commit comments

Comments
 (0)