Skip to content

Instantly share code, notes, and snippets.

View stefanmaric's full-sized avatar
🐿️
ship it

Stefan Maric stefanmaric

🐿️
ship it
View GitHub Profile
@stefanmaric
stefanmaric / selectRelativeTimeUnit.js
Last active September 21, 2021 22:20
Util function to select unit and value for the Intl.RelativeTimeFormat API
export const UNITS = [
{
multiplier: 1000,
name: 'second',
threshold: 45,
},
{
multiplier: 60,
name: 'minute',
threshold: 45,
@stefanmaric
stefanmaric / README.md
Created March 21, 2020 18:47
CS:Source setup on Ubuntu

CS:Source setup on Ubuntu

@stefanmaric
stefanmaric / deepDefaults.js
Created July 2, 2019 16:21
Deep Defaults function in JavaScript
const defaultPredicate = (x) => x === null || x === undefined
const deepDefaults = (target, defaults, predicate = defaultPredicate) => {
if (Object(target) !== target) return Object.assign({}, defaults)
return Object.keys(defaults).reduce((acc, key) => {
if (Object(defaults[key]) === defaults[key]) {
acc[key] = deepDefaults(target[key] || {}, defaults[key], predicate)
} else {
acc[key] = predicate(target[key], key) ? defaults[key] : target[key]
@stefanmaric
stefanmaric / loadIframe.js
Created July 2, 2019 16:20
Async loading of diff resource types in the DOM
const loadIframe = (url, attributes = {}, target = document.body) =>
new Promise((resolve, reject) => {
const iframe = document.createElement('iframe')
const {
height = 0,
width = 0,
style = {
display: 'none',
},
@stefanmaric
stefanmaric / luhnCheck.js
Created July 2, 2019 16:16
Luhn Check function in JavaScript for credit card validation
const map = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
const luhnCheck = (input) => {
const number = String(input).replace(/\D/g, '')
let sum = 0
let digit = 0
let i = number.length
let even = true
while (i) {
@stefanmaric
stefanmaric / toBase.js
Created April 1, 2019 14:16
Get a string represnetation of an integer in any base
/**
* Transform a number to any base based on a provided range.
* TODO: doesn't support floats.
* @param {number} baseTen A regular, primitive integer number
* @param {Array<string>} range And array of characters to represent the positional system.
* @returns {string} A representation of the provided number in the positional system provided.
*/
const toBase = (baseTen, range = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')) => {
if (baseTen % 1 > 0) throw new TypeError('Only integers are supported')
if (baseTen === 0) return range[0]
@stefanmaric
stefanmaric / tick.mjs
Created December 18, 2018 17:41
Javascript tick function, wrapper around requestAnimationFrame for easier usage
/**
* Wrapper around requestAnimationFrame for easier usage.
* @param {Function} callback Function to call on each tick, receives last value as only param.
* @param {*} [initValue] Optional initial value.
* @returns {Function} Clear interval function.
*/
const tick = (callback, initValue) => {
let unsubscribed = false
let lastValue = initValue
@stefanmaric
stefanmaric / notes.md
Created August 13, 2018 13:33
Fixes for Linux running on the Dell XPS 15 9550

Bluetooth

Download the BCM20703A1-0a5c-6410.hcd file as /lib/firmware/brcm/BCM-0a5c-6410.hcd.

@stefanmaric
stefanmaric / exclusion-a.mjs
Created July 26, 2018 23:36
Alternatives for exclusion with lodash
import * as _ from 'lodash'
const exclusion = (...arrays) =>
_.flatMap(arrays, x => _.difference(x, ..._.without(arrays, x)))
@stefanmaric
stefanmaric / defer.js
Created February 12, 2018 13:00
Javascript ES6 defer function to create deferred Promises with extra resolve and reject methods
/**
* Create an new deferred promise that can be resolved/rejected from outside.
* @return {Promise} A new Promise with two extra methods: resolve and reject.
*
* @example
* const unknownResult = () => {
* const deferredPromise = defer()
*
* const errorTimeoutId = setTimeout(
* () => {