Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
ValentaTomas / unique.ts
Last active November 2, 2022 18:06
Filter unique or duplicate values from an array
function onlyUnique<T>(value: T, index: number, self: T[]) {
return self.indexOf(value) === index;
}
function onlyNonUnique<T>(value: T, index: number, self: T[]) {
return self.indexOf(value) !== self.lastIndexOf(value);
}
const data = [];
@ValentaTomas
ValentaTomas / makeSync.ts
Last active April 26, 2022 13:38
Make function synchronous
export type Procedure = (...args: any[]) => void;
function makeSync<F extends Procedure>(
func: F,
): (this: ThisParameterType<F>, ...args: Parameters<F>) => void {
let inProgress = false;
return async function(this: ThisParameterType<F>, ...args: Parameters<F>) {
const context = this;
@ValentaTomas
ValentaTomas / useQueryParams.ts
Last active April 26, 2022 13:35
Hook for extracting route query parameters
import { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
type Params<T extends string> = { [key in T]: string };
// Returns object that contains all path query parameters listed in the `names` argument.
// If any path query parameters from the `names` is missing in the path, the returned object is undefined.
function useQueryParams<T extends string>(...names: T[]) {
const location = useLocation();
@ValentaTomas
ValentaTomas / notEmpty.ts
Last active April 26, 2022 13:35
Filter null and undefined values out of an array
export function notEmpty<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined;
}
@ValentaTomas
ValentaTomas / executeCommand.ts
Last active April 26, 2022 13:34
Execute Shell command in Node
import { exec, execFile } from 'child_process';
function executeCommand(command: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout) => {
if (error) {
return reject(error);
}
return resolve(stdout);
});
@ValentaTomas
ValentaTomas / getDatabaseURL.js
Created March 21, 2021 15:33
Retrieves and prints the database URL from the Google Secret Manager
#!/usr/bin/env node
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManager = new SecretManagerServiceClient();
async function getSecret(name) {
const [secretVersion] = await secretManager.accessSecretVersion({
name: `${name}/versions/latest`,
});
@ValentaTomas
ValentaTomas / routeVersioning.ts
Last active April 26, 2022 13:34
Add versioning to API routes
import {
RequestHandler,
Request,
Response,
NextFunction,
Router,
} from 'express';
export enum APIVersion {
v0 = 'v0',
@ValentaTomas
ValentaTomas / upload-gcp.sh
Created May 9, 2021 13:15
Upload files in a directory to GCP bucket without caching
#!/bin/sh
DIR=$1
export BUCKET=$2
cd $DIR
ls -l ./ | tr -s ' ' | cut -d' ' -f9 |
tr ' ' '\n' |
xargs -n 1 -I{} gsutil -h "Cache-Control:no-cache, max-age=0" cp {} gs://$BUCKET
@ValentaTomas
ValentaTomas / esbuild.js
Last active December 4, 2021 12:56
Esbuild build script for Node.js projects
#!/usr/bin/env node
const esbuild = require('esbuild');
const path = require('path');
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
const filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, args => ({ path: args.path, external: true }));
@ValentaTomas
ValentaTomas / docker-run.sh
Created June 9, 2021 17:49
Run Docker image with a mounted GCP credentials
#!/bin/sh
docker run -p 8000:80 \
-e GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds/in/container.json \
-v $GOOGLE_APPLICATION_CREDENTIALS:/path/to/creds/in/container.json \
gcr.io/$PROJECT_ID/$IMAGE_ID