Skip to content

Instantly share code, notes, and snippets.

View gm50x's full-sized avatar
🐺
Rise and rise again until lambs become lions!

Getulio Magela Silva gm50x

🐺
Rise and rise again until lambs become lions!
View GitHub Profile
/**
* Demonstrating Imprecision
*/
let n = 1.22
while (n && n > 0) {
let m = n;
n = n - 0.01
console.log(`${m} - ${0.01} = ${n}`)
/**
* PROBLEM: Change
* You are writing a change calculating algorithm that will
* determine the proper change with the smallest possible amount
* of coins and bills.
* For the problem, consider you have:
* - Bills: 100, 50, 20, 10, 5 and 2
* - Coins: 1, 0.5, 0.25, 0.10, 0.05 and 0.01
*
* Enough talk... Get coding!
/**
* Demonstrates Imprecision in C# floats running on top of .NET Core 3.1
**/
using System;
public class Program
{
public static void Main()
{
float n = 1.00f;
const wordPlural = (word, amount) => {
word = word.toLowerCase()
if (amount === 1) {
return word
}
return pluralize(word)
}
const pluralize = word => {
// Function to create the enums
const getEnum = (obj) => {
const EEnum = {}
if (Array.isArray(obj)) {
obj.forEach((enumerator, i) => EEnum[enumerator] = i)
} else {
Object.getOwnPropertyNames(obj)
.forEach(property => {
EEnum[property] = obj[property]
/************************************************************
* *
* ObservablesOne *
* Simples example of playing with rxjs-observables *
* *
* reqruiements: npm install rxjs *
* *
************************************************************/
@gm50x
gm50x / Dockerfile
Created November 11, 2020 23:15
Node-Nestjs multiphased dockerfile
FROM node:lts-alpine3.12 AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:lts-alpine3.12 AS runtime
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
/***************************************************
* Getúlio Magela Silva (gm50x)
*
* Running Instructions:
* node async-iterators {for-each|for-of|map}
*
* Reference Material (NOT MINE):
* https://zellwk.com/blog/async-await-in-loops/
***************************************************/
@gm50x
gm50x / promisify.js
Last active December 19, 2020 14:37
Promisified a callback function
const { promisify } = require("util");
const getCustomerInfo = (callback) => {
setTimeout(
() => callback(null, { name: "John Doe", birthdate: "1967-04-14" }),
1000
);
};
const getCustomerInfoAsync = promisify(getCustomerInfo);
const http = require('http')
const baseApiUrl = 'http://jsonplaceholder.typicode.com'
// using error-back pattern --> or anti-pattern :)
const fetchDataWithPromise = () => {
return new Promise((resolve, reject) => {
http.get(`${baseApiUrl}/posts`, res => {
let rawData = '';