Skip to content

Instantly share code, notes, and snippets.

View phillippelevidad's full-sized avatar

Phillippe Santana phillippelevidad

View GitHub Profile
@phillippelevidad
phillippelevidad / brainjs-stockmarket-prediction-data.json
Last active December 21, 2023 14:37
Data for the stock market prediction sample.
[
{
"date": "2018-11-02",
"open": 141.0716,
"high": 141.1014,
"low": 138.7762,
"close": 139.7898,
"volume": 7673303,
"unadjustedVolume": 7673303,
"change": -0.139114,
@phillippelevidad
phillippelevidad / brainjs-stockmarket-prediction.js
Last active December 21, 2023 17:39
Uses brain.js to predict the next position for a stock market ticker.
const brain = require("brain.js");
const rawData = [
{
date: "2018-11-02",
open: 141.0716,
high: 141.1014,
low: 138.7762,
close: 139.7898,
volume: 7673303,
@phillippelevidad
phillippelevidad / benchmark-delete-vs-spread.js
Created November 19, 2023 13:58
Benchmarks for delete, Reflect.deleteProperty and the spread operator
import Benchmark from "benchmark";
function useDelete() {
const obj = {};
obj.a = 1;
obj.b = 2;
delete obj.b;
}
function useReflect() {
@phillippelevidad
phillippelevidad / ref-value-test.js
Last active November 18, 2023 21:11
References vs values in function parameters in JS
const assert = require("assert");
// When we change the contents of an object, we can see
// the change outside the function.
function f1(obj) {
obj.a += 1;
assert.deepStrictEqual(obj, { a: 2 });
}
const obj1 = { a: 1 };
f1(obj1);
@phillippelevidad
phillippelevidad / nodejs-duplex-chat.js
Created November 14, 2023 13:01
Chat implementation in Node.js using a Duplex
import net from "net";
import { Duplex } from "stream";
function createClient(socket) {
return new Duplex({
read(size) {
// This function is called when the stream wants to pull more data in.
// In this chat application example, data reading is handled by the socket's 'data' event,
// so you might not need to implement anything here.
},
@phillippelevidad
phillippelevidad / javascript-decorator.js
Created November 6, 2023 21:08
Implementação de Decorator em Javascript
// Como visto em
// https://refactoring.guru/design-patterns/decorator
// https://www.youtube.com/watch?v=GCraGHx6gso
// Artifício para simular uma interface
// e obrigar a implementação dos métodos
class IRepository {
find(filter) {
throw new Error("Method 'find' must be implemented");
}
@phillippelevidad
phillippelevidad / Product.js
Created November 4, 2023 00:27
Product class, getters/setters, validation, business rules
/*
In the realm of OOP, it's preferable for business logic to be in close proximity to the data it pertains to.
This not only aligns with the 'S' in the SOLID principles—emphasizing, that Single Responsibility is about segregating unrelated
duties but also about consolidating related ones.
The method outlined below leverages getters and setters to intercept data handling, thereby preventing the model from accepting
any invalid values. This ensures that an invalid product could never exist within the system.
It should be noted that the validations demonstrated here are simplistic and meant for illustrative purposes.
Such rudimentary checks would typically reside outside the domain class, shifted towards the outer layers of the architecture,
/**
* The UniquePropertySet class is designed to manage a collection of objects,
* ensuring that each object is unique based on its properties and values
* rather than its reference in memory.
*
* Beware, though, that this implementation is not optimized for performance,
* and that the JSON.stringify method does not handle non-serializable values
* consistently, such as functions, undefined, or circular references, which
* will cause objects containing such values to not be handled correctly.
*/
@phillippelevidad
phillippelevidad / StringObfuscator.cs
Last active July 27, 2020 20:32
Replace letters and numbers with RANDOM letters and numbers, respectively. Good for obfuscating senstive data before logging.
public static class StringExtensions
{
// Consecutive runs for "Foo Bar 0123456789" generated:
// xuc iih 3156679205
// lzr xai 1153091860
// xxm uoy 5532413549
public static string Obfuscate(this string input)
{
if (input == null) return null;
var obfuscated = new char[input.Length];
@phillippelevidad
phillippelevidad / AwsCostExplorerHelper.cs
Created February 13, 2020 19:11
A quick-start class to pull sumarized costs from your AWS account.
namespace NameIt
{
public static class AwsCostExplorer
{
private const string metricType = "BlendedCost";
private static readonly RegionEndpoint regionEndpoint = RegionEndpoint.USEast1;
public static AwsCostStructure GetCostForAccount(string accessKey, string secretKey, Period period)
{
var client = new AmazonCostExplorerClient(