This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Description | |
* @param {Date} {date | |
* @param {number} offset=0 | |
* @param {'nice|short|iso|none':string} format='nice'} | |
* @returns {Date} | |
*/ | |
export const niceDate = ({ date, offset = 0, format = 'nice' }) => { | |
const offsetDate = getOffsetDate(date, offset) | |
switch (format) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Description | |
* @param {Date} date | |
* @param {number} offset | |
* @returns {Date} | |
*/ | |
export const getOffsetDate = (date, offset) => { | |
return new Date(date.setDate(date.getDate() - offset * 7)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Description | |
* @param {any} value | |
* @returns {string} | |
*/ | |
function typeOf(value) { | |
return Object.prototype.toString | |
.call(value) | |
.replace(/^\[object\s+([a-z]+)\]$/i, '$1') | |
.toLowerCase() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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})) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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') { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function typeOf (value) { | |
return Object.prototype.toString.call(value) | |
.replace(/^\[object\s+([a-z]+)\]$/i, '$1') | |
.toLowerCase(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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…') |