This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse Query String | |
* @param {string} search | |
* @returns {Object.<string, string>} | |
*/ | |
const parseQueryString = (search: string): Record<string, string> => | |
(search || '') | |
.replace(/^\?/g, '') | |
.split('&') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse Query String | |
* @param {string} search | |
* @returns {Object.<string, string>} | |
*/ | |
const parseQueryString = (search) => | |
(search || '') | |
.replace(/^\?/g, '') | |
.split('&') | |
.reduce((acc, query) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Encode credit card number, except last 4 digits | |
*/ | |
const maskSensitivityInfos = (card) => { | |
card.number = card.number.replace(/.(?=.{4})/g, '#'); | |
return card; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**--------------------- 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepCompare = (source, target) => { | |
if (typeOf(source) !== typeOf(target)) { | |
return false; | |
} | |
if (typeOf(source) === 'array') { | |
if (source.length !== target.length) { | |
return false; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
NewerOlder