Skip to content

Instantly share code, notes, and snippets.

View bernardoadc's full-sized avatar
🚀
Escaping the past by embracing the future

Bernardo A. Dal Corno bernardoadc

🚀
Escaping the past by embracing the future
View GitHub Profile
@bernardoadc
bernardoadc / get.vanilla.js
Last active August 19, 2021 02:58
This is a vanilla one-liner similar to lodash's `_.get`.
function get (obj, path, def) {
return (path.split && path.split('.') || path).reduce((o, p) => ((o || obj)[p] ?? def), 0)
}
@bernardoadc
bernardoadc / readme.md
Last active August 19, 2021 04:38
A vanilla one-liner similar to lodash's `_.set`

Usage: set(obj, 'a.b.c.d.e', value) || set(obj, [a, 'b', '@#$', 'd', 'e'], [1, 2, 3]) || set(arr, '0.1.2', true)

  • For bracket notation (e.g. obj['@#$']), use arrays
  • Creates arrays with numbered keys (numbers not allowed as objects keys, see other revisions for that)

Explanation

function set (
  obj,  // object in which to set nested properties
@bernardoadc
bernardoadc / reduceRev.vanilla.js
Created May 3, 2018 19:09
Vanilla's reduce reversed (back to front || right to left)
function reduceRev(array, cb, result) {
var length = array? array.length : 0;
while (length--) {
result = cb(result, array[length], length, array);
}
return result;
}
@bernardoadc
bernardoadc / forInDeep.vanilla.js
Last active May 3, 2018 19:58
Vanilla to forIn deeply in an object
function forInDeep(obj,cbBefore,cbAfter) {
var key;
for (key in obj) {
if (cbBefore) cbBefore(obj[key],key,obj)
if (typeof obj[key] == 'object') forInDeep(obj[key], cbBefore, cbAfter)
if (cbAfter) cbAfter(obj[key],key,obj)
}
}
@bernardoadc
bernardoadc / objFilter.vanilla.js
Last active August 21, 2021 06:23
Vanilla filter for objects, with several filter types accepted
function objFilter (obj, filter, nonstrict) {
const r = {}
if (!filter) return r
if (typeof filter == 'string') return {[filter]: obj[filter]}
for (const p in obj) {
if (typeof filter == 'object' && nonstrict && obj[p] == filter[p]) r[p] = obj[p]
else if (typeof filter == 'object' && !nonstrict && obj[p] === filter[p]) r[p] = obj[p]
else if (typeof filter == 'function') if (filter(obj[p], p, obj)) r[p] = obj[p]
else if (filter.length && filter.includes(p)) r[p] = obj[p]
}
@bernardoadc
bernardoadc / escape-json.js
Created January 3, 2019 20:16 — forked from matthewmueller/escape-json.js
Escape JSON strings before trying to run JSON.parse
/*
Escape JSON
*/
var escapeJSON = exports.escapeJSON = function(json) {
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
var meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
@bernardoadc
bernardoadc / .gitalias
Last active March 22, 2022 20:16
GIT Alias
[alias]
ae = !"$(git ed) \"$(git ad)$1.gitalias\" #" # edit a given .gitalias file (or main one)
al = !"afs=($(git afs)) && for n in \"${!afs[@]}\"; do echo \"$n - ${afs[$n]}\"; done && read n && trap 'return' SIGINT && $(git ed) ${afs[$n]} #" # list all .gitalias files + choose one to edit
af = !"git config --global --get-all include.path | grep "/.gitalias" #" # pq grep -l ".gitalias" $(git config --global --get-all include.path) funciona?
afs = !"git config --global --get-all include.path #"
ad = !"echo $(git af) | head -c-10"
alias-sync = !"git -C \"$(git ad)\" pull origin master && git -C \"$(git ad)\" push origin master #"
alias = ae
as = !"git alias-sync #"
au = !"git -C \"$(git ad)\" ac \"$1\" && git as #"
@bernardoadc
bernardoadc / .gitignore
Last active January 7, 2021 22:26
Sublime settings
Package Control.last-run
Package Control.ca-list
Package Control.ca-bundle
Package Control.system-ca-bundle
Package Control.cache/
Package Control.ca-certs/
@bernardoadc
bernardoadc / vanilla.md
Last active November 18, 2019 18:25
Vanilla gists
@bernardoadc
bernardoadc / configs.gitconfig
Last active October 10, 2023 18:27
GIT Configs
# should not contain [user], [credential] and [include]s
# nor machine/remotes specific stuff
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[branch]
autosetuprebase = always
autosetupmerge = always # com checkout (remoto ou nao) faz tracking do branch