2017年6月7日水曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の art 2(Moving Forward)、Chapter 13(Regexes and Grammars)の Named Rules (a.k.a. Subrules)、Exercise 13-1: Getting the Feburary Dates Right.を JavaScript で取り組んでみる。

Exercise 13-1: Getting the Feburary Dates Right.

コード(Emacs)

HTML5

<pre id="output0"></pre>
<input id="date0" type="date" value="2016-12-31">
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_date = document.querySelector('#date0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];

        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };

let output = () => {
    let date = input_date.value,
        dates = ['Chrimstmas : 2016-12-25.', '2016-02-28', '2016-02-30', date];

    dates.forEach((date) => {
        p(date);
        let m1 = date.match(/(\d{4})[-/](0[469]|11)[-/](0[1-9]|[12][0-9]|30)/),
            m2 = date.match(
                    /(\d{4})[-/](0[13578]|1[02])[-/](0[1-9]|[12][0-9]|3[01])/),
            m3 = date.match(/(\d{4})[-/](02)[-/](0[1-9]|[12][0-8])/),
            m = m1 || m2 || m3;

        if (m) {
            p(m[0]);
            p(`Day   = ${m[1]}`);
            p(`Month = ${m[2]}`);
            p(`Year  = ${m[3]}`);
        }
        p('');
    });
};
let clear = () => pre0.textContent = '';

input_date.onchange = output;
btn0.onclick = output;
btn1.onclick = clear;

output();














						

0 コメント:

コメントを投稿