| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /*
-
- ╔═════ juin 2017 ══════╤════ juillet 2017 ════╤══════ août 2017 ═════╗
- ║ lu ma me je ve sa di │ lu ma me je ve sa di │ lu ma me je ve sa di ║
- ╟──────────────────────┼──────────────────────┼──────────────────────╢
- ║ 1 2 3 4 │ 1 2 │ 1 2 3 4 5 6 ║
- ║ 5 6 7 8 9 10 11 │ 3 4 5 6 7 8 9 │ 7 8 9 10 11 12 13 ║
- ║ 12 13 14 15 16 17 18 │ 10 11 12 13 14 15 16 │ 14 15 16 17 18 19 20 ║
- ║ 19 20 21 22 23 24 25 │ 17 18 19 20 21 22 23 │ 21 22 23 24 25 26 27 ║
- ║ 26 27 28 29 30 │ 24 25 26 27 28 29 30 │ 28 29 30 31 ║
- ║ │ 31 │ ║
- ╚══════════════════════╧══════════════════════╧══════════════════════╝
-
- */
-
- Array.prototype.chunk = Array.prototype.chunk || function(n) {
- return !this.length ? [] : [this.slice(0, n)].concat(this.slice(n).chunk(n))
- }
-
- const CalPrinter = date => {
- date = date || new Date
-
- const monthData = (date, offset) => {
- date = new Date((new Date).setMonth(date.getMonth() + (offset || 0)))
-
- const year = date.getFullYear()
- const month = date.toLocaleString('fr-FR', { month: 'long'})
- const text = ` ${month} ${year} `
- const padding = (22 - text.length) / 2
- const title = '═'.repeat(Math.floor(padding)) + text + '═'.repeat(Math.ceil(padding))
-
- let start = new Date(year, date.getMonth()).getDay() - 1
- start = start < 0 ? 6 : start
- const end = new Date(year, date.getMonth() + 1, 0).getDate()
- let dates = [...Array(end + 1).keys()].slice(1).map(n =>` ${n}`.slice(-2))
- const lines = [...Array(start).fill(' '), ...dates, ...Array(18).fill(' ') ]
- .chunk(7).map(c => ` ${c.join(' ')} `)
-
- return { title, lines }
- }
-
- const days = ' ' + [..."0123456"].map(n => (new Date(2017, 1, n + 6))
- .toLocaleString('fr-FR', { weekday: 'short' })
- .substr(0, 2)).join(' ') + ' '
-
- const months = [..."012"].map((v, i) => monthData(date, i))
-
- const printer = ({ days, months }) =>
- `
- ╔${ months[0].title }╤${ months[1].title }╤${ months[2].title }╗
- ║${ days }│${ days }│${ days }║
- ╟──────────────────────┼──────────────────────┼──────────────────────╢
- ║${months[0].lines[0] }│${months[1].lines[0] }│${months[2].lines[0] }║
- ║${months[0].lines[1] }│${months[1].lines[1] }│${months[2].lines[1] }║
- ║${months[0].lines[2] }│${months[1].lines[2] }│${months[2].lines[2] }║
- ║${months[0].lines[3] }│${months[1].lines[3] }│${months[2].lines[3] }║
- ║${months[0].lines[4] }│${months[1].lines[4] }│${months[2].lines[4] }║
- ║${months[0].lines[5] }│${months[1].lines[5] }│${months[2].lines[5] }║
- ╚══════════════════════╧══════════════════════╧══════════════════════╝
- `
-
- return printer.bind(null, { days, months })
- }
-
- console.log(CalPrinter()())
|