Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


re-export javascript function -- 05.10.2021 %

When splitting logic into files in JavaScript it sometimes happens you have a folder of files; say for a single domain like Google analytics tracking. There comes a time in another part of the code where you need to import some functions and types from this GATracking folder but you may not want to dig into which file in the folder that function is. Re-exporting allows us to keep the API of the GATracking logic consise and allow the rest of the program to not worry about how exactly the files are structured. Example:

src/client/hooks/GATracking/index.tsx

export { default as TrackingAction } from './actions';
export const track = (action: TrackingAction) =>
...

Then somewhere else: src/client/components/GoButton/index.tsx

import {TrackingAction, track} from '../hooks/GATracking';
...

source: https://stackoverflow.com/questions/34576276/re-export-a-default-export

\- [ js ]

images to animation -- 23.09.2021 %

Today I had a set of jpg images I wanted to turn into a .webm animation for my poverty line blog post. After some searching I found a nice solution on hamelot blog.

With image arranged in a folder naming like map_0001, map_0002 and so on. I ran the following command

$ ffmpeg -r 1 -f image2  -i map_%04d.png -vcodec libx264 -crf 25  -pix_fmt yuv420p map.mp4

Then I converted that map.mp4 into a smaller .webm with my helper script.

https://paste.sr.ht/~travisshears/1742232acc9c8458370e7b81c140a46a12f61ba4

source: hamelot post

\- [ ffmpeg ]

creating a temporary tabel in sqlite -- 21.09.2021 %

Today I needed to export some data from an sqlite tabel to csv, as part of my poverty line project. Using sqlitebrowser I could see the table had to much data and I only wanted to export a few columns. To accomplish this I learned how to create a temporary table and exported that.

DROP TABLE IF EXISTS processing_data_export;
CREATE TEMPORARY TABLE processing_data_export AS
SELECT x,y, county_id FROM grid;

source: stackoverflow

\- [ sqlite, sql ]

wipe dynamodb table via terraform -- 17.09.2021 %

Recently I came across the problem of how to wipe a dynamodb table. I though there would be an ui button for this or at lease an api/sdk method but from what I found the only way it to individually delete the items by key. This method can be batched up to 10 items per call but is still very slow and incurs write costs.

Since I’m used Terraform to create the table in the first place I thought I’ll just destroy it and remake it… Then came the problem of not wanting to destroy everything but simply remake the table. After some digging in Terraform docs I found a pretty clean solution.

main.tf

module "dynamodb_table" {
  source   = "terraform-aws-modules/dynamodb-table/aws"

  name     = "poverty-line-county-gps-map"
  hash_key = "id"
  stream_enabled = false

  attributes = [
    {
      name = "id"
      type = "S"
    }
  ]

  tags = {
    Terraform   = "true"
    Project = "poverty_line"
  }
}

In this case I wanted to remake “poverty-line-county-gps-map”. To find out what exactly it is called I ran terraform state:

$ terraform state list
aws_sqs_queue.dead_letter_queue
aws_sqs_queue.terraform_queue
module.dynamodb_table.aws_dynamodb_table.this[0]

from here the resource can be remake like:

$ terraform apply -replace="module.dynamodb_table.aws_dynamodb_table.this[0]"

Happy times remaking things. 👋

pushing to remote branch with different name -- 08.09.2021 %

Some times you rename a branch locally and want to push it to a remote branch with a different name. This is how:

$ git push -u origin localBranch:remoteBranch

source: stack-overflow

\- [ git ]

measuring node.js function performance -- 23.08.2021 %

How fast is that new function?

 import { xx } from 'xx';
+import { performance } from 'perf_hooks';
@@ -160,7 +161,10 @@ const userSessionMiddleware = async (req: XRequest, res: ExpressResponse, ne
+        var t0 = performance.now();
         req.isBot = headersIndicateBot(req.headers);
+        var t1 = performance.now();
+        console.log('Took', (t1 - t0).toFixed(4), 'milliseconds to calculate is bot');

source: https://www.sitepoint.com/measuring-javascript-functions-performance/

\- [ js, node-js ]

init yubikey -- 16.07.2021 %

Some servers at work require yubikey authentication and for some reason I have to fetch and verify every time I want to use it.

$ export SSH_AUTH_SOCK=~/.gnupg/S.gpg-agent.ssh
$ gpg --card-edit

fetch
verify
enter key
via pass yubikey
quit

$ ssh-add -L
$ ssh travis@serverX
\- [ yubikey, gpg ]

add types to a javascript when using typescript -- 12.07.2021 %

Today I had a .js file that I needed to import into the rest of a TypeScript app but could not convert it to a .ts file. To get around this limitation I added an accompanying .d.ts file.

countryConfig.js

const countries = [
    {
        code: 'de',
        domainExtension: '.de'
    },
    ...
];

exports.countries = countries;

countryConfig.d.ts

interface Country {
    code: string;
    domainExtension: string;
}

export const countries: Array<Country>;

app.ts

import countries from './countryConfig'

...

source: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

\- [ js, typescript ]

filtered git diff -- 25.06.2021 %

How to browse git diff of two hashes but exclude some files. In this case any file with test in the name won’t be in the diff.

gd xxxsha001xxx...xxxsha002xxx -- $(gd --name-only xxxsha001xxx...xxxsha002xxx | rg --invert-match test)

“gd” == “git diff”, via zsh git plugin

source

\- [ git ]

locally host istanbul js results -- 23.06.2021 %

At work we use istanbul js for code coverage but at times the cli output is not enough to debug. Luckily istanbul also outputs a HTML report that can be viewed with the following command:

$ npx http-server ./coverage/lcov-report
\- [ js, npx, npm ]