Skip to content

Instantly share code, notes, and snippets.

function minimumDistances(numbers) {
const numberQuantity = numbers.length;
const firstOccurrences = {};
let minDistance = Infinity;
for (let idx = 0; idx < numberQuantity; idx += 1) {
const num = numbers[idx];
const firstOccurrenceIdx = firstOccurrences[num];
if (firstOccurrenceIdx === undefined) {
const convertTimeDiff = diff => ({
dd: Math.floor(diff / (1000 * 60 * 60 * 24)),
hh: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
mm: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
ss: Math.floor((diff % (1000 * 60)) / 1000)
});
const useTimer = timestamp => {
const [timer, setTimer] = useState(null);
const array0 = [1, 2];
const array1 = [array0, [3, 4]];
const array2 = [array0, [3, 4]];
console.log(array1.filter(x => array2.includes(x))); // [[1, 2]]
[…Array(10)].map((_, idx) => moment().add(idx, 'year').format('YYYY'));
function isChanged<ValueType>(change: ChangeRecord<ValueType>){
return change.oldValue !== change.newValue;
}
const reverseString = (inputStr) => inputStr.split('').reverse().join('');
const arr = [1, 0, 2, 3, 1, 0];
// Oops, we have an empty array
const newArray = arr.filter(item => {
if (item === 0) { return item }
});
// This works properly
const filteredArray = arr.filter(item => item === 0);
// Can handle only simple keys
const logValues1 = (obj, keys) => {
keys.forEach(key => console.log(obj[key]));
}
// Can handle complex keys
const logValues2 = (obj, keys) => {
keys.forEach(key => console.log(_.at(obj, key)));
}
Pet.propTypes = {
pet: PropTypes.shape({
age: PropTypes.number,
color: PropTypes.string,
name: PropTypes.string
})
};
@toxi-kb
toxi-kb / email.js
Last active February 7, 2020 08:23
const stylesObject = {
wrapper: {
'background': 'linear-gradient(135deg, #aa00ff 0%,#6a1b9a 100%)',
'border-radius': '4px',
},
}
const styles = Object.fromEntries(Object.entries(stylesObject).map(([element, style]) => [element, getStyles(style)]));
<div style="${styles.wrapper}">