UNPKG

git-documentdb

Version:

Offline-first database that syncs with Git

62 lines 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SerializeFormatFrontMatter = exports.SerializeFormatJSON = void 0; const const_1 = require("./const"); const utils_1 = require("./utils"); class SerializeFormatJSON { constructor() { this.format = 'json'; this.firstExtension = const_1.JSON_POSTFIX; this.secondExtension = undefined; } extension() { return const_1.JSON_POSTFIX; } removeExtension(path) { if (path.endsWith(this.firstExtension)) return path.replace(new RegExp(this.firstExtension + '$'), ''); return path; } hasObjectExtension(path) { if (path.endsWith(this.firstExtension)) return true; return false; } serialize(doc) { return { extension: const_1.JSON_POSTFIX, data: (0, utils_1.toSortedJSONString)(doc) }; } } exports.SerializeFormatJSON = SerializeFormatJSON; class SerializeFormatFrontMatter { constructor() { this.format = 'front-matter'; this.firstExtension = const_1.FRONT_MATTER_POSTFIX; this.secondExtension = const_1.YAML_POSTFIX; } extension(doc) { if (doc !== undefined && doc._body === undefined) return const_1.YAML_POSTFIX; return const_1.FRONT_MATTER_POSTFIX; } removeExtension(path) { if (path.endsWith(this.firstExtension)) return path.replace(new RegExp(this.firstExtension + '$'), ''); if (path.endsWith(this.secondExtension)) return path.replace(new RegExp(this.secondExtension + '$'), ''); return path; } hasObjectExtension(path) { if (path.endsWith(this.firstExtension) || path.endsWith(this.secondExtension)) return true; return false; } serialize(doc) { const extension = this.extension(doc); return { extension, data: extension === const_1.YAML_POSTFIX ? (0, utils_1.toYAML)(doc) : (0, utils_1.toFrontMatterMarkdown)(doc), }; } } exports.SerializeFormatFrontMatter = SerializeFormatFrontMatter; //# sourceMappingURL=serialize_format.js.map