質問編集履歴
11
ロジックの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -37,8 +37,10 @@
|
|
37
37
|
function formatCheck (fmt) {
|
38
38
|
let _str = this;
|
39
39
|
return (_str.length < 1) ? void(0) : (check(_str, fmt)) ? true : false;
|
40
|
+
// let _helper = new _str._formatCheckHelper(_str, fmt); // <- 代案
|
41
|
+
// return (_str.length < 1) ? void(0) : (_helper.executeFormatCheck()) ? true : false;
|
40
42
|
|
41
|
-
function check (s, f) {
|
43
|
+
function check (s, f) { // <- コレは冗長かも...直接、ヘルパーのチェックをcallする方が分かりやすい?
|
42
44
|
let _helper = new s._formatCheckHelper(s, f);
|
43
45
|
return _helper.executeFormatCheck();// こんな感じで呼び出したい
|
44
46
|
}
|
10
初学者のため、実行箇所を○数字で修正。。。
title
CHANGED
File without changes
|
body
CHANGED
@@ -49,6 +49,7 @@
|
|
49
49
|
this.checkedFormat = fmt;
|
50
50
|
return this; // 受け取ったformatを正規表現化して返す..とか
|
51
51
|
}
|
52
|
+
// [以下、prototype代入を実行。。。。記述順は考慮外(関数評価と実行スコープの入れ子は無視ください。)]
|
52
53
|
// フォーマットと一致するかチェックする処理(親)
|
53
54
|
_formatCheckHelper.prototype.executeFormatCheck = function () {
|
54
55
|
let checkedResult = false;
|
@@ -68,12 +69,13 @@
|
|
68
69
|
noonEn: ['AM', 'PM']
|
69
70
|
};
|
70
71
|
_formatCheckHelper.prototype._eras = {
|
72
|
+
// 西暦を最初に一致させる。。。javascriptの「for in」句の仕様(実行順)が変更されると不具合でます。。。その時は直す
|
73
|
+
AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'},
|
71
74
|
M: {name: 'M', firstDate: 18691023, lastDate: 19120729, year: 0, yearDeff: 1867, longName: '明治', shortName: '明', alphaName: 'M'},
|
72
75
|
T: {name: 'T', firstDate: 19120730, lastDate: 19261224, year: 0, yearDeff: 1911, longName: '大正', shortName: '大', alphaName: 'T'},
|
73
76
|
S: {name: 'S', firstDate: 19261225, lastDate: 19890107, year: 0, yearDeff: 1925, longName: '昭和', shortName: '昭', alphaName: 'S'},
|
74
77
|
H: {name: 'H', firstDate: 19890108, lastDate: 20190430, year: 0, yearDeff: 1988, longName: '平成', shortName: '平', alphaName: 'H'},
|
75
|
-
R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'}
|
78
|
+
R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'}
|
76
|
-
AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'}
|
77
79
|
};
|
78
80
|
_formatCheckHelper.prototype.padSlice = function (val, num) {
|
79
81
|
val = (val == void(0)) ? 0 : val;
|
9
文言の微修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,6 +19,7 @@
|
|
19
19
|
"use strict";
|
20
20
|
|
21
21
|
(function (Str) {
|
22
|
+
// Stringオブジェクトの関数を追加
|
22
23
|
Str.prototype.format = format;
|
23
24
|
Str.prototype.formatCheck = formatCheck;
|
24
25
|
Str.prototype._formatCheckHelper = _formatCheckHelper;
|
@@ -38,10 +39,8 @@
|
|
38
39
|
return (_str.length < 1) ? void(0) : (check(_str, fmt)) ? true : false;
|
39
40
|
|
40
41
|
function check (s, f) {
|
41
|
-
// チェック処理 <- ここのアルゴリズムを検討したい
|
42
|
-
// return s._formatCheckHelper(f).test(s);// こんな感じか??
|
43
42
|
let _helper = new s._formatCheckHelper(s, f);
|
44
|
-
return
|
43
|
+
return _helper.executeFormatCheck();// こんな感じで呼び出したい
|
45
44
|
}
|
46
45
|
}
|
47
46
|
function _formatCheckHelper (str, fmt) {
|
@@ -50,6 +49,12 @@
|
|
50
49
|
this.checkedFormat = fmt;
|
51
50
|
return this; // 受け取ったformatを正規表現化して返す..とか
|
52
51
|
}
|
52
|
+
// フォーマットと一致するかチェックする処理(親)
|
53
|
+
_formatCheckHelper.prototype.executeFormatCheck = function () {
|
54
|
+
let checkedResult = false;
|
55
|
+
// チェック処理 <- ここのアルゴリズムを検討したい
|
56
|
+
return checkedResult;
|
57
|
+
}
|
53
58
|
// 指定できるフォーマットのキーリスト
|
54
59
|
_formatCheckHelper.prototype._formatKeys = ['yyyy','yy','ee','e','ggg','gg','g','MMM','MM','M','dd','d','HH','H','hh','h','mm','m','ss','s','fff','f','TT','tt','DDD','DD'];
|
55
60
|
_formatCheckHelper.prototype._jp = {
|
8
呼出元の引数"format"が想定と違うので修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -82,7 +82,7 @@
|
|
82
82
|
let dateString1 = '2020-01-11 12:46:16.234';
|
83
83
|
let dateString2 = '2020月1月11日';
|
84
84
|
window._TEST_RESULT = {
|
85
|
-
dateString1Check: dateString1.formatCheck('yyyy
|
85
|
+
dateString1Check: dateString1.formatCheck('yyyy-MM-dd hh:mm:ss.fff'),// <- 期待は"True"(一致したよ)
|
86
86
|
dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
|
87
87
|
};
|
88
88
|
console.log(window._TEST_RESULT);
|
7
秒の部分があり得ない数値だったので修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -79,7 +79,7 @@
|
|
79
79
|
|
80
80
|
// 実行
|
81
81
|
(function () {
|
82
|
-
let dateString1 = '2020-01-11 12:46:
|
82
|
+
let dateString1 = '2020-01-11 12:46:16.234';
|
83
83
|
let dateString2 = '2020月1月11日';
|
84
84
|
window._TEST_RESULT = {
|
85
85
|
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
|
6
実装の経過を反映
title
CHANGED
File without changes
|
body
CHANGED
@@ -45,11 +45,36 @@
|
|
45
45
|
}
|
46
46
|
}
|
47
47
|
function _formatCheckHelper (str, fmt) {
|
48
|
+
if (!(this instanceof _formatCheckHelper)) { return void(0); } // メソッドとしてコールされた場合は何もせず返却
|
48
49
|
this.checkedString = str;
|
49
50
|
this.checkedFormat = fmt;
|
50
51
|
return this; // 受け取ったformatを正規表現化して返す..とか
|
51
52
|
}
|
53
|
+
// 指定できるフォーマットのキーリスト
|
52
54
|
_formatCheckHelper.prototype._formatKeys = ['yyyy','yy','ee','e','ggg','gg','g','MMM','MM','M','dd','d','HH','H','hh','h','mm','m','ss','s','fff','f','TT','tt','DDD','DD'];
|
55
|
+
_formatCheckHelper.prototype._jp = {
|
56
|
+
// MMMで使用する日本の月名の配列
|
57
|
+
month: ['睦月', '如月', '弥生', '卯月', '皐月', '水無月', '文月', '葉月', '長月', '神無月', '霜月', '師走'],
|
58
|
+
// DDD、DDで使用する曜日に使用する配列
|
59
|
+
day: ['日', '月', '火', '水', '木', '金', '土'],
|
60
|
+
// TTで使用する配列
|
61
|
+
noonJp: ['午前', '午後'],
|
62
|
+
// ttで使用する配列
|
63
|
+
noonEn: ['AM', 'PM']
|
64
|
+
};
|
65
|
+
_formatCheckHelper.prototype._eras = {
|
66
|
+
M: {name: 'M', firstDate: 18691023, lastDate: 19120729, year: 0, yearDeff: 1867, longName: '明治', shortName: '明', alphaName: 'M'},
|
67
|
+
T: {name: 'T', firstDate: 19120730, lastDate: 19261224, year: 0, yearDeff: 1911, longName: '大正', shortName: '大', alphaName: 'T'},
|
68
|
+
S: {name: 'S', firstDate: 19261225, lastDate: 19890107, year: 0, yearDeff: 1925, longName: '昭和', shortName: '昭', alphaName: 'S'},
|
69
|
+
H: {name: 'H', firstDate: 19890108, lastDate: 20190430, year: 0, yearDeff: 1988, longName: '平成', shortName: '平', alphaName: 'H'},
|
70
|
+
R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'},
|
71
|
+
AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'}
|
72
|
+
};
|
73
|
+
_formatCheckHelper.prototype.padSlice = function (val, num) {
|
74
|
+
val = (val == void(0)) ? 0 : val;
|
75
|
+
num = (num == void(0)) ? 2 : num;
|
76
|
+
return (new Array(num).join('0') + val).slice(num * -1);
|
77
|
+
};
|
53
78
|
})(String);
|
54
79
|
|
55
80
|
// 実行
|
@@ -60,7 +85,9 @@
|
|
60
85
|
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
|
61
86
|
dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
|
62
87
|
};
|
88
|
+
console.log(window._TEST_RESULT);
|
63
89
|
})();
|
90
|
+
|
64
91
|
```
|
65
92
|
|
66
93
|
### 試したこと
|
5
ソースコードを修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -18,34 +18,48 @@
|
|
18
18
|
```JavaScirpt
|
19
19
|
"use strict";
|
20
20
|
|
21
|
-
// 定義
|
22
21
|
(function (Str) {
|
22
|
+
Str.prototype.format = format;
|
23
23
|
Str.prototype.formatCheck = formatCheck;
|
24
24
|
Str.prototype._formatCheckHelper = _formatCheckHelper;
|
25
25
|
|
26
|
+
function format () {
|
27
|
+
let _str = this;
|
28
|
+
// 'My name is {0} !'.format('Tama Shiro'); -> output 'My name is Tama Shiro !'
|
29
|
+
return (!arguments || arguments.length <= 0 ) ? _str : (function (str, args) {
|
30
|
+
for (var idx = 0, len = args.length; idx < len; idx++) {
|
31
|
+
str = str.replace(new RegExp('\{' + idx + '\}', 'g'), args[idx]);
|
32
|
+
}
|
33
|
+
return str;
|
34
|
+
})('' + _str, arguments);
|
35
|
+
}
|
26
36
|
function formatCheck (fmt) {
|
27
37
|
let _str = this;
|
28
38
|
return (_str.length < 1) ? void(0) : (check(_str, fmt)) ? true : false;
|
29
39
|
|
30
40
|
function check (s, f) {
|
31
41
|
// チェック処理 <- ここのアルゴリズムを検討したい
|
32
|
-
return s._formatCheckHelper(f).test(s);// こんな感じか??
|
42
|
+
// return s._formatCheckHelper(f).test(s);// こんな感じか??
|
43
|
+
let _helper = new s._formatCheckHelper(s, f);
|
44
|
+
return false;
|
33
45
|
}
|
34
46
|
}
|
35
|
-
function _formatCheckHelper (
|
47
|
+
function _formatCheckHelper (str, fmt) {
|
48
|
+
this.checkedString = str;
|
49
|
+
this.checkedFormat = fmt;
|
36
|
-
return
|
50
|
+
return this; // 受け取ったformatを正規表現化して返す..とか
|
37
51
|
}
|
52
|
+
_formatCheckHelper.prototype._formatKeys = ['yyyy','yy','ee','e','ggg','gg','g','MMM','MM','M','dd','d','HH','H','hh','h','mm','m','ss','s','fff','f','TT','tt','DDD','DD'];
|
38
53
|
})(String);
|
39
54
|
|
40
55
|
// 実行
|
41
56
|
(function () {
|
42
|
-
let dateString1 = '2020-01-11 12:46:
|
57
|
+
let dateString1 = '2020-01-11 12:46:78.234';
|
43
58
|
let dateString2 = '2020月1月11日';
|
44
59
|
window._TEST_RESULT = {
|
45
60
|
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
|
46
61
|
dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
|
47
62
|
};
|
48
|
-
console.log(window._TEST_RESULT);
|
49
63
|
})();
|
50
64
|
```
|
51
65
|
|
4
タイトルが冗長なので修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
文字列定義された日付のフォーマットチェック機能のレシピ案
|
body
CHANGED
File without changes
|
3
実行すると「Uncaught SyntaxError: Unexpected identifier」エラーなので、修正、カンマが抜けてた
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
let dateString1 = '2020-01-11 12:46:17.234';
|
43
43
|
let dateString2 = '2020月1月11日';
|
44
44
|
window._TEST_RESULT = {
|
45
|
-
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff')// <- 期待は"True"(一致したよ)
|
45
|
+
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
|
46
46
|
dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
|
47
47
|
};
|
48
48
|
console.log(window._TEST_RESULT);
|
2
秒の部分があり得ない数値だったので修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,7 +39,7 @@
|
|
39
39
|
|
40
40
|
// 実行
|
41
41
|
(function () {
|
42
|
-
let dateString1 = '2020-01-11 12:46:
|
42
|
+
let dateString1 = '2020-01-11 12:46:17.234';
|
43
43
|
let dateString2 = '2020月1月11日';
|
44
44
|
window._TEST_RESULT = {
|
45
45
|
dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff')// <- 期待は"True"(一致したよ)
|
1
誤字の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
// 定義
|
22
22
|
(function (Str) {
|
23
23
|
Str.prototype.formatCheck = formatCheck;
|
24
|
-
Str.prototype._formatCheckHelper =
|
24
|
+
Str.prototype._formatCheckHelper = _formatCheckHelper;
|
25
25
|
|
26
26
|
function formatCheck (fmt) {
|
27
27
|
let _str = this;
|
@@ -32,7 +32,7 @@
|
|
32
32
|
return s._formatCheckHelper(f).test(s);// こんな感じか??...フォーマットを分割や予め定義するべきか?
|
33
33
|
}
|
34
34
|
}
|
35
|
-
function
|
35
|
+
function _formatCheckHelper (format) {
|
36
36
|
return new RegExp(); // 受け取ったformatを正規表現化して返す..とか
|
37
37
|
}
|
38
38
|
})(String);
|