Skip to content

Instantly share code, notes, and snippets.

View jkohlin's full-sized avatar

Johan jkohlin

  • Sweden
  • 22:46 (UTC +02:00)
View GitHub Profile
@jkohlin
jkohlin / index.html
Created June 28, 2017 10:58
Frequency and amplitude visualiser using web audio API
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Web audio API</title>
<style>
</style>
</head>
<body>
<button id="play-btn" style="display:none;">play/pause</button>

Palpatine

So writing md in the app without the option of seeing a preview?

However, it seems very slow on save/re-edit. If you save, click done, to see the preview and then click edit it says it's still saving it and it takes quite a while.

@jkohlin
jkohlin / asyncAlert.js
Created May 16, 2022 12:59
Await an alert dialogue
async function Alert(msg) {
await (msg => {
return Promise.resolve(alert(msg))
})(msg)
// things you want to do after the alert OK-button is clicked
}
Alert('Take your time…')
@jkohlin
jkohlin / A better typeof
Created June 1, 2022 08:41
Returns the actual type of a value
function typeOf (value) {
return Object.prototype.toString.call(value)
.replace(/^\[object\s+([a-z]+)\]$/i, '$1')
.toLowerCase();
}
@jkohlin
jkohlin / objectFilter
Created July 4, 2022 09:44
A function to filter an object based on another object, or array. Filter either by key or by value, including or excluding.
/**
* Description: Remove properties from an object based on a filter
* @param {object} obj The object to filter out elements from
* @param {[string]|object} filter The array, or object to use as filter
* @param {boolean} filterIsKey (default true) wether the filter is referring to the key or the value
* @param {boolean} inclusive (default true) wether the filter is what we want to keep, or what we want to remove
* @returns {object} A new filtered object
*/
function objectFilter (obj, filter, filterIsKey = true, inclusive = true) {
return Array.isArray(filter)
@jkohlin
jkohlin / groupBy.js
Created December 19, 2022 09:22
From array of objects to an object with keys taken from a value in each object
/**
* Description
* @param {Array} list
* @param {string} key
* @returns {any}
*/
export const groupBy = (list, key) => {
return (
list?.reduce((acc, row) => {
// acc = {}, row[key] = exv 'SITE' (key='listType') acc.SITE är undefined från början, men propen skapas iom exp=exp och sätts till [] eftersom acc[row[key]] är undefined. Nästa varv finns propen med arrayen med en rad i.
@jkohlin
jkohlin / getParams.js
Created December 19, 2022 09:23
Return all search params from a url
/**
* Description Plockar ut URL-parametrar som ett objekt plus andra godsaker.
* @param {string} href
* @returns {Object} {...allParams, href:getterFunction, baseUrl:URL, params:URLSearchParams}
*/
function getParams(href) {
const baseUrl = new URL(href)
var paramSeparator = baseUrl.hash.startsWith('#') ? '#' : '?'
var params = baseUrl.hash.startsWith('#') ? new URLSearchParams(baseUrl.hash.slice(1)) : new URLSearchParams(baseUrl.search)
const obj = { baseUrl, get href() { return this.baseUrl.origin + this.baseUrl.pathname + paramSeparator + this.params.toString() }, params }
@jkohlin
jkohlin / objectFilter.js
Created December 19, 2022 09:36
Filter out keys and or values in an object
/**
* Description
* @param {Object} obj
* @param {function} keyFilter
* @param {function} valueFilter
* @returns {Object}
*/
const objectFilter = (obj, keyFilter, valueFilter) => {
let array = Object.entries(obj) // [ [key, value], [key, value] ]
if (typeof keyFilter === 'function') {
@jkohlin
jkohlin / arrayFromObject.js
Created December 19, 2022 09:41
Turn an object to an array of one-key-objects
/**
* Description Splits out all keys from an object and returns them as separate objects with single key/value pairs
* @param {Object} obj
* @returns {Array}
*/
function ArrayFromObject(obj) {
return Object.entries(obj).map(([v,k])=>({[v]:k}))
}
@jkohlin
jkohlin / typeOf.js
Created December 19, 2022 09:44
A more fine grained typeof that returns e.g. 'array' | 'date' | etc
/**
* Description
* @param {any} value
* @returns {string}
*/
function typeOf(value) {
return Object.prototype.toString
.call(value)
.replace(/^\[object\s+([a-z]+)\]$/i, '$1')
.toLowerCase()