Skip to content

Commit 9e276dc

Browse files
author
Kristján Oddsson
authored
Inline type-detect as a simple function (#1544)
* inline `type-detect` as a simple function * Add type-detec tests * update type-detect function * update existing tests
1 parent de9fa39 commit 9e276dc

19 files changed

+856
-33
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ Chai offers a robust Plugin architecture for extending Chai's assertions and int
145145
- [chaijs / chai-docs](https://github.com/chaijs/chai-docs): The chaijs.com website source code.
146146
- [chaijs / assertion-error](https://github.com/chaijs/assertion-error): Custom `Error` constructor thrown upon an assertion failing.
147147
- [chaijs / deep-eql](https://github.com/chaijs/deep-eql): Improved deep equality testing for Node.js and the browser.
148-
- [chaijs / type-detect](https://github.com/chaijs/type-detect): Improved typeof detection for Node.js and the browser.
149148
- [chaijs / check-error](https://github.com/chaijs/check-error): Error comparison and information related utility for Node.js and the browser.
150149
- [chaijs / loupe](https://github.com/chaijs/loupe): Inspect utility for Node.js and browsers.
151150
- [chaijs / pathval](https://github.com/chaijs/pathval): Object value retrieval given a string path.

karma.conf.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = function(config) {
55
{ pattern: 'chai.js', type: 'module', included: false, served: true }
66
, { pattern: 'test/bootstrap/index.js', type: 'module'}
77
, { pattern: 'test/*.js', type: 'module' }
8+
, { pattern: 'test/type-detect/*.js', type: 'module' }
89
]
910
, reporters: [ 'progress' ]
1011
, colors: true

lib/chai/core/assertions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ Assertion.addProperty('all', function () {
243243
* ### .a(type[, msg])
244244
*
245245
* Asserts that the target's type is equal to the given string `type`. Types
246-
* are case insensitive. See the `type-detect` project page for info on the
247-
* type detection algorithm: https://github.com/chaijs/type-detect.
246+
* are case insensitive. See the utility file `./type-detect.js` for info on the
247+
* type detection algorithm.
248248
*
249249
* expect('foo').to.be.a('string');
250250
* expect({a: 1}).to.be.an('object');

lib/chai/utils/expectTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import AssertionError from 'assertion-error';
2222
import {flag} from './flag.js';
23-
import {default as type} from 'type-detect';
23+
import {type} from './type-detect.js';
2424

2525
export function expectTypes(obj, types) {
2626
var flagMsg = flag(obj, 'message');

lib/chai/utils/getOperator.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {flag} from './flag.js';
2-
import type from 'type-detect';
3-
2+
import {type} from './type-detect.js';
43

54
function isObjectType(obj) {
65
var objectType = type(obj);
7-
var objectTypes = ['Array', 'Object', 'function'];
6+
var objectTypes = ['Array', 'Object', 'Function'];
87

98
return objectTypes.indexOf(objectType) !== -1;
109
}

lib/chai/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export {test} from './test.js';
2020
* type utility
2121
*/
2222

23-
export {default as type} from 'type-detect';
23+
export {type} from './type-detect.js';
2424

2525
/*!
2626
* expectTypes utility

lib/chai/utils/type-detect.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function type(obj) {
2+
if (typeof obj === 'undefined') {
3+
return 'undefined';
4+
}
5+
6+
if (obj === null) {
7+
return 'null';
8+
}
9+
10+
const stringTag = obj[Symbol.toStringTag];
11+
if (typeof stringTag === 'string') {
12+
return stringTag;
13+
}
14+
const type = Object.prototype.toString.call(obj).slice(8, -1);
15+
return type;
16+
}

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
"check-error": "^2.0.0",
4848
"deep-eql": "^5.0.0",
4949
"loupe": "^2.3.1",
50-
"pathval": "^2.0.0",
51-
"type-detect": "^4.0.5"
50+
"pathval": "^2.0.0"
5251
},
5352
"devDependencies": {
5453
"bump-cli": "^1.1.3",

test/assert.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,19 @@ describe('assert', function () {
171171

172172
err(function(){
173173
assert.instanceOf(new Foo(), 1, 'blah');
174-
}, "blah: The instanceof assertion needs a constructor but number was given.");
174+
}, "blah: The instanceof assertion needs a constructor but Number was given.");
175175

176176
err(function(){
177177
assert.instanceOf(new Foo(), 'batman');
178-
}, "The instanceof assertion needs a constructor but string was given.");
178+
}, "The instanceof assertion needs a constructor but String was given.");
179179

180180
err(function(){
181181
assert.instanceOf(new Foo(), {});
182182
}, "The instanceof assertion needs a constructor but Object was given.");
183183

184184
err(function(){
185185
assert.instanceOf(new Foo(), true);
186-
}, "The instanceof assertion needs a constructor but boolean was given.");
186+
}, "The instanceof assertion needs a constructor but Boolean was given.");
187187

188188
err(function(){
189189
assert.instanceOf(new Foo(), null);
@@ -198,12 +198,12 @@ describe('assert', function () {
198198
var t = new Thing();
199199
Thing.prototype = 1337;
200200
assert.instanceOf(t, Thing);
201-
}, 'The instanceof assertion needs a constructor but function was given.', true);
201+
}, 'The instanceof assertion needs a constructor but Function was given.', true);
202202

203203
if (typeof Symbol !== 'undefined' && typeof Symbol.hasInstance !== 'undefined') {
204204
err(function(){
205205
assert.instanceOf(new Foo(), Symbol());
206-
}, "The instanceof assertion needs a constructor but symbol was given.");
206+
}, "The instanceof assertion needs a constructor but Symbol was given.");
207207

208208
err(function() {
209209
var FakeConstructor = {};
@@ -233,19 +233,19 @@ describe('assert', function () {
233233

234234
err(function(){
235235
assert.notInstanceOf(new Foo(), 1, 'blah');
236-
}, "blah: The instanceof assertion needs a constructor but number was given.");
236+
}, "blah: The instanceof assertion needs a constructor but Number was given.");
237237

238238
err(function(){
239239
assert.notInstanceOf(new Foo(), 'batman');
240-
}, "The instanceof assertion needs a constructor but string was given.");
240+
}, "The instanceof assertion needs a constructor but String was given.");
241241

242242
err(function(){
243243
assert.notInstanceOf(new Foo(), {});
244244
}, "The instanceof assertion needs a constructor but Object was given.");
245245

246246
err(function(){
247247
assert.notInstanceOf(new Foo(), true);
248-
}, "The instanceof assertion needs a constructor but boolean was given.");
248+
}, "The instanceof assertion needs a constructor but Boolean was given.");
249249

250250
err(function(){
251251
assert.notInstanceOf(new Foo(), null);
@@ -258,7 +258,7 @@ describe('assert', function () {
258258
if (typeof Symbol !== 'undefined' && typeof Symbol.hasInstance !== 'undefined') {
259259
err(function(){
260260
assert.notInstanceOf(new Foo(), Symbol());
261-
}, "The instanceof assertion needs a constructor but symbol was given.");
261+
}, "The instanceof assertion needs a constructor but Symbol was given.");
262262

263263
err(function() {
264264
var FakeConstructor = {};

test/bootstrap/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (typeof Error.captureStackTrace !== 'undefined') {
3030
*/
3131

3232
globalThis.err = function globalErr (fn, val, skipStackTest) {
33-
if (chai.util.type(fn) !== 'function')
33+
if (chai.util.type(fn) !== 'Function')
3434
throw new chai.AssertionError('Invalid fn');
3535

3636
try {

test/expect.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,23 +403,23 @@ describe('expect', function () {
403403

404404
err(function(){
405405
expect(new Foo()).to.an.instanceof(1, 'blah');
406-
}, "blah: The instanceof assertion needs a constructor but number was given.");
406+
}, "blah: The instanceof assertion needs a constructor but Number was given.");
407407

408408
err(function(){
409409
expect(new Foo(), 'blah').to.an.instanceof(1);
410-
}, "blah: The instanceof assertion needs a constructor but number was given.");
410+
}, "blah: The instanceof assertion needs a constructor but Number was given.");
411411

412412
err(function(){
413413
expect(new Foo()).to.an.instanceof('batman');
414-
}, "The instanceof assertion needs a constructor but string was given.");
414+
}, "The instanceof assertion needs a constructor but String was given.");
415415

416416
err(function(){
417417
expect(new Foo()).to.an.instanceof({});
418418
}, "The instanceof assertion needs a constructor but Object was given.");
419419

420420
err(function(){
421421
expect(new Foo()).to.an.instanceof(true);
422-
}, "The instanceof assertion needs a constructor but boolean was given.");
422+
}, "The instanceof assertion needs a constructor but Boolean was given.");
423423

424424
err(function(){
425425
expect(new Foo()).to.an.instanceof(null);
@@ -434,12 +434,12 @@ describe('expect', function () {
434434
var t = new Thing();
435435
Thing.prototype = 1337;
436436
expect(t).to.an.instanceof(Thing);
437-
}, 'The instanceof assertion needs a constructor but function was given.', true)
437+
}, 'The instanceof assertion needs a constructor but Function was given.', true)
438438

439439
if (typeof Symbol !== 'undefined' && typeof Symbol.hasInstance !== 'undefined') {
440440
err(function(){
441441
expect(new Foo()).to.an.instanceof(Symbol());
442-
}, "The instanceof assertion needs a constructor but symbol was given.");
442+
}, "The instanceof assertion needs a constructor but Symbol was given.");
443443

444444
err(function() {
445445
var FakeConstructor = {};

test/should.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,19 @@ describe('should', function() {
468468

469469
err(function(){
470470
new Foo().should.be.an.instanceof(1, 'blah');
471-
}, "blah: The instanceof assertion needs a constructor but number was given.");
471+
}, "blah: The instanceof assertion needs a constructor but Number was given.");
472472

473473
err(function(){
474474
new Foo().should.be.an.instanceof('batman');
475-
}, "The instanceof assertion needs a constructor but string was given.");
475+
}, "The instanceof assertion needs a constructor but String was given.");
476476

477477
err(function(){
478478
new Foo().should.be.an.instanceof({});
479479
}, "The instanceof assertion needs a constructor but Object was given.");
480480

481481
err(function(){
482482
new Foo().should.be.an.instanceof(true);
483-
}, "The instanceof assertion needs a constructor but boolean was given.");
483+
}, "The instanceof assertion needs a constructor but Boolean was given.");
484484

485485
err(function(){
486486
new Foo().should.be.an.instanceof(null);
@@ -495,12 +495,12 @@ describe('should', function() {
495495
var t = new Thing();
496496
Thing.prototype = 1337;
497497
t.should.be.an.instanceof(Thing);
498-
}, 'The instanceof assertion needs a constructor but function was given.', true);
498+
}, 'The instanceof assertion needs a constructor but Function was given.', true);
499499

500500
if (typeof Symbol !== 'undefined' && typeof Symbol.hasInstance !== 'undefined') {
501501
err(function(){
502502
new Foo().should.be.an.instanceof(Symbol());
503-
}, "The instanceof assertion needs a constructor but symbol was given.");
503+
}, "The instanceof assertion needs a constructor but Symbol was given.");
504504

505505
err(function() {
506506
var FakeConstructor = {};

test/type-detect/deno-test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* global Deno:readonly */
2+
// @ts-nocheck
3+
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
4+
import typeDetect from '../index.ts';
5+
Deno.test('type detect works', () => {
6+
assertEquals(typeDetect('hello'), 'string');
7+
});

0 commit comments

Comments
 (0)