Skip to content

Instantly share code, notes, and snippets.

View victornpb's full-sized avatar
⌨️

Victor victornpb

⌨️
View GitHub Profile
@victornpb
victornpb / ajax.js
Created October 16, 2019 19:19
Minimal ajax library
/**
* Minimal Ajax library
* @param {object} options Options
* @param {string} options.url Url
* @param {string} [options.method='GET'] Method GET|POST|PUT|DELTE
* @param {string} [options.data] Body of the request
* @param {string} [options.contentType='application/x-www-form-urlencoded'] Set the content type of the body
* @param {number} [options.timeout] Timout in milliseconds
* @param {boolean} [options.withCredentials=false] withCredentials
* @return {Promise} Returns a promise
@victornpb
victornpb / param.js
Created October 16, 2019 19:19
Simple location.hash parser
/**
* url hash manager
* @author Victor N <victornunes@lett.digital>
*/
const param = (function HashParams() {
function parse(string) {
var params = {};
string.replace(/^[#?]/, '').split('&').forEach(text => {
var p = text.split('=');
/**
* domReady for IE6+
* @param {Function} callback Callback
*
* @author http://beeker.io/jquery-document-ready-equivalent-vanilla-javascript
* @date 2016-10-04
*/
function domReady(callback) {
var ready = false;
var detach = function() {
function shuffle(arr) {
const a = arr.slice();
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
return a;
}
@victornpb
victornpb / sortBy.js
Last active January 18, 2021 01:16
Sort by field
function sortBy(arr, fields) {
if (typeof fields === 'string') fields = [fields];
function compareFn(a, b) {
return fields
.map(field => {
let dir = 1;
if (field[0] === "-") {
dir = -1;
field = field.substring(1);
}
@victornpb
victornpb / Import-Export-Beanstalk-Enviroment-Variables.js
Last active February 20, 2020 18:46
Import/Export Beanstalk Enviroment Variables
(function() {
var html = `<div style="position: fixed; top: 50px; left: 50px; z-index: 99999999; background-color:black; color: white; padding: 5px;">
<div style="display: flex;">
READ:
<button class="readAsJsonBtn">as JSON</button>
<button class="readAsEnvBtn">as ENV</button>
WRITE:
<button class="writeFromJsonBtn">from JSON</button>
<button class="writeFromEnvBtn">from ENV</button>
<span style="flex-grow: 1;"></span>
@victornpb
victornpb / .eslintrc.json5
Last active May 22, 2019 18:16
eslintrc containing all rules with description
// All rules
// (R=Recommended,F=Fixable)
// Use .json5 highlighting
{
// Possible Errors
// These rules relate to possible syntax or logic errors in JavaScript code:
"for-direction": "error", // (R,) enforce “for” loop update clause moving the counter in the right direction.
"getter-return": "error", // (R,) enforce return statements in getters
"no-async-promise-executor": "error", // (R,) disallow using an async function as a Promise executor
"no-await-in-loop": "off", // (,) disallow await inside of loops
@victornpb
victornpb / v-tooltip.js
Last active July 6, 2023 06:28
Using Bootstrap tooltips in Vue.js with a simple directive
/**
* Enable Bootstrap tooltips using Vue directive
* @author Vitim.us
* @see https://gist.github.com/victornpb/020d393f2f5b866437d13d49a4695b47
* @example
* <button v-tooltip="foo">Hover me</button>
* <button v-tooltip.click="bar">Click me</button>
* <button v-tooltip.html="baz">Html</button>
* <button v-tooltip:top="foo">Top</button>
* <button v-tooltip:left="foo">Left</button>
@victornpb
victornpb / bindForm.js
Created April 14, 2019 02:25
Two way data binding DOM and vanilla Javascript in under 100 lines
function dd(context = {}) {
const values = {};
const boundElms = {};
const elms = Array.from(document.querySelectorAll('[data-bind]'));
elms.forEach(el => {
const prop = el.getAttribute('data-bind');
if (!boundElms[prop]) boundElms[prop] = [];
@victornpb
victornpb / deleteDiscordMessages.js
Last active April 16, 2024 09:32
Delete all your messages from DM or Channel in Discord
/*
This file is now hosted here:
https://github.com/victornpb/undiscord
*/