UNPKG

als-path-tree

Version:

A Node.js library for managing a virtual file system structure, offering functionalities for file and directory operations with a shared storage system.

42 lines (37 loc) 1.27 kB
const MemoryMonitor = require('./memory-monitor') const options = require('./options') const EventEmitter = require('als-event-emitter') class Item { static get options() { return options } static memMonitor = new MemoryMonitor() static emitter = new EventEmitter(true) get main() { return Item } constructor(name, parent, isFile = false) { this.name = name this.parent = parent this.isFile = isFile } get path() { return (this.parent ? this.parent.path + '/' : '') + this.name } get root() { if (this._root) return this._root this._root = this while (this._root.parent) { this._root = this._root.parent } return this._root } getParts(path) { return Array.isArray(path) ? path : path.split('/').filter(Boolean) } splitPath(path) { const parts = this.getParts(path) const filename = parts.pop() return { parts, filename } } rename(newName) { if (this.name === newName) return; if (this.parent) { const parentObj = this.isFile ? this.parent.files : this.parent.subdirectories delete parentObj[this.name] parentObj[newName] = this } this.name = newName; } } module.exports = Item