Put everything in src directories
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
export type Deferred<T> = Promise<T> & {
|
||||
resolve: (arg: T) => void
|
||||
reject: (arg: T) => void
|
||||
}
|
||||
|
||||
export const defer = <T>(): Deferred<T> => {
|
||||
let resolve, reject
|
||||
const p = new Promise((resolve_, reject_) => {
|
||||
resolve = resolve_
|
||||
reject = reject_
|
||||
})
|
||||
|
||||
return (Object.assign(p, {resolve, reject}) as unknown) as Deferred<T>
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import {EventEmitter} from 'events'
|
||||
|
||||
export class Emitter extends EventEmitter {
|
||||
emit(type: string | number, ...args: any[]) {
|
||||
const a = super.emit(type, ...args)
|
||||
const b = super.emit('*', type, ...args)
|
||||
|
||||
return a && b
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import {last} from './Tools'
|
||||
|
||||
export class Fluent<T> {
|
||||
constructor(readonly xs: T[]) {}
|
||||
|
||||
static create() {
|
||||
return this.from([])
|
||||
}
|
||||
|
||||
static from<T>(xs: Iterable<T>) {
|
||||
return new Fluent<T>(Array.from(xs))
|
||||
}
|
||||
|
||||
clone<K extends Fluent<T>>(this: K, xs: T[]): K {
|
||||
return new (this.constructor as { new (xs: T[]): K })(xs)
|
||||
}
|
||||
|
||||
valueOf = () => this.xs
|
||||
|
||||
first = () => this.xs[0]
|
||||
|
||||
nth = (i: number) => this.xs[i]
|
||||
|
||||
last = () => last(this.xs)
|
||||
|
||||
count = () => this.xs.length
|
||||
|
||||
exists = () => this.xs.length > 0
|
||||
|
||||
has = (v: T) => this.xs.includes(v)
|
||||
|
||||
every = (f: (t: T) => boolean) => this.xs.every(f)
|
||||
|
||||
some = (f: (t: T) => boolean) => this.xs.some(f)
|
||||
|
||||
find = (f: (t: T) => boolean) => this.xs.find(f)
|
||||
|
||||
uniq = () => this.clone(Array.from(new Set(this.xs)))
|
||||
|
||||
slice = (a: number, b?: number) => this.clone(this.xs.slice(a, b))
|
||||
|
||||
take = (n: number) => this.slice(0, n)
|
||||
|
||||
drop = (n: number) => this.slice(n)
|
||||
|
||||
filter = (f: (t: T) => boolean) => this.clone(this.xs.filter(f))
|
||||
|
||||
reject = (f: (t: T) => boolean) => this.clone(this.xs.filter(t => !f(t)))
|
||||
|
||||
keep = (xs: T[]) => this.filter(x => xs.includes(x))
|
||||
|
||||
without = (xs: T[]) => this.reject(x => xs.includes(x))
|
||||
|
||||
map = (f: (t: T) => T) => this.clone(this.xs.map(f))
|
||||
|
||||
mapTo = <U>(f: (t: T) => U) => Fluent.from(this.xs.map(f))
|
||||
|
||||
flatMap = <U>(f: (t: T) => U[]) => Fluent.from(this.xs.flatMap(f))
|
||||
|
||||
forEach = (f: (t: T, i: number) => void) => this.xs.forEach(f)
|
||||
|
||||
join = (s: string) => this.valueOf().join(s)
|
||||
|
||||
set = (i: number, x: T) => this.clone([...this.xs.slice(0, i), x, ...this.xs.slice(i + 1)])
|
||||
|
||||
concat = (xs: T[]) => this.clone(this.xs.concat(xs))
|
||||
|
||||
append = (x: T) => this.concat([x])
|
||||
|
||||
prepend = (x: T) => this.clone([x].concat(this.xs))
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
export class LRUCache<T, U> {
|
||||
map = new Map<T, U>()
|
||||
keys: T[] = []
|
||||
|
||||
constructor(readonly maxSize: number = Infinity) {}
|
||||
|
||||
has(k: T) {
|
||||
return this.map.has(k)
|
||||
}
|
||||
|
||||
get(k: T) {
|
||||
const v = this.map.get(k)
|
||||
|
||||
if (v !== undefined) {
|
||||
this.keys.push(k as T)
|
||||
|
||||
if (this.keys.length > this.maxSize * 2) {
|
||||
this.keys.splice(-this.maxSize)
|
||||
}
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
set(k: T, v: U) {
|
||||
this.map.set(k, v)
|
||||
this.keys.push(k)
|
||||
|
||||
if (this.map.size > this.maxSize) {
|
||||
this.map.delete(this.keys.shift() as T)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function cached<T, V, Args extends any[]>({
|
||||
maxSize,
|
||||
getKey,
|
||||
getValue,
|
||||
}: {
|
||||
maxSize: number
|
||||
getKey: (args: Args) => T
|
||||
getValue: (args: Args) => V
|
||||
}) {
|
||||
const cache = new LRUCache<T, V>(maxSize)
|
||||
|
||||
const get = (...args: Args) => {
|
||||
const k = getKey(args)
|
||||
|
||||
let v = cache.get(k)
|
||||
|
||||
if (!v) {
|
||||
v = getValue(args)
|
||||
|
||||
cache.set(k, v)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
get.cache = cache
|
||||
get.getKey = getKey
|
||||
get.getValue = getValue
|
||||
|
||||
return get
|
||||
}
|
||||
|
||||
export function simpleCache<V, Args extends any[]>(getValue: (args: Args) => V) {
|
||||
return cached({maxSize: 10**10, getKey: xs => xs.join(':'), getValue})
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
import {throttle} from "throttle-debounce"
|
||||
import {ensurePlural, identity} from "./Tools"
|
||||
|
||||
// Deprecated: use svelte's stores instead. I did all this to add a `get` convenience
|
||||
// method that was more perfomant than subscribing/unsubscribing. That turned out to be
|
||||
// a mistake, since that makes it much harder to make custom stores that don't run when
|
||||
// there are no subscribers.
|
||||
|
||||
export type Invalidator<T> = (value?: T) => void
|
||||
export type Subscriber<T> = (value: T) => void
|
||||
|
||||
type Derivable = IDerivable<any> | IDerivable<any>[]
|
||||
type Unsubscriber = () => void
|
||||
type R = Record<string, any>
|
||||
type M<T> = Map<string, T>
|
||||
|
||||
export interface ISubscribable<T> {
|
||||
subscribe(this: void, run: Subscriber<T>, invalidate?: Invalidator<T>): Unsubscriber
|
||||
}
|
||||
|
||||
export interface ISettable<T> {
|
||||
set: (xs: T) => void
|
||||
}
|
||||
|
||||
export interface IDerivable<T> extends ISubscribable<T> {
|
||||
get: () => T
|
||||
}
|
||||
|
||||
export interface IReadable<T> extends IDerivable<T> {
|
||||
derived: <U>(f: (v: T) => U) => IReadable<U>
|
||||
throttle(t: number): IReadable<T>
|
||||
}
|
||||
|
||||
export interface IWritable<T> extends IReadable<T> {
|
||||
set: (xs: T) => void
|
||||
}
|
||||
|
||||
export class Writable<T> implements IWritable<T> {
|
||||
value: T
|
||||
subs: Subscriber<T>[] = []
|
||||
|
||||
constructor(defaultValue: T, t?: number) {
|
||||
this.value = defaultValue
|
||||
|
||||
if (t) {
|
||||
this.notify = throttle(t, this.notify)
|
||||
}
|
||||
}
|
||||
|
||||
notify = () => {
|
||||
for (const sub of this.subs) {
|
||||
sub(this.value)
|
||||
}
|
||||
}
|
||||
|
||||
get() {
|
||||
return this.value
|
||||
}
|
||||
|
||||
set(newValue: T) {
|
||||
this.value = newValue
|
||||
this.notify()
|
||||
}
|
||||
|
||||
update(f: (v: T) => T) {
|
||||
this.set(f(this.value))
|
||||
}
|
||||
|
||||
subscribe(f: Subscriber<T>) {
|
||||
this.subs.push(f)
|
||||
|
||||
f(this.value)
|
||||
|
||||
return () => {
|
||||
this.subs.splice(this.subs.findIndex(x => x === f), 1)
|
||||
}
|
||||
}
|
||||
|
||||
derived<U>(f: (v: T) => U): Derived<U> {
|
||||
return new Derived<U>(this, f)
|
||||
}
|
||||
|
||||
throttle = (t: number): Derived<T> => {
|
||||
return new Derived<T>(this, identity, t)
|
||||
}
|
||||
}
|
||||
|
||||
export class Derived<T> implements IReadable<T> {
|
||||
callerSubs: Subscriber<T>[] = []
|
||||
mySubs: Unsubscriber[] = []
|
||||
stores: Derivable
|
||||
getValue: (values: any) => T
|
||||
latestValue: T | undefined
|
||||
|
||||
constructor(stores: Derivable, getValue: (values: any) => T, t = 0) {
|
||||
this.stores = stores
|
||||
this.getValue = getValue
|
||||
|
||||
if (t) {
|
||||
this.notify = throttle(t, this.notify)
|
||||
}
|
||||
}
|
||||
|
||||
notify = () => {
|
||||
this.latestValue = undefined
|
||||
this.callerSubs.forEach(f => f(this.get()))
|
||||
}
|
||||
|
||||
getInput() {
|
||||
if (Array.isArray(this.stores)) {
|
||||
return this.stores.map(s => s.get())
|
||||
} else {
|
||||
return this.stores.get()
|
||||
}
|
||||
}
|
||||
|
||||
get = (): T => {
|
||||
// Recalculate if we're not subscribed, because we won't get notified when deps change
|
||||
if (this.latestValue === undefined || this.mySubs.length === 0) {
|
||||
this.latestValue = this.getValue(this.getInput())
|
||||
}
|
||||
|
||||
return this.latestValue
|
||||
}
|
||||
|
||||
subscribe(f: Subscriber<T>) {
|
||||
if (this.callerSubs.length === 0) {
|
||||
for (const s of ensurePlural(this.stores)) {
|
||||
this.mySubs.push(s.subscribe(this.notify))
|
||||
}
|
||||
}
|
||||
|
||||
this.callerSubs.push(f)
|
||||
|
||||
f(this.get())
|
||||
|
||||
return () => {
|
||||
this.callerSubs.splice(this.callerSubs.findIndex(x => x === f), 1)
|
||||
|
||||
if (this.callerSubs.length === 0) {
|
||||
for (const unsub of this.mySubs.splice(0)) {
|
||||
unsub()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
derived<U>(f: (v: T) => U): IReadable<U> {
|
||||
return new Derived(this, f) as IReadable<U>
|
||||
}
|
||||
|
||||
throttle = (t: number): IReadable<T> => {
|
||||
return new Derived<T>(this, identity, t)
|
||||
}
|
||||
}
|
||||
|
||||
export class Key<T extends R> implements IReadable<T> {
|
||||
readonly pk: string
|
||||
readonly key: string
|
||||
base: Writable<M<T>>
|
||||
store: IReadable<T>
|
||||
|
||||
constructor(base: Writable<M<T>>, pk: string, key: string) {
|
||||
if (!(base.get() instanceof Map)) {
|
||||
throw new Error("`key` can only be used on map collections")
|
||||
}
|
||||
|
||||
this.pk = pk
|
||||
this.key = key
|
||||
this.base = base
|
||||
this.store = base.derived<T>(m => m.get(key) as T)
|
||||
}
|
||||
|
||||
get = () => this.base.get().get(this.key) as T
|
||||
|
||||
subscribe = (f: Subscriber<T>) => this.store.subscribe(f)
|
||||
|
||||
derived = <U>(f: (v: T) => U) => this.store.derived<U>(f)
|
||||
|
||||
throttle = (t: number) => this.store.throttle(t)
|
||||
|
||||
exists = () => this.base.get().has(this.key)
|
||||
|
||||
update = (f: (v: T) => T) =>
|
||||
this.base.update((m: M<T>) => {
|
||||
if (!this.key) {
|
||||
throw new Error(`Cannot set key: "${this.key}"`)
|
||||
}
|
||||
|
||||
// Make sure the pk always get set on the record
|
||||
const {pk, key} = this
|
||||
const oldValue = {...m.get(key), [pk]: key} as T
|
||||
const newValue = {...f(oldValue), [pk]: key}
|
||||
|
||||
m.set(this.key, newValue)
|
||||
|
||||
return m
|
||||
})
|
||||
|
||||
set = (v: T) => this.update(() => v)
|
||||
|
||||
merge = (d: Partial<T>) => this.update(v => ({...v, ...d}))
|
||||
|
||||
remove = () =>
|
||||
this.base.update(m => {
|
||||
m.delete(this.key)
|
||||
|
||||
return m
|
||||
})
|
||||
|
||||
pop = () => {
|
||||
const v = this.get()
|
||||
|
||||
this.remove()
|
||||
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
export class DerivedKey<T extends R> implements IReadable<T> {
|
||||
readonly pk: string
|
||||
readonly key: string
|
||||
base: IReadable<M<T>>
|
||||
store: IReadable<T>
|
||||
|
||||
constructor(base: IReadable<M<T>>, pk: string, key: string) {
|
||||
if (!(base.get() instanceof Map)) {
|
||||
throw new Error("`key` can only be used on map collections")
|
||||
}
|
||||
|
||||
this.pk = pk
|
||||
this.key = key
|
||||
this.base = base
|
||||
this.store = base.derived<T>(m => m.get(key) as T)
|
||||
}
|
||||
|
||||
get = () => this.base.get().get(this.key) as T
|
||||
|
||||
subscribe = (f: Subscriber<T>) => this.store.subscribe(f)
|
||||
|
||||
derived = <U>(f: (v: T) => U) => this.store.derived<U>(f)
|
||||
|
||||
throttle = (t: number) => this.store.throttle(t)
|
||||
|
||||
exists = () => this.base.get().has(this.key)
|
||||
}
|
||||
|
||||
export class Collection<T extends R> implements IReadable<T[]> {
|
||||
readonly pk: string
|
||||
readonly mapStore: Writable<M<T>>
|
||||
readonly listStore: IReadable<T[]>
|
||||
|
||||
constructor(pk: string, t?: number) {
|
||||
this.pk = pk
|
||||
this.mapStore = writable(new Map())
|
||||
this.listStore = this.mapStore.derived<T[]>((m: M<T>) => Array.from(m.values()))
|
||||
|
||||
if (t) {
|
||||
this.mapStore.notify = throttle(t, this.mapStore.notify)
|
||||
}
|
||||
}
|
||||
|
||||
get = () => this.listStore.get()
|
||||
|
||||
getMap = () => this.mapStore.get()
|
||||
|
||||
subscribe = (f: Subscriber<T[]>) => this.listStore.subscribe(f)
|
||||
|
||||
derived = <U>(f: (v: T[]) => U) => this.listStore.derived<U>(f)
|
||||
|
||||
throttle = (t: number) => this.listStore.throttle(t)
|
||||
|
||||
key = (k: string) => new Key(this.mapStore, this.pk, k)
|
||||
|
||||
set = (xs: T[]) => {
|
||||
const m = new Map()
|
||||
|
||||
for (const x of xs) {
|
||||
if (!x) {
|
||||
console.error("Empty value passed to collection store")
|
||||
} else if (!x[this.pk]) {
|
||||
console.error(`Value with empty ${this.pk} passed to collection store`, x)
|
||||
} else {
|
||||
m.set(x[this.pk], x)
|
||||
}
|
||||
}
|
||||
|
||||
this.mapStore.set(m)
|
||||
}
|
||||
|
||||
update = (f: (v: T[]) => T[]) => this.set(f(this.get()))
|
||||
|
||||
updateAsync = async (f: (v: T[]) => Promise<T[]>) => this.set(await f(this.get()))
|
||||
|
||||
reject = (f: (v: T) => boolean) => this.update((xs: T[]) => xs.filter(x => !f(x)))
|
||||
|
||||
filter = (f: (v: T) => boolean) => this.update((xs: T[]) => xs.filter(f))
|
||||
|
||||
map = (f: (v: T) => T) => this.update((xs: T[]) => xs.map(f))
|
||||
}
|
||||
|
||||
export class DerivedCollection<T extends R> implements IReadable<T[]> {
|
||||
readonly listStore: Derived<T[]>
|
||||
readonly mapStore: IReadable<M<T>>
|
||||
|
||||
constructor(
|
||||
readonly pk: string,
|
||||
stores: Derivable,
|
||||
getValue: (values: any) => T[],
|
||||
t = 0,
|
||||
) {
|
||||
this.listStore = new Derived(stores, getValue, t)
|
||||
this.mapStore = new Derived(this.listStore, xs => new Map(xs.map((x: T) => [x[pk], x])))
|
||||
}
|
||||
|
||||
get = () => this.listStore.get()
|
||||
|
||||
getMap = () => this.mapStore.get()
|
||||
|
||||
subscribe = (f: Subscriber<T[]>) => this.listStore.subscribe(f)
|
||||
|
||||
derived = <U>(f: (v: T[]) => U) => this.listStore.derived<U>(f)
|
||||
|
||||
throttle = (t: number) => this.listStore.throttle(t)
|
||||
|
||||
key = (k: string) => new DerivedKey(this.mapStore, this.pk, k)
|
||||
}
|
||||
|
||||
export const writable = <T>(v: T) => new Writable(v)
|
||||
|
||||
export const derived = <T>(stores: Derivable, getValue: (values: any) => T) =>
|
||||
new Derived(stores, getValue)
|
||||
|
||||
export const readable = <T>(v: T) => derived(new Writable(v), identity) as IReadable<T>
|
||||
|
||||
export const derivedCollection = <T extends R>(
|
||||
pk: string,
|
||||
stores: Derivable,
|
||||
getValue: (values: any) => T[],
|
||||
) => new DerivedCollection(pk, stores, getValue)
|
||||
|
||||
export const key = <T extends R>(base: Writable<M<T>>, pk: string, key: string) =>
|
||||
new Key<T>(base, pk, key)
|
||||
|
||||
export const collection = <T extends R>(pk: string) => new Collection<T>(pk)
|
||||
|
||||
export const asReadable = <T>(store: IDerivable<T>) => {
|
||||
return {
|
||||
...store,
|
||||
derived: <U>(f: (v: T) => U) => new Derived<U>(store, f),
|
||||
throttle: (t: number) => new Derived<T>(store, identity, t),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
import {throttle} from 'throttle-debounce'
|
||||
import {bech32, utf8} from "@scure/base"
|
||||
|
||||
// Dealing with nil
|
||||
|
||||
export type Nil = null | undefined
|
||||
|
||||
export const isNil = (x: any) => [null, undefined].includes(x)
|
||||
|
||||
// Regular old utils
|
||||
|
||||
export const now = () => Math.round(Date.now() / 1000)
|
||||
|
||||
export const first = <T>(xs: T[], ...args: unknown[]) => xs[0]
|
||||
|
||||
export const last = <T>(xs: T[], ...args: unknown[]) => xs[xs.length - 1]
|
||||
|
||||
export const identity = <T>(x: T, ...args: unknown[]) => x
|
||||
|
||||
export const always = <T>(x: T, ...args: unknown[]) => () => x
|
||||
|
||||
export const inc = (x: number | Nil) => (x || 0) + 1
|
||||
|
||||
export const dec = (x: number | Nil) => (x || 0) - 1
|
||||
|
||||
export const max = (xs: number[]) => xs.reduce((a, b) => Math.max(a, b), 0)
|
||||
|
||||
export const min = (xs: number[]) => xs.reduce((a, b) => Math.min(a, b), 0)
|
||||
|
||||
export const sum = (xs: number[]) => xs.reduce((a, b) => a + b, 0)
|
||||
|
||||
export const avg = (xs: number[]) => sum(xs) / xs.length
|
||||
|
||||
export const drop = <T>(n: number, xs: T[]) => xs.slice(n)
|
||||
|
||||
export const take = <T>(n: number, xs: T[]) => xs.slice(0, n)
|
||||
|
||||
export const omit = <T extends Record<string, any>>(ks: string[], x: T) => {
|
||||
const r: T = {...x}
|
||||
|
||||
for (const k of ks) {
|
||||
delete r[k]
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const pick = <T extends Record<string, any>>(ks: string[], x: T) => {
|
||||
const r: T = {...x}
|
||||
|
||||
for (const k of Object.keys(x)) {
|
||||
if (!ks.includes(k)) {
|
||||
delete r[k]
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export function* range(a: number, b: number, step = 1) {
|
||||
for (let i = a; i < b; i += step) {
|
||||
yield i
|
||||
}
|
||||
}
|
||||
|
||||
export const mapKeys = <T extends Record<string, any>>(f: (v: string) => string, x: T) => {
|
||||
const r: Record<string, any> = {}
|
||||
|
||||
for (const [k, v] of Object.entries(x)) {
|
||||
r[f(k)] = v
|
||||
}
|
||||
|
||||
return r as T
|
||||
}
|
||||
|
||||
export const mapVals = <T extends Record<string, any>>(f: (v: any) => any, x: T) => {
|
||||
const r: Record<string, any> = {}
|
||||
|
||||
for (const [k, v] of Object.entries(x)) {
|
||||
r[k] = f(v)
|
||||
}
|
||||
|
||||
return r as T
|
||||
}
|
||||
|
||||
export const between = (low: number, high: number, n: number) => n > low && n < high
|
||||
|
||||
export const randomId = (): string => Math.random().toString().slice(2)
|
||||
|
||||
export const stripProtocol = (url: string) => url.replace(/.*:\/\//, "")
|
||||
|
||||
export const sleep = (t: number) => new Promise(resolve => setTimeout(resolve, t))
|
||||
|
||||
export const concat = <T>(...xs: (T | Nil)[][]) => xs.flatMap(x => x || [])
|
||||
|
||||
export const append = <T>(xs: (T | Nil)[], x: T) => concat(xs, [x])
|
||||
|
||||
export const union = <T>(a: T[], b: T[]) => uniq([...a, ...b])
|
||||
|
||||
export const intersection = <T>(a: T[], b: T[]) => {
|
||||
const s = new Set(b)
|
||||
|
||||
return a.filter(x => s.has(x))
|
||||
}
|
||||
|
||||
export const difference = <T>(a: T[], b: T[]) => {
|
||||
const s = new Set(b)
|
||||
|
||||
return a.filter(x => !s.has(x))
|
||||
}
|
||||
|
||||
export const remove = <T>(a: T, b: T[]) => b.filter(x => x !== a)
|
||||
|
||||
export const without = <T>(a: T[], b: T[]) => b.filter(x => !a.includes(x))
|
||||
|
||||
export const clamp = ([min, max]: [number, number], n: number) => Math.min(max, Math.max(min, n))
|
||||
|
||||
export const tryCatch = async <T>(f: () => Promise<T | void> | T | void, onError?: (e: Error) => void): Promise<T | void> => {
|
||||
try {
|
||||
return await f()
|
||||
} catch (e) {
|
||||
onError?.(e as Error)
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Curried utils
|
||||
|
||||
export const nth = (i: number) => <T>(xs: T[], ...args: unknown[]) => xs[i]
|
||||
|
||||
export const nthEq = (i: number, v: any) => (xs: any[], ...args: unknown[]) => xs[i] === v
|
||||
|
||||
export const eq = <T>(v: T) => (x: T) => x === v
|
||||
|
||||
export const ne = <T>(v: T) => (x: T) => x !== v
|
||||
|
||||
export const prop = (k: string) => <T>(x: Record<string, T>) => x[k]
|
||||
|
||||
export const assoc = <K extends string, T, U>(k: K, v: T) => (o: U) => ({...o, [k as K]: v}) as U & Record<K, T>
|
||||
|
||||
export const hash = (s: string) =>
|
||||
Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)).toString()
|
||||
|
||||
// Collections
|
||||
|
||||
export const splitAt = <T>(n: number, xs: T[]) => [xs.slice(0, n), xs.slice(n)]
|
||||
|
||||
export const choice = <T>(xs: T[]): T => xs[Math.floor(xs.length * Math.random())]
|
||||
|
||||
export const shuffle = <T>(xs: Iterable<T>): T[] => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1)
|
||||
|
||||
export const isIterable = (x: any) => Symbol.iterator in Object(x)
|
||||
|
||||
export const toIterable = (x: any) => isIterable(x) ? x : [x]
|
||||
|
||||
export const ensurePlural = <T>(x: T | T[]) => (x instanceof Array ? x : [x])
|
||||
|
||||
export const ensureNumber = (x: number | string) => parseFloat(x as string)
|
||||
|
||||
export const fromPairs = <T>(pairs: [k?: string, v?: T, ...args: unknown[]][]) => {
|
||||
const r: Record<string, T> = {}
|
||||
|
||||
for (const [k, v] of pairs) {
|
||||
if (k && v) {
|
||||
r[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const flatten = <T>(xs: T[][]) => xs.flatMap(identity)
|
||||
|
||||
export const partition = <T>(f: (x: T) => boolean, xs: T[]) => {
|
||||
const a: T[] = []
|
||||
const b: T[] = []
|
||||
|
||||
for (const x of xs) {
|
||||
if (f(x)) {
|
||||
a.push(x)
|
||||
} else {
|
||||
b.push(x)
|
||||
}
|
||||
}
|
||||
|
||||
return [a, b]
|
||||
}
|
||||
|
||||
export const uniq = <T>(xs: T[]) => Array.from(new Set(xs))
|
||||
|
||||
export const uniqBy = <T>(f: (x: T) => any, xs: T[]) => {
|
||||
const s = new Set<any>()
|
||||
const r = []
|
||||
|
||||
for (const x of xs) {
|
||||
const k = f(x)
|
||||
|
||||
if (s.has(k)) {
|
||||
continue
|
||||
}
|
||||
|
||||
s.add(k)
|
||||
r.push(x)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const sort = <T>(xs: T[]) => [...xs].sort()
|
||||
|
||||
export const sortBy = <T>(f: (x: T) => number, xs: T[]) =>
|
||||
[...xs].sort((a: T, b: T) => f(a) - f(b))
|
||||
|
||||
export const groupBy = <T, K>(f: (x: T) => K, xs: T[]) => {
|
||||
const r = new Map<K, T[]>()
|
||||
|
||||
for (const x of xs) {
|
||||
const k = f(x)
|
||||
let v = r.get(k)
|
||||
|
||||
if (!v) {
|
||||
v = []
|
||||
r.set(k, v)
|
||||
}
|
||||
|
||||
v.push(x)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const indexBy = <T, K>(f: (x: T) => K, xs: T[]) => {
|
||||
const r = new Map<K, T>()
|
||||
|
||||
for (const x of xs) {
|
||||
r.set(f(x), x)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
export const sample = <T>(n: number, xs: T[]) => {
|
||||
const result: T[] = []
|
||||
const limit = Math.min(n, xs.length)
|
||||
|
||||
for (let i = 0; i < limit; i++) {
|
||||
result.push(xs.splice(Math.floor(xs.length * Math.random()), 1)[0])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const initArray = <T>(n: number, f: () => T) => {
|
||||
const result = []
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
result.push(f())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const chunk = <T>(chunkLength: number, xs: T[]) => {
|
||||
const result: T[][] = []
|
||||
const current: T[] = []
|
||||
|
||||
for (const item of xs) {
|
||||
if (current.length < chunkLength) {
|
||||
current.push(item)
|
||||
} else {
|
||||
result.push(current.splice(0))
|
||||
}
|
||||
}
|
||||
|
||||
if (current.length > 0) {
|
||||
result.push(current)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const chunks = <T>(n: number, xs: T[]) => {
|
||||
const result: T[][] = initArray(n, () => [])
|
||||
|
||||
for (let i = 0; i < xs.length; i++) {
|
||||
result[i % n].push(xs[i])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const once = (f: (...args: any) => void) => {
|
||||
let called = false
|
||||
|
||||
return (...args: any) => {
|
||||
if (!called) {
|
||||
called = true
|
||||
f(...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const batch = <T>(t: number, f: (xs: T[]) => void) => {
|
||||
const xs: T[] = []
|
||||
const cb = throttle(t, () => xs.length > 0 && f(xs.splice(0)))
|
||||
|
||||
return (x: T) => {
|
||||
xs.push(x)
|
||||
cb()
|
||||
}
|
||||
}
|
||||
|
||||
export const addToKey = <T>(m: Record<string, Set<T>>, k: string, v: T) => {
|
||||
const s = m[k] || new Set<T>()
|
||||
|
||||
s.add(v)
|
||||
m[k] = s
|
||||
}
|
||||
|
||||
export const pushToKey = <T>(m: Record<string, T[]>, k: string, v: T) => {
|
||||
const a = m[k] || []
|
||||
|
||||
a.push(v)
|
||||
m[k] = a
|
||||
}
|
||||
|
||||
export const addToMapKey = <K, T>(m: Map<K, Set<T>>, k: K, v: T) => {
|
||||
const s = m.get(k) || new Set<T>()
|
||||
|
||||
s.add(v)
|
||||
m.set(k, s)
|
||||
}
|
||||
|
||||
export const pushToMapKey = <K, T>(m: Map<K, T[]>, k: K, v: T) => {
|
||||
const a = m.get(k) || []
|
||||
|
||||
a.push(v)
|
||||
m.set(k, a)
|
||||
}
|
||||
|
||||
// Random obscure stuff
|
||||
|
||||
export const hexToBech32 = (prefix: string, url: string) =>
|
||||
bech32.encode(prefix, bech32.toWords(utf8.decode(url)), false)
|
||||
|
||||
export const bech32ToHex = (b32: string) =>
|
||||
utf8.encode(bech32.fromWords(bech32.decode(b32, false).words))
|
||||
|
||||
// https://github.com/microsoft/TypeScript/issues/4628#issuecomment-1147905253
|
||||
export type OmitStatics<T, S extends string> =
|
||||
T extends {new(...args: infer A): infer R} ?
|
||||
{new(...args: A): R}&Omit<T, S> :
|
||||
Omit<T, S>;
|
||||
|
||||
// https://github.com/microsoft/TypeScript/issues/4628#issuecomment-1147905253
|
||||
export type OmitAllStatics<T extends {new(...args: any[]): any, prototype: any}> =
|
||||
T extends {new(...args: infer A): infer R, prototype: infer P} ?
|
||||
{new(...args: A): R, prototype: P} :
|
||||
never;
|
||||
@@ -0,0 +1,71 @@
|
||||
const ANY = Symbol("worker/ANY")
|
||||
|
||||
export type WorkerOpts<T> = {
|
||||
getKey?: (x: T) => any
|
||||
shouldDefer?: (x: T) => boolean
|
||||
}
|
||||
|
||||
export class Worker<T> {
|
||||
buffer: T[] = []
|
||||
handlers: Map<any, Array<(x: T) => void>> = new Map()
|
||||
timeout: number | undefined
|
||||
|
||||
constructor(readonly opts: WorkerOpts<T> = {}) {}
|
||||
|
||||
#doWork = async () => {
|
||||
for (let i = 0; i < 50; i++) {
|
||||
if (this.buffer.length === 0) {
|
||||
break
|
||||
}
|
||||
|
||||
// Pop the buffer one at a time so handle can modify the queue
|
||||
const [message] = this.buffer.splice(0, 1)
|
||||
|
||||
if (this.opts.shouldDefer?.(message)) {
|
||||
this.buffer.push(message)
|
||||
} else {
|
||||
for (const handler of this.handlers.get(ANY) || []) {
|
||||
await handler(message)
|
||||
}
|
||||
|
||||
if (this.opts.getKey) {
|
||||
const k = this.opts.getKey(message)
|
||||
|
||||
for (const handler of this.handlers.get(k) || []) {
|
||||
await handler(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.timeout = undefined
|
||||
this.#enqueueWork()
|
||||
}
|
||||
|
||||
#enqueueWork = () => {
|
||||
if (!this.timeout && this.buffer.length > 0) {
|
||||
this.timeout = setTimeout(this.#doWork, 50)
|
||||
}
|
||||
}
|
||||
|
||||
push = (message: T) => {
|
||||
this.buffer.push(message)
|
||||
this.#enqueueWork()
|
||||
}
|
||||
|
||||
addHandler = (k: any, handler: (message: T) => void) => {
|
||||
this.handlers.set(k, (this.handlers.get(k) || []).concat(handler))
|
||||
}
|
||||
|
||||
addGlobalHandler = (handler: (message: T) => void) => {
|
||||
this.addHandler(ANY, handler)
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.buffer = []
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearTimeout(this.timeout)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export * from './Deferred'
|
||||
export * from './Emitter'
|
||||
export * from './Fluent'
|
||||
export * from './LRUCache'
|
||||
export * from './Store'
|
||||
export * from './Tools'
|
||||
export * from './Worker'
|
||||
export {default as normalizeUrl} from './normalize-url'
|
||||
@@ -0,0 +1,583 @@
|
||||
export type Options = {
|
||||
/**
|
||||
@default 'http'
|
||||
*/
|
||||
readonly defaultProtocol?: 'https' | 'http';
|
||||
|
||||
/**
|
||||
Prepends `defaultProtocol` to the URL if it's protocol-relative.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('//sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
|
||||
//=> '//sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly normalizeProtocol?: boolean;
|
||||
|
||||
/**
|
||||
Normalizes HTTPS URLs to HTTP.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('https://sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('https://sindresorhus.com', {forceHttp: true});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly forceHttp?: boolean;
|
||||
|
||||
/**
|
||||
Normalizes HTTP URLs to HTTPS.
|
||||
|
||||
This option cannot be used with the `forceHttp` option at the same time.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('http://sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com', {forceHttps: true});
|
||||
//=> 'https://sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly forceHttps?: boolean;
|
||||
|
||||
/**
|
||||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of a URL.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('user:password@sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
|
||||
//=> 'https://user:password@sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly stripAuthentication?: boolean;
|
||||
|
||||
/**
|
||||
Removes hash from the URL.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('sindresorhus.com/about.html#contact');
|
||||
//=> 'http://sindresorhus.com/about.html#contact'
|
||||
|
||||
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
|
||||
//=> 'http://sindresorhus.com/about.html'
|
||||
```
|
||||
*/
|
||||
readonly stripHash?: boolean;
|
||||
|
||||
/**
|
||||
Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
|
||||
|
||||
It will only remove `https://` and `http://` protocols.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('https://sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('sindresorhus.com', {stripProtocol: true});
|
||||
//=> 'sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly stripProtocol?: boolean;
|
||||
|
||||
/**
|
||||
Strip the [text fragment](https://web.dev/text-fragments/) part of the URL
|
||||
|
||||
__Note:__ The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
|
||||
//=> 'http://sindresorhus.com/about.html#'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
|
||||
//=> 'http://sindresorhus.com/about.html#section'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
|
||||
//=> 'http://sindresorhus.com/about.html#:~:text=hello'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
|
||||
//=> 'http://sindresorhus.com/about.html#section:~:text=hello'
|
||||
```
|
||||
*/
|
||||
readonly stripTextFragment?: boolean;
|
||||
|
||||
/**
|
||||
Removes `www.` from the URL.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('http://www.sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
|
||||
//=> 'http://www.sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly stripWWW?: boolean;
|
||||
|
||||
/**
|
||||
Removes query parameters that matches any of the provided strings or regexes.
|
||||
|
||||
@default [/^utm_\w+/i]
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
||||
removeQueryParameters: ['ref']
|
||||
});
|
||||
//=> 'http://sindresorhus.com/?foo=bar'
|
||||
```
|
||||
|
||||
If a boolean is provided, `true` will remove all the query parameters.
|
||||
|
||||
```
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar', {
|
||||
removeQueryParameters: true
|
||||
});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
`false` will not remove any query parameter.
|
||||
|
||||
```
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
|
||||
removeQueryParameters: false
|
||||
});
|
||||
//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
|
||||
```
|
||||
*/
|
||||
readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;
|
||||
|
||||
/**
|
||||
Keeps only query parameters that matches any of the provided strings or regexes.
|
||||
|
||||
__Note__: It overrides the `removeQueryParameters` option.
|
||||
|
||||
@default undefined
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
|
||||
keepQueryParameters: ['ref']
|
||||
});
|
||||
//=> 'https://sindresorhus.com/?ref=unicorn'
|
||||
```
|
||||
*/
|
||||
readonly keepQueryParameters?: ReadonlyArray<RegExp | string>;
|
||||
|
||||
/**
|
||||
Removes trailing slash.
|
||||
|
||||
__Note__: Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('http://sindresorhus.com/redirect/');
|
||||
//=> 'http://sindresorhus.com/redirect'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
|
||||
//=> 'http://sindresorhus.com/redirect/'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly removeTrailingSlash?: boolean;
|
||||
|
||||
/**
|
||||
Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('https://sindresorhus.com/');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
|
||||
//=> 'https://sindresorhus.com/'
|
||||
```
|
||||
*/
|
||||
readonly removeSingleSlash?: boolean;
|
||||
|
||||
/**
|
||||
Removes the default directory index file from path that matches any of the provided strings or regexes.
|
||||
When `true`, the regex `/^index\.[a-z]+$/` is used.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
||||
removeDirectoryIndex: [/^default\.[a-z]+$/]
|
||||
});
|
||||
//=> 'http://sindresorhus.com/foo'
|
||||
```
|
||||
*/
|
||||
readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;
|
||||
|
||||
/**
|
||||
Removes an explicit port number from the URL.
|
||||
|
||||
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('sindresorhus.com:123', {
|
||||
removeExplicitPort: true
|
||||
});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
*/
|
||||
readonly removeExplicitPort?: boolean;
|
||||
|
||||
/**
|
||||
Sorts the query parameters alphabetically by key.
|
||||
|
||||
@default true
|
||||
|
||||
@example
|
||||
```
|
||||
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
|
||||
sortQueryParameters: false
|
||||
});
|
||||
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
|
||||
```
|
||||
*/
|
||||
readonly sortQueryParameters?: boolean;
|
||||
};
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
|
||||
const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain'
|
||||
const DATA_URL_DEFAULT_CHARSET = 'us-ascii'
|
||||
|
||||
const testParameter = (name: string, filters: any[]) => filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name)
|
||||
|
||||
const supportedProtocols = new Set([
|
||||
'https:',
|
||||
'http:',
|
||||
'file:',
|
||||
])
|
||||
|
||||
const hasCustomProtocol = (urlString: string) => {
|
||||
try {
|
||||
const {protocol} = new URL(urlString)
|
||||
return protocol.endsWith(':') && !supportedProtocols.has(protocol)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeDataURL = (urlString: string, {stripHash}: {stripHash: boolean}) => {
|
||||
const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString)
|
||||
|
||||
if (!match) {
|
||||
throw new Error(`Invalid URL: ${urlString}`)
|
||||
}
|
||||
|
||||
let {type, data, hash} = match.groups as any
|
||||
const mediaType = type.split(';')
|
||||
hash = stripHash ? '' : hash
|
||||
|
||||
let isBase64 = false
|
||||
if (mediaType[mediaType.length - 1] === 'base64') {
|
||||
mediaType.pop()
|
||||
isBase64 = true
|
||||
}
|
||||
|
||||
// Lowercase MIME type
|
||||
const mimeType = mediaType.shift()?.toLowerCase() ?? ''
|
||||
const attributes = mediaType
|
||||
.map((attribute: string) => {
|
||||
let [key, value = ''] = attribute.split('=').map((s: string) => s.trim())
|
||||
|
||||
// Lowercase `charset`
|
||||
if (key === 'charset') {
|
||||
value = value.toLowerCase()
|
||||
|
||||
if (value === DATA_URL_DEFAULT_CHARSET) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
return `${key}${value ? `=${value}` : ''}`
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
const normalizedMediaType = [
|
||||
...attributes,
|
||||
]
|
||||
|
||||
if (isBase64) {
|
||||
normalizedMediaType.push('base64')
|
||||
}
|
||||
|
||||
if (normalizedMediaType.length > 0 || (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)) {
|
||||
normalizedMediaType.unshift(mimeType)
|
||||
}
|
||||
|
||||
return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`
|
||||
}
|
||||
|
||||
/**
|
||||
[Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL.
|
||||
|
||||
URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`.
|
||||
|
||||
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
|
||||
|
||||
@example
|
||||
```
|
||||
import normalizeUrl from 'normalize-url';
|
||||
|
||||
normalizeUrl('sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
|
||||
//=> 'http://sindresorhus.com/baz?a=foo&b=bar'
|
||||
```
|
||||
*/
|
||||
|
||||
export default function normalizeUrl(urlString: string, opts?: Options): string {
|
||||
const options = {
|
||||
defaultProtocol: 'http',
|
||||
normalizeProtocol: true,
|
||||
forceHttp: false,
|
||||
forceHttps: false,
|
||||
stripAuthentication: true,
|
||||
stripHash: false,
|
||||
stripTextFragment: true,
|
||||
stripWWW: true,
|
||||
removeQueryParameters: [/^utm_\w+/i],
|
||||
removeTrailingSlash: true,
|
||||
removeSingleSlash: true,
|
||||
removeDirectoryIndex: false,
|
||||
removeExplicitPort: false,
|
||||
sortQueryParameters: true,
|
||||
...opts,
|
||||
}
|
||||
|
||||
// Legacy: Append `:` to the protocol if missing.
|
||||
if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) {
|
||||
options.defaultProtocol = `${options.defaultProtocol}:`
|
||||
}
|
||||
|
||||
urlString = urlString.trim()
|
||||
|
||||
// Data URL
|
||||
if (/^data:/i.test(urlString)) {
|
||||
return normalizeDataURL(urlString, options)
|
||||
}
|
||||
|
||||
if (hasCustomProtocol(urlString)) {
|
||||
return urlString
|
||||
}
|
||||
|
||||
const hasRelativeProtocol = urlString.startsWith('//')
|
||||
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString)
|
||||
|
||||
// Prepend protocol
|
||||
if (!isRelativeUrl) {
|
||||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol)
|
||||
}
|
||||
|
||||
const urlObject = new URL(urlString)
|
||||
|
||||
if (options.forceHttp && options.forceHttps) {
|
||||
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together')
|
||||
}
|
||||
|
||||
if (options.forceHttp && urlObject.protocol === 'https:') {
|
||||
urlObject.protocol = 'http:'
|
||||
}
|
||||
|
||||
if (options.forceHttps && urlObject.protocol === 'http:') {
|
||||
urlObject.protocol = 'https:'
|
||||
}
|
||||
|
||||
// Remove auth
|
||||
if (options.stripAuthentication) {
|
||||
urlObject.username = ''
|
||||
urlObject.password = ''
|
||||
}
|
||||
|
||||
// Remove hash
|
||||
if (options.stripHash) {
|
||||
urlObject.hash = ''
|
||||
} else if (options.stripTextFragment) {
|
||||
urlObject.hash = urlObject.hash.replace(/#?:~:text.*?$/i, '')
|
||||
}
|
||||
|
||||
// Remove duplicate slashes if not preceded by a protocol
|
||||
// NOTE: This could be implemented using a single negative lookbehind
|
||||
// regex, but we avoid that to maintain compatibility with older js engines
|
||||
// which do not have support for that feature.
|
||||
if (urlObject.pathname) {
|
||||
// TODO: Replace everything below with `urlObject.pathname = urlObject.pathname.replace(/(?<!\b[a-z][a-z\d+\-.]{1,50}:)\/{2,}/g, '/');` when Safari supports negative lookbehind.
|
||||
|
||||
// Split the string by occurrences of this protocol regex, and perform
|
||||
// duplicate-slash replacement on the strings between those occurrences
|
||||
// (if any).
|
||||
const protocolRegex = /\b[a-z][a-z\d+\-.]{1,50}:\/\//g
|
||||
|
||||
let lastIndex = 0
|
||||
let result = ''
|
||||
for (;;) {
|
||||
const match = protocolRegex.exec(urlObject.pathname)
|
||||
if (!match) {
|
||||
break
|
||||
}
|
||||
|
||||
const protocol = match[0]
|
||||
const protocolAtIndex = match.index
|
||||
const intermediate = urlObject.pathname.slice(lastIndex, protocolAtIndex)
|
||||
|
||||
result += intermediate.replace(/\/{2,}/g, '/')
|
||||
result += protocol
|
||||
lastIndex = protocolAtIndex + protocol.length
|
||||
}
|
||||
|
||||
const remnant = urlObject.pathname.slice(lastIndex, urlObject.pathname.length)
|
||||
result += remnant.replace(/\/{2,}/g, '/')
|
||||
|
||||
urlObject.pathname = result
|
||||
}
|
||||
|
||||
// Decode URI octets
|
||||
if (urlObject.pathname) {
|
||||
try {
|
||||
urlObject.pathname = decodeURI(urlObject.pathname)
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Remove directory index
|
||||
if (options.removeDirectoryIndex === true) {
|
||||
options.removeDirectoryIndex = [/^index\.[a-z]+$/]
|
||||
}
|
||||
|
||||
if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) {
|
||||
let pathComponents = urlObject.pathname.split('/')
|
||||
const lastComponent = pathComponents[pathComponents.length - 1]
|
||||
|
||||
if (testParameter(lastComponent, options.removeDirectoryIndex)) {
|
||||
pathComponents = pathComponents.slice(0, -1)
|
||||
urlObject.pathname = pathComponents.slice(1).join('/') + '/'
|
||||
}
|
||||
}
|
||||
|
||||
if (urlObject.hostname) {
|
||||
// Remove trailing dot
|
||||
urlObject.hostname = urlObject.hostname.replace(/\.$/, '')
|
||||
|
||||
// Remove `www.`
|
||||
if (options.stripWWW && /^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(urlObject.hostname)) {
|
||||
// Each label should be max 63 at length (min: 1).
|
||||
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
||||
// Each TLD should be up to 63 characters long (min: 2).
|
||||
// It is technically possible to have a single character TLD, but none currently exist.
|
||||
urlObject.hostname = urlObject.hostname.replace(/^www\./, '')
|
||||
}
|
||||
}
|
||||
|
||||
// Remove query unwanted parameters
|
||||
if (Array.isArray(options.removeQueryParameters)) {
|
||||
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
|
||||
for (const key of [...urlObject.searchParams.keys()]) {
|
||||
if (testParameter(key, options.removeQueryParameters)) {
|
||||
urlObject.searchParams.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
|
||||
urlObject.search = ''
|
||||
}
|
||||
|
||||
// Keep wanted query parameters
|
||||
if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
|
||||
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
|
||||
for (const key of [...urlObject.searchParams.keys()]) {
|
||||
if (!testParameter(key, options.keepQueryParameters)) {
|
||||
urlObject.searchParams.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort query parameters
|
||||
if (options.sortQueryParameters) {
|
||||
urlObject.searchParams.sort()
|
||||
|
||||
// Calling `.sort()` encodes the search parameters, so we need to decode them again.
|
||||
try {
|
||||
urlObject.search = decodeURIComponent(urlObject.search)
|
||||
} catch {}
|
||||
}
|
||||
|
||||
if (options.removeTrailingSlash) {
|
||||
urlObject.pathname = urlObject.pathname.replace(/\/$/, '')
|
||||
}
|
||||
|
||||
// Remove an explicit port number, excluding a default port number, if applicable
|
||||
if (options.removeExplicitPort && urlObject.port) {
|
||||
urlObject.port = ''
|
||||
}
|
||||
|
||||
const oldUrlString = urlString
|
||||
|
||||
// Take advantage of many of the Node `url` normalizations
|
||||
urlString = urlObject.toString()
|
||||
|
||||
if (!options.removeSingleSlash && urlObject.pathname === '/' && !oldUrlString.endsWith('/') && urlObject.hash === '') {
|
||||
urlString = urlString.replace(/\/$/, '')
|
||||
}
|
||||
|
||||
// Remove ending `/` unless removeSingleSlash is false
|
||||
if ((options.removeTrailingSlash || urlObject.pathname === '/') && urlObject.hash === '' && options.removeSingleSlash) {
|
||||
urlString = urlString.replace(/\/$/, '')
|
||||
}
|
||||
|
||||
// Restore relative protocol, if applicable
|
||||
if (hasRelativeProtocol && !options.normalizeProtocol) {
|
||||
urlString = urlString.replace(/^http:\/\//, '//')
|
||||
}
|
||||
|
||||
// Remove http/https
|
||||
if (options.stripProtocol) {
|
||||
urlString = urlString.replace(/^(?:https?:)?\/\//, '')
|
||||
}
|
||||
|
||||
return urlString
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,324 @@
|
||||
# normalize-url [](https://codecov.io/gh/sindresorhus/normalize-url)
|
||||
|
||||
> [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
|
||||
|
||||
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
|
||||
|
||||
**Note:** This package does **not** do URL sanitization. [Garbage in, garbage out.](https://en.wikipedia.org/wiki/Garbage_in,_garbage_out) If you use this in a server context and accept URLs as user input, it's up to you to protect against invalid URLs, [path traversal attacks](https://owasp.org/www-community/attacks/Path_Traversal), etc.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install normalize-url
|
||||
```
|
||||
|
||||
*If you need Safari support, use version 4: `npm i normalize-url@4`*
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import normalizeUrl from 'normalize-url';
|
||||
|
||||
normalizeUrl('sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
|
||||
//=> 'http://sindresorhus.com/baz?a=foo&b=bar'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### normalizeUrl(url, options?)
|
||||
|
||||
URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`.
|
||||
|
||||
#### url
|
||||
|
||||
Type: `string`
|
||||
|
||||
URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object`
|
||||
|
||||
##### defaultProtocol
|
||||
|
||||
Type: `string`\
|
||||
Default: `'http'`\
|
||||
Values: `'https' | 'http'`
|
||||
|
||||
##### normalizeProtocol
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Prepend `defaultProtocol` to the URL if it's protocol-relative.
|
||||
|
||||
```js
|
||||
normalizeUrl('//sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
|
||||
//=> '//sindresorhus.com'
|
||||
```
|
||||
|
||||
##### forceHttp
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Normalize HTTPS to HTTP.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('https://sindresorhus.com', {forceHttp: true});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
##### forceHttps
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Normalize HTTP to HTTPS.
|
||||
|
||||
```js
|
||||
normalizeUrl('http://sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com', {forceHttps: true});
|
||||
//=> 'https://sindresorhus.com'
|
||||
```
|
||||
|
||||
This option cannot be used with the `forceHttp` option at the same time.
|
||||
|
||||
##### stripAuthentication
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL.
|
||||
|
||||
```js
|
||||
normalizeUrl('user:password@sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
|
||||
//=> 'https://user:password@sindresorhus.com'
|
||||
```
|
||||
|
||||
##### stripHash
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Strip the hash part of the URL.
|
||||
|
||||
```js
|
||||
normalizeUrl('sindresorhus.com/about.html#contact');
|
||||
//=> 'http://sindresorhus.com/about.html#contact'
|
||||
|
||||
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
|
||||
//=> 'http://sindresorhus.com/about.html'
|
||||
```
|
||||
|
||||
##### stripProtocol
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
|
||||
|
||||
It will only remove `https://` and `http://` protocols.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
|
||||
//=> 'sindresorhus.com'
|
||||
```
|
||||
|
||||
##### stripTextFragment
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
|
||||
|
||||
**Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
|
||||
|
||||
```js
|
||||
normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
|
||||
//=> 'http://sindresorhus.com/about.html#'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
|
||||
//=> 'http://sindresorhus.com/about.html#section'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
|
||||
//=> 'http://sindresorhus.com/about.html#:~:text=hello'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
|
||||
//=> 'http://sindresorhus.com/about.html#section:~:text=hello'
|
||||
```
|
||||
|
||||
##### stripWWW
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Remove `www.` from the URL.
|
||||
|
||||
```js
|
||||
normalizeUrl('http://www.sindresorhus.com');
|
||||
//=> 'http://sindresorhus.com'
|
||||
|
||||
normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
|
||||
//=> 'http://www.sindresorhus.com'
|
||||
```
|
||||
|
||||
##### removeQueryParameters
|
||||
|
||||
Type: `Array<RegExp | string> | boolean`\
|
||||
Default: `[/^utm_\w+/i]`
|
||||
|
||||
Remove query parameters that matches any of the provided strings or regexes.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
||||
removeQueryParameters: ['ref']
|
||||
});
|
||||
//=> 'http://sindresorhus.com/?foo=bar'
|
||||
```
|
||||
|
||||
If a boolean is provided, `true` will remove all the query parameters.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar', {
|
||||
removeQueryParameters: true
|
||||
});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
`false` will not remove any query parameter.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
|
||||
removeQueryParameters: false
|
||||
});
|
||||
//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
|
||||
```
|
||||
|
||||
##### keepQueryParameters
|
||||
|
||||
Type: `Array<RegExp | string>`\
|
||||
Default: `undefined`
|
||||
|
||||
Keeps only query parameters that matches any of the provided strings or regexes.
|
||||
|
||||
**Note:** It overrides the `removeQueryParameters` option.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
|
||||
keepQueryParameters: ['ref']
|
||||
});
|
||||
//=> 'https://sindresorhus.com/?ref=unicorn'
|
||||
```
|
||||
|
||||
##### removeTrailingSlash
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Remove trailing slash.
|
||||
|
||||
**Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
|
||||
|
||||
```js
|
||||
normalizeUrl('http://sindresorhus.com/redirect/');
|
||||
//=> 'http://sindresorhus.com/redirect'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
|
||||
//=> 'http://sindresorhus.com/redirect/'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
##### removeSingleSlash
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com/');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
|
||||
//=> 'https://sindresorhus.com/'
|
||||
```
|
||||
|
||||
##### removeDirectoryIndex
|
||||
|
||||
Type: `boolean | Array<RegExp | string>`\
|
||||
Default: `false`
|
||||
|
||||
Removes the default directory index file from path that matches any of the provided strings or regexes. When `true`, the regex `/^index\.[a-z]+$/` is used.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
||||
removeDirectoryIndex: [/^default\.[a-z]+$/]
|
||||
});
|
||||
//=> 'http://sindresorhus.com/foo'
|
||||
```
|
||||
|
||||
##### removeExplicitPort
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Removes an explicit port number from the URL.
|
||||
|
||||
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
|
||||
|
||||
```js
|
||||
normalizeUrl('sindresorhus.com:123', {
|
||||
removeExplicitPort: true
|
||||
});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
##### sortQueryParameters
|
||||
|
||||
Type: `boolean`\
|
||||
Default: `true`
|
||||
|
||||
Sorts the query parameters alphabetically by key.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
|
||||
sortQueryParameters: false
|
||||
});
|
||||
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
<b>
|
||||
<a href="https://tidelift.com/subscription/pkg/npm-normalize-url?utm_source=npm-normalize-url&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
||||
</b>
|
||||
<br>
|
||||
<sub>
|
||||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
||||
</sub>
|
||||
</div>
|
||||
Reference in New Issue
Block a user