├── .gitignore ├── package.json ├── README.md ├── LICENSE ├── index.d.ts └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "material-ui-pickers-jalali-utils", 3 | "version": "0.4.3", 4 | "description": "Calendar utils that add full Persian calendar system support to material-ui-pickers", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/alitaheri/material-ui-pickers-jalali-utils.git" 9 | }, 10 | "author": "Ali Taheri Moghaddar", 11 | "keywords": [ 12 | "material-ui", 13 | "material-ui-pickers", 14 | "calendar", 15 | "persian", 16 | "date-picker", 17 | "datepicker", 18 | "jalali", 19 | "jalaali" 20 | ], 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/alitaheri/material-ui-pickers-jalali-utils/issues" 24 | }, 25 | "homepage": "https://github.com/alitaheri/material-ui-pickers-jalali-utils#readme", 26 | "dependencies": { 27 | "moment": "^2.22.2", 28 | "moment-jalaali": "^0.7.4", 29 | "moment-range": "^4.0.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Persian calendar utils for Material UI Pickers 2 | 3 | This library provides implementation specific utility functions that can 4 | be used by the [material-ui-picker](https://github.com/dmtrKovalenko/material-ui-pickers) 5 | components to correctly render a Persian calendar. 6 | 7 | Please refer to material-ui-pickers's 8 | [documentation](https://github.com/dmtrKovalenko/material-ui-pickers) 9 | under localized example to learn how to use it. 10 | 11 | 12 | ## Installation 13 | 14 | You can install this package with the following command: 15 | 16 | ```sh 17 | npm install material-ui-pickers-jalali-utils 18 | ``` 19 | 20 | ## Thanks 21 | 22 | Great thanks to [material-ui](https://github.com/callemall/material-ui) and 23 | [material-ui-pickers](https://github.com/dmtrKovalenko/material-ui-pickers) 24 | for their great react components. 25 | 26 | ## License 27 | This project is licensed under the [MIT license](https://github.com/alitaheri/material-ui-pickers-jalali-utils/blob/master/LICENSE). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ali Taheri Moghaddar, ali.taheri.m@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import { Moment } from 'moment-jalaali'; 2 | 3 | declare class PersianUtils { 4 | public toJMoment(date: any): Moment; 5 | public parse(value: any, format: string): Moment; 6 | public date(value: any, format?: string): Moment; 7 | 8 | public isValid(date: Moment): boolean; 9 | public isNull(date: Moment): boolean; 10 | public isEqual(value: Moment, comparing: Moment): boolean; 11 | public isAfter(date: Moment, value: Moment): boolean; 12 | public isBefore(date: Moment, value: Moment): boolean; 13 | public isAfterDay(date: Moment, value: Moment): boolean; 14 | public isBeforeDay(date: Moment, value: Moment): boolean; 15 | public isBeforeYear(date: Moment, value: Moment): boolean; 16 | public isAfterYear(date: Moment, value: Moment): boolean; 17 | public isSameDay(date: Moment, comparing: Moment): boolean; 18 | 19 | public addDays(date: Moment, count: number): Moment; 20 | public startOfDay(date: Moment): Moment; 21 | public endOfDay(date: Moment): Moment; 22 | 23 | public format(date: Moment, format: string): string; 24 | public formatNumber(num: number): string; 25 | public getMeridiemText(ampm: 'am' | 'pm'): string; 26 | 27 | public getHours(date: Moment): number; 28 | public setHours(date: Moment, value: number): Moment; 29 | 30 | public getMinutes(date: Moment): number; 31 | public setMinutes(date: Moment, value: number): Moment; 32 | 33 | public getMonth(date: Moment): number; 34 | public getStartOfMonth(date: Moment): Moment; 35 | public getNextMonth(date: Moment): Moment; 36 | public getPreviousMonth(date: Moment): Moment; 37 | 38 | public getYear(date: Moment): number; 39 | public setYear(date: Moment, year: number): Moment; 40 | public mergeDateAndTime(date: Moment, time: Moment): Moment; 41 | 42 | public getDiff(date: Moment, comparing: Moment): number; 43 | public getWeekdays(): string[]; 44 | public getWeekArray(date: Moment): Moment[][]; 45 | public getYearRange(start: Moment, end: Moment): Moment[]; 46 | 47 | public getCalendarHeaderText(date: Moment): string; 48 | public getDatePickerHeaderText(date: Moment): string; 49 | public getDateTimePickerHeaderText(date: Moment): string; 50 | public getDayText(date: Moment): string; 51 | public getHourText(date: Moment, ampm: boolean): string; 52 | public getMinuteText(date: Moment): string; 53 | public getYearText(date: Moment): string; 54 | } 55 | 56 | export default PersianUtils; 57 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Moment = require('moment'); 4 | var jMoment = require('moment-jalaali'); 5 | var extendMoment = require('moment-range').extendMoment; 6 | 7 | var moment = extendMoment(Moment); 8 | var symbolMap = { 9 | 1: '۱', 10 | 2: '۲', 11 | 3: '۳', 12 | 4: '۴', 13 | 5: '۵', 14 | 6: '۶', 15 | 7: '۷', 16 | 8: '۸', 17 | 9: '۹', 18 | 0: '۰', 19 | }; 20 | 21 | function parse(value, format) { 22 | return jMoment(value, format).locale('fa'); 23 | } 24 | 25 | function toJMoment(date) { 26 | return jMoment(date ? date.clone() : undefined).locale('fa'); 27 | } 28 | 29 | var Utils = function utils() { }; 30 | 31 | Utils.prototype.toJMoment = toJMoment; 32 | 33 | Utils.prototype.date = Utils.prototype.parse = parse; 34 | 35 | Utils.prototype.isValid = function isValid(date) { 36 | return date.isValid(); 37 | }; 38 | 39 | Utils.prototype.isNull = function isNull(date) { 40 | return date.parsingFlags().nullInput; 41 | }; 42 | 43 | Utils.prototype.isEqual = function isEqual(value, comparing) { 44 | return parse(value).isSame(comparing); 45 | }; 46 | 47 | Utils.prototype.isAfter = function isAfter(date, value) { 48 | return date.isAfter(value); 49 | }; 50 | 51 | Utils.prototype.isBefore = function isBefore(date, value) { 52 | return date.isBefore(value); 53 | }; 54 | 55 | Utils.prototype.isAfterDay = function isAfterDay(date, value) { 56 | return date.isAfter(value, 'day'); 57 | }; 58 | 59 | Utils.prototype.isBeforeDay = function isBeforeDay(date, value) { 60 | return date.isBefore(value, 'day'); 61 | }; 62 | 63 | Utils.prototype.isBeforeYear = function isBeforeYear(date, value) { 64 | return date.jYear() < value.jYear(); 65 | }; 66 | 67 | Utils.prototype.isAfterYear = function isAfterYear(date, value) { 68 | return date.jYear() > value.jYear(); 69 | }; 70 | 71 | Utils.prototype.startOfDay = function startOfDay(date) { 72 | return date.startOf('day'); 73 | }; 74 | 75 | Utils.prototype.endOfDay = function endOfDay(date) { 76 | return date.endOf('day'); 77 | }; 78 | 79 | Utils.prototype.format = function format(date, formatString) { 80 | return date.format(formatString); 81 | }; 82 | 83 | Utils.prototype.formatNumber = function formatNumber(num) { 84 | return (num || '').replace(/\d/g, function (match) { 85 | return symbolMap[match]; 86 | }).replace(/,/g, '،'); 87 | }; 88 | 89 | Utils.prototype.getMeridiemText = function getMeridiemText(ampm) { 90 | return ampm === 'am' ? toJMoment().hours(2).format('A') : toJMoment().hours(14).format('A'); 91 | }; 92 | 93 | Utils.prototype.addDays = function addDays(date, count) { 94 | return count < 0 ? date.clone().subtract(Math.abs(count), 'days') : date.clone().add(count, 'days'); 95 | }; 96 | 97 | Utils.prototype.isSameDay = function isSameDay(date, comparing) { 98 | return date.isSame(comparing, 'day'); 99 | }; 100 | 101 | Utils.prototype.getHours = function getHours(date) { 102 | return date.get('hours'); 103 | }; 104 | 105 | Utils.prototype.setHours = function setHours(date, value) { 106 | return date.clone().hours(value); 107 | }; 108 | 109 | Utils.prototype.getMinutes = function getMinutes(date) { 110 | return date.get('minutes'); 111 | }; 112 | 113 | Utils.prototype.setMinutes = function setMinutes(date, value) { 114 | return date.clone().minutes(value); 115 | }; 116 | 117 | Utils.prototype.getMonth = function getMonth(date) { 118 | return date.jMonth(); 119 | }; 120 | 121 | Utils.prototype.getStartOfMonth = function getStartOfMonth(date) { 122 | return date.clone().startOf('jMonth'); 123 | }; 124 | 125 | Utils.prototype.getNextMonth = function getNextMonth(date) { 126 | return date.clone().add(1, 'jMonth'); 127 | }; 128 | 129 | Utils.prototype.getPreviousMonth = function getPreviousMonth(date) { 130 | return date.clone().subtract(1, 'jMonth'); 131 | }; 132 | 133 | Utils.prototype.getYear = function getYear(date) { 134 | return date.jYear(); 135 | }; 136 | 137 | Utils.prototype.setYear = function setYear(date, year) { 138 | return date.clone().jYear(year); 139 | }; 140 | 141 | Utils.prototype.mergeDateAndTime = function mergeDateAndTime(date, time) { 142 | return this.setMinutes(this.setHours(date, this.getHours(time)), this.getMinutes(time)); 143 | }; 144 | 145 | Utils.prototype.getDiff = function getDiff(date, comparing) { 146 | return date.diff(comparing); 147 | } 148 | 149 | Utils.prototype.getWeekdays = function getWeekdays() { 150 | return [0, 1, 2, 3, 4, 5, 6].map(function (dayOfWeek) { 151 | return toJMoment().weekday(dayOfWeek).format('dd'); 152 | }); 153 | }; 154 | 155 | Utils.prototype.getWeekArray = function getWeekArray(date) { 156 | var start = toJMoment(date).startOf('jMonth').startOf('week'); 157 | var end = toJMoment(date).endOf('jMonth').endOf('week'); 158 | 159 | var weeks = Array.from(moment.range(start, end).by('week')); 160 | 161 | var nestedWeeks = []; 162 | 163 | weeks.forEach(function (week) { 164 | var end = week.clone().endOf('week'); 165 | nestedWeeks.push(Array.from(moment.range(week, end).by('day')).map(toJMoment)); 166 | }); 167 | 168 | return nestedWeeks; 169 | }; 170 | 171 | Utils.prototype.getYearRange = function getYearRange(start, end) { 172 | var startDate = parse(start); 173 | var endDate = parse(end); 174 | var years = []; 175 | 176 | var current = startDate; 177 | while (current.isBefore(endDate)) { 178 | years.push(current); 179 | current = current.clone().add(1, 'jYear'); 180 | } 181 | 182 | return years; 183 | }; 184 | 185 | Utils.prototype.getCalendarHeaderText = function getCalendarHeaderText(date) { 186 | return date.format('jMMMM jYYYY'); 187 | }; 188 | 189 | Utils.prototype.getYearText = function getYearText(date) { 190 | return date.format('jYYYY'); 191 | }; 192 | 193 | Utils.prototype.getDatePickerHeaderText = function getDatePickerHeaderText(date) { 194 | return date.format('ddd, jMMM jD'); 195 | }; 196 | 197 | Utils.prototype.getDateTimePickerHeaderText = function getDateTimePickerHeaderText(date) { 198 | return date.format('jMMM jD'); 199 | }; 200 | 201 | Utils.prototype.getDayText = function getDayText(date) { 202 | return date.format('jD'); 203 | }; 204 | 205 | Utils.prototype.getHourText = function getHourText(date, ampm) { 206 | return date.format(ampm ? 'hh' : 'HH'); 207 | }; 208 | 209 | Utils.prototype.getMinuteText = function getMinuteText(date) { 210 | return date.format('mm'); 211 | }; 212 | 213 | Utils['default'] = Utils; 214 | 215 | module.exports = Utils; 216 | --------------------------------------------------------------------------------