Category Archives: Software Development

This category contains all my posts about software development. Tutorials, solutions to problems I’ve met, interesting things I’ve stumbled over, code samples, stuff I don’t want to forget, et cetera.

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');

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

Using SSH keys with BitBucket/GitHub on Windows

Couldn’t get this to work, but now it does, so… time for another “note to self”. 🙂

Prerequisites

  • Git, obviously…
  • PuTTY, with puttygen, plink and pageant, to be exact…

Setup

  1. Open puttygen.
  2. Load your private key, or generate a new one and save it.
  3. Copy your public key (“Public key for pasting …”) to BitBucket/GitHub/etc.
  4. Open pageant
  5. Load your private key.
  6. Check that the key authentication works by running e.g.
    plink -v git@bitbucket.org
    plink -v git@github.com
  7. Point the GIT_SSH environment variable to plink.exe.

Now, as long as pageant is running with your private key loaded, it should work to clone, pull, push, etc. to/from both private and public repos. E.g. git clone git@github.com:example/some-private-repo.git.

Key here, for me, was the GIT_SSH variable that so many blogs, StackOverflow answers and forum posts failed to mention… Without that set, the plink check did still work, but git clone did not. It just failed with an authentication error. With that variable set it now works perfectly. 🙄🙂👍

Sources: makandracards.com, vladmihalcea.com