Skip to content

Instantly share code, notes, and snippets.

@Warry
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Warry/d1fcffb7de7d0a3b76f4 to your computer and use it in GitHub Desktop.
Save Warry/d1fcffb7de7d0a3b76f4 to your computer and use it in GitHub Desktop.
Runtime Type Checker in JS

Runtime Type checker in JS.

is(String, "foo") // true
is(/foo/, "foo") // true
is("foo", "foo") // true

// See test file for more exemples...

// Objects
var Address = {
  number: Number,
  street: String,
  city: String,
  zip: Number
}

var Email = /(.+)@(.+){2,}\.(.+){2,}/;

var Person = {
  name: String,
  age: Number,
  credentials: "granted",
  level: 1000,
  birthday: Date,
  human: Boolean,
  address: Address,
  emails: [Email]
}

is(Person, {
  name: "foo",
  age: 42,
  credentials: "granted",
  level: 1000,
  human: true,
  birthday: new Date(1985,10,12),
  address: {
    number: 42,
    street: "foo",
    city: "foo",
    zip: 42
  },
  emails: ["hello@provider.com"]
}) // should return true
function is(type, value) {
var typeoftype = typeof type;
var typeofvalue = typeof value;
if (type === null) return value != undefined;
else if (typeoftype == "string") return type === value;
else if (typeoftype == "number") return type === value;
else if (typeoftype == "boolean") return type === value;
else if (typeoftype == "function") return (typeofvalue === typeof type() || value instanceof type);
else if (type instanceof Array) {
if (!(value instanceof Array)) return false;
if (type.length == 1 && value.length >= 0) {
for (var i in value){
if (!is(type[0], value[i])) return false;
}
}
return true;
}
else if (type instanceof RegExp) {
if (!(typeoftype != "string")) return false;
return type.test(value);
}
else if (typeoftype == "object") {
if (!value) return false;
for (var i in type){
if (!is(type[i], value[i])) return false;
}
return true;
}
}
function should(type, value){
if (!is(type, value)) console.error("KO", value)
else console.log("OK")
}
function shount(type, value){
if (is(type, value)) console.error("KO", value)
else console.log("OK")
}
// String
should(String, "")
should(String, "foo")
should("foo", "foo")
should(/foo/, "foo")
shount(String, false)
shount(String, 1)
shount(String, [])
shount(String, {})
shount(String, String)
shount("foo", "bar")
shount(/foo/, "bar")
// Number
should(Number, 42)
should(42, 42)
shount(Number, "")
shount(Number, "foo")
shount(Number, false)
shount(Number, [])
shount(Number, {})
shount(Number, Number)
shount(42, 2)
// Number
should(Boolean, true)
should(Boolean, false)
should(true, true)
should(false, false)
shount(Boolean, "")
shount(Boolean, "foo")
shount(Boolean, [])
shount(Boolean, {})
shount(Boolean, Number)
shount(true, false)
// Array
should(Array, [])
should([], [])
should([], ["foo", "bar"])
should(Array, ["foo", "bar"])
should([String], ["foo", "bar"])
should(["foo"], ["foo"])
shount([Number], ["foo", "bar"])
shount([Boolean], ["foo", [], 4])
// Date
should(Date, new Date(1985,10,12))
// Objects
var Address = {
number: Number,
street: String,
city: String,
zip: Number
}
var Email = /(.+)@(.+){2,}\.(.+){2,}/;
var Person = {
name: String,
age: Number,
credentials: "granted",
level: 1000,
birthday: Date,
human: Boolean,
address: Address,
emails: [Email]
}
should(Person, {
name: "foo",
age: 42,
credentials: "granted",
level: 1000,
human: true,
birthday: new Date(1985,10,12),
address: {
number: 42,
street: "foo",
city: "foo",
zip: 42
},
emails: ["hello@provider.com"]
})
shount(Person, {
name: String,
age: Number,
credentials: "granted",
level: 1000,
human: Boolean,
address: {
number: Number,
street: String,
city: String,
zip: Number
},
emails: [String]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment