rewrite of types and names
This commit is contained in:
@ -1,14 +1,7 @@
|
|||||||
import { type Entry } from "./main"
|
import { type Entry } from "./types/entry"
|
||||||
|
import { wrap_bsearch, type WrapBSearch } from "./wrap"
|
||||||
|
|
||||||
interface Wrap<T> {
|
export default function directory_search<T extends Entry>(entry_collection: T[], file_name: string): WrapBSearch<T> | undefined {
|
||||||
readonly result: T,
|
|
||||||
readonly index: number
|
|
||||||
}
|
|
||||||
function wrap_result<T>(result: T, index: number): Wrap<T> {
|
|
||||||
return { result: result, index: index }
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function entry_search<T extends Entry>(entry_collection: T[], file_name: string): Wrap<T> | undefined {
|
|
||||||
let start = 0
|
let start = 0
|
||||||
let end = entry_collection.length-1
|
let end = entry_collection.length-1
|
||||||
while (start<=end) {
|
while (start<=end) {
|
||||||
@ -16,7 +9,7 @@ export default function entry_search<T extends Entry>(entry_collection: T[], fil
|
|||||||
const median_name = entry_collection[median].name
|
const median_name = entry_collection[median].name
|
||||||
|
|
||||||
if (median_name === file_name) {
|
if (median_name === file_name) {
|
||||||
return wrap_result(entry_collection[median], median)
|
return wrap_bsearch(median, entry_collection[median])
|
||||||
} else if (median_name<file_name) {
|
} else if (median_name<file_name) {
|
||||||
start = median+1
|
start = median+1
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,83 +1,93 @@
|
|||||||
import entry_search from "./index"
|
import { EntryType, PushStatus, ReadStatus, Permissions } from "./enum"
|
||||||
|
import { Entry, EntryCollection, FileInner, EntryFile, EntryCollectionManipulate, EntryFileInner } from "./types/entry"
|
||||||
|
|
||||||
const enum EntryType {
|
import directory_search from "./index"
|
||||||
Directory,
|
import { wrap_entry, wrap_none, WrapResultEntry, WrapResultNone } from "./wrap"
|
||||||
File
|
|
||||||
}
|
|
||||||
const enum Permissions {
|
|
||||||
r,
|
|
||||||
w,
|
|
||||||
rw,
|
|
||||||
none
|
|
||||||
}
|
|
||||||
const enum PushStatus {
|
|
||||||
Ok,
|
|
||||||
Duplicate,
|
|
||||||
Denied,
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Entry {
|
|
||||||
readonly name: string,
|
|
||||||
readonly type: EntryType,
|
|
||||||
readonly timestamp: number,
|
|
||||||
readonly permissions: Permissions,
|
|
||||||
}
|
|
||||||
interface EntryCollectionInner<T extends Entry> extends Entry {
|
|
||||||
readonly collection: T[],
|
|
||||||
}
|
|
||||||
interface EntryFile<T> extends Entry {
|
|
||||||
inner: T
|
|
||||||
}
|
|
||||||
interface EntryCollection<T extends Entry> extends EntryCollectionInner<T> {
|
|
||||||
pop: (file_name: string) => T | undefined,
|
|
||||||
find: (file_name: string) => T | undefined
|
|
||||||
push: (entry: Entry) => PushStatus,
|
|
||||||
sort: () => void,
|
|
||||||
}
|
|
||||||
interface Rfwfs {
|
interface Rfwfs {
|
||||||
is_dir: <T extends Entry>(entry: T) => boolean,
|
directory: <T extends Entry>(name: string, permissions: Permissions, timestamp: number, inner_default: T[]) => EntryCollection<T>,
|
||||||
is_file: <T extends Entry>(entry: T) => boolean,
|
is_file: <T extends Entry>(entry: T) => boolean,
|
||||||
entry: <T>(name: string, permissions: Permissions, timestamp: number, inner: T) => EntryFile<T>,
|
is_dir: <T extends Entry>(entry: T) => boolean,
|
||||||
collection: <T extends Entry>(inner: T[]) => EntryCollection<T>,
|
file: (name: string, permissions: Permissions, timestamp: number, inner_default: FileInner) => EntryFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
function readable<E extends Entry>(self: EntryCollection<E>): boolean {
|
function read_write_access(permissions: Permissions): boolean {
|
||||||
return self.permissions === Permissions.rw || self.permissions === Permissions.r
|
return permissions === Permissions.rw
|
||||||
|
}
|
||||||
|
function read_access(permissions: Permissions): boolean {
|
||||||
|
return read_write_access(permissions) || permissions === Permissions.r
|
||||||
|
}
|
||||||
|
function write_access(permissions: Permissions): boolean {
|
||||||
|
return read_write_access(permissions) || permissions === Permissions.w
|
||||||
}
|
}
|
||||||
|
|
||||||
function writable<E extends Entry>(self: EntryCollection<E>): boolean {
|
function directory_sort<E extends Entry>(self: EntryCollectionManipulate<E>) {
|
||||||
return self.permissions === Permissions.rw || self.permissions === Permissions.w
|
self.__body.sort((a,z) => a.name.localeCompare(z.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
function sort<E extends Entry>(self: EntryCollection<E>) {
|
function directory_push<E extends Entry>(self: EntryCollection<E>, entry: E): WrapResultNone<PushStatus> {
|
||||||
self.collection.sort((a,z) => a.name.localeCompare(z.name))
|
if (write_access(self.permissions)) {
|
||||||
}
|
const no_duplicates = directory_search(self.inner.__body, entry.name)
|
||||||
|
|
||||||
function push<E extends Entry>(self: EntryCollection<E>, entry: Entry): PushStatus {
|
|
||||||
if (writable(self)) {
|
|
||||||
const no_duplicates = entry_search(self.collection, entry.name)
|
|
||||||
if (!no_duplicates) {
|
if (!no_duplicates) {
|
||||||
self.push(entry)
|
self.inner.__body.push(entry)
|
||||||
self.sort()
|
self.inner.__body.sort()
|
||||||
return PushStatus.Ok
|
return wrap_none(PushStatus.Ok)
|
||||||
}
|
}
|
||||||
return PushStatus.Duplicate
|
return wrap_none(PushStatus.Duplicate)
|
||||||
}
|
}
|
||||||
return PushStatus.Denied
|
return wrap_none(PushStatus.Denied)
|
||||||
}
|
}
|
||||||
|
|
||||||
function find<E extends Entry>(self: EntryCollection<E>, file_name: string) {
|
function directory_find<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
|
||||||
const file_search = entry_search(self.collection, file_name)
|
if (read_access(self.permissions)) {
|
||||||
return file_search ? file_search.result : undefined
|
const file_search = directory_search(self.inner.__body, file_name)
|
||||||
|
if (file_search) {
|
||||||
|
return wrap_entry(ReadStatus.Ok, file_search.result)
|
||||||
|
}
|
||||||
|
return wrap_entry(ReadStatus.NotFound)
|
||||||
|
}
|
||||||
|
return wrap_entry(ReadStatus.Denied)
|
||||||
}
|
}
|
||||||
|
|
||||||
function pop<E extends Entry>(self: EntryCollection<E>, file_name: string) {
|
function directory_pop<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
|
||||||
const file_search = entry_search(self.collection, file_name)
|
if (read_write_access(self.permissions)) {
|
||||||
if (file_search) {
|
const pop_find = directory_search(self.inner.__body, file_name)
|
||||||
self.collection.splice(file_search.index, 1)
|
if (pop_find) {
|
||||||
return file_search.result
|
self.inner.__body.splice(pop_find.some, 1)
|
||||||
|
return wrap_entry(ReadStatus.Ok, pop_find.result)
|
||||||
|
}
|
||||||
|
return wrap_entry(ReadStatus.NotFound)
|
||||||
}
|
}
|
||||||
return undefined
|
return wrap_entry(ReadStatus.Denied)
|
||||||
|
}
|
||||||
|
|
||||||
|
function file_inner_read(self: EntryFileInner, permissions: Permissions): FileInner | undefined {
|
||||||
|
return read_access(permissions) ? self.__body : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function file_inner_write(self: EntryFileInner, permissions: Permissions, item: FileInner): boolean {
|
||||||
|
if (write_access(permissions)) {
|
||||||
|
self.__body = item
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function file_inner(permissions: Permissions, inner_default: FileInner): EntryFileInner {
|
||||||
|
const file_inner_trait = { __body: inner_default } as EntryFileInner
|
||||||
|
file_inner_trait.read = function() { return file_inner_read(this, permissions) }
|
||||||
|
file_inner_trait.write = function(item) { return file_inner_write(this, permissions, item) }
|
||||||
|
return file_inner_trait
|
||||||
|
}
|
||||||
|
|
||||||
|
function dir_inner<T extends Entry>(self: EntryCollection<T>, collection: T[]): EntryCollectionManipulate<T> {
|
||||||
|
const collection_trait = { __body: collection } as EntryCollectionManipulate<T>
|
||||||
|
collection_trait.pop = function(file_name) { return directory_pop(self, file_name) }
|
||||||
|
collection_trait.find = function(file_name) { return directory_find(self, file_name) }
|
||||||
|
collection_trait.push = function(entry) { return directory_push(self, entry) }
|
||||||
|
collection_trait.sort = function() { return directory_sort(this) }
|
||||||
|
collection_trait.sort() //the default collection is automatically sorted on directory creation.
|
||||||
|
return collection_trait
|
||||||
}
|
}
|
||||||
|
|
||||||
const rfwfs = {} as Rfwfs
|
const rfwfs = {} as Rfwfs
|
||||||
@ -85,35 +95,27 @@ const rfwfs = {} as Rfwfs
|
|||||||
rfwfs.is_dir = function(entry) {
|
rfwfs.is_dir = function(entry) {
|
||||||
return entry.type === EntryType.Directory
|
return entry.type === EntryType.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
rfwfs.is_file = function(entry) {
|
rfwfs.is_file = function(entry) {
|
||||||
return entry.type === EntryType.File
|
return entry.type === EntryType.File
|
||||||
}
|
}
|
||||||
|
|
||||||
rfwfs.entry = function(name, permissions, timestamp, inner) {
|
rfwfs.file = function(name, permissions, timestamp, inner_default) {
|
||||||
return {
|
const file = { type: EntryType.File } as EntryFile
|
||||||
name: name,
|
file.permissions = permissions
|
||||||
type: typeof inner === "object" ? EntryType.Directory : EntryType.File,
|
file.timestamp = timestamp
|
||||||
inner: inner,
|
file.inner = file_inner(permissions, inner_default)
|
||||||
timestamp: timestamp ? timestamp : (Date.now()/1000)|0,
|
file.name = name
|
||||||
permissions: permissions,
|
file.hash = "0"
|
||||||
}
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
rfwfs.collection = function<T extends Entry>(collection: T[]): EntryCollection<T> {
|
rfwfs.directory = function<T extends Entry>(name: string, permissions: Permissions, timestamp: number, inner_default: T[]): EntryCollection<T> {
|
||||||
const collection_trait = { collection: collection } as EntryCollection<T>
|
const directory = { type: EntryType.Directory } as EntryCollection<T>
|
||||||
collection_trait.pop = function(file_name) { return pop(this, file_name) }
|
directory.permissions = permissions
|
||||||
collection_trait.find = function(file_name) { return find(this, file_name) }
|
directory.timestamp = timestamp
|
||||||
collection_trait.push = function(entry) { return push(this, entry) }
|
directory.inner = dir_inner(directory, inner_default)
|
||||||
collection_trait.sort = function() { return sort(this) }
|
directory.name = name
|
||||||
collection_trait.sort()
|
return directory
|
||||||
return collection_trait
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default rfwfs
|
export default rfwfs
|
||||||
export {
|
|
||||||
EntryType,
|
|
||||||
Permissions,
|
|
||||||
type EntryCollection,
|
|
||||||
type Entry,
|
|
||||||
}
|
|
Reference in New Issue
Block a user