Typescript / Javascript

Arrays

Create an array from 0 to n-1

let x = [...Array(n).keys()];

Create a m x n array with a default value

const defaultValue = 0;
let x = Array.from(Array(m), () => Array.from(Array(n), () => defaultValue));

Encode/decode

Encode a string to Run Length Encoding (RLE)

function encodeRLE(input: string) {
let output = '';
const match = input.match(/(.)\1*/g);
if (match !== null) {
match.forEach(s => {
if (s.length > 1) {
output += s.length.toString();
}
output += s[0];
});
}
return output;
}

Decode a string from Run Length Encoding (RLE)

function decodeRLE(input: string) {
let output = input.replace(/([0-9]+)([^0-9])/g,
(match, p1, p2) => {
return p2.repeat(parseInt(p1));
});
return output;
}