Last active
March 27, 2018 18:45
-
-
Save VitorLuizC/8777d6851c9200100469086167ea5f40 to your computer and use it in GitHub Desktop.
Time module.
This file contains hidden or 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
| /** | |
| * Second in milliseconds. | |
| */ | |
| const SECOND = 1000 | |
| /** | |
| * Minute in milliseconds. | |
| */ | |
| const MINUTE = 60 * SECOND | |
| /** | |
| * Hour in milliseconds. | |
| */ | |
| const HOUR = 60 * MINUTE | |
| /** | |
| * Day in milliseconds. | |
| */ | |
| const DAY = 24 * HOUR | |
| /** | |
| * Week in milliseconds. | |
| */ | |
| const WEEK = 7 * DAY | |
| export { SECOND, MINUTE, HOUR, DAY, WEEK } |
This file contains hidden or 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
| import { DAY, MINUTE } from './constants' | |
| import { isType as is } from './type' | |
| /** | |
| * Get time in milliseconds. | |
| * @param {string|number|Date} [date] | |
| * @returns {number} | |
| */ | |
| export const getTime = (date = null) => { | |
| const offset = +is(date, 'String') && new Date(date).getTimezoneOffset() * MINUTE | |
| const time = is(date, 'String') ? new Date(date).getTime() + offset | |
| : is(date, 'Date') ? date.getTime() | |
| : is(date, 'Number') ? date | |
| : Date.now() | |
| return time | |
| } | |
| /** | |
| * Get time diff in type defined. | |
| * @param {Date} from | |
| * @param {Date} [to] | |
| * @param {number} [type] | |
| * @returns {number} | |
| */ | |
| export const getDiff = (from, to = new Date(), type = DAY) => { | |
| const diff = getTime(to) - getTime(from) | |
| const left = ~~(diff / type) | |
| return left | |
| } | |
| /** | |
| * @param {Date} from | |
| * @param {Date} [to] | |
| * @param {number} type | |
| * @returns {Array.<Date>} | |
| */ | |
| export const range = (from, to = new Date(), type = DAY) => { | |
| if (getTime(from) >= getTime(to)) | |
| throw new Error('Invalid range, from value should be lower than to value.') | |
| const length = getDiff(from, to, type) + 1 | |
| const range = Array.from({ length }, (_, index) => { | |
| const time = getTime(from) + index * type | |
| const date = new Date(time) | |
| return date | |
| }) | |
| return range | |
| } | |
| /** | |
| * Date formats. | |
| * @type {Object.<string,function(Date):string>} | |
| */ | |
| const formats = { | |
| 'D': (date) => String(date.getDate()), | |
| 'DD': (date) => String(date.getDate()).padStart(2, '0'), | |
| 'M': (date) => String(date.getMonth() + 1), | |
| 'MM': (date) => String(date.getMonth() + 1).padStart(2, '0'), | |
| 'YY': (date) => String(date.getFullYear()).substring(2), | |
| 'YYYY': (date) => String(date.getFullYear()).padStart(4, '0'), | |
| 'HH': (date) => String(date.getHours()).padStart(2, '0'), | |
| 'mm': (date) => String(date.getMinutes()).padStart(2, '0') | |
| } | |
| /** | |
| * Format date. | |
| * @param {string|number|Date} date | |
| * @param {string} format | |
| * @returns {string} | |
| */ | |
| export const format = (date, format) => { | |
| const instance = new Date(getTime(date)) | |
| const regex = new RegExp(Object.keys(formats).join('|'), 'g') | |
| const result = format.replace(regex, (format) => formats[format](instance)) | |
| return result | |
| } | |
| export * from './constants' |
This file contains hidden or 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
| /** | |
| * Get value's type name. | |
| * @param {*} value | |
| * @returns {string} | |
| */ | |
| export const getType = (value) => { | |
| const string = Object.prototype.toString.call(value) | |
| const [, type ] = /\[object (\w*)\]/.exec(string) | |
| return type | |
| } | |
| /** | |
| * Check if value's type is type (argument). | |
| * @param {*} value | |
| * @param {string} type | |
| * @returns {boolean} | |
| */ | |
| export const isType = (value, type) => getType(value) === type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment