can-reflect
Advanced tools
Comparing version 1.15.0 to 1.15.1
{ | ||
"name": "can-reflect", | ||
"version": "1.15.0", | ||
"version": "1.15.1", | ||
"description": "reflection on unknown data types", | ||
@@ -5,0 +5,0 @@ "homepage": "http://canjs.com", |
var QUnit = require('steal-qunit'); | ||
var canSymbol = require('can-symbol'); | ||
var schemaReflections = require("./schema"); | ||
var shapeReflections = require("../shape"); | ||
@@ -81,1 +82,50 @@ QUnit.module('can-reflect: shape reflections: schema'); | ||
}); | ||
QUnit.test("canReflect.convert", function(){ | ||
var res = schemaReflections.convert("1", Number); | ||
QUnit.equal(typeof res, "number", "is right type"); | ||
QUnit.equal(res, 1, "string -> number"); | ||
QUnit.equal( schemaReflections.convert("Infinity", Number), Infinity, "string -> number"); | ||
QUnit.equal( schemaReflections.convert(1, String), "1", "string"); | ||
QUnit.equal( schemaReflections.convert(true, String), "true", "boolean -> string"); | ||
QUnit.equal( schemaReflections.convert(false, String), "false", "boolean -> string"); | ||
QUnit.equal( schemaReflections.convert("true", Boolean), true, "string true -> boolean"); | ||
//QUnit.equal( schemaReflections.convert("false", Boolean), false, "string false -> boolean"); | ||
//QUnit.equal( schemaReflections.convert("1", Boolean), false, "string 1 -> boolean false"); | ||
// Basic constructor tests | ||
var MyConstructor = function(val){ | ||
this.val = val; | ||
}; | ||
MyConstructor.prototype.method = function(){}; | ||
QUnit.equal( schemaReflections.convert("abc", MyConstructor).val, "abc", "creates new instance"); | ||
var abc= new MyConstructor("abc"); | ||
QUnit.equal( schemaReflections.convert(abc, MyConstructor), abc, "is instance"); | ||
// MaybeString type | ||
var MaybeString = shapeReflections.assignSymbols({},{ | ||
"can.new": function(val){ | ||
if (val == null) { | ||
return val; | ||
} | ||
return '' + val; | ||
} | ||
}); | ||
QUnit.equal( schemaReflections.convert("1", MaybeString), "1", "'1' -> MaybeString"); | ||
QUnit.equal( schemaReflections.convert(null, MaybeString), null, "null -> MaybeString"); | ||
// Convert symbol | ||
var toStringIsh = function(val){ | ||
if (val == null) { | ||
return val; | ||
} | ||
return '' + val; | ||
}; | ||
QUnit.equal( schemaReflections.convert("1", toStringIsh), "1", "'1' -> MaybeString"); | ||
QUnit.equal( schemaReflections.convert(null, toStringIsh), null, "null -> MaybeString"); | ||
}); |
@@ -6,3 +6,5 @@ var canSymbol = require("can-symbol"); | ||
var getSchemaSymbol = canSymbol.for("can.getSchema"); | ||
var getSchemaSymbol = canSymbol.for("can.getSchema"), | ||
isMemberSymbol = canSymbol.for("can.isMember"), | ||
newSymbol = canSymbol.for("can.new"); | ||
@@ -40,2 +42,6 @@ function comparator(a, b) { | ||
function isPrimitiveConverter(Type){ | ||
return Type === Number || Type === String || Type === Boolean; | ||
} | ||
var schemaReflections = { | ||
@@ -188,4 +194,37 @@ /** | ||
return sort(obj); | ||
}, | ||
convert: function(value, Type){ | ||
if(isPrimitiveConverter(Type)) { | ||
return Type(value); | ||
} | ||
// check if value is already a member | ||
var isMemberTest = Type[isMemberSymbol], | ||
isMember = false, | ||
type = typeof Type, | ||
createNew = Type[newSymbol]; | ||
if(isMemberTest !== undefined) { | ||
isMember = isMemberTest.call(Type, value); | ||
} else if(type === "function") { | ||
if(typeReflections.isConstructorLike(Type)) { | ||
isMember = (value instanceof Type); | ||
} | ||
} | ||
if(isMember) { | ||
return value; | ||
} | ||
if(createNew !== undefined) { | ||
return createNew.call(Type, value); | ||
} else if(type === "function") { | ||
if(typeReflections.isConstructorLike(Type)) { | ||
return new Type(value); | ||
} else { | ||
// call it like a normal function | ||
return Type(value); | ||
} | ||
} else { | ||
throw new Error("can-reflect: Can not convert values into type. Type must provide `can.new` symbol."); | ||
} | ||
} | ||
}; | ||
module.exports = schemaReflections; |
169435
4597