git-cat-file
Version:
Pure JavaScript `git cat-file -p` for node.js
70 lines (69 loc) • 1.72 kB
JavaScript
;
/**
* https://github.com/kawanet/git-cat-file
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjItem = void 0;
class ObjItem {
constructor(obj, store) {
this.obj = obj;
this.store = store;
//
}
getId() {
return this.obj.oid;
}
parseMeta() {
if (this.meta)
return;
const { data } = this.obj;
const meta = this.meta = {};
const lines = data.toString().split(/\r?\n/);
let headerMode = true;
let message;
for (const line of lines) {
if (headerMode) {
const [key, val] = splitBySpace(line);
if (meta[key]) {
meta[key].push(val);
}
else if (key) {
meta[key] = [val];
}
else {
headerMode = false;
}
}
else {
if (message) {
message += "\n" + line;
}
else {
message = line;
}
}
}
this.message = message;
}
getMeta(key) {
const array = this.getMetaArray(key);
if (array) {
if (array.length > 1)
return array.join(" ");
return array[0];
}
}
getMetaArray(key) {
this.parseMeta();
return this.meta[key];
}
getMessage() {
this.parseMeta();
return this.message;
}
}
exports.ObjItem = ObjItem;
function splitBySpace(line) {
const sp = line.indexOf(" ");
return [line.slice(0, sp), line.slice(sp + 1)];
}