Skip to content

Instantly share code, notes, and snippets.

View osartun's full-sized avatar

Oliver Sartun osartun

  • Berlin, Germany
View GitHub Profile
@osartun
osartun / coderPadTSTestUtils.ts
Last active March 22, 2024 22:46
Test utilities for TypeScript pads on CoderPad
/**
* You want to use mocha in a TypeScript CoderPad but you get the error
* message that `describe` isn't defined even though you're already using
* the `mocha.suite.emit('pre-require', this, 'solution', mocha)` hack?
*
* > Cannot find name 'describe'. Do you need to install type definitions
* > for a test runner? Try `npm i --save-dev @types/jest` or
* > `npm i --save-dev @types/mocha`.
*
* Here are a couple of lines to copy & paste into your pad to use
@osartun
osartun / async.test.ts
Created July 1, 2019 16:02
Code graveyard
describe('promiseRetry', () => {
it('resolves immediately if successful', async () => {
const expectedValue = 'Expected Value'
const promiseFn = jest.fn().mockResolvedValue(expectedValue)
const result = await promiseRetry(promiseFn)
expect(promiseFn).toHaveBeenCalledTimes(1)
expect(result).toBe(expectedValue)
})
it('retries if unsuccessful', async () => {
@osartun
osartun / hooks.ts
Created June 14, 2019 14:03
Code graveyard
import { useEffect } from 'react'
export const useOnResize = (
onResize: (this: Window, ev: UIEvent) => unknown,
options?: boolean | AddEventListenerOptions | undefined
) => {
useEffect(() => {
if (typeof window === 'object') {
window.addEventListener(
'resize',
@osartun
osartun / getPrototypeChain.js
Created May 15, 2013 11:19
Defines a global getPrototoypeChain function that returns an array of all the objects which are part of the passed object's prototype chain.
var getPrototype = typeof Object.getPrototypeOf === "function" ?
Object.getPrototypeOf :
typeof "".__proto__ === "object" ?
function (obj) {
return obj ? obj.__proto__ : null;
} :
function (obj) {
return obj && obj.constructor ? obj.constructor.prototype : null;
},
nativeIndexOf = Array.prototype.indexOf,
@osartun
osartun / arrangeArguments.js
Created March 19, 2013 15:16
UnderscoreJS-Mixin: Redefine the order of your arguments. Useful for functions which get called by different callers (i.e. DOM-Eventlistener and Backbone-Eventlistener) with different arguments orders. Doesn't change the "this"-context. Use it like this: var funcB = _.arrange(funcA, 1,0,2) funcA(1,2,3) => arguments order: 1,2,3 funcB(1,2,3) => a…
(function (_) {
_.mixin({
arrange : function(func) {
var newOrder = [].slice.call(arguments, 1), arranged, i,l;
return function() {
for (arranged = [], i=0, l=newOrder.length; i<l; i++)
arranged.push(arguments[newOrder[i]]);
return func.apply(this, arranged);
}
}
@osartun
osartun / Math.random-with-scale.js
Created March 8, 2013 16:06
A shorthand for Math.random() * scale: Instead of multiplying Math.random's result, you can pass your scale as the argument to Math.random Before: Math.random() * 4 Now: Math.random(4)
Math.random = (function (_random) {
// _random is the reference to the original / native random
return function (scale) {
(scale && scale === +scale) || (scale = 1); // Make sure scale is set and a number !== 0
return _random() * scale;
}
})(Math.random);
@osartun
osartun / toCamelCase.js
Created March 5, 2013 16:04
Formats a string and returns it in camel case. "Hello World" becomes "helloWorld". As this was just a quick idea, I attached this function to the String's prototype, and so giving a sh*t on best practices. But anyway. You can use it like this: "Hello World".toCamelCase()
String.prototype.toCamelCase = (function (regExp) {
return function () {
return this.toLowerCase().replace(regExp, function (m, a, b) {return a + b.toUpperCase();})
}
})(/(\w)[ \t\r\n\-](\w)/g);
@osartun
osartun / NumberingSystem.js
Last active December 11, 2015 09:29
In case you've always wanted to create your own Numbering System, here you go.
// Your own numbering system
function NumberingSystem(digits) {
this.setSystem(digits);
}
NumberingSystem.prototype = {
setSystem: function (digits) {
this.digits = typeof digits === "string" ? digits : "0123456789";
this.base = this.digits.length;
},
toDecimal: function (nr) {
@osartun
osartun / UnderscoreJS are-Addon
Created November 30, 2012 16:09
Adds areFunctions, areStrings, areNumbers, areDates and areRegExps to the UnderscoreJS-Object
(function (_) {
_.each("Function String Number Date RegExp".split(" "), function(name) {
_["are" + name + (name.substr(-1) !== "s" ? "s" : "")] = function () {
return _.all(_.toArray(arguments), _["is" + name]);
}
});
})(_);
@osartun
osartun / elementsFromPoint
Last active April 16, 2018 22:55
Get a jQuery-set of all the visible elements from one Point in your document. The elements are in reversed order (the element on the top is the last element in the set).
(function ($, document, undefined) {
$.extend({
/**
* A static jQuery-method.
* @param {number} x The x-coordinate of the Point.
* @param {number} y The y-coordinate of the Point.
* @param {Element} until (optional) The element at which traversing should stop. Default is document.body
* @return {jQuery} A set of all elements visible at the given point.
*/
elementsFromPoint: function(x,y, until) {