@stencila/jesta
Version:
Stencila plugin for executable documents using JavaScript
42 lines (41 loc) • 1.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.record = exports.needed = void 0;
const crypto_1 = __importDefault(require("crypto"));
const md5json = (entity) => {
const json = JSON.stringify(entity, (key, value) => {
return key === 'meta' ? undefined : value;
});
return crypto_1.default.createHash('md5').update(json).digest('hex');
};
const needed = (entity, method) => {
const { meta = {}, ...rest } = entity;
const { history = {} } = meta;
const previous = history[method];
if (previous === undefined)
return true;
return previous.md5json !== md5json(rest);
};
exports.needed = needed;
const record = (entity, method, seconds) => {
const { meta: { history = {}, ...metaRest } = {}, ...entityRest } = entity;
return {
...entityRest,
meta: {
...metaRest,
history: {
...history,
[method]: {
plugin: 'jesta',
time: new Date().toISOString(),
seconds,
md5json: md5json(entityRest),
},
},
},
};
};
exports.record = record;