type errors
This commit is contained in:
@ -14,33 +14,45 @@ interface EntryCollection<T> extends EntryCollectionManipulate {
|
||||
readonly inner: Files<T>
|
||||
}
|
||||
|
||||
function entry_trait<T extends Entry<T>>() {
|
||||
const trait = {} as EntryCollection<T>
|
||||
trait.sort = function() {
|
||||
this.inner.sort((a,z) => a.name.localeCompare(z.name))
|
||||
function sort<E extends Entry<E>>(self: EntryCollection<E>) {
|
||||
self.inner.sort((a,z) => a.name.localeCompare(z.name))
|
||||
}
|
||||
trait.push = function(entry) {
|
||||
const no_duplicates = entry_search(this.inner, entry.name)
|
||||
|
||||
function push<E extends Entry<E>, T extends Entry<T>>(self: EntryCollection<E>, entry: Entry<T>) {
|
||||
const no_duplicates = entry_search(self.inner, entry.name)
|
||||
if (!no_duplicates) {
|
||||
this.push(entry)
|
||||
this.sort()
|
||||
self.push(entry)
|
||||
self.sort()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
trait.pop = function(file_name) {
|
||||
const file_search = entry_search(this.inner, file_name)
|
||||
|
||||
function get<E extends Entry<E>>(self: EntryCollection<E>, file_name: string) {
|
||||
const file_search = entry_search(self.inner, file_name)
|
||||
return file_search ? file_search.result : undefined
|
||||
}
|
||||
|
||||
function pop<E extends Entry<E>>(self: EntryCollection<E>, file_name: string) {
|
||||
const file_search = entry_search(self.inner, file_name)
|
||||
if (file_search) {
|
||||
this.inner.splice(file_search.index, 1)
|
||||
self.inner.splice(file_search.index, 1)
|
||||
return file_search.result
|
||||
}
|
||||
return
|
||||
return undefined
|
||||
}
|
||||
return trait
|
||||
|
||||
function entry_collection<E extends Entry<E>>(inner: Files<E>): EntryCollection<E> {
|
||||
const collection = { inner: inner } as EntryCollection<E>
|
||||
collection.sort = function() { return sort(this) }
|
||||
collection.push = function(entry) { return push(this, entry) }
|
||||
collection.get = function(file_name) { return get(this, file_name) }
|
||||
collection.pop = function(file_name) { return pop(this, file_name) }
|
||||
return collection
|
||||
}
|
||||
|
||||
export {
|
||||
entry_trait,
|
||||
entry_collection,
|
||||
type EntryCollection,
|
||||
type Files,
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
import { type Entry } from "./main"
|
||||
import { type Files } from "./collection"
|
||||
|
||||
interface SearchResult<T> {
|
||||
interface Wrap<T extends Entry<T>> {
|
||||
readonly result: T,
|
||||
readonly index: number
|
||||
}
|
||||
function wrap_result<T>(result: T, index: number): SearchResult<T> {
|
||||
function wrap_result<T extends Entry<T>>(result: T, index: number): Wrap<T> {
|
||||
return { result: result, index: index }
|
||||
}
|
||||
|
||||
export default function entry_search<T>(cloned_file_collection: Files<T>, file_name: string): SearchResult<Entry<T>> | undefined {
|
||||
export default function entry_search<T extends Entry<T>>(cloned_file_collection: Files<T>, file_name: string): Wrap<Entry<T>> | undefined {
|
||||
let start = 0
|
||||
let end = cloned_file_collection.length-1
|
||||
while (start<=end) {
|
||||
|
Reference in New Issue
Block a user