Skip to content

Commit

Permalink
updae test spec
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Apr 25, 2016
1 parent 2cb8a73 commit 77aa0d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ $ npm install --save is-native
```js
var isNative = require('is-native');

var obj = { a: 1 };
var arr = [1, 2, 3];

isNative(obj.valueOf); // => true
isNative(arr.push); // => true
isNative(Math.min); // => true
isNative(console.log); // => true

isNative(function () { console.log(123); });
// => false

isNative(); // => false
isNative(obj); // => false
isNative(arr); // => false
isNative(/x/); // => false
isNative(new Date); // => false
isNative(new Error); // => false
```


Expand Down
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ var toSource = require('to-source-code');

function isObject(value) {

var type = typeof value;
if (!value) {
return false;
}

return !!value && (type === 'object' || type === 'function');
var type = typeof value;
return type === 'object' || type === 'function';
}

// Checks if `value` is a host object in IE < 9.
Expand All @@ -17,7 +20,6 @@ function isHostObject(value) {
// despite having improperly defined `toString` methods.

var result = false;

if (!isNil(value) && typeof value.toString !== 'function') {
try {
result = ('' + value) !== '';
Expand All @@ -27,9 +29,7 @@ function isHostObject(value) {
}

function isFunction(value) {

var tag = isObject(value) ? Object.prototype.toString.call(value) : '';

return tag === '[object Function]' || tag === '[object GeneratorFunction]';
}

Expand All @@ -40,23 +40,23 @@ module.exports = function (value) {
return false;
}

var pattern;

if (isFunction(value) || isHostObject(value)) {

var toString = Function.prototype.toString;
var fnToString = Function.prototype.toString;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;

pattern = new RegExp('^' +
toString.call(hasOwnProperty)
.replace(reRegExpChar, '\\$&')
var reIsNative = new RegExp('^' +
fnToString
.call(hasOwnProperty)
.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
);
} else {
// detect host constructors (Safari).
pattern = /^\[object .+?Constructor\]$/;

return reIsNative.test(toSource(value));
}

return pattern.test(toSource(value));
// used to detect host constructors (Safari > 4; really typed array specific)
var reIsHostCtor = /^\[object .+?Constructor\]$/;

return reIsHostCtor.test(toSource(value));
};
22 changes: 16 additions & 6 deletions test/spec/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
'use strict';

var expect = require('chai').expect;


describe('is-native', function () {

var isNative = require('../../');

it('common test', function () {
it('should return `true` for native methods', function () {

var obj = { a: 1 };
var arr = [1, 2, 3];

expect(isNative(obj.valueOf)).to.be.true;
expect(isNative(arr.push)).to.be.true;
expect(isNative(Math.min)).to.be.true;
});

it('should return `false` for non-native methods', function () {

expect(isNative(function () {})).to.be.false;

expect(isNative()).to.be.false;
Expand All @@ -17,8 +24,11 @@ describe('is-native', function () {
expect(isNative('abc')).to.be.false;
expect(isNative(void 0)).to.be.false;
expect(isNative(true)).to.be.false;
expect(isNative({})).to.be.false;
expect(isNative([])).to.be.false;
expect(isNative({ a: 1 })).to.be.false;
expect(isNative([1, 2, 3])).to.be.false;
expect(isNative(NaN)).to.be.false;
expect(isNative(/x/)).to.be.false;
expect(isNative(new Date)).to.be.false;
expect(isNative(new Error)).to.be.false;
});
});

0 comments on commit 77aa0d9

Please sign in to comment.