UNPKG

cluster-id

Version:

Database cluster friendly object id with great query isolation.

32 lines (29 loc) 929 B
var ref = require('./config'); var SEGMENT_LEN = ref.SEGMENT_LEN; var META_LEN = ref.META_LEN; var TIME_LEN = ref.TIME_LEN; // len of segment in symbols (scope or child) var HALF_LEN = SEGMENT_LEN + META_LEN + TIME_LEN var FULL_LEN = 2 * HALF_LEN /** * Return scope id for give id. For root ids scope is id itself. * @param {string} idStr a scopeid instance */ function parseScope (idStr) { if (idStr.length === HALF_LEN) { // root id return idStr } else if (idStr.length === FULL_LEN) { // scoped id, convert it to normal form and return var scopedStr = idStr.slice(HALF_LEN) var segmentStr = scopedStr.slice(0, SEGMENT_LEN) var timeStr = scopedStr.slice(SEGMENT_LEN, SEGMENT_LEN + TIME_LEN) var metaStr = scopedStr.slice(-META_LEN) return segmentStr + metaStr + timeStr } else { throw new Error(("Invalid id length: " + idStr)) } } module.exports = { parseScope: parseScope }