type errors

This commit is contained in:
2025-03-04 20:29:21 -05:00
parent c994698e2d
commit c2ac2ba28c
2 changed files with 38 additions and 26 deletions

View File

@ -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))
}
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) {
self.push(entry)
self.sort()
return true
}
trait.push = function(entry) {
const no_duplicates = entry_search(this.inner, entry.name)
if (!no_duplicates) {
this.push(entry)
this.sort()
return true
}
return false
return false
}
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) {
self.inner.splice(file_search.index, 1)
return file_search.result
}
trait.pop = function(file_name) {
const file_search = entry_search(this.inner, file_name)
if (file_search) {
this.inner.splice(file_search.index, 1)
return file_search.result
}
return
}
return trait
return undefined
}
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,
}

View File

@ -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) {
@ -25,4 +25,4 @@ export default function entry_search<T>(cloned_file_collection: Files<T>, file_n
}
}
return undefined
}
}