Skip to content

Instantly share code, notes, and snippets.

@Jackman3005
Last active June 19, 2024 04:34
Show Gist options
  • Save Jackman3005/84fbb34dd799254d8fda8ed89a23a526 to your computer and use it in GitHub Desktop.
Save Jackman3005/84fbb34dd799254d8fda8ed89a23a526 to your computer and use it in GitHub Desktop.
This is a gist for handy commands I find over time that I want to keep safe

Index

Javascript

Add Ids to Objects

Useful to debug re-render issues by printing object IDs and seeing which ones change. Usage: Object.id(myObj)

Place the following IIFE somewhere it will run on start of the code.

(function () {
  if (typeof Object.id == "undefined") {
    let id = 0;

    Object.id = function (o) {
      if (typeof o.__uniqueid == "undefined") {
        Object.defineProperty(o, "__uniqueid", {
          value: ++id,
          enumerable: false,
          writable: false,
        });
      }

      return o.__uniqueid;
    };
  }
})()

Await inside a [].reduce()

Source Article

const reduceLoop = async _ => {
  console.log('Start')

  const sum = await fruitsToGet.reduce(async (promisedSum, fruit) => {
    const sum = await promisedSum
    const numFruit = await getNumFruit(fruit)
    return sum + numFruit
  }, 0)

  console.log(sum)
  console.log('End')
}

Run a test many times

.each(new Array(100).fill(undefined))

Catch and report on all outgoing 3rd party calls

nock("something", {
  filteringScope: function (scope) {
    if (/127\.0\.0\.1/.exec(scope)) {
      return false;
    }
    console.log("scope: ", scope);
    return true;
  },
})
  .filteringPath(function (path) {
    console.log("path: ", path);
    return "/";
  })
  .post("/")
  .reply(500);

Shell

Handy shell commands I've found over time that I want to keep safe.

Notably, I use a workstation-setup-script and check-in my dotfiles to simplify onboarding a new machine.

Identify current network interface

Shows the network connection that OSX is using currently. Useful if you are connected to more than one connection at a time and are unsure of which network it will use.

$ route get default | grep interface
# interface: en8

I have evolved its usage to this alias which prints my local ip based on the currently active network interface

alias myip='ipconfig getifaddr $(route get default | grep interface | cut -d" " -f4)'

Recent command names

Looks at last 1000 (configurable) shell commands and prints a unique list of command names. This is hardly accurate as it only prints the first word in a shell command, but helps me find things I forgot about.

history | tail -1000 | sed -E 's/^[[:blank:]]*[0-9]+[[:blank:]]+([^[:blank:]]+).*$/\1/' | sort | uniq

Git push

Git push that will set the upstream to track against the name of the current branch if it is not yet set. Helpful on the first push of a branch or any follow-up pushes as well. Takes params and passes them to git push so you can do the following: git pu --force for example.

$ g config --get alias.pu
!push() { if git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then git push $@; else git push --set-upstream origin $(git symbolic-ref --short HEAD) $@; fi; }; push

Git Prune Branches

From this stackoverflow;

git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

Git Branch List

Helps with keeping track of recent branches, both on your machine and remotely. Might be overwhelming on a highly active repo, could change to only observe local recent branches.

Takes a number as an argument to override the default of showing the last 25 lines.

$ git config --get alias.bl
!list_recent_branches() { local lines=$1; git branch --sort=committerdate --format="%(align:width=20)%(committerdate:relative)%(end)%(align:width=20)%(committername)%(end)%(if)%(refname:rstrip=3)%(then)%(color:dim)%(else)%(end)%(align:width=50)%(HEAD)%(refname:short)%(color:reset)%(if)%(upstream:track)%(then)%(color:bold yellow) %(upstream:track)%(else)%(end)%(end)%(color:reset)" --color --all | tail -"${lines:-25}"; }; list_recent_branches

$ git bl
2 weeks ago         Jack Coy             origin/discord
13 days ago         Jack Coy             discord [ahead 1]
13 days ago         Jack Coy             stripe
13 days ago         Jack Coy             origin/stripe
13 days ago         Jack Coy             analytics
13 days ago         Jack Coy             origin/analytics
13 days ago         GitHub               qm-cli-3
12 days ago         Jack Coy             integrations-templates
12 days ago         Jack Coy             origin/integrations-templates
11 days ago         Jack Coy             fix-keyboard-tab
11 days ago         Jack Coy             origin/fix-keyboard-tab
5 days ago          Jack Coy             origin/expressions
2 days ago          Jack Coy             fix-android
2 days ago          Jack Coy             origin/fix-android
2 days ago          Jack Coy             expressions [ahead 1]
23 hours ago        GitHub               development
23 hours ago        GitHub               origin/development
23 hours ago        GitHub               origin/master
22 hours ago        GitHub               origin/dependabot/npm_and_yarn/ws-6.2.3
14 hours ago        Jack Coy             expressions-ui
14 hours ago        Jack Coy             origin/expressions-ui
2 hours ago         Jack Coy             inline-dropdown-auto-close-backup
25 minutes ago      Jack Coy            *inline-dropdown-auto-close
25 minutes ago      Jack Coy             temp
25 minutes ago      Jack Coy             origin/inline-dropdown-auto-close

$ git bl 5
14 hours ago        Jack Coy             origin/expressions-ui
2 hours ago         Jack Coy             inline-dropdown-auto-close-backup
26 minutes ago      Jack Coy            *inline-dropdown-auto-close
26 minutes ago      Jack Coy             temp
26 minutes ago      Jack Coy             origin/inline-dropdown-auto-close
Screenshot 2024-06-19 at 2 26 30 PM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment