This file contains 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
/** | |
* 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 |
This file contains 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
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 () => { |
This file contains 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
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', |
This file contains 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
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, |
This file contains 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 (_) { | |
_.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); | |
} | |
} |
This file contains 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
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); |
This file contains 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
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); |
This file contains 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
// 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) { |
This file contains 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 (_) { | |
_.each("Function String Number Date RegExp".split(" "), function(name) { | |
_["are" + name + (name.substr(-1) !== "s" ? "s" : "")] = function () { | |
return _.all(_.toArray(arguments), _["is" + name]); | |
} | |
}); | |
})(_); |
This file contains 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 ($, 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) { |