Comparing version 0.2.1 to 0.3.0
@@ -106,3 +106,5 @@ (function () { | ||
} | ||
if (Class === Object) { | ||
return context.subjectType === "Object"; | ||
} | ||
if (context.subject instanceof Class) { | ||
@@ -114,8 +116,2 @@ return true; | ||
if (typeof primitive === "function") { | ||
if (Class === Object) { | ||
// If we're checking for an object, primitive types are treated as objects. | ||
// This is not true in pure JavaScript, but since you can write | ||
// var num = 3; num.toString(); I think it's legitimate to treat them as objects. | ||
return true; | ||
} | ||
if (primitive === Class) { | ||
@@ -122,0 +118,0 @@ if (Class === Number) { |
{ | ||
"name": "value", | ||
"description": "Convenient type-checking in JavaScript", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "http://github.com/peerigon/value", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/peerigon/value.git", |
@@ -34,12 +34,20 @@ value | ||
value(null).typeOf(Object); // = false | ||
value([]).typeOf(Array); // = true | ||
value({}).typeOf(Object); // = true | ||
value([]).typeOf(Object); // = false | ||
value(2).typeOf(Number); // = true | ||
value("hello").typeOf(String); // = true | ||
value(new String("hello")).typeOf(String); // = true | ||
value(new String("hello")).typeOf(Object); // = false | ||
value(/myRegExp/).typeOf(RegExp); // = true | ||
value(document.createElement("a")).typeOf(HTMLAnchorElement); // = true | ||
// value also provides a negation for better readability | ||
value(2).notTypeOf(String); // = true | ||
// you can also check conveniently for null and undefined with one call | ||
@@ -49,2 +57,3 @@ value(undefined).isSet(); // = false | ||
// value also supports convenient type-checking within collections | ||
@@ -114,3 +123,3 @@ value([1, 2, 3]).each.typeOf(Number); // = true | ||
### new String() is a String | ||
### new String() is a String and not an Object | ||
@@ -121,13 +130,14 @@ In constrast to `typeof new String() === "object"` | ||
value(new String()).typeOf(String); // = true | ||
value(new String()).typeOf(Object); // = false | ||
``` | ||
### All set values are objects | ||
### [] is an Array and not an Object | ||
In constrast to `typeof [] === "object"` | ||
```javascript | ||
value("hello").typeOf(Object); // = true | ||
value(new String("hello")).typeOf(Object); // = true | ||
value([]).typeOf(Object); // = false | ||
value([]).typeOf(Array); // = true | ||
``` | ||
The primitive value `"hello"` is not an Object in JavaScript. But it actually *acts* like an Object because it is [casted to an Object](http://stackoverflow.com/a/2051893) internally when trying to access a property. | ||
### Infinity and NaN are not numbers | ||
@@ -134,0 +144,0 @@ |
@@ -16,3 +16,3 @@ "use strict"; // run code in ES5 strict mode | ||
describe("value", function () { | ||
describe("#getConstructor", function () { | ||
describe(".getConstructor()", function () { | ||
function A() {} | ||
@@ -48,3 +48,3 @@ | ||
}); | ||
describe("#isSet", function () { | ||
describe(".isSet()", function () { | ||
it("should return true when the value is neither null nor undefined", function () { | ||
@@ -75,3 +75,3 @@ _expect(value(false).isSet()).to.be(true); | ||
}); | ||
describe("#isNotSet", function () { | ||
describe(".isNotSet()", function () { | ||
// We're assuming that the negation works when this test runs | ||
@@ -82,3 +82,3 @@ it("should return true when the value is null", function () { | ||
}); | ||
describe("#instanceOf", function () { | ||
describe(".instanceOf()/.typeOf()", function () { | ||
function A() {} | ||
@@ -91,16 +91,2 @@ | ||
function D() {} | ||
D.Extends = A; | ||
// Here we've got an inconsitent inheritance: E has an "Extends"-property | ||
// which points to A AND has B.prototype as its prototype. | ||
// | ||
// In this case the prototype should take precedence over Extends. | ||
// Actually it's not possible the other way round, because checking for | ||
// e.constructor to get the Extends-prototype does not return E. Instead | ||
// of it returns the prototype of E, which is B.prototype | ||
function E() {} | ||
E.Extends = A; | ||
E.prototype = B.prototype; | ||
// Inheriting from a native function | ||
@@ -126,2 +112,15 @@ function MyNativeExtension() {} | ||
}); | ||
it("should not treat built-in values as Object (except Object itself)", function () { | ||
var builtInValues = [true, 2, "2", [1, 2, 3], | ||
new Date(), /hello/gi, new Error()]; | ||
builtInValues.forEach(function (builtIn, index) { | ||
_expect(value(builtIn).instanceOf(Object)).to.be(false); | ||
}); | ||
}); | ||
it("should not treat strings, numbers or booleans created with the new operator as Object", function () { | ||
_expect(value(new String("hello")).typeOf(Object)).to.be(false); | ||
_expect(value(new Number(2)).typeOf(Object)).to.be(false); | ||
_expect(value(new Boolean(true)).typeOf(Object)).to.be(false); | ||
}); | ||
it("should detect undefined", function () { | ||
@@ -138,9 +137,2 @@ value(undefined).typeOf(undefined); | ||
}); | ||
it("should treat all set values as objects", function () { | ||
_expect(value(true).instanceOf(Object)).to.be(true); | ||
_expect(value(2).instanceOf(Object)).to.be(true); | ||
_expect(value("2").instanceOf(Object)).to.be(true); | ||
_expect(value("").instanceOf(Object)).to.be(true); | ||
_expect(value([1, 2, 3]).instanceOf(Object)).to.be(true); | ||
}); | ||
it("should treat null and undefined as non-objects", function () { | ||
@@ -178,8 +170,2 @@ _expect(value(null).instanceOf(Object)).to.be(false); | ||
}); | ||
it("should return false when the object exposes an unfitting Extends property", function () { | ||
var d = new D(), | ||
valueD = value(d); | ||
_expect(valueD.instanceOf(B)).to.be(false); | ||
}); | ||
it("should return false when you check an undefined or null value", function () { | ||
@@ -226,3 +212,3 @@ _expect(value(null).instanceOf(B)).to.be(false); | ||
}); | ||
describe("#notInstanceOf", function () { | ||
describe(".notInstanceOf()/.notTypeOf()", function () { | ||
it("should be aliased with 'notTypeOf'", function () { | ||
@@ -238,3 +224,3 @@ var valueObj = value(null); | ||
}); | ||
describe("#each", function () { | ||
describe(".each", function () { | ||
var homoArr = [1, 2, 3], | ||
@@ -247,3 +233,3 @@ heteroArr = [1, "2", 3], | ||
describe("#isSet", function () { | ||
describe(".isSet()", function () { | ||
it("should return true when every value(item).isSet returned true", function () { | ||
@@ -258,3 +244,3 @@ _expect(value(homoArr).each.isSet()).to.be(true); | ||
}); | ||
describe("#instanceOf", function () { | ||
describe(".instanceOf()/.typeOf()", function () { | ||
it("should be aliased with 'typeOf'", function () { | ||
@@ -273,3 +259,3 @@ var valueObj = value(null); | ||
}); | ||
describe("#any", function () { | ||
describe(".any", function () { | ||
var homoArr = [1, 2, 3], | ||
@@ -282,3 +268,3 @@ heteroArr = [1, "2", 3], | ||
describe("#isNotSet", function () { | ||
describe(".isNotSet()", function () { | ||
it("should return true when any value(item).isSet returned false", function () { | ||
@@ -293,3 +279,3 @@ _expect(value(homoArr).any.isNotSet()).to.be(false); | ||
}); | ||
describe("#notInstanceOf", function () { | ||
describe(".notInstanceOf()/.typeOf()", function () { | ||
it("should be aliased with 'notTypeOf'", function () { | ||
@@ -296,0 +282,0 @@ var valueObj = value(null); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
166
0
67319
477