Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

precise-typeof

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

precise-typeof - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

20

index.js
'use strict';
var isBuffer = require('is-buffer');
// Public API

@@ -12,12 +10,15 @@ module.exports = preciseTypeOf;

* @param {mixed} obj - object to get type of
* @param {object} [options] - object to get type of
* @returns {string} precise type
*/
function preciseTypeOf(obj)
function preciseTypeOf(obj, options)
{
var type, stamp = Object.prototype.toString.call(obj);
options = options || {};
if (!type && obj === undefined) type = 'undefined';
if (!type && obj === null) type = 'null';
if (!type && isBuffer(obj)) type = 'buffer';
if (!type && obj.constructor && typeof obj.constructor.isBuffer == 'function' && obj.constructor.isBuffer(obj)) type = 'buffer';

@@ -35,5 +36,14 @@ if (!type && typeof window == 'object' && obj === window) type = 'global';

// last resort
if (!type) type = Object.prototype.toString.call(obj).match(/\[object\s*([^\]]+)\]/)[1].toLowerCase();
if (!type) type = stamp.match(/\[object\s*([^\]]+)\]/)[1].toLowerCase();
// be even more precise by reporting "instance of" names
if (type == 'object' && options.pojoOnly) {
// some constructors don't have names
type = obj.constructor.name || 'unknown';
// precerve `object` response for POJOs
if (type == 'Object') type = 'object';
}
return type;
}
{
"name": "precise-typeof",
"version": "1.0.2",
"version": "2.0.0",
"description": "Better 'typeof'. Detects real type of the objects like 'Array()', 'new Number(1)', 'new Boolean(true)', etc.",

@@ -12,5 +12,11 @@ "main": "index.js",

"report": "istanbul report",
"size": "browserify index.js | size-table precise-typeof",
"files": "pkgfiles --sort=name"
"size": "cat index.js | size-table precise-typeof",
"testall": "npm run test && npm run browser && npm run report"
},
"files": [
"index.js",
"LICENSE",
"package.json",
"README.md"
],
"pre-commit": [

@@ -22,7 +28,6 @@ "lint",

"report",
"size",
"files"
"size"
],
"engines": {
"node": ">= 0.10"
"node": ">= 6"
},

@@ -40,3 +45,7 @@ "repository": {

"object",
"array"
"array",
"pojo",
"instance",
"instanceof",
"plain object"
],

@@ -50,10 +59,10 @@ "author": "Alex Indigo <iam@alexindigo.com>",

"devDependencies": {
"browserify": "^13.0.0",
"browserify-istanbul": "^2.0.0",
"coveralls": "^2.11.8",
"eslint": "^2.3.0",
"browserify": "^16.2.2",
"browserify-istanbul": "^3.0.1",
"coveralls": "^3.0.2",
"eslint": "^5.3.0",
"istanbul": "^0.4.2",
"nyc": "^6.1.1",
"nyc": "^12.0.2",
"obake": "^0.1.2",
"phantomjs-prebuilt": "^2.1.5",
"phantomjs-prebuilt": "^2.1.10",
"pre-commit": "^1.1.2",

@@ -63,8 +72,6 @@ "reamde": "^1.1.0",

"size-table": "^0.2.0",
"tap-spec": "^4.1.1",
"tap-spec": "^5.0.0",
"tape": "^4.5.1"
},
"dependencies": {
"is-buffer": "^1.1.1"
}
"dependencies": {}
}

@@ -6,16 +6,18 @@ # precise-typeof [![NPM Module](https://img.shields.io/npm/v/precise-typeof.svg?style=flat)](https://www.npmjs.com/package/precise-typeof)

