fable-core
Advanced tools
Comparing version 0.7.17 to 0.7.18
{ | ||
"name": "fable-core", | ||
"version": "0.7.17", | ||
"version": "0.7.18", | ||
"description": "Fable core lib & bindings for native JS objects, browser and node APIs", | ||
@@ -5,0 +5,0 @@ "main": "Main.js", |
117
Serialize.js
@@ -75,47 +75,59 @@ import FSymbol from "./Symbol"; | ||
} | ||
function inflate(val, typ, path) { | ||
function needsInflate(enclosing) { | ||
var typ = enclosing.head; | ||
if (typeof typ === "string") { | ||
return false; | ||
} | ||
if (typ instanceof NonDeclaredType) { | ||
switch (typ.kind) { | ||
case "Option": | ||
case "Array": | ||
return needsInflate(new List(typ.generics, enclosing)); | ||
case "Tuple": | ||
return typ.generics.some(function (x) { | ||
return needsInflate(new List(x, enclosing)); | ||
}); | ||
case "GenericParam": | ||
return needsInflate(resolveGeneric(typ.definition, enclosing.tail)); | ||
case "GenericType": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
return true; | ||
function needsInflate(enclosing) { | ||
var typ = enclosing.head; | ||
if (typeof typ === "string") { | ||
return false; | ||
} | ||
function inflateArray(arr, enclosing, path) { | ||
if (!Array.isArray) { | ||
invalidate(arr, "array", path); | ||
if (typ instanceof NonDeclaredType) { | ||
switch (typ.kind) { | ||
case "Option": | ||
case "Array": | ||
return needsInflate(new List(typ.generics, enclosing)); | ||
case "Tuple": | ||
return typ.generics.some(function (x) { | ||
return needsInflate(new List(x, enclosing)); | ||
}); | ||
case "GenericParam": | ||
return needsInflate(resolveGeneric(typ.definition, enclosing.tail)); | ||
case "GenericType": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
// TODO: Validate non-inflated elements | ||
return needsInflate(enclosing) | ||
? arr.map(function (x, i) { return inflate(x, enclosing, combine(path, i)); }) | ||
: arr; | ||
} | ||
function inflateMap(obj, keyEnclosing, valEnclosing, path) { | ||
var inflateKey = keyEnclosing.head !== "string"; | ||
var inflateVal = needsInflate(valEnclosing); | ||
return Object | ||
.getOwnPropertyNames(obj) | ||
.map(function (k) { | ||
var key = inflateKey ? inflate(JSON.parse(k), keyEnclosing, combine(path, k)) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing, combine(path, k)) : obj[k]; | ||
return [key, val]; | ||
}); | ||
return true; | ||
} | ||
function inflateArray(arr, enclosing, path) { | ||
if (!Array.isArray) { | ||
invalidate(arr, "array", path); | ||
} | ||
// TODO: Validate non-inflated elements | ||
return needsInflate(enclosing) | ||
? arr.map(function (x, i) { return inflate(x, enclosing, combine(path, i)); }) | ||
: arr; | ||
} | ||
function inflateMap(obj, keyEnclosing, valEnclosing, path) { | ||
var inflateKey = keyEnclosing.head !== "string"; | ||
var inflateVal = needsInflate(valEnclosing); | ||
return Object | ||
.getOwnPropertyNames(obj) | ||
.map(function (k) { | ||
var key = inflateKey ? inflate(JSON.parse(k), keyEnclosing, combine(path, k)) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing, combine(path, k)) : obj[k]; | ||
return [key, val]; | ||
}); | ||
} | ||
function inflateList(val, enclosing, path) { | ||
var ar = [], li = new List(), cur = val, inf = needsInflate(enclosing); | ||
while (cur.tail != null) { | ||
ar.push(inf ? inflate(cur.head, enclosing, path) : cur.head); | ||
cur = cur.tail; | ||
} | ||
ar.reverse(); | ||
for (var i = 0; i < ar.length; i++) { | ||
li = new List(ar[i], li); | ||
} | ||
return li; | ||
} | ||
function inflate(val, typ, path) { | ||
var enclosing = null; | ||
@@ -158,4 +170,7 @@ if (typ instanceof List) { | ||
if (def === List) { | ||
return listOfArray(inflateArray(val, resolveGeneric(0, enclosing), path)); | ||
return Array.isArray(val) | ||
? listOfArray(inflateArray(val, resolveGeneric(0, enclosing), path)) | ||
: inflateList(val, resolveGeneric(0, enclosing), path); | ||
} | ||
// TODO: Should we try to inflate also sets and maps serialized with `JSON.stringify`? | ||
if (def === FSet) { | ||
@@ -185,6 +200,11 @@ return setCreate(inflateArray(val, resolveGeneric(0, enclosing), path)); | ||
if (info.cases) { | ||
var u = { Fields: [] }; | ||
var uCase = void 0, uFields = []; | ||
// Cases withouth fields are serialized as strings by `toJson` | ||
if (typeof val === "string") { | ||
u.Case = val; | ||
uCase = val; | ||
} | ||
else if (typeof val.Case === "string" && Array.isArray(val.Fields)) { | ||
uCase = val.Case; | ||
uFields = val.Fields; | ||
} | ||
else { | ||
@@ -195,13 +215,14 @@ var caseName = Object.getOwnPropertyNames(val)[0]; | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = caseName; | ||
uCase = caseName; | ||
path = combine(path, caseName); | ||
for (var i = 0; i < fieldTypes.length; i++) { | ||
u.Fields.push(inflate(fields[i], new List(fieldTypes[i], enclosing), combine(path, i))); | ||
uFields.push(inflate(fields[i], new List(fieldTypes[i], enclosing), combine(path, i))); | ||
} | ||
} | ||
} | ||
if (u.Case in info.cases === false) { | ||
// Validate | ||
if (uCase in info.cases === false) { | ||
invalidate(val, typ, path); | ||
} | ||
return Object.assign(new typ(), u); | ||
return new typ(uCase, uFields); | ||
} | ||
@@ -208,0 +229,0 @@ if (info.properties) { |
@@ -85,47 +85,59 @@ (function (dependencies, factory) { | ||
} | ||
function inflate(val, typ, path) { | ||
function needsInflate(enclosing) { | ||
var typ = enclosing.head; | ||
if (typeof typ === "string") { | ||
return false; | ||
} | ||
if (typ instanceof Util_3.NonDeclaredType) { | ||
switch (typ.kind) { | ||
case "Option": | ||
case "Array": | ||
return needsInflate(new List_1.default(typ.generics, enclosing)); | ||
case "Tuple": | ||
return typ.generics.some(function (x) { | ||
return needsInflate(new List_1.default(x, enclosing)); | ||
}); | ||
case "GenericParam": | ||
return needsInflate(Reflection_1.resolveGeneric(typ.definition, enclosing.tail)); | ||
case "GenericType": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
return true; | ||
function needsInflate(enclosing) { | ||
var typ = enclosing.head; | ||
if (typeof typ === "string") { | ||
return false; | ||
} | ||
function inflateArray(arr, enclosing, path) { | ||
if (!Array.isArray) { | ||
invalidate(arr, "array", path); | ||
if (typ instanceof Util_3.NonDeclaredType) { | ||
switch (typ.kind) { | ||
case "Option": | ||
case "Array": | ||
return needsInflate(new List_1.default(typ.generics, enclosing)); | ||
case "Tuple": | ||
return typ.generics.some(function (x) { | ||
return needsInflate(new List_1.default(x, enclosing)); | ||
}); | ||
case "GenericParam": | ||
return needsInflate(Reflection_1.resolveGeneric(typ.definition, enclosing.tail)); | ||
case "GenericType": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
// TODO: Validate non-inflated elements | ||
return needsInflate(enclosing) | ||
? arr.map(function (x, i) { return inflate(x, enclosing, combine(path, i)); }) | ||
: arr; | ||
} | ||
function inflateMap(obj, keyEnclosing, valEnclosing, path) { | ||
var inflateKey = keyEnclosing.head !== "string"; | ||
var inflateVal = needsInflate(valEnclosing); | ||
return Object | ||
.getOwnPropertyNames(obj) | ||
.map(function (k) { | ||
var key = inflateKey ? inflate(JSON.parse(k), keyEnclosing, combine(path, k)) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing, combine(path, k)) : obj[k]; | ||
return [key, val]; | ||
}); | ||
return true; | ||
} | ||
function inflateArray(arr, enclosing, path) { | ||
if (!Array.isArray) { | ||
invalidate(arr, "array", path); | ||
} | ||
// TODO: Validate non-inflated elements | ||
return needsInflate(enclosing) | ||
? arr.map(function (x, i) { return inflate(x, enclosing, combine(path, i)); }) | ||
: arr; | ||
} | ||
function inflateMap(obj, keyEnclosing, valEnclosing, path) { | ||
var inflateKey = keyEnclosing.head !== "string"; | ||
var inflateVal = needsInflate(valEnclosing); | ||
return Object | ||
.getOwnPropertyNames(obj) | ||
.map(function (k) { | ||
var key = inflateKey ? inflate(JSON.parse(k), keyEnclosing, combine(path, k)) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing, combine(path, k)) : obj[k]; | ||
return [key, val]; | ||
}); | ||
} | ||
function inflateList(val, enclosing, path) { | ||
var ar = [], li = new List_1.default(), cur = val, inf = needsInflate(enclosing); | ||
while (cur.tail != null) { | ||
ar.push(inf ? inflate(cur.head, enclosing, path) : cur.head); | ||
cur = cur.tail; | ||
} | ||
ar.reverse(); | ||
for (var i = 0; i < ar.length; i++) { | ||
li = new List_1.default(ar[i], li); | ||
} | ||
return li; | ||
} | ||
function inflate(val, typ, path) { | ||
var enclosing = null; | ||
@@ -168,4 +180,7 @@ if (typ instanceof List_1.default) { | ||
if (def === List_1.default) { | ||
return List_2.ofArray(inflateArray(val, Reflection_1.resolveGeneric(0, enclosing), path)); | ||
return Array.isArray(val) | ||
? List_2.ofArray(inflateArray(val, Reflection_1.resolveGeneric(0, enclosing), path)) | ||
: inflateList(val, Reflection_1.resolveGeneric(0, enclosing), path); | ||
} | ||
// TODO: Should we try to inflate also sets and maps serialized with `JSON.stringify`? | ||
if (def === Set_1.default) { | ||
@@ -195,6 +210,11 @@ return Set_2.create(inflateArray(val, Reflection_1.resolveGeneric(0, enclosing), path)); | ||
if (info.cases) { | ||
var u = { Fields: [] }; | ||
var uCase = void 0, uFields = []; | ||
// Cases withouth fields are serialized as strings by `toJson` | ||
if (typeof val === "string") { | ||
u.Case = val; | ||
uCase = val; | ||
} | ||
else if (typeof val.Case === "string" && Array.isArray(val.Fields)) { | ||
uCase = val.Case; | ||
uFields = val.Fields; | ||
} | ||
else { | ||
@@ -205,13 +225,14 @@ var caseName = Object.getOwnPropertyNames(val)[0]; | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = caseName; | ||
uCase = caseName; | ||
path = combine(path, caseName); | ||
for (var i = 0; i < fieldTypes.length; i++) { | ||
u.Fields.push(inflate(fields[i], new List_1.default(fieldTypes[i], enclosing), combine(path, i))); | ||
uFields.push(inflate(fields[i], new List_1.default(fieldTypes[i], enclosing), combine(path, i))); | ||
} | ||
} | ||
} | ||
if (u.Case in info.cases === false) { | ||
// Validate | ||
if (uCase in info.cases === false) { | ||
invalidate(val, typ, path); | ||
} | ||
return Object.assign(new typ(), u); | ||
return new typ(uCase, uFields); | ||
} | ||
@@ -218,0 +239,0 @@ if (info.properties) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
4629352
13185