Skip to content

Instantly share code, notes, and snippets.

View smitroshin's full-sized avatar

Serghei Mitroșin smitroshin

  • Amdaris
  • Chisinau, Moldova
View GitHub Profile
@smitroshin
smitroshin / decodeUrlifiedObject.js
Created October 8, 2020 20:34
Decode URL-ified object
/**
* Decode URL-ified object
*
* @param {string} urlifiedObj
* @returns {any}
*/
const decodeUrlifiedObject = (urlifiedObj) => JSON.parse(decodeURI(string));
@smitroshin
smitroshin / classNames.js
Created November 13, 2020 15:34
Easy way to put conditions into class names
/**
* Filter a list of expressions to final string.
*
* @param args
* @returns {string}
*/
const classNames = (...args) =>
args
.reduce((accumulator, itm) => {
if (itm) {
@smitroshin
smitroshin / deepFind.js
Created November 24, 2020 14:04
Get value from object by path
/**
* Get value from object by path (ex. parent.child.id)
* Source: https://stackoverflow.com/questions/8817394/javascript-get-deep-value-from-object-by-passing-path-to-it-as-string
*
* @param {object} obj
* @param {string} path
* @returns {undefined|*}
*/
const deepFind = (obj, path) => {
const paths = path.split('.');
@smitroshin
smitroshin / objectRemoveEmpty.js
Created January 20, 2021 00:16
Clear object from empty values (recursively)
/**
* Clear object from empty values (recursively)
* Source: https://stackoverflow.com/a/38340374
*
* @param {object} obj
* @returns {object}
*/
const objectRemoveEmpty = (obj) => {
const newObj = {};
Object.keys(obj).forEach((key) => {
@smitroshin
smitroshin / arrIsEqual.js
Last active March 26, 2021 22:57
arrIsEqual
/**
* Checking equality between two arrays ignoring order.
*
* Source: https://www.30secondsofcode.org/blog/s/javascript-array-comparison
*
* Limitations:
* - primitive values only
* - first level only
* - can't compare objects
*
@smitroshin
smitroshin / requestSimulator.js
Last active March 23, 2021 16:05
Tool for simulation of http requests
const randomInteger = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
const randomBoolean = () => Math.random() >= 0.5;
const defaultTime = () => randomInteger(1000, 3000);
/**
* Tool for simulating http requests
*
@smitroshin
smitroshin / name-that-color.js
Created August 18, 2021 20:55 — forked from ffffranklin/name-that-color.js
Name That Color - Created by Chirag Mehta
/*
+-----------------------------------------------------------------+
| Created by Chirag Mehta - http://chir.ag/projects/ntc |
|-----------------------------------------------------------------|
| ntc js (Name that Color JavaScript) |
+-----------------------------------------------------------------+
All the functions, code, lists etc. have been written specifically
for the Name that Color JavaScript by Chirag Mehta unless otherwise
@smitroshin
smitroshin / pipe.js
Created August 25, 2021 16:38
Function composition - from left to right
/**
* Compose functions from left to right.
*
* Source: https://1loc.dev/#compose-functions-from-left-to-right
*
* Read more by googling: "js pipe", "js pipeline", "js compose from left to right".
*
* @param {...function} fns
* @returns {*}
*/
@smitroshin
smitroshin / randomRange.js
Created December 1, 2021 14:29
Generate Random Whole Numbers within a Range
/**
* Generate Random Whole Numbers within a Range
*
* Source: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range
*
* @param {number} min
* @param {number} max
*/
const randomRange(min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
@smitroshin
smitroshin / spinalCase.js
Last active December 18, 2021 12:44
Parse string to spinal case format
/**
* Transforms string from any format case in spinal case (ex. spinal-case)
*
* Source: https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-spinal-tap-case/16078
*
* @param {string} str
*/
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins