@phensley/cldr-core
Version:
Core library for @phensley/cldr
145 lines • 5.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var cldr_schema_1 = require("@phensley/cldr-schema");
var types_1 = require("../types");
var string_1 = require("../utils/string");
/**
* Generates field offsets for the schema builder.
*/
var Generator = /** @class */ (function () {
function Generator() {
this.offset = 0;
}
Generator.prototype.field = function () {
return this.offset++;
};
Generator.prototype.vector1 = function (dim) {
var off = this.offset;
this.offset += dim;
return off;
};
Generator.prototype.vector2 = function (dim1, dim2) {
var off = this.offset;
this.offset += (dim1 * dim2);
return off;
};
return Generator;
}());
var time = function (n) {
return new types_1.Decimal(n[0]).add(new types_1.Decimal(n[1]).movePoint(-9));
};
var elapsed = function (start, end) {
return time(end).subtract(time(start)).movePoint(6).toString();
};
/**
* Builds the schema accessor singleton.
*/
var SchemaBuilder = /** @class */ (function () {
function SchemaBuilder(debug) {
if (debug === void 0) { debug = false; }
this.generator = new Generator();
this._times = [];
this.captureTimes = debug && process !== undefined && process.hrtime !== undefined;
}
SchemaBuilder.prototype.construct = function (obj, inst) {
switch (inst.type) {
case 'digits':
this.constructDigits(obj, inst);
break;
case 'field':
this.constructField(obj, inst);
break;
case 'origin':
this.constructOrigin(obj, inst);
break;
case 'scope':
this.constructScope(obj, inst);
break;
case 'scopemap':
this.constructScopeMap(obj, inst);
break;
case 'vector1':
this.constructVector1(obj, inst);
break;
case 'vector2':
this.constructVector2(obj, inst);
break;
}
};
SchemaBuilder.prototype.constructDigits = function (obj, inst) {
var offset = this.generator.vector2(inst.dim0.size, inst.values.length * 2);
obj[inst.name] = new cldr_schema_1.DigitsArrow(offset, inst.dim0, inst.values);
};
SchemaBuilder.prototype.constructField = function (obj, inst) {
var offset = this.generator.field();
obj[inst.name] = new cldr_schema_1.FieldArrow(offset);
};
SchemaBuilder.prototype.constructOrigin = function (obj, inst) {
var capture = this.captureTimes;
for (var _i = 0, _a = inst.block; _i < _a.length; _i++) {
var i = _a[_i];
var start = capture ? process.hrtime() : [0, 0];
this.construct(obj, i);
var end = capture ? process.hrtime() : [0, 0];
if (capture) {
this._times.push([i.identifier, elapsed(start, end)]);
}
}
if (capture) {
console.log('Scope construct times (microseconds):');
for (var _b = 0, _c = this._times; _b < _c.length; _b++) {
var t = _c[_b];
console.log(string_1.leftPad(t[0], 20), t[1]);
}
}
};
SchemaBuilder.prototype.constructScope = function (obj, inst) {
var curr = {};
obj[inst.identifier] = curr;
for (var _i = 0, _a = inst.block; _i < _a.length; _i++) {
var i = _a[_i];
this.construct(curr, i);
}
};
SchemaBuilder.prototype.constructScopeMap = function (obj, inst) {
var map = {};
for (var _i = 0, _a = inst.fields; _i < _a.length; _i++) {
var field = _a[_i];
var child = {};
for (var _b = 0, _c = inst.block; _b < _c.length; _b++) {
var i = _c[_b];
this.construct(child, i);
}
map[field] = child;
}
var undef = {};
for (var _d = 0, _e = inst.block; _d < _e.length; _d++) {
var i = _e[_d];
this.construct(undef, i);
}
obj[inst.name] = new cldr_schema_1.ScopeArrow(map, undef);
};
SchemaBuilder.prototype.constructVector1 = function (obj, inst) {
var offset = this.generator.field(); // header
this.generator.vector1(inst.dim0.size);
obj[inst.name] = new cldr_schema_1.Vector1Arrow(offset, inst.dim0);
};
SchemaBuilder.prototype.constructVector2 = function (obj, inst) {
var offset = this.generator.field(); // header
this.generator.vector2(inst.dim0.size, inst.dim1.size);
obj[inst.name] = new cldr_schema_1.Vector2Arrow(offset, inst.dim0, inst.dim1);
};
return SchemaBuilder;
}());
exports.SchemaBuilder = SchemaBuilder;
var SCHEMA;
exports.buildSchema = function (debug) {
if (debug === void 0) { debug = false; }
if (SCHEMA === undefined) {
var builder = new SchemaBuilder(debug);
SCHEMA = {};
builder.construct(SCHEMA, cldr_schema_1.ORIGIN);
}
return SCHEMA;
};
//# sourceMappingURL=machine.js.map
;