Skip to content

Instantly share code, notes, and snippets.

View enigma1's full-sized avatar

Mark Samios enigma1

  • Mark Samios
  • London, United Kingdom
View GitHub Profile
@enigma1
enigma1 / gist:08597ef04b07885701c0955f81c12574
Created January 22, 2022 20:53
Example of a visitor pattern in js
// Visitor Pattern
// Some existing raw data to be altered
const cars = [
["Ford Escort", 10000, 50],
["Fiat Panda", 20000, 42],
["VW Polo", 25000, 59],
["BMW 320", 100000, 10],
["Dodge Viper", 200000, 3],
["Honda Civic", 21000, 33]
];
@enigma1
enigma1 / MyComponent.js
Created December 28, 2021 13:46
Simple profiling of a react component
// Simple react profiling code to wrap components into react profiler
// Event timings are shown via a console statement of the profiler's callback
// Below the list of the documented arguments passed to the callback as shown in React docs
// ----------
// id - the "id" prop of the Profiler tree that has just committed
// phase - either "mount" (if the tree just mounted) or "update" (if it re-rendered)
// actualDuration - time spent rendering the committed update
// baseDuration - estimated time to render the entire subtree without memoization
// startTime - when React began rendering this update
// commitTime - when React committed this update
@enigma1
enigma1 / convert-strings.js
Created December 5, 2021 20:38
Converts template literal type strings with
/* specify an array of strings with parameters and a matching pattern for the template used */
/* Default is template literals style */
const convertStrings = (stringsArray, matchPattern=/\$\{(\w+)\}/g) => {
let inp = stringsArray;
if( typeof inp === 'object' && !Array.isArray(inp) ) {
inp = [stringsArray];
} else if(!Array.isArray(inp)) {
throw `Error: invalid data type on convertStrings got ${typeof stringsArray} instead of an Array or Object`;
}