Skip to content

Instantly share code, notes, and snippets.

@hagemann
hagemann / countries.json
Created January 3, 2018 15:33
List of countries in JSON
{
"AF": "Afghanistan",
"AX": "Åland Islands",
"AL": "Albania",
"DZ": "Algeria",
"AS": "American Samoa",
"AD": "Andorra",
"AO": "Angola",
"AI": "Anguilla",
"AQ": "Antarctica",
@hagemann
hagemann / blocksToHtml.js
Last active October 27, 2022 08:11
Render EditorJS blocks to HTML.
const renderHtml = (blocks) => {
var html = blocks.map(block => {
switch (block.type) {
case 'code':
return `<pre><code>${ block.data.code }</pre></code>`
/**
* Original type is 'header' but I renamed the block to follow correct terminology:
* https://github.com/editor-js/header/issues/21
*/
@hagemann
hagemann / database.js
Last active December 9, 2022 10:24
Promisified MySQL middleware for Node.js
const util = require('util')
const mysql = require('mysql')
const pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'password',
database: 'my_database'
})
@hagemann
hagemann / pdf.js
Created June 4, 2018 14:14
Example to generate PDF with external image
const Printer = require('pdfmake')
const axios = require('axios')
const path = require('path')
module.exports.pdf = async (req, res, next) => {
var printer = new Printer({
Roboto: {
normal: path.resolve('src', 'fonts', 'Roboto.ttf'),
bold: path.resolve('src', 'fonts', 'Roboto-Bold.ttf'),
}
@hagemann
hagemann / slugify.js
Last active October 30, 2023 09:10
Slugify makes a string URI-friendly
function slugify(string) {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word characters