Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More than types #6

Merged
merged 6 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
publish:
name: "Publish"
timeout-minutes: 60
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: "Checkout source code"
uses: "actions/checkout@v3"
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
"transactly",
"typedly",
"uply"
]
],
"typescript.tsdk": "node_modules/typescript/lib"
}
14 changes: 14 additions & 0 deletions Array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { typedly } from "./index"

describe("Array", () => {
it("Union", () => {
type UnionArray = typedly.Array.UnionValues<"foo" | "bar" | "baz">
const array: UnionArray = ["foo", "bar", "baz"]
expect(array).toEqual(["foo", "bar", "baz"])
})
it("OmitFirst", () => {
const array = [1, 2, 3] as const
const result: typedly.Array.OmitFirst<typeof array> = [2, 3]
expect(result).toEqual(array.slice(1))
})
})
14 changes: 14 additions & 0 deletions Array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type TypedlyArray<T> = T[]
export namespace TypedlyArray {
// https://catchts.com/union-array
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
type UnionToOverloads<U> = UnionToIntersection<U extends any ? (f: U) => void : never>
type PopUnion<U> = UnionToOverloads<U> extends (a: infer A) => void ? A : never
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true
type BuildUnion<T, A extends unknown[] = []> = IsUnion<T> extends true
? BuildUnion<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]>
: [T, ...A]
export type UnionValues<T> = BuildUnion<T>
// https://stackoverflow.com/questions/65532007/how-do-i-omit-an-element-from-an-array-type-such-as-parameterst
export type OmitFirst<T extends readonly any[]> = T extends readonly [any, ...infer R] ? R : never
}
142 changes: 142 additions & 0 deletions Collection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { typedly } from "./index"

