JSR?
This commit is contained in:
25 files changed
+186
-94
No files matched your search
+4
-4
@@ -46,7 +46,7 @@ export class EventStream<T> implements AsyncIterable<T> {
|
||||
this.#promise = new Promise(r => this.#resolve = r)
|
||||
}
|
||||
|
||||
async*[Symbol.asyncIterator]() {
|
||||
async*[Symbol.asyncIterator](): AsyncGenerator<Awaited<T>, void, undefined> {
|
||||
while (!this.#done) {
|
||||
await this.#promise
|
||||
yield* this.#events
|
||||
@@ -74,7 +74,7 @@ export class EventStream<T> implements AsyncIterable<T> {
|
||||
* const click = stream.once(ev => ev.type === 'click')
|
||||
* @since 2021-12-22
|
||||
*/
|
||||
once(predicate: (value: T) => boolean) {
|
||||
once(predicate: (value: T) => boolean): Promise<Awaited<T> | undefined> {
|
||||
return once(this, predicate)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ export class EventStream<T> implements AsyncIterable<T> {
|
||||
* @author MindfulMinun
|
||||
* @since 2024-06-02
|
||||
*/
|
||||
each(callback: (value: T) => void) {
|
||||
each(callback: (value: T) => void): Promise<void> {
|
||||
return each(this, callback)
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ export class EventStream<T> implements AsyncIterable<T> {
|
||||
* const click = once(stream, ev => ev.type === 'click')
|
||||
* @since 2021-12-22
|
||||
*/
|
||||
export async function once<T>(source: AsyncIterable<T>, predicate: (value: T) => boolean) {
|
||||
export async function once<T>(source: AsyncIterable<T>, predicate: (value: T) => boolean): Promise<T | undefined> {
|
||||
for await (const value of source) {
|
||||
if (predicate(value)) return value
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
/// <reference lib="dom" />
|
||||
// <reference lib="dom" />
|
||||
import { templateNoop, HTML_ESCAPES, xss } from "./string.ts"
|
||||
|
||||
export { HTML_ESCAPES }
|
||||
|
||||
+5
-2
@@ -61,7 +61,7 @@ export function* range(a: number, b?: number, c?: number) {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-07
|
||||
*/
|
||||
export function* limit<T>(count: number, iterable: Iterable<T>) {
|
||||
export function* limit<T>(count: number, iterable: Iterable<T>): Generator<T, void, unknown> {
|
||||
let i = 0
|
||||
for (const value of iterable) {
|
||||
if (i < count) {
|
||||
@@ -77,7 +77,10 @@ export function* limit<T>(count: number, iterable: Iterable<T>) {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-07
|
||||
*/
|
||||
export async function* limitAsync<T>(count: number, iterable: AsyncIterable<T>) {
|
||||
export async function* limitAsync<T>(
|
||||
count: number,
|
||||
iterable: AsyncIterable<T>
|
||||
): AsyncGenerator<Awaited<T>, void, unknown> {
|
||||
let i = 0
|
||||
for await (const value of iterable) {
|
||||
if (i < count) {
|
||||
|
||||
+5
-7
@@ -3,10 +3,8 @@
|
||||
* @author MindfulMinun <https://benjic.xyz>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
console.error("Hey there, past me! Please don't import `mod.ts`, instead import specific files!")
|
||||
|
||||
// export * from "./array.ts"
|
||||
// export * from "./helpers.ts"
|
||||
// export * from "./math.ts"
|
||||
// export * from "./dom.ts"
|
||||
export * as async from './async.ts'
|
||||
export * as dom from './dom.ts'
|
||||
export * as rng from './rng.ts'
|
||||
export * as shell from './shell.ts'
|
||||
export * as string from './string.ts'
|
||||
+9
-9
@@ -24,7 +24,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-16
|
||||
*/
|
||||
*[Symbol.iterator]() {
|
||||
*[Symbol.iterator](): Generator<number, void, unknown> {
|
||||
while (true) yield this.float()
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-16
|
||||
*/
|
||||
float() {
|
||||
float(): number {
|
||||
return this.nextValue()
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-16
|
||||
*/
|
||||
choose<T>(arr: T[]) {
|
||||
choose<T>(arr: T[]): T {
|
||||
return arr[Math.floor(this.float() * arr.length)]
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-16
|
||||
*/
|
||||
shuffle<T>(arr: T[]) {
|
||||
shuffle<T>(arr: T[]): T[] {
|
||||
let i, j
|
||||
for (i = arr.length - 1; i > 0; i--) {
|
||||
j = Math.floor(this.float() * (i + 1))
|
||||
@@ -89,7 +89,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @author MindfulMinun
|
||||
* @since 2022-07-16
|
||||
*/
|
||||
chooseWithWeights<T>(items: T[], weights: number[]) {
|
||||
chooseWithWeights<T>(items: T[], weights: number[]): T {
|
||||
if (items.length != weights.length) throw Error(`Expected items and weights to have the same number of elements, but items has ${items.length} elements, and weights has ${weights.length}.`)
|
||||
|
||||
const sumOfWeights = weights.reduce((acc, v) => acc + v)
|
||||
@@ -113,7 +113,7 @@ export abstract class RNG implements Iterable<number> {
|
||||
* @since 2022-07-08
|
||||
*/
|
||||
export class Random extends RNG {
|
||||
nextValue() { return Math.random() }
|
||||
nextValue(): number { return Math.random() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,18 +125,18 @@ export class Mulberry32 extends RNG {
|
||||
#seed!: number
|
||||
originalSeed!: number
|
||||
|
||||
constructor(seed = Date.now()) {
|
||||
constructor(seed: number = Date.now()) {
|
||||
super()
|
||||
this.resetSeed(seed)
|
||||
}
|
||||
|
||||
/** Resets the seed */
|
||||
resetSeed(seed = this.#seed) {
|
||||
resetSeed(seed: number = this.#seed) {
|
||||
this.originalSeed = seed
|
||||
this.#seed = seed
|
||||
}
|
||||
|
||||
nextValue() {
|
||||
nextValue(): number {
|
||||
// Mulberry32
|
||||
let a = this.#seed
|
||||
a |= 0
|
||||
|
||||
+9
-9
@@ -38,24 +38,24 @@ export class Troopa implements PromiseLike<Deno.CommandOutput> {
|
||||
* @author MindfulMinun
|
||||
* @since 2023-12-26
|
||||
*/
|
||||
start() { return this.process }
|
||||
start(): Deno.ChildProcess { return this.process }
|
||||
|
||||
get process() {
|
||||
get process(): Deno.ChildProcess {
|
||||
if (this.#process) return this.#process
|
||||
this.#process = this.cmd.spawn()
|
||||
return this.#process
|
||||
}
|
||||
|
||||
get stdin() { return this.process.stdin }
|
||||
get stdout() { return this.process.stdout }
|
||||
get stderr() { return this.process.stderr }
|
||||
get stdin(): WritableStream<Uint8Array> { return this.process.stdin }
|
||||
get stdout(): ReadableStream<Uint8Array> { return this.process.stdout }
|
||||
get stderr(): ReadableStream<Uint8Array> { return this.process.stderr }
|
||||
|
||||
response(src: 'stdout' | 'stderr' = 'stdout') {
|
||||
response(src: 'stdout' | 'stderr' = 'stdout'): Response {
|
||||
const source = this.process[src]
|
||||
return new Response(source)
|
||||
}
|
||||
|
||||
async code() {
|
||||
async code(): Promise<number> {
|
||||
return (await this).code
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ export class Troopa implements PromiseLike<Deno.CommandOutput> {
|
||||
return this
|
||||
}
|
||||
|
||||
async close() {
|
||||
async close(): Promise<this> {
|
||||
await this.writer.close()
|
||||
return this
|
||||
}
|
||||
@@ -119,7 +119,7 @@ export class Troopa implements PromiseLike<Deno.CommandOutput> {
|
||||
* Factory facade to easily interact and run shell scripts
|
||||
* @since 2023-11-25
|
||||
*/
|
||||
export default function koopa(opts: Partial<ShellSpawnerOpts> = {}) {
|
||||
export default function koopa(opts: Partial<ShellSpawnerOpts> = {}): ShellSpawner<Troopa> {
|
||||
const actualOpts: ShellSpawnerOpts = {
|
||||
shell: 'bash',
|
||||
shellArgs: cmd => ['-c', `${opts.prefix || 'set -euo pipefail'}; ${cmd}`],
|
||||
|
||||
Reference in new issue
Block a user