Skip to content

Instantly share code, notes, and snippets.

@ilearnio
ilearnio / debounce.js
Last active September 17, 2021 04:19
Debounce JS function with leading / fire-first option
function debounce(fn, delay, leading) {
var timeout
var firedAt
return function() {
var args = arguments
function fire () {
firedAt = Date.now()
fn.apply(null, args)
}
if (timeout) clearTimeout(timeout)
@ilearnio
ilearnio / findKeyDeep.js
Last active March 23, 2021 16:21
Find keys of objects in a deeply nested object or array and return references of that objects
function findKeyDeep(obj, key) {
const foundKeysReferences = [];
if (Array.isArray(obj)) {
obj.forEach(entry =>
foundKeysReferences.push(...findKeyDeep(entry, key)));
} else if (typeof obj === 'object' && obj !== null) {
if (obj[key]) {
foundKeysReferences.push(obj);
}
Object.values(obj).forEach(value =>
@ilearnio
ilearnio / keybindings.json
Created June 8, 2019 11:05
VSCode keybindings
// Place your key bindings in this file to override the defaults
[
{
"key": "cmd+l",
"command": "console.log.wrap.down.prefix",
"when": "editorTextFocus"
},
{
"key": "ctrl+alt+w ctrl+alt+down",
"command": "-console.log.wrap.down.prefix",
@ilearnio
ilearnio / js-mocha.code-snippets
Created June 8, 2019 10:49
VSCode Mocha Snippets
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"context": {
"scope": "javascript,typescript",
@ilearnio
ilearnio / js.code-snippets
Created June 8, 2019 10:50
VSCode JS Snippets
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"console.log": {
"scope": "javascript,typescript",
const typeOf = require('just-typeof')
/**
* Update object values recursively
* @param {Object} obj
* @param {Function} handler
* @returns {Object} - new object
*/
const updateObjectValuesRecursively = (obj, handler) => {
const newObj = {}
@ilearnio
ilearnio / schema-validation.js
Last active July 16, 2018 14:53
Simple schema validation
/**
* Simple object validation
*/
const typeOf = require('just-typeof')
const validateEmail = require('./utils/helpers/strings/validateEmail')
const RULES = {
/**
* Type of the value
* @param {mixed} value
var mc = 12 // maximum columns
var m = 2 // margin in %
var scw = (100 - (m * (mc - 1))) / mc
var str = `
.row,
.column {
box-sizing: border-box;
}
@ilearnio
ilearnio / yieldable.js
Created June 14, 2016 18:44
Converst function into yieldable (JavaScript)
/**
* Converst function into yieldable
* @param {Function}
* @return {Function} yieldable callback
*/
function yieldable (func) {
return function () {
let args = [].slice.call(arguments);
let ctx = this;
return function (done) {
@ilearnio
ilearnio / sortObjectByKeys.js
Last active June 14, 2016 18:42
Sort object by keys (JavaScript)
function sortObjectByKeys(obj, recursive = false) {
const keys = Object.keys(obj).sort();
let sorted_obj = {};
keys.forEach(key => {
sorted_obj[key] = obj[key];
if (recursive && isObject(sorted_obj[key])) {
sorted_obj[key] = sortObjectByKeys(sorted_obj[key]);
}