[![PhantomJS Build](https://img.shields.io/travis/alexindigo/precise-typeof/master.svg?label=browser&style=flat)](https://travis-ci.org/alexindigo/precise-typeof)
[![Linux Build](https://img.shields.io/travis/alexindigo/precise-typeof/master.svg?label=linux:0.10-5.x&style=flat)](https://travis-ci.org/alexindigo/precise-typeof)
[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/precise-typeof/master.svg?label=windows:0.10-5.x&style=flat)](https://ci.appveyor.com/project/alexindigo/precise-typeof)
[![Linux Build](https://img.shields.io/travis/alexindigo/precise-typeof/master.svg?label=linux:6.x-10.x&style=flat)](https://travis-ci.org/alexindigo/precise-typeof)
[![MacOS Build](https://img.shields.io/travis/alexindigo/precise-typeof/master.svg?label=macos:6.x-10.x&style=flat)](https://travis-ci.org/alexindigo/precise-typeof)
[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/precise-typeof/master.svg?label=windows:6.x-10.x&style=flat)](https://ci.appveyor.com/project/alexindigo/precise-typeof)
[![Coverage Status](https://img.shields.io/coveralls/alexindigo/precise-typeof/master.svg?label=code+coverage&style=flat)](https://coveralls.io/github/alexindigo/precise-typeof?branch=master)
[![Dependency Status](https://img.shields.io/david/alexindigo/precise-typeof.svg?style=flat)](https://david-dm.org/alexindigo/precise-typeof)
[![bitHound Overall Score](https://www.bithound.io/github/alexindigo/precise-typeof/badges/score.svg)](https://www.bithound.io/github/alexindigo/precise-typeof)
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://opensource.org/licenses/MIT)
<!-- Not yet ![Readme](https://img.shields.io/badge/readme-tested-brightgreen.svg?style=flat)
Too many false positives [![Codacy Badge](https://img.shields.io/codacy/5f1289b78b7346498797f9f3cd674408.svg)](https://www.codacy.com/app/alexindigo/precise-typeof) -->
| compression | size |
| :----------------------- | ------: |
| precise-typeof.js | 2.35 kB |
| precise-typeof.min.js | 1.41 kB |
| precise-typeof.min.js.gz | 639 B |
| compression | size |
| :----------------------- | -----: |
| precise-typeof.js | 1.6 kB |
| precise-typeof.min.js | 794 B |
| precise-typeof.min.js.gz | 389 B |

@@ -26,5 +28,11 @@

```sh
$ npm install precise-typeof --save
$ yarn add precise-typeof
```
or
```sh
$ npm install --save precise-typeof
```
## Examples

@@ -58,2 +66,5 @@

// instances
typeOf(new function Moment(){}); // -> 'object'
// special objects

@@ -91,4 +102,41 @@ typeOf(/s/); // -> 'regexp'

### `{ pojoOnly: true }`
With `pojoOnly` flag it only reports Plain-Old Javascript Objects as `object`,
and reports "instances" by their constructor names (e.g. `Moment` for `moment` instance).
In case if object was created from the nameless function, it will be reported as `unknown`.
```javascript
var typeOf = require('precise-typeof');
// reported differently with `{pojoOnly: true}`
typeOf(new function Moment(){}, {pojoOnly: true}); // -> 'Moment'
typeOf(new function ABC(){}, {pojoOnly: true}); // -> 'ABC'
typeOf(new function(){}, {pojoOnly: true}); // -> 'unknown'
// same with or without `{pojoOnly: true}`
typeOf({}, {pojoOnly: true}); // -> 'object'
typeOf([], {pojoOnly: true}); // -> 'array'
typeOf(25, {pojoOnly: true}); // -> 'number'
typeOf(Infinity, {pojoOnly: true}); // -> 'number'
typeOf('ABC', {pojoOnly: true}); // -> 'string'
typeOf(function(){}, {pojoOnly: true}); // -> 'function'
typeOf(Math.sin, {pojoOnly: true}); // -> 'function'
typeOf(undefined, {pojoOnly: true}); // -> 'undefined'
typeOf(true, {pojoOnly: true}); // -> 'boolean'
typeOf(null, {pojoOnly: true}); // -> 'null'
typeOf(NaN, {pojoOnly: true}); // -> 'nan'
typeOf(new Object(), {pojoOnly: true}); // -> 'object'
typeOf(new Array(), {pojoOnly: true}); // -> 'array'
typeOf(new Number(5), {pojoOnly: true}); // -> 'number'
typeOf(new Number(Infinity), {pojoOnly: true}); // -> 'number'
typeOf(new String('ABC'), {pojoOnly: true}); // -> 'string'
typeOf(new Function(), {pojoOnly: true}); // -> 'function'
typeOf(new Boolean(), {pojoOnly: true}); // -> 'boolean'
typeOf(new Number('blabla'), {pojoOnly: true}); // -> 'nan'
```
## License
Precise-TypeOf is licensed under the MIT license.
Precise-TypeOf is released under the MIT license.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc