Created
March 15, 2018 16:39
-
-
Save smmoosavi/1231a6c828ca88aec03ba49a37524f99 to your computer and use it in GitHub Desktop.
material-ui-pickers-jalaali-utils
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 jMoment from 'moment-jalaali' | |
import { toPersian } from './toPersian' | |
export default class jalaaliUtils { | |
static toJMoment (date) { | |
return jMoment(date ? date.clone() : undefined) | |
} | |
static parse (value, format) { | |
return jMoment(value, format) | |
} | |
static date (value, format) { | |
return jMoment(value, format) | |
} | |
static isValid (date) { | |
return date.isValid() | |
} | |
static isNull (date) { | |
return date.parsingFlags().nullInput | |
} | |
static isAfter (date, value) { | |
return date.isAfter(value) | |
} | |
static isBefore (date, value) { | |
return date.isBefore(value) | |
} | |
static isAfterDay (date, value) { | |
return date.isAfter(value, 'day') | |
} | |
static isBeforeDay (date, value) { | |
return date.isBefore(value, 'day') | |
} | |
static isBeforeYear (date, value) { | |
return date.isBefore(value, 'jYear') | |
} | |
static isAfterYear (date, value) { | |
return date.isAfter(value, 'jYear') | |
} | |
static startOfDay (date) { | |
return date.startOf('day') | |
} | |
static endOfDay (date) { | |
return date.endOf('day') | |
} | |
static format (date, formatString) { | |
switch (formatString) { | |
case 'D': | |
return date.format('jD') | |
case 'MMMM YYYY': | |
return date.format('jMMMM jYYYY') | |
case 'YYYY': | |
return date.format('jYYYY') | |
case 'ddd, MMM D': | |
return date.format('ddd, jMMM jDD') | |
case 'MMM D': | |
return date.format('jMMM jDD') | |
case 'MMMM Do': | |
return date.format('jMMMM jDo') | |
default: | |
return date.format(formatString) | |
} | |
} | |
static formatNumber (num) { | |
return toPersian(num) | |
} | |
static getHours (date) { | |
return date.get('hours') | |
} | |
static addDays (date, count) { | |
return count < 0 | |
? date.clone().subtract(Math.abs(count), 'days') | |
: date.clone().add(count, 'days') | |
} | |
static setHours (date, value) { | |
return date.clone().hours(value) | |
} | |
static getMinutes (date) { | |
return date.get('minutes') | |
} | |
static setMinutes (date, value) { | |
return date.clone().minutes(value) | |
} | |
static getMonth (date) { | |
return date.jMonth() | |
} | |
static isSameDay (date, comparing) { | |
return date.isSame(comparing, 'day') | |
} | |
static getMeridiemText (ampm) { | |
return ampm === 'am' | |
? jalaaliUtils.toJMoment().hours(2).format('A') | |
: jalaaliUtils.toJMoment().hours(14).format('A') | |
} | |
static getStartOfMonth (date) { | |
return date.clone().startOf('jMonth') | |
} | |
static getNextMonth (date) { | |
return date.clone().add(1, 'jMonth') | |
} | |
static getPreviousMonth (date) { | |
return date.clone().subtract(1, 'jMonth') | |
} | |
static getYear (date) { | |
return date.jYear() | |
} | |
static setYear (date, year) { | |
return date.clone().jYear(year) | |
} | |
static getWeekdays () { | |
return [0, 1, 2, 3, 4, 5, 6].map(dayOfWeek => jalaaliUtils.toJMoment().weekday(dayOfWeek).format('dd')[0]) | |
} | |
static isEqual (value, comparing) { | |
return jalaaliUtils.date(value).isSame(comparing) | |
} | |
static getWeekArray (date) { | |
const start = date.clone().startOf('jMonth').startOf('week') | |
const end = date.clone().endOf('jMonth').endOf('week') | |
const nestedWeeks = [] | |
let count = 0 | |
let current = start | |
while (current.isBefore(end)) { | |
const weekNumber = Math.floor(count / 7) | |
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [] | |
nestedWeeks[weekNumber].push(current) | |
current = current.clone().add(1, 'day') | |
count += 1 | |
} | |
return nestedWeeks | |
} | |
static getYearRange (start, end) { | |
const startDate = jalaaliUtils.date(start) | |
const endDate = jalaaliUtils.date(end) | |
const years = [] | |
let current = startDate | |
while (current.isBefore(endDate)) { | |
years.push(current) | |
current = current.clone().add(1, 'jYear') | |
} | |
return years | |
} | |
// displaying methods | |
static getCalendarHeaderText (date) { | |
return toPersian(date.format('jMMMM jYYYY')) | |
} | |
static getYearText (date) { | |
return toPersian(date.format('jYYYY')) | |
} | |
static getDatePickerHeaderText (date) { | |
return toPersian(date.format('ddd, jMMMM jD')) | |
} | |
static getDateTimePickerHeaderText (date) { | |
return toPersian(date.format('jMMM jD')) | |
} | |
static getDayText (date) { | |
return toPersian(date.format('jD')) | |
} | |
static getHourText (date, ampm) { | |
return toPersian(date.format(ampm ? 'hh' : 'HH')) | |
} | |
static getMinuteText (date) { | |
return toPersian(date.format('mm')) | |
} | |
} |
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
const dict = { | |
'0': '۰', | |
'1': '۱', | |
'2': '۲', | |
'3': '۳', | |
'4': '۴', | |
'5': '۵', | |
'6': '۶', | |
'7': '۷', | |
'8': '۸', | |
'9': '۹', | |
} | |
export const toPersian = function (str = '') { | |
return `${str}`.replace(/[0-9]/g, (v) => { | |
return dict[v] || v | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment