Skip to content

Commit

Permalink
Inline type-detect as a simple function (#1544)
Browse files Browse the repository at this point in the history
* inline `type-detect` as a simple function

* Add type-detec tests

* update type-detect function

* update existing tests
  • Loading branch information
Kristján Oddsson authored Oct 20, 2023
1 parent de9fa39 commit 9e276dc
Show file tree
Hide file tree
Showing 19 changed files with 856 additions and 33 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ Chai offers a robust Plugin architecture for extending Chai's assertions and int
- [chaijs / chai-docs](https://github.com/chaijs/chai-docs): The chaijs.com website source code.
- [chaijs / assertion-error](https://github.com/chaijs/assertion-error): Custom `Error` constructor thrown upon an assertion failing.
- [chaijs / deep-eql](https://github.com/chaijs/deep-eql): Improved deep equality testing for Node.js and the browser.
- [chaijs / type-detect](https://github.com/chaijs/type-detect): Improved typeof detection for Node.js and the browser.
- [chaijs / check-error](https://github.com/chaijs/check-error): Error comparison and information related utility for Node.js and the browser.
- [chaijs / loupe](https://github.com/chaijs/loupe): Inspect utility for Node.js and browsers.
- [chaijs / pathval](https://github.com/chaijs/pathval): Object value retrieval given a string path.
Expand Down
1 change: 1 addition & 0 deletions karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = function(config) {
{ pattern: 'chai.js', type: 'module', included: false, served: true }
, { pattern: 'test/bootstrap/index.js', type: 'module'}
, { pattern: 'test/*.js', type: 'module' }
, { pattern: 'test/type-detect/*.js', type: 'module' }
]
, reporters: [ 'progress' ]
, colors: true
Expand Down
4 changes: 2 additions & 2 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Assertion.addProperty('all', function () {
* ### .a(type[, msg])
*
* Asserts that the target's type is equal to the given string `type`. Types
* are case insensitive. See the `type-detect` project page for info on the
* type detection algorithm: https://github.com/chaijs/type-detect.
* are case insensitive. See the utility file `./type-detect.js` for info on the
* type detection algorithm.
*
* expect('foo').to.be.a('string');
* expect({a: 1}).to.be.an('object');
Expand Down
2 changes: 1 addition & 1 deletion lib/chai/utils/expectTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import AssertionError from 'assertion-error';
import {flag} from './flag.js';
import {default as type} from 'type-detect';
import {type} from './type-detect.js';

export function expectTypes(obj, types) {
var flagMsg = flag(obj, 'message');
Expand Down
5 changes: 2 additions & 3 deletions lib/chai/utils/getOperator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {flag} from './flag.js';
import type from 'type-detect';

import {type} from './type-detect.js';

function isObjectType(obj) {
var objectType = type(obj);
var objectTypes = ['Array', 'Object', 'function'];
var objectTypes = ['Array', 'Object', 'Function'];

return objectTypes.indexOf(objectType) !== -1;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export {test} from './test.js';
* type utility
*/

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

/*!
* expectTypes utility
Expand Down
16 changes: 16 additions & 0 deletions lib/chai/utils/type-detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function type(obj) {
if (typeof obj === 'undefined') {
return 'undefined';
}

if (obj === null) {
return 'null';
}

const stringTag = obj[Symbol.toStringTag];
if (typeof stringTag === 'string') {
return stringTag;
}
const type = Object.prototype.toString.call(obj).slice(8, -1);
return type;
}
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"check-error": "^2.0.0",
"deep-eql": "^5.0.0",
"loupe": "^2.3.1",
"pathval": "^2.0.0",
"type-detect": "^4.0.5"
"pathval": "^2.0.0"
},
"devDependencies": {
"bump-cli": "^1.1.3",
Expand Down
18 changes: 9 additions & 9 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,19 @@ describe('assert', function () {

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

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

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

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

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

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

err(function() {
var FakeConstructor = {};
Expand Down Expand Up @@ -233,19 +233,19 @@ describe('assert', function () {

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

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

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

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

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

err(function() {
var FakeConstructor = {};
Expand Down
2 changes: 1 addition & 1 deletion test/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (typeof Error.captureStackTrace !== 'undefined') {
*/

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

try {
Expand Down
12 changes: 6 additions & 6 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,23 +403,23 @@ describe('expect', function () {

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

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

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

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

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

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

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

err(function() {
var FakeConstructor = {};
Expand Down
10 changes: 5 additions & 5 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,19 @@ describe('should', function() {

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

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

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

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

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

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

err(function() {
var FakeConstructor = {};
Expand Down
7 changes: 7 additions & 0 deletions test/type-detect/deno-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* global Deno:readonly */
// @ts-nocheck
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
import typeDetect from '../index.ts';
Deno.test('type detect works', () => {
assertEquals(typeDetect('hello'), 'string');
});
Loading

0 comments on commit 9e276dc

Please sign in to comment.