Skip to content

Instantly share code, notes, and snippets.

View ggondim's full-sized avatar
Building a more reusable JavaScript ecosystem

Gustavo Gondim ggondim

Building a more reusable JavaScript ecosystem
View GitHub Profile
@ggondim
ggondim / index.ts
Created November 3, 2022 03:37
ggondim's Bucket-Matrioska-Union Algorithm
// ggondim's Bucket-Matrioska-Union Algorithm
type Item = {
key: string,
properties: object,
setOrder: number,
setKey: string,
setVolume: number,
setSize: number,
setProperties: object,
@ggondim
ggondim / Notion unique ID formula.md
Last active February 9, 2022 22:52
Notion unique ID formula

This is an algorithm to generate short IDs for Notion pages, based on:

  • year unit digit
  • week number
  • page author's Nth character where N is the 8th digit of created timestamp, replacing spaces by "x"
  • 25th character of Notion random id
  • 9th digit of created timestamp
  • 3rd character of page title

The generated ID is expected to have 6-7 characters lenght, including alphanumeric, case-sensitive, diacritics-sensitive, characters.

@ggondim
ggondim / BuildTree.ts
Created January 18, 2022 00:03
Build a object tree (parent/children) given an object array of type T, the object ID key (elementKey) and the object parent key (parentKey)
type TreeItem<T> = T & { children: TreeItem<T>[] };
export default function buildTree<T>(array: T[], elementKey: keyof T, parentKey: keyof T): TreeItem<T>[] {
let tree = [] as TreeItem<T>[];
for (let i = 0; i < array.length; i++) {
if (array[i][parentKey]) {
let parent = array.filter(elem => elem[elementKey] === array[i][parentKey]).pop() as TreeItem<T>;
if (!parent['children']) {
parent.children = [];
}
@ggondim
ggondim / combinacoes.js
Created April 23, 2021 00:36
Array Combination (n, m=2)- Combinação simples de n elementos com m = 2 - Checagem com fatorial
const fatorial = (n) => n === 0 ? 1 : n * fatorial(n - 1);
const combinacoes = (n, m) => fatorial(n) / (fatorial(m) * fatorial(n - m));
function combinacoesDoisElementos(array: any[]) {
return array.reduce((combinacoes, item, i) => array
.slice(i+1)
.map(c => [item, c])
.concat(combinacoes), []);
}
@ggondim
ggondim / GMapCustomControl.vue
Last active July 2, 2020 19:24
vue2-google-maps custom control
<template>
<div :id="id">
<slot>
CustomControl
</slot>
</div>
</template>
<script>
import { MapElementFactory } from 'vue2-google-maps';
@ggondim
ggondim / index.js
Created June 18, 2020 00:10
Azure Monofunction example - Index of function with separated routes file
const app = require('azure-monofunction');
const AuthMiddleware = require('auth-middleware');
const routes = require('./routes');
app.useIf(context => context.meta.auth, new AuthMiddleware(authInfo));
app.addRoutes(routes);
module.exports = app.listen();
@ggondim
ggondim / routes.js
Created June 18, 2020 00:07
Azure Monofunction example - Separated routes module
const userController = require('../controllers/user.controller');
module.exports = [{
path: '/users/:id',
methods: ['GET'],
run: userController.getUser,
}, {
path: '/users',
methods: ['POST'],
run: userController.createUser,
@ggondim
ggondim / user.controller.js
Created June 18, 2020 00:06
Azure Monofunction example - User Controller
async function getUser({ req, res, params }) {
context.res.body = await db.getUser(params.id);
}
async function createUser({ req }) {
context.res.status = await db.createUser(req.body);
}
module.exports = {
getUser,
@ggondim
ggondim / function.js
Created June 17, 2020 23:56
Function Blend Anti-Pattern
async function entryPoint(context) {
if (context.req.method === 'GET') {
// it is a "get single resource" operation
} else if (context.req.method === 'PUT') {
// it is a "replace resource" operation
} else {
// must respond with 403 forbidden or not supported
}
}
@ggondim
ggondim / function.json
Created June 17, 2020 23:45
Azure Monofunction example - Function trigger
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "{*segments}"
},
{