Skip to content

Instantly share code, notes, and snippets.

View MaxXxiMast's full-sized avatar

Purujit Negi MaxXxiMast

View GitHub Profile
@MaxXxiMast
MaxXxiMast / combinedClassWidth.js
Created May 17, 2018 05:50
Get the combined width of all the elements with specific className present in a DOM
const classObj = document.querySelectorAll('.mv-title');
const classArr = classObj.map((value,index) => [value.offsetWidth]);
const sumOfWidths = classArr.reduce((total, value) => total + value);
console.log(sumOfWidths);
@MaxXxiMast
MaxXxiMast / flex.csv
Last active November 5, 2018 06:26
Flex Shorthand Cheat-sheet
Value Longhand Summary
initial 0 1 auto cannot grow but can shrink when there isn't enough space
auto 1 1 auto can grow and shrink to fit available space
none 0 0 auto cannot grow or shrink AKA inflexible
<positive-number> <positive-number> 1 0 can grow and shrink with extent of growth depending on flex factor
//Deep copying objects
//Method 1
var cloneObject = jsonObject => JSON.parse(JSON.stringify(jsonObject));
//Method 2
function cloneObject(jsonObject){
var cloned_obj = {};
for(let key in jsonObject){
if(jsonObject.hasOwnProperty(key)){
cloned_obj[key] = jsonObject[key];
https://reqres.in/
http://www.omdbapi.com/
function textSize(text) {
if (!d3) return;
var container = d3.select('body').append('svg');
container.append('text').attr('x', -99999).attr('y',-99999).text(text);
var size = container.node().getBBox();
container.remove();
return { width: size.width, height: size.height };
}
// Usage: textSize("This is a very long text");
@MaxXxiMast
MaxXxiMast / array_intersect.min.js
Last active January 16, 2019 08:54
Calculate the Intersection of Arrays
function array_intersect(){var a,b,c,d,e,f,g=[],h={},i;i=arguments.length-1;d=arguments[0].length;c=0;for(a=0;a<=i;a++){e=arguments[a].length;if(e<d){c=a;d=e}}for(a=0;a<=i;a++){e=a===c?0:a||c;f=arguments[e].length;for(var j=0;j<f;j++){var k=arguments[e][j];if(h[k]===a-1){if(a===i){g.push(k);h[k]=0}else{h[k]=a}}else if(a===0){h[k]=0}}}return g}
@MaxXxiMast
MaxXxiMast / spiral_matrix.js
Created January 21, 2019 16:33
Flatten a 2D Array into a spiral format
//ES6
function spiral(matrix) {
const arr = [];
while (matrix.length) {
arr.push(
...matrix.shift(),
...matrix.map(a => a.pop()),
...(matrix.pop() || []).reverse(),
...matrix.map(a => a.shift()).reverse()
@MaxXxiMast
MaxXxiMast / plusplus.js
Created June 24, 2019 13:15
How a ++ operator works
function plusplus(orig_x){
let orig_x_changed = Number(orig_x);
x = orig_x_changed + 1;
return orig_x_changed;
}
let x = '2';
plusplus(x); // 2
x; // 3
@MaxXxiMast
MaxXxiMast / checkSign.js
Last active June 25, 2019 06:45
Check the Sign on a Number taking -ve 0 into consideration
function checkSign(x){
return x !== 0 ? Math.sign(x) : Object.is(x, -0) ? -1 : 1;
}
checkSign(-0) // -1 i.e. -ve sign
checkSign(0) // 1 i.e. +ve sign
checkSign(-6) // -1
checkSign(6) // 1
credits: Kyle Simpson (getify)
@MaxXxiMast
MaxXxiMast / objectIsPolyfill.js
Last active June 25, 2019 08:12
Object.is Polyfill
if (!Object.is) {
Object.is = function ObjectIs(x, y) {
if (x === y) {
return x !== 0 || 1/x === 1/y
} else {
return x !== x && y !== y;
}
}
}