Skip to content

Instantly share code, notes, and snippets.

View rxluz's full-sized avatar

Ricardo Luz rxluz

View GitHub Profile
@rxluz
rxluz / deepFreeze.js
Last active April 22, 2020 16:22
The problem with Object.freeze, see more at: https://medium.com/p/1a258c160c07
function deepFreeze(obj) {
Object.freeze(obj)
const propertiesList = Object.getOwnPropertyNames(obj)
propertiesList.forEach((key) => {
const isNotNullOrUndefined =
obj[key] !== null || typeof obj[key] !== 'undefined'
const hasOwnProperty = obj.hasOwnProperty(key)
@rxluz
rxluz / navigateObjectTree.js
Last active January 18, 2019 02:35
The problem with Object.freeze, see more at: https://medium.com/p/1a258c160c07
const navigateObj = (obj, level = 0) => {
const methodsNamesFromFirstLevel = Object.getOwnPropertyNames(obj);
methodsNamesFromFirstLevel.forEach(prop => {
console.log(`Prop: ${prop} from level ${level}`);
if (typeof obj[prop] === "object") {
navigateObj(obj[prop], ++level);
}
});
};
@rxluz
rxluz / DESIGN_PATTERNS_AdapterPatternExample.js
Last active January 18, 2019 02:36
JS Design Patterns: Adapter, see more at: https://medium.com/p/1a665e35a957
class UsersLanguageAdapter {
constructor(lang = "en") {
this.language = lang;
this.languageMessages = {
add: {
en: "Added succefully",
fr: "Ajouté avec succès",
pt: "Adicionado com sucesso",
},
class Users {
constructor(lang = "en") {
this.data = [];
this.language = lang;
this.languageMessages = {
add: {
en: "Added succefully",
fr: "Ajouté avec succès",
class Products {
constructor() {
this.data = [];
}
add(product) {
this.data.push(product);
return true;
}
class Products {
constructor() {
this.data = [];
}
add(product) {
this.data.push(product);
return true;
}
class Products {
constructor(User) {
if (typeof User !== "object") {
throw new Error("User object is required to initialize products class");
return false;
}
this.currentUser = User;
this.data = [];
}
@rxluz
rxluz / ObjectFreezeProblem.js
Last active January 18, 2019 02:46
The problem with Object.freeze, see more at: https://medium.com/p/1a258c160c07
const myCustomObj = {
even: {
frozen: {
thisObjectWillChange: "nope 😳",
},
},
};
Object.freeze(myCustomObj);
@rxluz
rxluz / DESIGN_PATTERNS_singletonExample.js
Last active January 18, 2019 02:39
JS Design (Anti?)Patterns: Singleton, see more at https://medium.com/p/f375865bef10
const utils = {
deepFreeze: obj => {
Object.freeze(obj);
Object.getOwnPropertyNames(obj).forEach(prop => {
const isNotNullOrUndefined = () =>
obj[prop] !== null || typeof obj[prop] !== "undefined";
const hasOwnProperty = () => obj.hasOwnProperty(prop);
@rxluz
rxluz / DESIGN_PATTERNS_withoutSingletonPattern.js
Last active January 18, 2019 02:40
JS Design (Anti?)Patterns: Singleton, see more at https://medium.com/p/f375865bef10
/* ---- DONT DO IT ----- */
const globalOrWindow = typeof global !== "undefined" ? global : window; //checks if the environment is node or browser
globalOrWindow.params = {
device: "iPhone",
useGPS: false,
useAcelerometer: true,
deviceUniqueID: Math.floor(Math.random() * 300),
};