Skip to content

Instantly share code, notes, and snippets.

View matiaslopezd's full-sized avatar
🛰️
matiaslopezd[at]404.city

Matías López matiaslopezd

🛰️
matiaslopezd[at]404.city
View GitHub Profile
@matiaslopezd
matiaslopezd / cloneCollection.js
Created December 1, 2020 20:52
Clone all documents of collection to another
/**
* Get all documents of collection
* @param db {Object} - MongoDB connection
* @param collectionName {String} - Collection name
* @returns {Promise<Array>}
*/
async function getDocs(db, collectionName) {
const collection = db.collection(collectionName);
const cursor = await collection.find();
return cursor.toArray();
@matiaslopezd
matiaslopezd / jsonToDot.js
Last active December 1, 2020 19:05
Convert JSON to array with dot notation
function toDots(object, array = []) {
const deepValue = (obj, path) => path.replace(/\[|\]\.?/g, '.').split('.').filter(s => s).reduce((acc, val) => acc && acc[val], obj);
function parseToDots(obj, key) {
const value = deepValue(obj, key);
array.push(key); // Add if you want add only certains fields
if (typeof value === 'object') Object.keys(value).forEach(key2 => parseToDots(obj, `${key}.${key2}`));
}
Object.keys(object).forEach(key => parseToDots(object, key));
@matiaslopezd
matiaslopezd / get-fqdn.js
Last active December 1, 2020 19:09
Get FQDN with Regex
/**
* Get FQDN from a string
* @name FQDN
* @param URL {String} - String you want get FQDN
* @return {String}
**/
function FQDN(URL = '') {
URL = URL.subtring(0, 50); // This will avoid evaluate long malicious fqdn
const regex = /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+/i;
const filtered = regex.exec(URL);
@matiaslopezd
matiaslopezd / count-character.js
Last active November 23, 2020 23:01
Count characters in DOM element with case sensitive option
/**
* Search number of times a character/string exist in DOM element
*
* @name countCharacter
* @param DOMElement {String|Object} - DOM element you want count characters inside
* @param character {String} - String you want count
* @param caseSensitive {Boolean} - Option to set sensitive to uppercase or lowercase
* @return {Number} - Total of times character match in DOM element
**/
function countCharacter(DOMElement = 'body', character = '', caseSensitive = false) {
upstream app_server {
ip_hash;
server app_react:3000 max_fails=3 fail_timeout=30s;
}
upstream api_server {
ip_hash;
server api_feathers:3040 max_fails=3 fail_timeout=30s;
}
@matiaslopezd
matiaslopezd / README.md
Created June 10, 2020 04:10
ngrok startup
ngrok http https://localhost:8080 -bind-tls=true -host-header="localhost:8080"
@matiaslopezd
matiaslopezd / cloud-function.serverless.js
Last active June 9, 2020 23:34
Submit Hubspot Form with serverless
const fetch = require('node-fetch');
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.hubspot = async (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
@matiaslopezd
matiaslopezd / node-gyp.md
Last active April 1, 2020 14:45
Solve to problem of permissions access to node-gyp

To solve the problem when execute npm install and get node-gyp error access permission:

EACCES current user ("$user") does not have permission to access the dev dir "/root/.cache/node-gyp/12.16.1"

Execute the follow commands:

sudo npm -g install node-gyp --unsafe-perm
@matiaslopezd
matiaslopezd / get-headers.js
Last active March 26, 2020 15:21
Simple object for get headers and properties of website with ajax
const myHeaders = {
get: function getHeader(url = window.location, obj = {}) {
return new Promise((resolve) => {
function checkWhichSplit(string = '') {
if (string.includes(',')) return ',';
else if (string.includes(';')) return ';';
else return undefined;
}
function parseHeader(string = '') {
@matiaslopezd
matiaslopezd / README.md
Created March 16, 2020 14:34
How to add authentication to MongoDB