UNPKG

rxdb

Version:

A local-first realtime NoSQL Database for JavaScript applications - https://rxdb.info/

50 lines (47 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRevision = createRevision; exports.getHeightOfRevision = getHeightOfRevision; exports.parseRevision = parseRevision; /** * Parses the full revision. * Do NOT use this if you only need the revision height, * then use getHeightOfRevision() instead which is faster. */ function parseRevision(revision) { var split = revision.split('-'); if (split.length !== 2) { throw new Error('malformatted revision: ' + revision); } return { height: parseInt(split[0], 10), hash: split[1] }; } /** * @hotPath Performance is very important here * because we need to parse the revision height very often. * Do not use `parseInt(revision.split('-')[0], 10)` because * only fetching the start-number chars is faster. */ function getHeightOfRevision(revision) { var useChars = ''; for (var index = 0; index < revision.length; index++) { var char = revision[index]; if (char === '-') { return parseInt(useChars, 10); } useChars += char; } throw new Error('malformatted revision: ' + revision); } /** * Creates the next write revision for a given document. */ function createRevision(databaseInstanceToken, previousDocData) { var newRevisionHeight = !previousDocData ? 1 : getHeightOfRevision(previousDocData._rev) + 1; return newRevisionHeight + '-' + databaseInstanceToken; } //# sourceMappingURL=utils-revision.js.map