Skip to content

Instantly share code, notes, and snippets.

View dperuo's full-sized avatar

Derek Peruo dperuo

View GitHub Profile
/** https://developer.mozilla.org/en-US/docs/Web/HTTP/Status */
export type HttpStatusCode = Information | Successful | Redirection | ClientError | ServerError;
/** https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#information_responses */
export enum Information {
/** This interim response indicates that the client should continue the request or ignore the response if the request is already finished. */
Continue = 100,
/** This code is sent in response to an Upgrade request header from the client and indicates the protocol the server is switching to. */
SwitchingProtocols = 101,
/**
* An array of mutable key-value pairs from an object, including optional keys.
*
* @see {@linkcode ReadonlyEntries} for a readonly version.
*
* @example
*
* ```ts
* Entries<{ a: number, b: string }>
* // => (["a", number] | ["b", string])[]
/**
* Returns the result of calling the function (if it is a function) or the value itself.
*
* @example
*
* ```ts
* callable(() => true)
* // => true
*
* callable(false)

Source

  1. No loops
  2. No IF statements
  3. Functions are a single return
  4. No side effects
  5. No assignment in functions
  6. No arrays
const { fromEvent, pipe } = Rx;
const { scan, throttleTime } = RxOperators;
const tick = () => scan(next => next + 1, 0);
const throttled = () => throttleTime(500);
const paced = () => pipe(throttled(), tick());
fromEvent(document, 'click').pipe(paced());
export const environment = (...features: ReadonlyArray<Record<string, unknown>>) => ({
redirectTo: {
pwa: 'http://localhost:8100',
},
...features.reduce((acc, next) => ({ ...acc, ...next}), {})
} as const);
// environment(withFoo(), withBar(), ...)
@dperuo
dperuo / range.ts
Last active August 30, 2023 15:21
const range = ({ min = 0, max = 10, step = 1}): number[] => {
return min > max ? [] : [min, ...range({ min: min + 1, max, step})].filter(item => item % step === 0);
}
@dperuo
dperuo / backoff.ts
Last active January 16, 2024 15:49
const backoff = (config?: { readonly scale?: number; readonly count?: number; }): readonly number[] => {
return [...Array(config?.count ?? 4).keys()].map(item => 2 ** item * (config?.scale ?? 1));
}
const backoff$ = (config?: { readonly delay?: number; readonly count?: number; readonly scale?: number;}) => {
return from([...Array(config?.count ?? 4).keys()]).pipe(
concatMap(next => timer((config?.scale ?? 2) ** next * (config?.delay ?? 500)))
)
}
TypeScript Type Definitions Files