Skip to content

Instantly share code, notes, and snippets.

@Dunkelheit
Dunkelheit / index.test.js
Created January 11, 2021 09:24
sinon's restore vs. reset
'use strict';
const chai = require('chai');
const sinon = require('sinon');
chai.use(require('sinon-chai'));
const expect = chai.expect;
const sandbox = sinon.createSandbox();
@Dunkelheit
Dunkelheit / deep.equal.2.js
Last active October 29, 2020 11:17
deep.equal.2
describe('A person', () => {
it('Has all the right properties and values', () => {
expect(person).to.be.an('object').and.have.keys('guid', 'name', 'company', 'email', 'phone',
'isActive', 'age', 'gender');
expect(person.guid).to.equal('79c936b0-c75f-4b09-b53e-8d7822bbe17b');
expect(person.name).to.equal('Mcguire English');
expect(person.company).to.equal('ZILLAR');
expect(person.email).to.equal('[email protected]');
expect(person.phone).to.equal('+1 (555) 555-5555');
expect(person.isActive).to.equal(true);
@Dunkelheit
Dunkelheit / deep.equal.js
Created October 29, 2020 10:48
deep.equal
describe('A person', () => {
it('Has all the right properties and values', () => {
expect(person).to.be.deep.equal({
guid: '79c936b0-c75f-4b09-b53e-8d7822bbe17b',
name: 'Mcguire English',
company: 'ZILLAR',
email: '[email protected]',
phone: '+1 (555) 555-5555',
isActive: false,
age: 35,
@Dunkelheit
Dunkelheit / merge.js
Created October 24, 2020 14:31
Deep object merging - 2
const lorem = {
ipsum: {
dolor: {
sit: 'amet'
}
}
};
const foo = {
ipsum: {
@Dunkelheit
Dunkelheit / merge.js
Last active October 24, 2020 14:29
Deep object merging - 1
const lorem = {
ipsum: {
dolor: {
sit: 'amet'
}
}
};
const foo = {
bar: 'baz'
@Dunkelheit
Dunkelheit / pending-promise-recycler-express.js
Created October 21, 2020 14:01
pending-promise-recycler-express - part 2
'use strict';
const express = require('express');
const recycle = require('pending-promise-recycler');
const server = express();
const someSlowAndExpensiveOperation = function () {
return new Promise((resolve, reject) => {
console.log('Starting a very slow operation!')
@Dunkelheit
Dunkelheit / pending-promise-recycler-express.js
Last active October 21, 2020 14:05
pending-promise-recycler-express - part 1
'use strict';
const express = require('express');
const recycle = require('pending-promise-recycler');
const server = express();
const someSlowAndExpensiveOperation = function () {
return new Promise((resolve, reject) => {
console.log('Starting a very slow operation!')
@Dunkelheit
Dunkelheit / pending-promise-recycler.js
Created October 17, 2020 11:51
pending-promise-recycler - step 1
const fetchSomethingExpensive = (arg1, arg2) => {
return new Promise(resolve => {
// Assume there is a call to a 3rd party API here -it will take ~300 ms. to respond
setTimeout(() => {
resolve({ foo: 'bar' });
}, 300);
});
};
@Dunkelheit
Dunkelheit / calculateWeaponDamage.js
Created August 19, 2020 17:44
Destructured function parameters and code maintainability: example 7
/**
* Calculates the damage of a melee attack.
*
* @param {object} weapon - Weapon used in the attack.
* @param {object} interactives - Interactive creatures involved in the melee attack.
* @param {object} interactives.attacker - Player who inflicts the attack.
* @param {object} [interactives.defender] - Player who inflicts the attack.
* @param {object} [options] - Options of this function.
* @param {boolean} [options.inflictInDefender=false] - Whether to actually inflict the damage on <code>defender</code>.
* @param {boolean} [options.ignoreMagicalResistance=false] - If <code>true</code> magical resistances will be ignored in the calculations.
@Dunkelheit
Dunkelheit / calculateWeaponDamage.js
Last active August 19, 2020 17:32
Destructured function parameters and code maintainability: example 6
/**
* Calculates the damage of a melee attack.
*
* @param {object} weapon - Weapon used in the attack.
* @param {object} interactives - Interactive creatures involved in the melee attack.
* @param {object} interactives.attacker - Player who inflicts the attack.
* @param {object} [interactives.defender] - Player who inflicts the attack.
* @param {object} options - Options of this function.
* @param {boolean} [options.inflictInDefender=false] - Whether to actually inflict the damage on <code>defender</code>.
* @param {boolean} [options.ignoreMagicalResistance=false] - If <code>true</code> magical resistances will be ignored in the calculations.