describe("Collection", () => {
const backend: readonly number[] = [0, 1, 2, 2]
class Collection<T> extends typedly.Collection<T> {
constructor(readonly backend: T[]) {
super()
}
entries(): IterableIterator<[number, T]> {
return this.backend.entries()
}
}
const collection = new Collection<number>([...backend])
it("toString", () => {
expect(collection.toString()).toEqual(backend.toString())
})
it("toLocaleString", () => {
expect(collection.toLocaleString()).toEqual(backend.toLocaleString())
})
it("concat", () => {
const argument = [[4]] as const
expect(collection.concat(...argument)).toEqual(backend.concat(...argument))
})
it("join", () => {
const argument = [", "] as const
expect(collection.join(...argument)).toEqual(backend.join(...argument))
})
it("slice", () => {
const argument = [1, 1] as const
expect(collection.slice(...argument)).toEqual(backend.slice(...argument))
})
it("indexOf", () => {
const argument = [2, 1] as const
expect(collection.indexOf(...argument)).toEqual(backend.indexOf(...argument))
})
it("lastIndexOf", () => {
const argument = [2, 1] as const
expect(collection.lastIndexOf(...argument)).toEqual(backend.lastIndexOf(...argument))
})
it("includes", () => {
const argument = [1] as const
expect(collection.includes(...argument)).toEqual(backend.includes(...argument))
})
it("every", () => {
const argument = [(value: number, index: number): boolean => typeof value == "number" && index <= value] as const
expect(collection.every(...argument)).toEqual(backend.every(...argument))
})
it("some", () => {
const argument = [(value: number, index: number, array: readonly number[]): boolean => value < index] as const
expect(collection.some(...argument)).toEqual(backend.some(...argument))
})
it("map", () => {
const argument = [
(value: number, index: number, array: readonly number[]): number => value + index + array.length,
] as const
expect(collection.map(...argument)).toEqual(backend.map(...argument))
})
it("filter", () => {
const argument = [(value: number, index: number): boolean => value == index] as const
expect(collection.filter(...argument)).toEqual(backend.filter(...argument))
})
it("reduce", () => {
const argument = [
(result: number, value: number, index: number, array: readonly number[]): number =>
result + value + index + array.length,
0,
] as const
expect(collection.reduce(...argument)).toEqual(backend.reduce(...argument))
})
it("reduceRight", () => {
const argument = [
(result: number, value: number, index: number, array: readonly number[]): number =>
result + value + index + array.length,
0,
] as const
expect(collection.reduceRight(...argument)).toEqual(backend.reduceRight(...argument))
})
it("find", () => {
const argument = [(value: number, index: number) => value < index] as const
expect(collection.find(...argument)).toEqual(backend.find(...argument))
})
it("findIndex", () => {
const argument = [(value: number, index: number) => value !== index] as const
expect(collection.findIndex(...argument)).toEqual(backend.findIndex(...argument))
})
it("entries", () => {
const argument = [] as const
expect(collection.entries(...argument)).toEqual(backend.entries(...argument))
})
it("keys", () => {
const argument = [] as const
expect([...collection.keys(...argument)]).toEqual([...backend.keys(...argument)])
})
it("values", () => {
const argument = [] as const
expect([...collection.values(...argument)]).toEqual([...backend.values(...argument)])
})
it("flatMap", () => {
const argument = [(value: number) => [value]] as const
expect(collection.flatMap(...argument)).toEqual(backend.flatMap(...argument))
})
it("flat", () => {
const argument = [2] as const
const backend = [[[1, 2, 3]], [[4, 5, 6]]]
expect(new Collection(backend).flat(...argument)).toEqual(backend.flat(...argument))
})
it("at", () => {
const argument = [-1] as const
expect(collection.at(...argument)).toEqual(backend.at(...argument))
})
it("findLast", () => {
const argument = [(value: number, index: number): boolean => value !== index] as const
expect(collection.findLast(...argument)).toEqual(backend.findLast(...argument))
})
it("findLastIndex", () => {
const argument = [(value: number, index: number): boolean => value !== index] as const
expect(collection.findLastIndex(...argument)).toEqual(backend.findLastIndex(...argument))
})
it("toReversed", () => {
const argument = [] as const
expect(collection.toReversed(...argument)).toEqual(backend.toReversed(...argument))
})
it("toSorted", () => {
const argument = [] as const
expect(collection.toSorted(...argument)).toEqual(backend.toSorted(...argument))
})
it("toSpliced", () => {
const argument = [1] as const
expect(collection.toSpliced(...argument)).toEqual(backend.toSpliced(...argument))
})
it("with", () => {
const argument = [0, 10] as const
expect(collection.with(...argument)).toEqual(backend.with(...argument))
})
it("toArray", () => {
const argument = [] as const
expect(collection.toArray(...argument)).toEqual(backend)
})
it("toJson", () => {
expect(JSON.stringify(collection)).toEqual(JSON.stringify(backend))
})
})
173 changes: 173 additions & 0 deletions Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
export abstract class Collection<T> {
toString(): string {
return this.toArray().toString()
}
toLocaleString(): string {
return this.toArray().toLocaleString()
}
concat(...items: ConcatArray<T>[] | (T | ConcatArray<T>)[]): T[] {
return this.toArray().concat(...items)
}
join(separator?: string): string {
return this.toArray().join(separator)
}
slice(start?: number, end?: number): T[] {
return this.toArray().slice(start, end)
}
indexOf(needle: T, start?: number): number {
return this.toArray().indexOf(needle, start)
}
lastIndexOf(needle: T, start?: number): number {
return this.toArray().lastIndexOf(needle, start)
}
includes(needle: T, start?: number): boolean {
return this.toArray().includes(needle, start)
}
every<S extends T>(
predicate: (value: T, index: number, array: readonly T[]) => value is S,
thisArgument?: any
): this is readonly S[]
every(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): boolean
every(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): boolean {
return this.toArray().every(predicate, thisArgument)
}
some(predicate: (value: T, index: number, array: readonly T[]) => unknown, thisArgument?: any): boolean {
return this.toArray().some(predicate, thisArgument)
}
forEach(callback: (value: T, index: number, array: readonly T[]) => void, thisArgument?: any): void {
this.toArray().forEach(callback, thisArgument)
}
map<U>(mapping: (value: T, index: number, array: readonly T[]) => U, thisArgument?: any): U[] {
return this.toArray().map(mapping, thisArgument)
}
filter<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArgument?: any): S[]
filter(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): T[]
filter(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): T[] {
return this.toArray().filter(predicate, thisArgument)
}
reduce(reducer: (last: T, current: T, index: number, array: readonly T[]) => T): T
reduce(reducer: (last: T, current: T, index: number, array: readonly T[]) => T, initial: T): T
reduce<U>(reducer: (last: U, current: T, index: number, array: readonly T[]) => U, initial: U): U
reduce<U>(
...argument:
| [(last: U, current: T, index: number, array: readonly T[]) => U, U]
| [(last: T, current: T, index: number, array: readonly T[]) => T]
): U | T {
return argument.length == 1 ? this.toArray().reduce(...argument) : this.toArray().reduce(...argument)
}
reduceRight(reducer: (last: T, current: T, index: number, array: readonly T[]) => T): T
reduceRight(reducer: (last: T, current: T, index: number, array: readonly T[]) => T, initial: T): T
reduceRight<U>(reducer: (last: U, current: T, index: number, array: readonly T[]) => U, initial: U): U
reduceRight<U>(
...argument:
| [(last: U, current: T, index: number, array: readonly T[]) => U, U]
| [(last: T, current: T, index: number, array: readonly T[]) => T]
): U | T {
return argument.length == 1 ? this.toArray().reduceRight(...argument) : this.toArray().reduce(...argument)
}
find<S extends T>(
predicate: (value: T, index: number, obj: readonly T[]) => value is S,
thisArgument?: any
): S | undefined
find(predicate: (value: T, index: number, obj: readonly T[]) => unknown, thisArgument?: any): T | undefined
find(predicate: (value: T, index: number, obj: readonly T[]) => unknown, thisArgument?: any): T | undefined {
return this.toArray().find(predicate, thisArgument)
}
findIndex(predicate: (value: T, index: number, obj: readonly T[]) => unknown, thisArgument?: any): number {
return this.toArray().findIndex(predicate, thisArgument)
}
abstract entries(): IterableIterator<[number, T]>
*keys(): IterableIterator<number> {
for (const [key] of this.entries())
yield key
}
*values(): IterableIterator<T> {
for (const [, value] of this.entries())
yield value
}
[Symbol.iterator](): IterableIterator<T> {
return this.values()
}
flatMap<U, This = undefined>(
mapping: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
thisArgument?: This | undefined
): U[] {
return this.toArray().flatMap(mapping, thisArgument)
}
flat<D extends number = 1>(depth?: D | undefined): FlatArray<readonly T[], D>[] {
return this.toArray().flat(depth)
}
at(index: number): T | undefined {
return this.toArray().at(index)
}
findLast<S extends T>(
predicate: (value: T, index: number, array: readonly T[]) => value is S,
thisArgument?: any
): S | undefined
findLast(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): T | undefined
findLast(predicate: (value: T, index: number, array: readonly T[]) => boolean, thisArgument?: any): T | undefined {
return this.toArray().findLast(predicate, thisArgument)
}
findLastIndex(predicate: (value: T, index: number, array: readonly T[]) => unknown, thisArgument?: any): number {
return this.toArray().findLastIndex(predicate, thisArgument)
}
toReversed(): T[] {
return this.toArray().toReversed()
}
toSorted(comparer?: ((left: T, right: T) => number) | undefined): T[] {
return this.toArray().toSorted(comparer)
}
toSpliced(start: number, remove: number, ...items: T[]): T[]
toSpliced(start: number, remove?: number): T[]
toSpliced(start: number): T[]
toSpliced(...argument: [number, number, ...T[]] | [number, number?]): T[] {
return argument.length == 1 || argument[1] == undefined
? this.toArray().toSpliced(argument[0])
: this.toArray().toSpliced(argument[0], argument[1], ...(([, , ...tail]) => tail)(argument))
}
with(index: number, value: T): T[] {
return this.toArray().with(index, value)
}
[Symbol.unscopables]: {
readonly [x: number]: boolean | undefined
readonly length?: boolean | undefined
toString?: boolean | undefined
toLocaleString?: boolean | undefined
concat?: boolean | undefined
join?: boolean | undefined
slice?: boolean | undefined
indexOf?: boolean | undefined
lastIndexOf?: boolean | undefined
every?: boolean | undefined
some?: boolean | undefined
forEach?: boolean | undefined
map?: boolean | undefined
filter?: boolean | undefined
reduce?: boolean | undefined
reduceRight?: boolean | undefined
find?: boolean | undefined
findIndex?: boolean | undefined
entries?: boolean | undefined
keys?: boolean | undefined
values?: boolean | undefined
includes?: boolean | undefined
flatMap?: boolean | undefined
flat?: boolean | undefined
at?: boolean | undefined
findLast?: boolean | undefined
findLastIndex?: boolean | undefined
toReversed?: boolean | undefined
toSorted?: boolean | undefined
toSpliced?: boolean | undefined
with?: boolean | undefined
[Symbol.iterator]?: boolean | undefined
readonly [Symbol.unscopables]?: boolean | undefined
}
toArray(): T[] {
return [...this]
}
toJSON(): T[] {
return this.toArray()
}
}
export namespace Collection {}
Loading
Loading