idk oh well
This commit is contained in:
10 files changed
+263
-72
No files matched your search
+12
-5
@@ -9,6 +9,7 @@ export type Repeated<
|
||||
R extends readonly T[] = [],
|
||||
> = R['length'] extends N ? R : Repeated<T, N, readonly [T, ...R]>
|
||||
|
||||
// deno-lint-ignore ban-types
|
||||
export type Prettify<T> = { [K in keyof T]: T[K] } & {}
|
||||
|
||||
/**
|
||||
@@ -22,7 +23,8 @@ export function* fill<T>(value = 0 as unknown as T, length = Infinity): Generato
|
||||
}
|
||||
|
||||
/**
|
||||
* A ganerator that yields numbers between a range, akin to Python's {@link https://docs.python.org/3.11/library/stdtypes.html#ranges range}.
|
||||
* A ganerator that yields numbers between a range, akin to Python's
|
||||
* {@link https://docs.python.org/3.11/library/stdtypes.html#ranges range}.
|
||||
* If `start` is greater than `end`, the range will decrement accordingly
|
||||
*
|
||||
* @example
|
||||
@@ -43,7 +45,8 @@ export function range(start: number, end: number, step: number): Generator<numbe
|
||||
export function* range(a: number, b?: number, c?: number) {
|
||||
const [start, end, step] = (
|
||||
typeof b === "undefined" ? [0, a, 1] :
|
||||
typeof c === "undefined" ? [a, b, 1] : [a, b, Math.abs(c)]
|
||||
typeof c === "undefined" ? [a, b, 1]
|
||||
: [a, b, Math.abs(c)]
|
||||
)
|
||||
|
||||
const positive = start < end
|
||||
@@ -106,12 +109,16 @@ export function removeFromArraybyIndexes<T>(
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides an array into two by a given predicate function. Those that pass are put into the first, the rest are put in the second.
|
||||
*
|
||||
* Divides an array into two by a given predicate function.
|
||||
* Those that pass are put into the first, the rest are put in the second.
|
||||
*
|
||||
* @author MindfulMinun
|
||||
* @since 2022-06-28
|
||||
*/
|
||||
export function divide<T>(list: T[], predicate: (this: typeof list, value: T, index: number, array: typeof list) => boolean): [T[], T[]] {
|
||||
export function divide<T>(
|
||||
list: T[],
|
||||
predicate: (this: typeof list, value: T, index: number, array: typeof list) => boolean
|
||||
): [T[], T[]] {
|
||||
const pass: T[] = []
|
||||
const fail: T[] = []
|
||||
|
||||
|
||||
Regular → Executable
+51
-12
@@ -1,15 +1,54 @@
|
||||
export interface ShellSpawnerOpts extends Omit<Deno.CommandOptions, 'args'> {
|
||||
shell: string
|
||||
shellArgs: (commandStr: string) => string[]
|
||||
autospawn: boolean,
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a shell command that can be aborted using an `AbortSignal`
|
||||
* @since 2021-08-13
|
||||
* Shell spawner.
|
||||
* ```
|
||||
* const $: ShellSpawner = koopa()
|
||||
* await $`rm -rf /home/*`
|
||||
* ```
|
||||
*/
|
||||
export async function abortableExec(opts: { cmd: string[], signal?: AbortSignal }): Promise<void> {
|
||||
const { cmd, signal } = opts
|
||||
if (signal && signal.aborted) return
|
||||
const p = Deno.run({
|
||||
cmd,
|
||||
stdout: "null"
|
||||
})
|
||||
signal && signal.addEventListener('abort', () => p.kill("SIGINT"))
|
||||
await p.status()
|
||||
return
|
||||
export type ShellSpawner<R = Deno.Command> = (a: string | TemplateStringsArray, ...values: unknown[]) => R
|
||||
|
||||
/**
|
||||
* Run a shell script.
|
||||
* Factory facade to easily interact and run shell scripts
|
||||
* @since 2023-11-25
|
||||
*/
|
||||
export default function koopa(opts: Partial<ShellSpawnerOpts> = {}) {
|
||||
const actualOpts: ShellSpawnerOpts = {
|
||||
shell: 'sh',
|
||||
shellArgs: cmd => ['-c', cmd],
|
||||
autospawn: true,
|
||||
...opts,
|
||||
}
|
||||
|
||||
const factory: ShellSpawner = function (
|
||||
templ: TemplateStringsArray | string,
|
||||
...values: unknown[]
|
||||
) {
|
||||
// TODO: Implement actual escaping
|
||||
const escape = (a: string) => a
|
||||
let execStr = ''
|
||||
if (typeof templ === 'string') {
|
||||
execStr += templ
|
||||
} else {
|
||||
for (let i = 0; i < templ.length; i++) {
|
||||
execStr += templ[i]
|
||||
execStr += typeof values[i] !== 'undefined'
|
||||
? escape('' + values[i])
|
||||
: ''
|
||||
}
|
||||
}
|
||||
|
||||
return new Deno.Command(actualOpts.shell, {
|
||||
args: actualOpts.shellArgs(execStr),
|
||||
...opts
|
||||
})
|
||||
}
|
||||
|
||||
return factory
|
||||
}
|
||||
+39
-6
@@ -21,7 +21,7 @@ export function dedent(
|
||||
let strings = Array.from(typeof templ === 'string' ? [templ] : templ)
|
||||
|
||||
// 1. Remove trailing whitespace.
|
||||
strings[strings.length - 1] = strings[strings.length - 1].trimRight()
|
||||
strings[strings.length - 1] = strings[strings.length - 1].trimEnd()
|
||||
|
||||
// 2. Find all line breaks to determine the highest common indentation level.
|
||||
const indentLengths = strings.reduce((arr, str) => {
|
||||
@@ -82,15 +82,48 @@ export function templateNoop(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes HTML on the server-side.
|
||||
*
|
||||
* @remarks Avoid calling this method twice on the same string.
|
||||
*
|
||||
* @author MindfulMinun
|
||||
* @since 2022-05-20
|
||||
*/
|
||||
export function html(
|
||||
templ: TemplateStringsArray | string,
|
||||
...values: unknown[]
|
||||
): string {
|
||||
if (typeof templ === 'string') return xss(templ)
|
||||
return templ.reduce((result, currentString, i) =>
|
||||
result + currentString + (typeof values[i] !== 'undefined' ? xss('' + values[i]) : ''),
|
||||
'' // Start with the empty string
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes unsafe strings for use in HTML
|
||||
*
|
||||
* ```js
|
||||
* xss(`<img src="/bogus/path" onerror="alert('xss')">`)
|
||||
* ```
|
||||
* @remarks Avoid calling this method twice on the same string.
|
||||
*
|
||||
* @example
|
||||
* xss(`<img src=":(" onerror="alert('xss')">`)
|
||||
* // -> `<img src=":(" onerror="alert('xss')">`
|
||||
*
|
||||
* const unsafe = `Bobby Tables <img src=":(" onerror="alert('xss')">`
|
||||
* xss`<p>My name is <strong>${unsafe}</strong></p>`
|
||||
* // -> `<p>My name is <strong>Bobby Tables <img src=":(" onerror="alert('xss')"></strong></p>`
|
||||
* @author MindfulMinun
|
||||
* @since 2020-06-23
|
||||
*/
|
||||
export function xss(unsafe: string): string {
|
||||
return unsafe.replace(/[&<>"'\/]/g, key => HTML_ESCAPES[key as keyof typeof HTML_ESCAPES])
|
||||
export function xss(unsafe: string): string
|
||||
export function xss(templ: TemplateStringsArray, ...values: unknown[]): string
|
||||
export function xss(templ: string | TemplateStringsArray, ...values: unknown[]): string {
|
||||
if (typeof templ === 'string') {
|
||||
return templ.replace(/[&<>"'\/]/g, key => HTML_ESCAPES[key as keyof typeof HTML_ESCAPES])
|
||||
}
|
||||
return templ.reduce((result, currentString, i) =>
|
||||
result + currentString + (typeof values[i] !== 'undefined' ? xss('' + values[i]) : ''),
|
||||
'' // Start with the empty string
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user