fable-core
Advanced tools
Comparing version 0.7.12 to 0.7.14
{ | ||
"name": "fable-core", | ||
"version": "0.7.12", | ||
"version": "0.7.14", | ||
"description": "Fable core lib & bindings for native JS objects, browser and node APIs", | ||
@@ -5,0 +5,0 @@ "main": "Main.js", |
@@ -17,4 +17,5 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
import { fold } from "./Seq"; | ||
import { resolveGeneric } from "./Reflection"; | ||
import { resolveGeneric, getTypeFullName } from "./Reflection"; | ||
import { parse as dateParse } from "./Date"; | ||
import { fsFormat } from "./String"; | ||
export function toJson(o) { | ||
@@ -49,3 +50,19 @@ return JSON.stringify(o, function (k, v) { | ||
} | ||
function inflate(val, typ) { | ||
function combine(path1, path2) { | ||
return typeof path2 === "number" ? path1 + "[" + path2 + "]" : (path1 ? path1 + "." : "") + path2; | ||
} | ||
function isNullable(typ) { | ||
if (typeof typ === "string") { | ||
return typ !== "boolean" && typ !== "number"; | ||
} else if (typ instanceof NonDeclaredType) { | ||
return typ.kind !== "Array" && typ.kind !== "Tuple"; | ||
} else { | ||
var info = typeof typ.prototype[FSymbol.reflection] === "function" ? typ.prototype[FSymbol.reflection]() : null; | ||
return info ? info.nullable : true; | ||
} | ||
} | ||
function invalidate(val, typ, path) { | ||
throw new Error(fsFormat("%A", val) + " " + (path ? "(" + path + ")" : "") + " is not of type " + getTypeFullName(typ)); | ||
} | ||
function inflate(val, typ, path) { | ||
function needsInflate(enclosing) { | ||
@@ -73,13 +90,17 @@ var typ = enclosing.head; | ||
} | ||
function inflateArray(arr, enclosing) { | ||
return Array.isArray(arr) && needsInflate(enclosing) ? arr.map(function (x) { | ||
return inflate(x, enclosing); | ||
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) { | ||
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) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing) : obj[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]; | ||
@@ -95,4 +116,12 @@ }); | ||
} | ||
if (val == null || typeof typ === "string") { | ||
if (val == null) { | ||
if (!isNullable(typ)) { | ||
invalidate(val, typ, path); | ||
} | ||
return val; | ||
} else if (typeof typ === "string") { | ||
if ((typ === "boolean" || typ === "number" || typ === "string") && (typeof val === "undefined" ? "undefined" : _typeof(val)) !== typ) { | ||
invalidate(val, typ, path); | ||
} | ||
return val; | ||
} else if (typ instanceof NonDeclaredType) { | ||
@@ -103,11 +132,11 @@ switch (typ.kind) { | ||
case "Option": | ||
return inflate(val, new List(typ.generics[0], enclosing)); | ||
return inflate(val, new List(typ.generics[0], enclosing), path); | ||
case "Array": | ||
return inflateArray(val, new List(typ.generics[0], enclosing)); | ||
return inflateArray(val, new List(typ.generics[0], enclosing), path); | ||
case "Tuple": | ||
return typ.generics.map(function (x, i) { | ||
return inflate(val[i], new List(x, enclosing)); | ||
return inflate(val[i], new List(x, enclosing), combine(path, i)); | ||
}); | ||
case "GenericParam": | ||
return inflate(val, resolveGeneric(typ.name, enclosing.tail)); | ||
return inflate(val, resolveGeneric(typ.name, enclosing.tail), path); | ||
// case "Interface": // case "Any": | ||
@@ -123,18 +152,18 @@ default: | ||
if (proto instanceof List) { | ||
return listOfArray(inflateArray(val, resolveGeneric(0, enclosing))); | ||
return listOfArray(inflateArray(val, resolveGeneric(0, enclosing), path)); | ||
} | ||
if (proto instanceof FSet) { | ||
return setCreate(inflateArray(val, resolveGeneric(0, enclosing))); | ||
return setCreate(inflateArray(val, resolveGeneric(0, enclosing), path)); | ||
} | ||
if (proto instanceof Set) { | ||
return new Set(inflateArray(val, resolveGeneric(0, enclosing))); | ||
return new Set(inflateArray(val, resolveGeneric(0, enclosing), path)); | ||
} | ||
if (proto instanceof FMap) { | ||
return mapCreate(inflateMap(val, resolveGeneric(0, enclosing), resolveGeneric(1, enclosing))); | ||
return mapCreate(inflateMap(val, resolveGeneric(0, enclosing), resolveGeneric(1, enclosing), path)); | ||
} | ||
if (proto instanceof Map) { | ||
return new Map(inflateMap(val, resolveGeneric(0, enclosing), resolveGeneric(1, enclosing))); | ||
return new Map(inflateMap(val, resolveGeneric(0, enclosing), resolveGeneric(1, enclosing), path)); | ||
} | ||
var info = typeof proto[FSymbol.reflection] === "function" ? proto[FSymbol.reflection]() : {}; | ||
// Union types | ||
var info = typeof proto[FSymbol.reflection] === "function" ? proto[FSymbol.reflection]() : {}; | ||
if (info.cases) { | ||
@@ -147,8 +176,14 @@ var u = { Fields: [] }; | ||
var fieldTypes = info.cases[caseName]; | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = caseName; | ||
for (var i = 0; i < fieldTypes.length; i++) { | ||
u.Fields.push(inflate(fields[i], new List(fieldTypes[i], enclosing))); | ||
if (Array.isArray(fieldTypes)) { | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = 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))); | ||
} | ||
} | ||
} | ||
if (u.Case in info.cases === false) { | ||
invalidate(val, typ, path); | ||
} | ||
return Object.assign(new typ(), u); | ||
@@ -166,3 +201,3 @@ } | ||
val[k] = inflate(val[k], new List(properties[k], enclosing)); | ||
val[k] = inflate(val[k], new List(properties[k], enclosing), combine(path, k)); | ||
} | ||
@@ -191,7 +226,7 @@ } catch (err) { | ||
function inflatePublic(val, genArgs) { | ||
return inflate(val, genArgs ? genArgs.T : null); | ||
return inflate(val, genArgs ? genArgs.T : null, ""); | ||
} | ||
export { inflatePublic as inflate }; | ||
export function ofJson(json, genArgs) { | ||
return inflate(JSON.parse(json), genArgs ? genArgs.T : null); | ||
return inflate(JSON.parse(json), genArgs ? genArgs.T : null, ""); | ||
} | ||
@@ -198,0 +233,0 @@ export function toJsonWithTypeInfo(o) { |
@@ -14,2 +14,5 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var formatRegExp = /\{(\d+)(,-?\d+)?(?:\:(.+?))?\}/g; | ||
function toHex(value) { | ||
return value < 0 ? "ff" + (16777215 - (Math.abs(value) - 1)).toString(16) : value.toString(16); | ||
} | ||
export function fsFormat(str) { | ||
@@ -50,2 +53,8 @@ var _cont = void 0; | ||
break; | ||
case "x": | ||
rep = toHex(Number(rep)); | ||
break; | ||
case "X": | ||
rep = toHex(Number(rep)).toUpperCase(); | ||
break; | ||
} | ||
@@ -110,2 +119,8 @@ var plusPrefix = flags.indexOf("+") >= 0 && parseInt(rep) >= 0; | ||
break; | ||
case "x": | ||
rep = toHex(Number(rep)); | ||
break; | ||
case "X": | ||
rep = toHex(Number(rep)).toUpperCase(); | ||
break; | ||
default: | ||
@@ -112,0 +127,0 @@ var m = /^(0+)(\.0+)?$/.exec(format); |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./Symbol", "./List", "./Set", "./Map", "./Util", "./Seq", "./Reflection", "./Date"], factory); | ||
define(["exports", "./Symbol", "./List", "./Set", "./Map", "./Util", "./Seq", "./Reflection", "./Date", "./String"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("./Symbol"), require("./List"), require("./Set"), require("./Map"), require("./Util"), require("./Seq"), require("./Reflection"), require("./Date")); | ||
factory(exports, require("./Symbol"), require("./List"), require("./Set"), require("./Map"), require("./Util"), require("./Seq"), require("./Reflection"), require("./Date"), require("./String")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.Symbol, global.List, global.Set, global.Map, global.Util, global.Seq, global.Reflection, global.Date); | ||
factory(mod.exports, global.Symbol, global.List, global.Set, global.Map, global.Util, global.Seq, global.Reflection, global.Date, global.String); | ||
global.Serialize = mod.exports; | ||
} | ||
})(this, function (exports, _Symbol, _List, _Set, _Map, _Util, _Seq, _Reflection, _Date) { | ||
})(this, function (exports, _Symbol, _List, _Set, _Map, _Util, _Seq, _Reflection, _Date, _String) { | ||
"use strict"; | ||
@@ -90,3 +90,19 @@ | ||
} | ||
function inflate(val, typ) { | ||
function combine(path1, path2) { | ||
return typeof path2 === "number" ? path1 + "[" + path2 + "]" : (path1 ? path1 + "." : "") + path2; | ||
} | ||
function isNullable(typ) { | ||
if (typeof typ === "string") { | ||
return typ !== "boolean" && typ !== "number"; | ||
} else if (typ instanceof _Util.NonDeclaredType) { | ||
return typ.kind !== "Array" && typ.kind !== "Tuple"; | ||
} else { | ||
var info = typeof typ.prototype[_Symbol2.default.reflection] === "function" ? typ.prototype[_Symbol2.default.reflection]() : null; | ||
return info ? info.nullable : true; | ||
} | ||
} | ||
function invalidate(val, typ, path) { | ||
throw new Error((0, _String.fsFormat)("%A", val) + " " + (path ? "(" + path + ")" : "") + " is not of type " + (0, _Reflection.getTypeFullName)(typ)); | ||
} | ||
function inflate(val, typ, path) { | ||
function needsInflate(enclosing) { | ||
@@ -114,13 +130,17 @@ var typ = enclosing.head; | ||
} | ||
function inflateArray(arr, enclosing) { | ||
return Array.isArray(arr) && needsInflate(enclosing) ? arr.map(function (x) { | ||
return inflate(x, enclosing); | ||
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) { | ||
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) : k; | ||
var val = inflateVal ? inflate(obj[k], valEnclosing) : obj[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]; | ||
@@ -136,4 +156,12 @@ }); | ||
} | ||
if (val == null || typeof typ === "string") { | ||
if (val == null) { | ||
if (!isNullable(typ)) { | ||
invalidate(val, typ, path); | ||
} | ||
return val; | ||
} else if (typeof typ === "string") { | ||
if ((typ === "boolean" || typ === "number" || typ === "string") && (typeof val === "undefined" ? "undefined" : _typeof(val)) !== typ) { | ||
invalidate(val, typ, path); | ||
} | ||
return val; | ||
} else if (typ instanceof _Util.NonDeclaredType) { | ||
@@ -144,11 +172,11 @@ switch (typ.kind) { | ||
case "Option": | ||
return inflate(val, new _List2.default(typ.generics[0], enclosing)); | ||
return inflate(val, new _List2.default(typ.generics[0], enclosing), path); | ||
case "Array": | ||
return inflateArray(val, new _List2.default(typ.generics[0], enclosing)); | ||
return inflateArray(val, new _List2.default(typ.generics[0], enclosing), path); | ||
case "Tuple": | ||
return typ.generics.map(function (x, i) { | ||
return inflate(val[i], new _List2.default(x, enclosing)); | ||
return inflate(val[i], new _List2.default(x, enclosing), combine(path, i)); | ||
}); | ||
case "GenericParam": | ||
return inflate(val, (0, _Reflection.resolveGeneric)(typ.name, enclosing.tail)); | ||
return inflate(val, (0, _Reflection.resolveGeneric)(typ.name, enclosing.tail), path); | ||
// case "Interface": // case "Any": | ||
@@ -164,18 +192,18 @@ default: | ||
if (proto instanceof _List2.default) { | ||
return (0, _List.ofArray)(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing))); | ||
return (0, _List.ofArray)(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing), path)); | ||
} | ||
if (proto instanceof _Set2.default) { | ||
return (0, _Set.create)(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing))); | ||
return (0, _Set.create)(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing), path)); | ||
} | ||
if (proto instanceof Set) { | ||
return new Set(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing))); | ||
return new Set(inflateArray(val, (0, _Reflection.resolveGeneric)(0, enclosing), path)); | ||
} | ||
if (proto instanceof _Map2.default) { | ||
return (0, _Map.create)(inflateMap(val, (0, _Reflection.resolveGeneric)(0, enclosing), (0, _Reflection.resolveGeneric)(1, enclosing))); | ||
return (0, _Map.create)(inflateMap(val, (0, _Reflection.resolveGeneric)(0, enclosing), (0, _Reflection.resolveGeneric)(1, enclosing), path)); | ||
} | ||
if (proto instanceof Map) { | ||
return new Map(inflateMap(val, (0, _Reflection.resolveGeneric)(0, enclosing), (0, _Reflection.resolveGeneric)(1, enclosing))); | ||
return new Map(inflateMap(val, (0, _Reflection.resolveGeneric)(0, enclosing), (0, _Reflection.resolveGeneric)(1, enclosing), path)); | ||
} | ||
var info = typeof proto[_Symbol2.default.reflection] === "function" ? proto[_Symbol2.default.reflection]() : {}; | ||
// Union types | ||
var info = typeof proto[_Symbol2.default.reflection] === "function" ? proto[_Symbol2.default.reflection]() : {}; | ||
if (info.cases) { | ||
@@ -188,8 +216,14 @@ var u = { Fields: [] }; | ||
var fieldTypes = info.cases[caseName]; | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = caseName; | ||
for (var i = 0; i < fieldTypes.length; i++) { | ||
u.Fields.push(inflate(fields[i], new _List2.default(fieldTypes[i], enclosing))); | ||
if (Array.isArray(fieldTypes)) { | ||
var fields = fieldTypes.length > 1 ? val[caseName] : [val[caseName]]; | ||
u.Case = caseName; | ||
path = combine(path, caseName); | ||
for (var i = 0; i < fieldTypes.length; i++) { | ||
u.Fields.push(inflate(fields[i], new _List2.default(fieldTypes[i], enclosing), combine(path, i))); | ||
} | ||
} | ||
} | ||
if (u.Case in info.cases === false) { | ||
invalidate(val, typ, path); | ||
} | ||
return Object.assign(new typ(), u); | ||
@@ -207,3 +241,3 @@ } | ||
val[k] = inflate(val[k], new _List2.default(properties[k], enclosing)); | ||
val[k] = inflate(val[k], new _List2.default(properties[k], enclosing), combine(path, k)); | ||
} | ||
@@ -232,7 +266,7 @@ } catch (err) { | ||
function inflatePublic(val, genArgs) { | ||
return inflate(val, genArgs ? genArgs.T : null); | ||
return inflate(val, genArgs ? genArgs.T : null, ""); | ||
} | ||
exports.inflate = inflatePublic; | ||
function ofJson(json, genArgs) { | ||
return inflate(JSON.parse(json), genArgs ? genArgs.T : null); | ||
return inflate(JSON.parse(json), genArgs ? genArgs.T : null, ""); | ||
} | ||
@@ -239,0 +273,0 @@ function toJsonWithTypeInfo(o) { |
@@ -44,2 +44,5 @@ (function (global, factory) { | ||
var formatRegExp = /\{(\d+)(,-?\d+)?(?:\:(.+?))?\}/g; | ||
function toHex(value) { | ||
return value < 0 ? "ff" + (16777215 - (Math.abs(value) - 1)).toString(16) : value.toString(16); | ||
} | ||
function fsFormat(str) { | ||
@@ -80,2 +83,8 @@ var _cont = void 0; | ||
break; | ||
case "x": | ||
rep = toHex(Number(rep)); | ||
break; | ||
case "X": | ||
rep = toHex(Number(rep)).toUpperCase(); | ||
break; | ||
} | ||
@@ -140,2 +149,8 @@ var plusPrefix = flags.indexOf("+") >= 0 && parseInt(rep) >= 0; | ||
break; | ||
case "x": | ||
rep = toHex(Number(rep)); | ||
break; | ||
case "X": | ||
rep = toHex(Number(rep)).toUpperCase(); | ||
break; | ||
default: | ||
@@ -142,0 +157,0 @@ var m = /^(0+)(\.0+)?$/.exec(format); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4563966
77
11227