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 makeCircle(pos = [0, 0], r = 0) { | |
let hits = 0; | |
const obj = { | |
pos, | |
getR: () => r, | |
getHits: () => hits, | |
incHits: () => (hits += 1), | |
area: () => r ** 2 * Math.PI, | |
intersect: (circle) => { |
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
const c1 = new Circle([2.5, 0], 1.5); | |
const c2 = new Circle([-2, 0], 2); | |
c1.area(); // -> 7.0685834705770345 | |
c2.area(); // -> 12.566370614359172 | |
c1.intersect(c2); // -> false | |
c1.getHits(); // -> 0 | |
c1.pos[0] = 1; |
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 Circle(pos = [0, 0], r = 0) { | |
this.pos = pos; | |
let hits = 0; | |
this.getR = () => r; | |
this.getHits = () => hits; | |
this.incHits = () => (hits += 1); | |
this.area = () => r ** 2 * Math.PI; | |
this.intersect = circle => { |
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
const c1 = new Circle([2.5, 0], 1.5); | |
const c2 = new Circle([-2, 0], 2); | |
c1.area(); // -> 7.0685834705770345 | |
c2.area(); // -> 12.566370614359172 | |
c1.intersect(c2); // -> false | |
c1.hits; // -> 0 | |
c1.pos[0] = 1; |
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
class Circle { | |
constructor(pos = [0, 0], r = 0) { | |
this.pos = pos; | |
this.radius = r; | |
this.hitCount = 0; | |
} | |
get r() { | |
return this.radius; | |
} |
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
const { parent: parentId } = item; | |
const { [parentId]: { types = [], name = 'Noname' } = {} } = items; |
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
const parentId = item.parent; | |
const parent = parentId ? (items[parentId] || {}) : {}; | |
const types = parent.types || []; | |
const name = parent.name || 'Noname'; |
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
// item and items are predefined | |
const parent = items[item.parent]; | |
const types = parent.types; | |
const name = parent.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
describe('publish/subscribe 3', () => { | |
it('should send a message from publish to subscribe', async () => { | |
let result; | |
const [promise, resolve] = triggerPromise(); | |
subscribe('ch1', (msg) => { | |
result = msg; | |
resolve(); | |
}); | |
publish('ch1', 1); | |
await asyncWithTimeout(promise); |
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
const asyncWithTimeout = (promise, timeout = 900) => | |
Promise.race([ | |
promise, | |
new Promise((_, reject) => | |
setTimeout(() => reject(Error('Async timeout')), timeout) | |
), | |
]); |