Skip to content

Instantly share code, notes, and snippets.

Constrained Identity Function
- Use Case: We want to create a Record with a limited set of Keys, and a defined type of Values.
- We also don't want to Type the Set of Keys, but leverage TypeScript inference instead (DRY)
- If we create a new inline object literal, TypeScript will prevent any new key from being added, by won't enforce the type of the Values.
- So Narrow Keys, Wide Value Type
- If we specify the type Record<string, number>, we'll be able to add any new Key after the object creation
- So Wide Keys, Narrow Value Type
- By using a Types Identity Function, we can build an object with enforced Value Type, and Narrow set of Keys.
// Sort List of Objects By Two Keys
type SortKey<T> = {
key: keyof T;
order: 1 | -1;
};
export function sortByKeys<T>(list: T[], keys: SortKey<T> | [SortKey<T>, SortKey<T>]) {
if (!Array.isArray(keys)) {
const { key: sortKey, order } = keys;
return list.sort((v1, v2) => (v1[sortKey] < v2[sortKey] ? order * 1 : -1 * order));
}
const keys = ['k1', 'k2', 'k3']
const Keys = typeof keys[number]
type Object = {
k1: number,
k2: boolean
}
type KeyValue = {
[k in Keys]: k extends keyof Object ? Object[k] : unknown
import * as cluster from 'cluster';
import * as os from 'os';
import { Logger } from '@nestjs/common';
export class Cluster {
static register(workers: number, logger: Logger, callback: CallableFunction): void {
const cpuCount = os.cpus().length;
if (cluster.isMaster) {
logger.log(`master process started on ${process.pid}`);
Cypress.Commands.add('interceptOnce', (method, path, alias, status) => {
cy.intercept({
method,
path,
times: 1,
}).as(alias);
if (status) {
return cy.wrap(() =>
cy
function getCurrentDateP(mongoConnection) {
const query = { _id: 1 };
const update = { $currentDate: { updated_at: true } };
const queryOptions = { upsert: true, returnOriginal: false };
return mongoConnection
.collection('current_date')
.findOneAndUpdate(query, update, queryOptions)
.then(mongoResult => mongoResult.value.updated_at)
}
.catch((err: Error | AxiosError) {
if (axios.isAxiosError(error)) {
// Access to config, request, and response
} else {
// Just a stock error
}
})
<form
ref={formRef}
onSubmit={(e: React.SyntheticEvent) => {
e.preventDefault();
const target = e.target as typeof e.target & {
email: { value: string };
password: { value: string };
};
const email = target.email.value; // typechecks!
const password = target.password.value; // typechecks!
import { mode } from '@chakra-ui/theme-tools';
import { extendTheme } from '@chakra-ui/core';
const styles = {
global: props => ({
":root": {
"--app-title-color": "#718096",
"--card-minw": "160px",
"--blackwhite": mode("black", "white")(props),
"--whiteblack": mode("white", "black")(props),
@bsitruk
bsitruk / docker-registry.sh
Created September 12, 2019 13:15
Commands and configuration file to configure docker registry
# Run a docker registry
# https://docs.docker.com/registry/deploying/
docker run -d -p 5000:5000 \
--restart=always \
--name registry\
-v /mnt/registry:/var/lib/registry \
-e REGISTRY_HTTP_ADDR=0.0.0.0:5000 \
registry:2
# Add private registry as insecure