capnpc-ts
Version:
Cap'n Proto schema compiler for TypeScript.
79 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitCamel = exports.decToUint64 = exports.decToInt64 = exports.decToHexBytes = exports.d2h = exports.c2t = exports.c2s = void 0;
const tslib_1 = require("tslib");
const types_1 = require("capnp-ts/src/types");
const util_1 = require("capnp-ts/src/util");
const debug_1 = tslib_1.__importDefault(require("debug"));
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/unbound-method */
const { decToHex } = require("hex2dec");
/* eslint-enable */
const trace = debug_1.default("capnpc:util");
trace("load");
function c2s(s) {
return splitCamel(s)
.map((x) => x.toUpperCase())
.join("_");
}
exports.c2s = c2s;
function c2t(s) {
return s.substr(0, 1).toUpperCase() + s.substr(1);
}
exports.c2t = c2t;
function d2h(d) {
let h = decToHex(d).substr(2);
let neg = false;
if (h[0] === "-") {
h = h.substr(1);
neg = true;
}
return neg ? `-${util_1.pad(h, 16)}` : util_1.pad(h, 16);
}
exports.d2h = d2h;
function decToHexBytes(d) {
let h = d2h(d);
const neg = h[0] === "-";
const out = neg ? ["-"] : [];
if (neg)
h = h.substr(1);
for (let i = 0; i < h.length; i += 2) {
out.push(h.substr(i, 2));
}
return out;
}
exports.decToHexBytes = decToHexBytes;
function decToInt64(d) {
const h = decToHexBytes(d);
const neg = h[0] === "-";
if (neg)
h.shift();
const v = new types_1.Int64(new Uint8Array(h.map((b) => parseInt(b, 16))));
if (neg)
v.negate();
return v;
}
exports.decToInt64 = decToInt64;
function decToUint64(d) {
const h = decToHexBytes(d);
return new types_1.Uint64(new Uint8Array(h.map((b) => parseInt(b, 16))));
}
exports.decToUint64 = decToUint64;
function splitCamel(s) {
let wasLo = false;
return s.split("").reduce((a, c) => {
const lo = c.toUpperCase() !== c;
const up = c.toLowerCase() !== c;
if (a.length === 0 || (wasLo && up)) {
a.push(c);
}
else {
const i = a.length - 1;
a[i] = a[i] + c;
}
wasLo = lo;
return a;
}, []);
}
exports.splitCamel = splitCamel;
//# sourceMappingURL=util.js.map