Skip to content

Instantly share code, notes, and snippets.

Avatar
🐿️
ship it

Stefan Maric stefanmaric

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

CS:Source setup on Ubuntu

@stefanmaric
stefanmaric / deepDefaults.js
Created Jul 2, 2019
Deep Defaults function in JavaScript
View deepDefaults.js
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 Jul 2, 2019
Async loading of diff resource types in the DOM
View loadIframe.js
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 Jul 2, 2019
Luhn Check function in JavaScript for credit card validation
View luhnCheck.js
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 Apr 1, 2019
Get a string represnetation of an integer in any base
View toBase.js
/**
* 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 Dec 18, 2018
Javascript tick function, wrapper around requestAnimationFrame for easier usage
View tick.mjs
/**
* 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 Aug 13, 2018
Fixes for Linux running on the Dell XPS 15 9550
View notes.md

Bluetooth

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

@stefanmaric
stefanmaric / exclusion-a.mjs
Created Jul 26, 2018
Alternatives for exclusion with lodash
View exclusion-a.mjs
import * as _ from 'lodash'
const exclusion = (...arrays) =>
_.flatMap(arrays, x => _.difference(x, ..._.without(arrays, x)))
@stefanmaric
stefanmaric / defer.js
Created Feb 12, 2018
Javascript ES6 defer function to create deferred Promises with extra resolve and reject methods
View defer.js
/**
* 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(
* () => {