Skip to content

Instantly share code, notes, and snippets.

View themgoncalves's full-sized avatar
⚜️
Software and cathedrals are much the same – first we build them, then we pray.

Marcos Gonçalves themgoncalves

⚜️
Software and cathedrals are much the same – first we build them, then we pray.
View GitHub Profile
@themgoncalves
themgoncalves / parse-query-string.ts
Last active June 8, 2021 15:04
Parsing URL Query String into Object
/**
* Parse Query String
* @param {string} search
* @returns {Object.<string, string>}
*/
const parseQueryString = (search: string): Record<string, string> =>
(search || '')
.replace(/^\?/g, '')
.split('&')
@themgoncalves
themgoncalves / parse-query-string.js
Last active June 8, 2021 15:04
Parsing URL Query String into Object
/**
* Parse Query String
* @param {string} search
* @returns {Object.<string, string>}
*/
const parseQueryString = (search) =>
(search || '')
.replace(/^\?/g, '')
.split('&')
.reduce((acc, query) => {
import typeOf from './type-of';
/**
* Shallow copy
* @description Create a copy of a original collection with same structure.
* @param value
*/
const shallowCopy = (value) => {
let copy;
import typeOf from './type-of';
/**
* Shallow copy
* @description Create a copy of a original collection with same structure.
* @param value
*/
const shallowCopy = <T>(value: T): T extends [] ? T[] : T => {
let copy: unknown;
const maskSensitivityInfos = (card) => {
// we use SPREAD OPERATOR to copy all properties from 'card' parameter
// this will remove the reference binding.
// In other words: NO MUTATION
const cc = { ...card };
cc.number = cc.number.replace(/.(?=.{4})/g, '#');
return cc;
}
/**
* Encode credit card number, except last 4 digits
*/
const maskSensitivityInfos = (card) => {
card.number = card.number.replace(/.(?=.{4})/g, '#');
return card;
}
@themgoncalves
themgoncalves / javascript-immutable.js
Created May 31, 2021 14:28
JavaScript - immutable data types
const person = 'John Doe';
console.log('initial', person);
// outputs: 'John Doe'
// try to mutate the data structure
// by using the 'upper case' method
person.toUpperCase();
console.log('mutation attempt', person);
@themgoncalves
themgoncalves / javascript-mutation.js
Created May 31, 2021 14:25
JavaScript - example of data structure mutation
/**--------------------- ARRAY ---------------------*/
const species = ['octopus', 'squid', 'shark',];
console.log('initial state', species);
// outputs: 'octopus', 'squid', 'shark'
// 'push' method mutates the array
// by adding a new item to its set
species.push('seahorse');
const deepCompare = (source, target) => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
if (source.length !== target.length) {
return false;
}
const deepCompare = (source: unknown | unknown[], target: unknown | unknown[]): boolean => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
if ((source as unknown[]).length !== (target as unknown[]).length) {
return false;
}