Benoit Sida 3 år sedan
förälder
incheckning
160a0d2240
2 ändrade filer med 81 tillägg och 0 borttagningar
  1. 65
    0
      cal.js
  2. 16
    0
      package.json

+ 65
- 0
cal.js Visa fil

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

+ 16
- 0
package.json Visa fil

@@ -0,0 +1,16 @@
1
+{
2
+  "name": "cal",
3
+  "version": "1.0.0",
4
+  "description": "node CLI command that display the next three month",
5
+  "main": "cal.js",
6
+  "scripts": {
7
+    "start": "node cal.js"
8
+  },
9
+  "repository": {
10
+    "type": "git",
11
+    "url": "ssh://git@git.coldiary.eu:10022/coldiary/cal.js.git"
12
+  },
13
+  "keywords": [],
14
+  "author": "",
15
+  "license": "ISC"
16
+}