Skip to content

Instantly share code, notes, and snippets.

View kylelambert101's full-sized avatar

Kyle kylelambert101

View GitHub Profile
@kylelambert101
kylelambert101 / CreateSet.js
Created August 18, 2020 17:34
JavaScript -> Filter array to unique values (set)
// indexOf returns the first index of a matching value, so this filters out repeated values after the first instance
foo.filter((value,index,self) => self.indexOf(value) === index)
@kylelambert101
kylelambert101 / JSON.py
Created August 18, 2020 17:32
Python -> JSON
''' How to work with JSON files and strings in Python '''
import json
# Load a JSON file
with open(filepath) as f:
data = json.load(f)
# Save a dict as json
with open(filepath) as f:
json.dump(foo)
@kylelambert101
kylelambert101 / Reflection.ts
Created August 18, 2020 17:31
TypeScript -> Object Reflection
/**
* Name and dataType of a property on an object
*/
export type TypedProperty = {
name: string;
dataType: string;
};
/**
* Get the name and datatype of all properties of an object
@kylelambert101
kylelambert101 / JSON.js
Created August 18, 2020 17:30
JavaScript -> JSON Strings
// Parse a string into a JSON object
const obj = JSON.parse(myString);
// Stringify an object
// Straight up, no formatting
const str1 = JSON.stringify(obj);
// Pretty-print the JSON with indent of 4
const str2 = JSON.stringify(obj, undefined, 4);
@kylelambert101
kylelambert101 / FlattenArray.js
Created August 18, 2020 17:29
JavaScript -> Flatten an array
// To flatten a JS array of arrays (a.k.a get a master array with all values from children)...
// flat is newer - not available in IE
const foo = myArray.flat();
// reduce is another option
const bar = myArray.reduce((allData,currentArray) => allData.concat(currentArray));
@kylelambert101
kylelambert101 / FileExists.py
Created August 18, 2020 17:27
Python -> Check if file or directory exists
from os import path
result = path.exists(myPath)
# Check whether it is specifically a file
result = path.isfile(myPath)
@kylelambert101
kylelambert101 / Arguments.py
Created August 18, 2020 17:26
Python -> Capture command line arguments
# Capture all arguments
args = sys.argv
# Capture only first argument
arg = sys.argv[0]
# Capture all arguments after the first
other_args = sys.argv[1:]
@kylelambert101
kylelambert101 / DictIteration.py
Created August 18, 2020 17:24
Python -> Iterate through a dict
for key, value in myDict.items():
print(f'Key is {key}, value is {value}')
@kylelambert101
kylelambert101 / Dates.py
Created August 18, 2020 17:23
Python -> Date Formatting and Parsing
# See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
from datetime import datetime
dateString = "2020-05-31"
# parse a string into a date
myDateTime = datetime.strptime(dateString, '%Y-%m-%d')
# if you want to strip out time
myDate = myDateTime.date()
@kylelambert101
kylelambert101 / TraceProps.tsx
Created August 18, 2020 17:21
React -> Trace Prop Changes
// Use this custom hook when you are trying to trace why a component is re-rendering
const useTraceUpdate = (componentName: string, props: any): void => {
// Capture previous prop state
const prev = React.useRef(props);
React.useEffect(() => {
// Check all field/value pairs of the new props for changes
const changedProps = Object.entries(props).reduce(
(changeSummary, [fieldName, currentValue]) => {
const previousValue = Reflect.get(prev.current, fieldName);
return previousValue === currentValue