├── .gitignore ├── README.md ├── package.json ├── index.d.ts ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Persian calendar utils for Material UI DatePicker 2 | 3 | This library provides implementation specific utility functions that can 4 | be used by the material-ui's DatePicker component to correctly render 5 | a Persian calendar. 6 | 7 | Please refer to material-ui's 8 | [documentation page for DatePicker](http://www.material-ui.com/#/components/date-picker) 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-persian-date-picker-utils 18 | ``` 19 | 20 | ## Thanks 21 | 22 | Great thanks to [material-ui](https://github.com/callemall/material-ui) for their great react components. 23 | 24 | ## License 25 | This project is licensed under the [MIT license](https://github.com/alitaheri/react-mixout/blob/master/LICENSE). -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "material-ui-persian-date-picker-utils", 3 | "version": "0.1.2", 4 | "description": "Calendar utils that add full Persian calendar system support to material-ui", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/alitaheri/material-ui-persian-date-picker-utils.git" 9 | }, 10 | "author": "Ali Taheri Moghaddar", 11 | "keywords": [ 12 | "material-ui", 13 | "calendar", 14 | "persian", 15 | "date-picker", 16 | "datepicker", 17 | "jalali", 18 | "jalaali" 19 | ], 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/alitaheri/material-ui-persian-date-picker-utils/issues" 23 | }, 24 | "homepage": "https://github.com/alitaheri/material-ui-persian-date-picker-utils#readme", 25 | "dependencies": { 26 | "moment-jalaali": "^0.7.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export declare function getWeekArray(date: Date, firstDayOfWeek: number): (Date | null)[][]; 2 | export declare function getYear(date: Date): number; 3 | export declare function setYear(date: Date, year: number): Date; 4 | export declare function addDays(date: Date, days: number): Date; 5 | export declare function addMonths(date: Date, months: number): Date; 6 | export declare function addYears(date: Date, years: number): Date; 7 | export declare function getFirstDayOfMonth(date: Date): Date; 8 | export declare function monthDiff(date1: Date, date2: Date): number; 9 | 10 | export interface PersianUtils { 11 | getWeekArray: typeof getWeekArray; 12 | getYear: typeof getYear; 13 | setYear: typeof setYear; 14 | addDays: typeof addDays; 15 | addMonths: typeof addMonths; 16 | addYears: typeof addYears; 17 | getFirstDayOfMonth: typeof getFirstDayOfMonth; 18 | monthDiff: typeof monthDiff; 19 | } 20 | 21 | declare const utils: PersianUtils; 22 | 23 | export default utils; 24 | -------------------------------------------------------------------------------- /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.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var moment = require('moment-jalaali'); 4 | 5 | function addWeek(weekArray, week) { 6 | var emptyDays = 7 - week.length; 7 | 8 | for (var i = 0; i < emptyDays; ++i) { 9 | week[weekArray.length ? 'push' : 'unshift'](null); 10 | } 11 | 12 | weekArray.push(week); 13 | }; 14 | 15 | function getWeekArray(d, firstDayOfWeek) { 16 | var daysInMonth = moment.jDaysInMonth(moment(d).jYear(), moment(d).jMonth()); 17 | 18 | var dayArray = []; 19 | for (var i = 1; i <= daysInMonth; i++) { 20 | dayArray.push(moment(d).jDate(i).toDate()); 21 | } 22 | 23 | var weekArray = []; 24 | var week = []; 25 | 26 | dayArray.forEach(function (day) { 27 | if (week.length > 0 && day.getDay() === firstDayOfWeek) { 28 | addWeek(weekArray, week); 29 | week = []; 30 | } 31 | 32 | week.push(day); 33 | 34 | if (dayArray.indexOf(day) === dayArray.length - 1) { 35 | addWeek(weekArray, week); 36 | } 37 | }); 38 | 39 | return weekArray; 40 | } 41 | 42 | function getYear(date) { 43 | return moment(date).jYear(); 44 | } 45 | 46 | function setYear(date, year) { 47 | return moment(date).jYear(year).toDate(); 48 | } 49 | 50 | function addDays(date, days) { 51 | return moment(date).add(days, 'days').toDate(); 52 | } 53 | 54 | function addMonths(date, months) { 55 | return moment(date).add(months, 'jMonth').toDate(); 56 | } 57 | 58 | function addYears(date, years) { 59 | return moment(date).add(years, 'jYear').toDate(); 60 | } 61 | 62 | function getFirstDayOfMonth(date) { 63 | return moment(date).jDate(1).toDate(); 64 | } 65 | 66 | function monthDiff(date1, date2) { 67 | var months = (moment(date1).jYear() - moment(date2).jYear()) * 12; 68 | months += moment(date1).jMonth(); 69 | months -= moment(date2).jMonth(); 70 | return months; 71 | } 72 | 73 | var utils = { 74 | getWeekArray: getWeekArray, 75 | getYear: getYear, 76 | setYear: setYear, 77 | addDays: addDays, 78 | addMonths: addMonths, 79 | addYears: addYears, 80 | getFirstDayOfMonth: getFirstDayOfMonth, 81 | monthDiff: monthDiff, 82 | }; 83 | 84 | utils['default'] = utils; 85 | 86 | module.exports = utils; --------------------------------------------------------------------------------