import entry_search from "./index" const enum EntryType { Directory, File } const enum Permissions { r, w, rw, none } interface Entry { readonly name: string, readonly type: EntryType, readonly timestamp: number, readonly permissions: Permissions, } interface EntryCollectionInner extends Entry { readonly collection: T[], } interface EntryFile extends Entry { inner: T } interface EntryCollection extends EntryCollectionInner { pop: (file_name: string) => T | undefined, get: (file_name: string) => T | undefined push: (entry: E) => boolean, sort: () => void, } interface Rfwfs { is_dir: (entry: T) => boolean, is_file: (entry: T) => boolean, new_entry: (name: string, permissions: Permissions, timestamp: number, inner: T) => EntryFile, new_collection: (inner: T[]) => EntryCollection, } function sort(self: EntryCollection) { self.collection.sort((a,z) => a.name.localeCompare(z.name)) } function push(self: EntryCollection, entry: Entry) { const no_duplicates = entry_search(self.collection, entry.name) if (!no_duplicates) { self.push(entry) self.sort() return true } return false } function get(self: EntryCollection, file_name: string) { const file_search = entry_search(self.collection, file_name) return file_search ? file_search.result : undefined } function pop(self: EntryCollection, file_name: string) { const file_search = entry_search(self.collection, file_name) if (file_search) { self.collection.splice(file_search.index, 1) return file_search.result } return undefined } const rfwfs = {} as Rfwfs rfwfs.is_dir = function(entry) { return entry.type === EntryType.Directory } rfwfs.is_file = function(entry) { return entry.type === EntryType.File } rfwfs.new_entry = function(name, permissions, timestamp, inner) { return { name: name, type: typeof inner === "object" ? EntryType.Directory : EntryType.File, inner: inner, timestamp: timestamp ? timestamp : (Date.now()/1000)|0, permissions: permissions, } } rfwfs.new_collection = function(collection: T[]): EntryCollection { const collection_trait = { collection: collection } as EntryCollection collection_trait.sort = function() { return sort(this) } collection_trait.push = function(entry) { return push(this, entry) } collection_trait.get = function(file_name) { return get(this, file_name) } collection_trait.pop = function(file_name) { return pop(this, file_name) } return collection_trait } export default rfwfs export { EntryType, Permissions, type EntryCollection, type Entry, }