chaijs / type-detect Goto Github PK
View Code? Open in Web Editor NEWImproved typeof detection for node.js and the browser.
License: MIT License
Improved typeof detection for node.js and the browser.
License: MIT License
README.md mentions some features only in passing, without supporting documentation, thus leaving some features 'hidden' and unusable.
It mentions ES2015 support and documents many of the added APIs, but not all. Surprised at no mention of iterators I still found them supported in the code.
Return possibilities
'Map Iterator'
'Set Iterator'
'Array Iterator'
'String Iterator'
should be documented, though it may be hard to come up with easily understood examples on when these values are encountered. A combination of built-in objects and method calls ...?
assert(type(new Map()).entries() === 'Map Iterator')
assert(type(new Set()).entries() === 'Set Iterator')
While spelunking I found these browser/DOM-related values in the code:
return value | notes on source |
---|---|
'global' | (browser) window , (node) global , others... |
'Location' | window.location |
'Document' | window.document |
'MimeTypeArray' | window.navigator.mimeTypes |
'PluginArray' | window.navigator.plugins |
'HTMLQuoteElement' | (IE fix) HTMLElement HTMLElement BLOCKQUOTE |
'HTMLTableDataCellElement' | (W3C vs. WhatWG fix) HTMLElement TD |
'HTMLTableHeaderCellElement' | (W3C vs. WhatWG fix) HTMLElement TH |
After much scrolling of doc/code windows I think the above are all the missing parts.
If the mentions in passing of "global object", "iterators", "browsers" and "HTML elements" in README.md can be matched up with documented type string return values then readers will know how to use all the features this package provides.
When running browserify to bundle test files using chai, I am getting the following error:
Cannot find module './lib-cov/type'
This is because index.js has a conditional require that is not supported by browserify. There was a similar problem in chai itself that has been fixed, is it possible to apply the same fix to type-detect too? See chaijs/chai#28 and chaijs/deep-eql#1.
With the update from version 4.0.3 to 4.0.3 I cannot require "type-detect/index.js" any longer, because it complains about a SyntaxError.
export default function typeDetect(obj) {
^^^^^^
Changes introduced by #129
After switching to use window
rather than globalObject
, tests running in node with a mock window
(e.g. jsdom) will fail when manipulating (or deleting) global.window
at runtime. I don't know how common this is, but it is a quick way to test modules intended to run on both client and server.
mocha --require config.js test
// config.js
const { JSDOM } = require('jsdom');
const { window } = new JSDOM('<!doctype html><html><body></body></html>');
global.window = window;
// test/foo.spec.js
const { expect } = require('chai');
describe('simplified example', function () {
it('should perform assertions in a browser-like environment', function () {
expect({}).to.eql({});
});
it('should not break when window removed', function () {
global.window = null; // ideally reset afterward
expect({}).to.eql({}); // boom!
// TypeError: Cannot read property 'location' of null
// at typeDetect (node_modules/type-detect/type-detect.js:127:47)
});
});
This could be avoided by chai
users with greater care (and clear documentation), but I believe it was likely an unintended side effect of the changes made in #129.
Possible solutions:
typeDetect
function, rather than cached in the module. This will be slightly less efficient but will be accurate. It also raises the question as to whether all feature checks should be evaluated on every call as well.It looks like there is a breaking change. We have locked our version on chai but we now get the following error:
/src/node_modules/type-detect/index.js:49
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
On my lightweight AWS EC2 instance with no browsers installed, it remains stuck at the following part when running the test suite with the new build process and plumbing in place:
type-detect@ test:browser /opt/code/type-detect
karma start --singleRun=true02 11 2017 21:33:01.012:INFO [framework.detect-browsers]: The following browsers were detected on your system: []
02 11 2017 21:33:01.050:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
Looks related to travis-ci/travis-ci#8836. There are suggestions of either adding sudo: required
to the config, or adding the --no-sandbox
flag to the karma launcher for Chrome.
15 01 2018 03:14:44.739:INFO [launcher]: Starting browser ChromeHeadless
15 01 2018 03:14:45.013:ERROR [launcher]: Cannot start ChromeHeadless
[0115/031444.860776:FATAL:setuid_sandbox_host.cc(157)] The SUID sandbox helper binary was found, but is not configured correctly. Rather than run without sandboxing I'm aborting now. You need to make sure that /opt/google/chrome/chrome-sandbox is owned by root and has mode 4755.
Failed to generate minidump.
[email protected] uses ES6 modules in its main
which causes it to throw the following error when run in node:
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
I assume this was accidental, since it's a massive breaking change in a patch release making chai unusable. You may wish to unpublish 4.0.4.
Hi, I'm trying to use chai 4.3.4 in an environment that is neither node.js or the browser, and it works fine except for the fact that the polyfill to detect the global object as of type-detect 4.0.5 does not work in my environment (global
does not exist, and neither does self
, but globalThis
does). Recent changes in #142 would absolutely work for my case, but there's no npm package right now that would pick them up.
Could type-detect and Chai get version bumps with these recent changes? thank you
@keithamus , @meeber
I was sleeping for a whole while in this project - sorry for that.
But I`ve awoken and found an awful lot of Greenkeeper-PRs having a failing build.
I want to tackle those PRs now but I want to talk a strategy with you first.
First of all, there is a lot of updated dependencies which simply fly around build-tooling. We could ask ourselves if we really need those updates.
Then there is a bunch of outdated PRs of updated dependncies. For example ESLint has PRs for laround 6 different Versions. For those, I would just close the old-version-PRs and only leave the newest ones - if we already gonna put work into this, then let's do it right.
That should already clear the field for a better overview.
What do you think?
I think, tests should pass:
const type = require('type-detect');
const assert = require('assert');
const asyncFn = async () => {};
const generatorFn = function* generator () {};
function getTag(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
assert(type(asyncFn) === getTag(asyncFn));
assert(type(generatorFn) === getTag(generatorFn));
Is it a bug? If not, you should add it to README.
@logicalparadox @keithamus
I was wondering how are the next steps here. Are you going to release a new version? Or you want me to move chai
to the current master branch?
Also, I found one more little mistake in the docs:
assert(type(new String('hello')) === 'object');
-> that should be 'string'
Its probably easier now to fix that directly on master than for me doing a pull request. ;)
The following assertion fails:
assert(type(Symbol()) === 'Symbol');
// AssertionError
It is taken from the README.md file. The actual result of the type-check is 'symbol' (lowercase)
So, type(Symbol()) !== Symbol.name
.
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
We are still using node6 LTS and this morning, our tests (using chai) fails with this error.
Merging #91 didn't trigger a release. Found this in the travis logs:
6.95s$ travis-after-all && npm run semantic-release
[travis-after-all] Job succeeded (40027526e0227d81659090077756bde5fb9776ff1d5e08fb38f991cd2a5144cc)
[travis-after-all] Waiting for 427.2, 427.3, 427.4 and 427.5 to be done...
[travis-after-all] Failed to parse JSON from "https://api.travis-ci.org/jobs/295118317".
Error: Cannot read property 'indexOf' of null
Hello ๐
I run a security community that finds and fixes vulnerabilities in OSS. A researcher (@austinoa18) has found a potential issue, which I would be eager to share with you.
Could you add a SECURITY.md
file with an e-mail address for me to send further details to? GitHub recommends a security policy to ensure issues are responsibly disclosed, and it would help direct researchers in the future.
Looking forward to hearing from you ๐
(cc @huntr-helper)
https://github.com/chaijs/type-detect/blob/master/rollup.conf.js#L17 seems to break type-detect
when running in nodejs environment because it generates
export default function typeDetect(obj) {
which is not supported by nodejs
hi all,
This library introduces some types that don't exist in ES5 and I found it slightly annoying to do a guesswork, which type starts with capital letter and which does not.
I couldn't find the list of types in Chai documentation website. Even if it's there hidden somewhere, it would be nice to add the list of all types that this library produces in this library's README.
For example (with correct capitalisation):
string
<< lowercaseObject
<< uppercaseFunction
<< uppercaseboolean
<< lowercaseand so on..
What do you think?
On node 8.1.0 (probably also on older versions supporting async functions) calling returns asyncfunction
:
const typeDetect = require('type-detect');
typeDetect(async function() {})
I believe this is not correct and should return function
since async functions are noting more than normal functions returning promises from the function user's PoV.
What are your thoughts on this?
Hello! After upgrading to version 4.0.4 I cannot run my mocha tests anymore. I am getting SyntaxError: Unexpected token export on the index.js file of the type-detect. Here is the full stack:
C:\Tools\nodejs\node.exe "\node_modules\mocha\bin_mocha" --ui bdd --reporter C:\Users\taakopa7.IntelliJIdea2017.2\config\plugins\NodeJS\js\mocha-intellij\lib\mochaIntellijReporter.js "\test\mytest.js" --grep "Test "
\node_modules\type-detect\index.js:49
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (\node_modules\chai\lib\chai\utils\index.js:23:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (\node_modules\chai\lib\chai.js:25:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (\node_modules\chai\index.js:1:80)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (\test\mytest.js:2:12)
Sorry in advance if the reason seems obvious to you and I am just bothering you. I can't sleep well at night with that line keep showing up in my head. Googling show nothing that could explain it. Is there any special case needs to be handled by that line?
We have specific conformance checks for td
elements being HTMLTableDataCellElement
and th
being HTMLTableHeaderCellElement
- as per what was (at the time) the WHATWG Spec.
However the WHATWG Spec has been aligned to the browsers (see whatwg/html#1125, whatwg/html#1115) and any implementation that was destined to reflect this has backed out. So we should too.
Proposal is to remove both of these conformance checks and change the tests to reflect this.
Relevant LOC:
Lines 177 to 207 in f79e0e4
These two tests will need to change (but we should keep them):
Lines 286 to 292 in f79e0e4
More info here: https://travis-ci.org/chaijs/type-detect/jobs/299299300
When importing type-detect in the browser, the function is called 'typeDetect'
If the usage examples are followed as-is, the browser will throw an error indicating that type() is not defined.
In order to run the examples as written in the browser, the user should declare a variable type that refers to typeDetect(). An alternative would be to provide another set of browser-specific examples using typeDetect() instead of type(), but I think that would be overly verbose and confusing.
I did chai
with yarn
(yarn add chai -D
) and it pulled type-detect
v4.0.5. After that it breaks on mocha
as it is not transpiled. I did same with npm install
and it worked. I see transpiled file.
It is Node.JS v8.9.1, npm v5.5.1, yarn v1.2.1
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
Advice?
During my build I'm getting:
/usr/src/app/node_modules/type-detect/index.js:49
export default function typeDetect(obj) {
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
It's intentional? Because it seems like huge change, not a minor patch โ v4.0.3
used old module syntax.
function
is not primitive so you should definitely return 'Function' instead
Hello,
I'm using Webstorm to run Mocha tests (on react components) using Chai together with Enzyme, Sinon and Sinon-chai.
I'm doing something like this:
expect(ChildSpy).to.have.been.calledWithMatch({a:'a', b: 'b'});
that basically is the same as saying:
sinon.assert.calledWith(ChildSpy, sinon.match({a:'a', b: 'b'}));
.
However I use it I receive and exception from this packages, exactly from this line:
ReferenceError: HTMLElement is not defined
at typeDetect (node_modules/type-detect/index.js:159:24)
at typeOf (node_modules/sinon/lib/sinon/util/core/typeOf.js:6:12)
at Object.match (node_modules/sinon/lib/sinon/match.js:103:16)
at Context.<anonymous> (test/main.js:106:45)
Apparently the HTMLElement
used in this function is not defined.
Does that depend on the node version or maybe was this package conceived to run in the browser and not in node?
if (obj instanceof HTMLElement && obj.tagName === 'BLOCKQUOTE') {
return 'HTMLQuoteElement';
}
Maybe there is a check somewhere to understand if we are in a browser or not but I could have make things difficult to check because before starting my tests I do this:
// Setup for enzyme: http://airbnb.io/enzyme/docs/guides/jsdom.html
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {
url: 'http://localhost'
});
const win = doc.defaultView;
// Setup for react test-utils 'renderIntoDocument':
// it requires window, window.document and window.document.createElement
// globally available before you can import React
global.document = doc;
global.window = win;
global.navigator = win.navigator;
So type-detect believes is in a browser when in fact it's not? ๐ค
$ grep version packages/postcss-tone-vars/node_modules/type-detect/package.json
"version": "4.0.5"
$ grep export packages/postcss-tone-vars/node_modules/type-detect/index.js
export default function typeDetect(obj) {
Feels like it requires rebuild ;-(
It would be great if this supported Proxies, and I would think it belongs since the other ES6 classes are supported (like Promise
, Set
, Map
, etc).
I will try it myself and open a PR unless someone is already working on it!
the various rollup-plugin-*
are deprecated, use instead @rollup/plugin-*
Might be a question more than a bug, but how would you expect this to react?
const type = require('type-detect');
class ExtendedArray extends Array {
get [Symbol.toStringTag]() {
return 'ExtendedArray';
}
}
console.log(type(new ExtendedArray())); //=> 'array'
My initial expectation was that I would receive back ExtendedArray
through the use of Symbol.toStringTag
, but the code returns array
, from here. Is this intended?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.