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
// 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) |
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
''' 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) |
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
/** | |
* 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 |
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 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); |
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
// 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)); |
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
from os import path | |
result = path.exists(myPath) | |
# Check whether it is specifically a file | |
result = path.isfile(myPath) |
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
# 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:] |
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
for key, value in myDict.items(): | |
print(f'Key is {key}, value is {value}') |
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
# 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() |
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
// 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 |