muschema
Version:
Schemas for mudb
150 lines • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var mustreams_1 = require("mustreams");
var index_1 = require("../index");
var constants_1 = require("../constants");
function muNumberSchema(muType) {
var map = {
float32: function () { return new index_1.MuFloat32(); },
float64: function () { return new index_1.MuFloat64(); },
int8: function () { return new index_1.MuInt8(); },
int16: function () { return new index_1.MuInt16(); },
int32: function () { return new index_1.MuInt32(); },
uint8: function () { return new index_1.MuUint8(); },
uint16: function () { return new index_1.MuUint16(); },
uint32: function () { return new index_1.MuUint32(); },
};
if (muType in map) {
return map[muType]();
}
}
exports.muNumberSchema = muNumberSchema;
function muPrimitiveSchema(muType) {
var map = {
ascii: function () { return new index_1.MuASCII(); },
boolean: function () { return new index_1.MuBoolean(); },
float32: function () { return new index_1.MuFloat32(); },
float64: function () { return new index_1.MuFloat64(); },
int8: function () { return new index_1.MuInt8(); },
int16: function () { return new index_1.MuInt16(); },
int32: function () { return new index_1.MuInt32(); },
string: function () { return new index_1.MuString(); },
uint8: function () { return new index_1.MuUint8(); },
uint16: function () { return new index_1.MuUint16(); },
uint32: function () { return new index_1.MuUint32(); },
};
if (muType in map) {
return map[muType]();
}
}
exports.muPrimitiveSchema = muPrimitiveSchema;
function simpleStrOfLeng(length) {
var ingredient = 'abc';
var chars = new Array(length);
for (var i = 0; i < length; ++i) {
chars[i] = ingredient.charAt(Math.random() * ingredient.length | 0);
}
return chars.join('');
}
exports.simpleStrOfLeng = simpleStrOfLeng;
function randomString() {
function randomCodePoint() {
var MAX_CODE_POINT = 0xD7FF;
return Math.random() * MAX_CODE_POINT | 0;
}
var length = Math.random() * 20 + 1 | 0;
var codePoints = new Array(length);
for (var i = 0; i < length; ++i) {
codePoints[i] = randomCodePoint();
}
return String.fromCharCode.apply(String, codePoints);
}
exports.randomString = randomString;
function randomShortStr() {
var length = Math.random() * 3 + 1 | 0;
return simpleStrOfLeng(length);
}
exports.randomShortStr = randomShortStr;
function randomValue(muType) {
function randomASCII() {
var length = Math.random() * 21 | 0;
var codePoints = new Array(length);
for (var i = 0; i < length; ++i) {
codePoints[i] = Math.random() * 0x80 | 0;
}
return String.fromCharCode.apply(null, codePoints);
}
function randomSign() {
return Math.random() < 0.5 ? -1 : 1;
}
function randomFloat32() {
function fround(n) {
var fa = new Float32Array(1);
fa[0] = n;
return fa[0];
}
var exponent = randomSign() * (Math.random() * 38 | 0);
return fround(Math.random() * Math.pow(10, exponent));
}
function randomFloat64() {
var exponent = randomSign() * (Math.random() * 308 | 0);
return Math.random() * Math.pow(10, exponent);
}
switch (muType) {
case 'ascii':
return randomASCII();
case 'boolean':
return Math.random() < 0.5 ? false : true;
case 'float32':
return randomFloat32();
case 'float64':
return randomFloat64();
case 'int8':
case 'int16':
case 'int32':
return randomSign() * Math.round(Math.random() * constants_1.Constants[muType].MAX);
case 'string':
return randomString();
case 'uint8':
case 'uint16':
case 'uint32':
return Math.round(Math.random() * constants_1.Constants[muType].MAX);
default:
return;
}
}
exports.randomValue = randomValue;
function testPatchingFactory(t, schema, fn) {
function diffPatch(a, b) {
var ws = new mustreams_1.MuWriteStream(2);
schema.diff(a, b, ws);
var rs = new mustreams_1.MuReadStream(ws.bytes());
if (rs.length) {
var r = schema.patch(a, rs);
t.equals(rs.offset, rs.length, 'no bytes left in stream');
return r;
}
else {
t.same(a, b, 'empty patch consistent');
return schema.clone(a);
}
}
return fn ?
function (a, b) {
t.same(diffPatch(a, b), fn(b));
}
:
function (a, b) {
t.same(diffPatch(a, b), b);
};
}
exports.testPatchingFactory = testPatchingFactory;
function testPatchingPairFactory(t, schema, fn) {
var test = fn ? testPatchingFactory(t, schema, fn) : testPatchingFactory(t, schema);
return function (a, b) {
test(a, b);
test(b, a);
};
}
exports.testPatchingPairFactory = testPatchingPairFactory;
//# sourceMappingURL=helper.js.map