React sample video recording app

cal.js 3.6KB

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