Skip to content

Instantly share code, notes, and snippets.

View audunolsen's full-sized avatar
🌷
Made a new avatar with Midjourney lmao

Audun Meek Olsen audunolsen

🌷
Made a new avatar with Midjourney lmao
View GitHub Profile
@audunolsen
audunolsen / 1-createElement.litcoffee
Last active November 27, 2018 11:24
Helper Function for Creating HTML DOM Element Objects · JS/CoffeeScript

Create HTML DOM Element Objects With Ease

A helper function which lets you create HTML DOM Element Objects where tagtype, ID and classes are set through basic CSS selector syntax. Pass an object with any arbitrary key value pairs as a second argument and this will set the element's other attributes. If a tagtype isn't defined a div is created.

createElement = (selectorName, attributes) ->

    identifiers = selectorName.match /(?=^[a-z|A-Z]|\.|#).+?(?=\.|#|$)/g
    identifiers =
        tag     : if (ref = identifiers[0]).charAt(0) not in [".", "#"] then ref else "div"
        id      : if (ref = identifiers.filter((str) => str.charAt(0) is "#")[0]) then ref.substring 1
@audunolsen
audunolsen / 1-template-string-oneline.js
Last active November 29, 2018 14:50
Single Line — Tagged Template Literals
oneline = (strings, ...values) => {
let interpolated = "", raw = strings.raw, ref;
for (let [i, string] of raw.entries())
interpolated += ((ref = values[i-1]) ? ref : "") + string;
return interpolated
.split("\n")
.map((line, i, lines) =>
/\\$/.test((ref = line.trim())) ? ref.slice(0, -1) :
@audunolsen
audunolsen / isInViewport.js
Last active March 5, 2019 14:50
Check if any part of element is visible inside viewport – vertical only, one-liner.
/* Check if any part of element is visible inside viewport – vertical only, one-liner.
*
* Inspired by @StokeMasterJack's reply to a gist thread about element visibility inside the browser:
* https://gist.github.com/davidtheclark/5515733#gistcomment-2113205
*
* I needed a vertical only one and wanted to rewrite it to be as short as possible w/
* ES6's destructuring, optional parameters and arrow function implicit returns
*
* @global
* @param1 {HTMLElement} - The element to check if visibile in viewport.
@audunolsen
audunolsen / roboto.scss
Created February 21, 2020 14:47
Quick way to set all my local Roboto fonts
/*
I've got the following file structure and want a quick way to set all my local Roboto fonts.
../assets/fonts/Roboto
Roboto-Black.woff
Roboto-Black-Italic.woff
Roboto-Bold.woff
Roboto-Bold-Italic.woff
Roboto-Light.woff
@audunolsen
audunolsen / stylelint.config.js
Last active March 27, 2020 10:43
Stylelint config covering all base rules. TODO: Sass syntax plugin
module.exports = {
plugins: [
"stylelint-order",
]
rules: {
/* Rules from https://stylelint.io/user-guide/rules/list
Fetched 25.03.2020 */
@audunolsen
audunolsen / logger-cli.js
Created March 27, 2020 09:52
A node logger script
/*
SCRIPT WHICH LOGS MISC MESSAGES
Parameters:
--success ||
--error ||
--warning ||
--info boolean Type of log entry, picks first argument found
@audunolsen
audunolsen / random.js
Last active May 8, 2020 16:18
Needed a function which could return either a random array item or a random ranged integer or decimal value with ability to control decimal precision.
const random = ({array: a, min, max, decimal = false, decimalPrecision = 2}) => {
if (Array.isArray(a)) return a[Math.floor(Math.random() * a.length)];
else if ([min, max].every(n => typeof n === "number")) {
min = Math.ceil(min),
max = Math.floor(max);
let num = Math.floor(Math.random() * (max - min + 1)) + min;
@audunolsen
audunolsen / classnames-48b.js
Last active November 6, 2020 09:13
.048kB classnames npm module alternative
/*
the npm module "classnames" has 5,549,574 weekly downloads
as of 06.11.2020. It has an unpacked size of 16.3 kB.
Why TF is such a trivial package so popular given its size?
Here's a 0.048 kB alternative:
*/
@audunolsen
audunolsen / elementVisibility.js
Last active May 13, 2021 21:16
Returns a detailed set of data regarding an elements position relative to the viewport
/* eslint-disable */
/*
type Directions {
top: boolean;
left: boolean;
bottom: boolean;
right: boolean;
horizontal: boolean;

c( 'cls' i === 3 && 'third', e.size === 'xl' ? 'x-large' : 'small', { variableName } )