Skip to content

Instantly share code, notes, and snippets.

@jplew
Created August 23, 2018 22:52
Show Gist options
  • Save jplew/9bf2b3852a440a8d9b8858c5deb5a66e to your computer and use it in GitHub Desktop.
Save jplew/9bf2b3852a440a8d9b8858c5deb5a66e to your computer and use it in GitHub Desktop.
helper functions to create fake dates
/**
* @author: JP Lew ([email protected])
* @date: Monday, 23rd July 2018 5:14:39 pm
* @lastModifiedBy: JP Lew ([email protected])
* @lastModifiedTime: Thursday, 23rd August 2018 1:03:37 pm
* project: Share Results
* @copyright (c) 2018 Hack Capital
*/
const randomNumber = (min = 0, max = 10) => Math.floor(Math.random() * max) + min;
const isAdvertiser = org => org.organization_type_id === 3;
const randomItemFromArray = array => array[Math.floor(Math.random() * array.length)];
const getPublisherIds = (orgs) => {
const publisherIdArray = orgs.reduce((acc, curr) => {
if (curr.organization_type_id === 2) {
return [...acc, curr.id];
}
return acc;
}, []);
return publisherIdArray;
};
const getProductsByMerchant = (products, orgId) => {
const productArray = products.reduce((acc, curr) => {
if (curr.organization_id === orgId) {
return [...acc, curr.product_id];
}
return acc;
}, []);
return productArray;
};
// random date in the last year (there are 3.1536e10 milliseconds in a year)
const randomDateMs = Math.ceil(Date.now() - Math.random() * 3.1536e10);
const randomDateSeconds = Math.ceil(randomDateMs / 1000);
const randomDateUtc = new Date(randomDateMs).toJSON();
const createRandomDate = () => {
const date = Math.ceil(Date.now() - Math.random() * 2 * 3.1536e10);
return new Date(date).toJSON();
};
// random date within a couple months of specified date
const randomDateInFuture = (date) => {
const futureDate = new Date(new Date(date).getTime() + Math.random() * 6e9);
return futureDate.toJSON();
};
module.exports = {
randomNumber,
randomItemFromArray,
randomDateMs,
randomDateSeconds,
randomDateUtc,
createRandomDate,
randomDateInFuture,
getPublisherIds,
getProductsByMerchant,
isAdvertiser,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment