UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

83 lines (82 loc) 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isFeature = isFeature; function isFeature(thing) { return (typeof thing === 'object' && thing !== null && typeof thing.get === 'function' && typeof thing.id === 'function'); } function isSimpleFeatureSerialized(args) { return 'uniqueId' in args && typeof args.data !== 'object'; } class SimpleFeature { constructor(args) { var _a; if (isSimpleFeatureSerialized(args)) { this.data = args; } else { this.data = args.data; this.parentHandle = args.parent; } const id = isSimpleFeatureSerialized(args) ? args.uniqueId : args.id; if (id === undefined || id === null) { throw new Error('SimpleFeature requires a unique `id` or `data.uniqueId` attribute'); } this.uniqueId = String(id); if (!(this.data.aliases || this.data.end - this.data.start >= 0)) { throw new Error(`invalid feature data, end less than start. end: ${this.data.end} start: ${this.data.start}`); } if (this.data.subfeatures) { this.subfeatures = (_a = this.data.subfeatures) === null || _a === void 0 ? void 0 : _a.map((f, i) => typeof f.get !== 'function' ? new SimpleFeature({ id: f.uniqueId || `${id}-${i}`, data: { strand: this.data.strand, ...f, }, parent: this, }) : f); } } get(name) { return name === 'subfeatures' ? this.subfeatures : name === 'parent' ? this.parent() : this.data[name]; } set(name, val) { this.data[name] = val; } tags() { return Object.keys(this.data); } id() { return this.uniqueId; } parent() { return this.parentHandle; } children() { return this.get('subfeatures'); } toJSON() { const d = { ...this.data, uniqueId: this.id() }; const p = this.parent(); if (p) { d.parentId = p.id(); } const c = this.children(); if (c) { d.subfeatures = c.map(child => child.toJSON()); } return d; } static fromJSON(json) { return new SimpleFeature({ ...json }); } } exports.default = SimpleFeature;