All posts by Torleif

JavaScript: In-browser export to CSV

This function will turn an array of data into a CSV file and get the browser to “download” it. It should also be wrapping and escaping values properly and, because of the BOM, be read correctly when opened in e.g. Excel.

It’s written in TypeScript ("lib": ["ES2017", "DOM"]), but should be easy to “downgrade” to regular JS if needed…

type GetValue <T> = <I extends T>(item: I) => any;
type FieldName <T> = keyof T;

export interface Columns<T> {
  [s: string]: FieldName<T> | GetValue<T>;
}

const COLUMN_SEPARATOR = ';';
const ROW_SEPARATOR = '\r\n';
const UNICODE_BOM = '\uFEFF';

const wrapValue = (value: string) => `"${value}"`;
const escapeValue = (value: string) => (value || '').replace(/"/, '""');

const toHeaderRow = <T> (columns: Columns<T>) => Object
  .keys(columns)
  .map(escapeValue)
  .map(wrapValue)
  .join(COLUMN_SEPARATOR);

const toRow = <T> (columns: Columns<T>, item: T) => Object
  .values(columns)
  .map(field => typeof field === 'function' ? field(item) : item[field])
  .map(String)
  .map(escapeValue)
  .map(wrapValue)
  .join(COLUMN_SEPARATOR);

export const exportToCsv = function <T> (data: T[], columns: Columns<T>, filename: string): void {
  const rows = [];

  rows.push(toHeaderRow(columns));

  for (const item of data) {
    rows.push(toRow(columns, item));
  }

  const csv = UNICODE_BOM + rows.join(ROW_SEPARATOR);
  const uri = `data:text/csv;charset=utf-8;header=present,${encodeURIComponent(csv)}`;

  const link = document.createElement('a');
  link.setAttribute('href', uri);
  link.setAttribute('download', filename);
  link.addEventListener('click', () => link.parentNode.removeChild(link));
  document.body.appendChild(link);

  link.click();
};

Usage

const users: User[] = [{id: 1, name: 'Alice', isCool: true}, ...];
const columns: Columns<User> = {
  'Id': 'id',
  'Name': 'name',
  'Is cool': user => user.isCool ? 'Yes' : 'No',
};
exportToCsv(users, columns, 'users.csv');

Minecraft: Remove invisible Roots 2 mob spawner

Playing Enigmatica 2 and found a Roots 2 hovel, half into the ground, thatch roof, chests and monsters inside. Cleaned it out, filled it with dirt. Thought it was gone. But monsters kept spawning… Inside the dirt… Opened it back up… Monsters kept spawning… No spawners in sight…

Turns out, there was an invisible entity doing the spawning… Was able to fix it by standing close by and running the following command:

/kill @e[type=!Player,r=10]
Killed entity.barrow.name

After that, no mobs… weird stuff…

Source: forum.feed-the-beast.com

Fix IntelliJ Alt Gr not working

I’ve several times had the Alt Gr key stop working while using IntelliJ. On norwegian keyboards, this key is essential in typing e.g. the brackets {} and [], so it not working is extremely annoying. And what increases the annoyance even more, is that, when this happens, the key for some reason stops working in other programs too.

Anyways, this seemed to fix it, on my machine at least, for now:

  1. Go to Help \ Edit Custom Properties
  2. Paste in this line:
    actionSystem.force.alt.gr=true
  3. Restart IntelliJ

What’s running on port 80 in Windows?

Trying to set up Apache on a server, something is hogging port 80, but very “helpfully” the Resource Monitor just reports “System” with PID 4…

However, some commands found in a StackOverflow answer and its comments were helpful:

netsh http show urlacl
netsh http show servicestate
net stop http

Note: Do not just run these commands blindly and turn off services (in particular you should probably answer N when the last one asks…), but use them to identify what service(s) might be to blame. Then do an intelligent decision on whether the service is needed or not, before you potentially stop it and disable it…