Skip to content

Instantly share code, notes, and snippets.

@raynoppe
Last active May 25, 2020 10:13
Show Gist options
  • Save raynoppe/d5626be4dddeb4298fc6ce7acfd134a0 to your computer and use it in GitHub Desktop.
Save raynoppe/d5626be4dddeb4298fc6ce7acfd134a0 to your computer and use it in GitHub Desktop.
JS Misc
const oldPackage = JSON.parse(JSON.stringify(this.package)); // copy object
setTimeout(() => {}, 1000);
// Arrays
array.splice(pos, 1); // Removes the first element of the array
// clone array
let arr2 = [...arr];
// reverse array
fruits.reverse();
import debounce from 'lodash/debounce';
loadStock: debounce(function (name) {
console.log('loadstock', name);
this.loadStockFetch(name)
}, 1000),
//promises
async getTeams(match) {
return new Promise(async (resolve, reject) => {
resolve(data);
});
}
async function isEmailExist(email) {
let count = await new Promise((resolve, reject) => {
db.count({email: email}, (err, count) => {
if (err) reject(err);
resolve(count);
});
});
return count > 0;
}
var start = new Date();
start.setHours(0,0,0,0);
var end = new Date();
end.setHours(23,59,59,999);
alert( start.toUTCString() + ':' + end.toUTCString() );
new Date().getTime();
if (!Date.now) {
Date.now = function() { return new Date().getTime(); }
}
To get the timestamp in seconds, you can use:
Math.floor(Date.now() / 1000)
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];
// get Mon, Tue
var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];
if (a === null)
Check for undefined
if (typeof a === "undefined")
// or
if (a === undefined)
Short cut
if (!a) {
// `a` is falsey, which includes `undefined` and `null`
// (and `""`, and `0`, and `NaN`, and [of course] `false`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment