Skip to content

Instantly share code, notes, and snippets.

@krazov
krazov / immutability-param-reassign.js
Last active March 12, 2017 20:39
Reassigning param you’re entering world of pain, son
function letsWasteParam(array, getOriginal) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i] * 2;
}
if (!getOriginal) {
return 'Doubled array: ' + array;
} else {
return 'Original array: ' + array; // oops
}
@krazov
krazov / immutability-params.js
Created March 12, 2017 21:05
Default param value
// ES5
function safeGuardES5(x) {
x = x || 7;
// magic
}
function safeGuardES5immutable(paramX) {
var x = paramX || 7;
@krazov
krazov / immutability-dto.js
Created March 12, 2017 21:45
Using object container
function doingSomething3D(params) {
var x = params.x || 0;
var y = params.y || 0;
var z = params.z || 0;
return 'x: ' + x +', y: ' + y + ', z: ' + z;
}
function theNewBlack({ x = 0, y = 0 , z = 0 } = {}) {
return `x: ${x}, y: ${y}, z: ${z}`;
@krazov
krazov / immutability-array.js
Last active March 12, 2017 22:08
Array reorganizing went South
function ArrayContainer(array) {
this.array = array;
}
// before
ArrayContainer.prototype.getNewOrder = function (startAt) {
if (!startAt) {
return this.array;
}
@krazov
krazov / object-assign.js
Created March 12, 2017 22:39
A moment of joy for such a high price
const object1 = {
name: 'John',
surname: 'Carbonlover'
};
const object2 = Object.assign(object1, {
name: 'Frank'
});
// object1.name is now 'Frank', the same like object2’s
@krazov
krazov / objet-freeze.js
Created March 12, 2017 22:56
Freezing of an object
const cantTouchThis = {
id: 1,
type: 'fromage',
items: {
knife: 'bowie',
fork: 'krautrock'
}
};
Object.freeze(cantTouchThis);
@krazov
krazov / fibonacci-eventlooped.js
Last active February 8, 2018 15:24
Some fun with `setImmediate`
function fibonacciProcessed(x) {
return new Promise(resolve => {
const calculation = function (resolveFn, n = 0, a = 0, b = 1) {
if (n > 1) {
setImmediate(() => {
calculation(resolveFn, n - 1, b, a + b);
});
}
// else
@krazov
krazov / reduce-map.js
Created March 19, 2017 22:18
Map with reduce
// piece taken from: https://medium.com/javascript-scene/reduce-composing-software-fe22f0c39a1d#.4chgx5fc0
const map = (fn, arr) => arr.reduce((acc, item, index, arr) => {
return acc.concat(fn(item, index, arr));
}, []);
@krazov
krazov / reduce-filter.js
Created March 19, 2017 22:20
Filter with reduce
// piece taken from: https://medium.com/javascript-scene/reduce-composing-software-fe22f0c39a1d#.4chgx5fc0
const filter = (fn, arr) => arr.reduce((newArr, item) => {
return fn(item) ? newArr.concat([item]) : newArr;
}, []);
@krazov
krazov / reduce-slice.js
Last active March 19, 2017 23:34
Slice with reduce
const slice = (array, from, to) => {
const startIndex = from < 0 ? array.length + from : from;
const endIndex = to ? (to < 0 ? index < array.length + to : index < to) : index < array.length;
return array.reduce((accumulated, current, index) => {
return index >= startIndex && index < endIndex ? accumulated.concat(current) : accumulated;
}, []);
};