Author Archives: Patrick Muff

Slickest Way to Have an Index Counter Variable in a for..of Loop in TypeScript

There is no index variable available in a for..of loop in TypeScript. You can either use Array.prototype.forEach or my custom generator function:

interface IndexEnumerator extends Array {
  0: T;
  1: number;
}

function* indexEnumerator(iterable: Iterable) {
  let index = 0;
  for (let item of iterable) {
    yield <IndexEnumerator>[item, index];
    index++;
  }
}

Usage example:

interface Person {
  name: string;
  age: number;
}

let people: Person[] = [
  { name: 'Patrick', age: 23 },
  { name: 'Trong', age: 22 },
  { name: 'Hansruedi', age: 57 }
];

for (const [person, index] of indexEnumerator(people)) {
  console.log(`${person.name} is ${person.age} years old.`);
}

How to Find Your Raspberry Pi in Your Network Using Nmap

Bildschirmfoto 2016-09-09 um 09.01.26

sudo nmap -sn 192.168.1.0/24 | grep -B 2 Raspberry

First you have to find your current IP address and netmask. If you don’t know what a netmask is, chances are you have a 24-bit netmask (which you may have seen in this format: 255.255.255.0).

The -sn option in nmap disables port scanning. This allows for a much faster, host discovery-only scan. After nmap is done, we are piping its output into grep, so that we can filter out our Raspberry Pi devices. The -B option stands for “before”, which means that grep will include the upper two lines of the output too. Without them we would only see the MAC address.

If you put this command in our ~/.bash_profile file with an alias, we can simply type “findpi” into a terminal and are done (see screenshot above)!

alias findpi='sudo nmap -sn 192.168.1.0/24 | grep -B 2 Raspberry'

Store a Semantic Versioning String in a 32-Bit Integer

Semantic Versioning is a specification, which describes how version strings should be handled. It works by having MAJOR, MINOR and PATCH versions in a dot-separated string. Example:

MAJOR.MINOR.PATCH

2.5.3

I needed to store this string in a single 32-bit integer due to system limitations, so I wrote two handy, well-documented functions in ES6. Let’s convert the version string “2.5.3” into an integer:

let numericVersion = convertVersionToInt32('2.5.3'); // 3150850
let version = convertInt32VersionToString(numericVersion); // "2.5.3"

Warum ich @PokeListener abstelle

TL;DR for English readers: I am shutting down a Twitter bot which was tweeting the location of rare Pokémon on Pokémon Go because of a cease and desist order from Niantic.

 

Ich habe im August 2016 einen Twitter Bot programmiert, der seltene Pokémon der App Pokémon Go in der Stadt Luzern ankündet. Der Bot verstösst gegen die Nutzungsbedingungen von Niantic, da er inoffizielle API-Anfragen an deren Server schickt, um so die seltenen Monster zu finden. Nun habe ich eine Abmahnung von Niantic erhalten und mich entschieden zu kooperieren. Ab sofort wird @pokelistener keine Tweets mehr von sich geben und der Code hinter dem Account wird gelöscht.

Ich danke allen ~840 Follower für ihr Verständnis und positives Feedback über die letzten Wochen. Ebenfalls ein Danke an die Facebook-Seite Pokémon Go Luzern und Sandro Bucher von 20 Minuten für ihre jeweiligen Beiträge, welche den Bot unter Pokémon Go Spielern in Luzern berühmt gemacht haben.

– Patrick Muff

How to Programmatically Print Labels/Barcodes on a Zebra Printer

Here are a few things you need to know for when you want to print labels/barcodes using a Zebra Technologies printer.

Once you’ve figured this stuff out it’s actually pretty easy to embed a Zebra printer into your environment. I hope this has saved you some time.

Accessing JavaScript Properties via a Dot Notation String at Runtime

Problem

You have something like the following object and a string which you would like to use to access a property on said object:

{
  common: {
    de: {
      library: 'Bibliothek',
      manufacturer: 'Hersteller'
    }
  }
}
"common.de.library"

Solution

var accessPropertyViaString = function(object, keys) {
  try {
    return keys.split('.').reduce((prev, curr) =&gt; prev[curr], object) || keys;
  } catch (ex) {
    return keys;
  }
};

This function returns the value of the wanted property. If it can’t find the property it will return the input string. Please do not try to solve this problem using eval() for reasons explained in this answer on StackOverflow.