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> readonly inner: Files<T>
} }
function entry_trait<T extends Entry<T>>() { function sort<E extends Entry<E>>(self: EntryCollection<E>) {
const trait = {} as EntryCollection<T> self.inner.sort((a,z) => a.name.localeCompare(z.name))
trait.sort = function() {
this.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) { if (!no_duplicates) {
this.push(entry) self.push(entry)
this.sort() self.sort()
return true return true
} }
return false 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) { if (file_search) {
this.inner.splice(file_search.index, 1) self.inner.splice(file_search.index, 1)
return file_search.result 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 { export {
entry_trait, entry_collection,
type EntryCollection, type EntryCollection,
type Files, type Files,
} }

View File

@ -1,15 +1,15 @@
import { type Entry } from "./main" import { type Entry } from "./main"
import { type Files } from "./collection" import { type Files } from "./collection"
interface SearchResult<T> { interface Wrap<T extends Entry<T>> {
readonly result: T, readonly result: T,
readonly index: number 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 } 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 start = 0
let end = cloned_file_collection.length-1 let end = cloned_file_collection.length-1
while (start<=end) { while (start<=end) {