質問編集履歴

11

ロジックの修正

2020/04/16 16:54

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -76,9 +76,13 @@
76
76
 
77
77
  return (_str.length < 1) ? void(0) : (check(_str, fmt)) ? true : false;
78
78
 
79
+ // let _helper = new _str._formatCheckHelper(_str, fmt); // <- 代案
80
+
81
+ // return (_str.length < 1) ? void(0) : (_helper.executeFormatCheck()) ? true : false;
82
+
79
83
 
80
84
 
81
- function check (s, f) {
85
+ function check (s, f) { // <- コレは冗長かも...直接、ヘルパーのチェックをcallする方が分かりやすい?
82
86
 
83
87
  let _helper = new s._formatCheckHelper(s, f);
84
88
 

10

初学者のため、実行箇所を○数字で修正。。。

2020/04/16 16:54

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -100,6 +100,8 @@
100
100
 
101
101
  }
102
102
 
103
+ // [以下、prototype代入を実行。。。。記述順は考慮外(関数評価と実行スコープの入れ子は無視ください。)]
104
+
103
105
  // フォーマットと一致するかチェックする処理(親)
104
106
 
105
107
  _formatCheckHelper.prototype.executeFormatCheck = function () {
@@ -138,6 +140,10 @@
138
140
 
139
141
  _formatCheckHelper.prototype._eras = {
140
142
 
143
+ // 西暦を最初に一致させる。。。javascriptの「for in」句の仕様(実行順)が変更されると不具合でます。。。その時は直す
144
+
145
+ AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'},
146
+
141
147
  M: {name: 'M', firstDate: 18691023, lastDate: 19120729, year: 0, yearDeff: 1867, longName: '明治', shortName: '明', alphaName: 'M'},
142
148
 
143
149
  T: {name: 'T', firstDate: 19120730, lastDate: 19261224, year: 0, yearDeff: 1911, longName: '大正', shortName: '大', alphaName: 'T'},
@@ -146,9 +152,7 @@
146
152
 
147
153
  H: {name: 'H', firstDate: 19890108, lastDate: 20190430, year: 0, yearDeff: 1988, longName: '平成', shortName: '平', alphaName: 'H'},
148
154
 
149
- R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'},
155
+ R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'}
150
-
151
- AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'}
152
156
 
153
157
  };
154
158
 

9

文言の微修正

2020/04/16 15:36

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,8 @@
40
40
 
41
41
  (function (Str) {
42
42
 
43
+ // Stringオブジェクトの関数を追加
44
+
43
45
  Str.prototype.format = format;
44
46
 
45
47
  Str.prototype.formatCheck = formatCheck;
@@ -78,30 +80,38 @@
78
80
 
79
81
  function check (s, f) {
80
82
 
83
+ let _helper = new s._formatCheckHelper(s, f);
84
+
85
+ return _helper.executeFormatCheck();// こんな感じで呼び出したい
86
+
87
+ }
88
+
89
+ }
90
+
91
+ function _formatCheckHelper (str, fmt) {
92
+
93
+ if (!(this instanceof _formatCheckHelper)) { return void(0); } // メソッドとしてコールされた場合は何もせず返却
94
+
95
+ this.checkedString = str;
96
+
97
+ this.checkedFormat = fmt;
98
+
99
+ return this; // 受け取ったformatを正規表現化して返す..とか
100
+
101
+ }
102
+
103
+ // フォーマットと一致するかチェックする処理(親)
104
+
105
+ _formatCheckHelper.prototype.executeFormatCheck = function () {
106
+
107
+ let checkedResult = false;
108
+
81
109
  // チェック処理 <- ここのアルゴリズムを検討したい
82
110
 
83
- // return s._formatCheckHelper(f).test(s);// こんな感じか??
84
-
85
- let _helper = new s._formatCheckHelper(s, f);
86
-
87
- return false;
111
+ return checkedResult;
88
-
89
- }
90
112
 
91
113
  }
92
114
 
93
- function _formatCheckHelper (str, fmt) {
94
-
95
- if (!(this instanceof _formatCheckHelper)) { return void(0); } // メソッドとしてコールされた場合は何もせず返却
96
-
97
- this.checkedString = str;
98
-
99
- this.checkedFormat = fmt;
100
-
101
- return this; // 受け取ったformatを正規表現化して返す..とか
102
-
103
- }
104
-
105
115
  // 指定できるフォーマットのキーリスト
106
116
 
107
117
  _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'];

8

呼出元の引数"format"が想定と違うので修正

2020/04/16 15:26

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -166,7 +166,7 @@
166
166
 
167
167
  window._TEST_RESULT = {
168
168
 
169
- dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
169
+ dateString1Check: dateString1.formatCheck('yyyy-MM-dd hh:mm:ss.fff'),// <- 期待は"True"(一致したよ)
170
170
 
171
171
  dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
172
172
 

7

秒の部分があり得ない数値だったので修正

2020/04/16 15:19

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -160,7 +160,7 @@
160
160
 
161
161
  (function () {
162
162
 
163
- let dateString1 = '2020-01-11 12:46:78.234';
163
+ let dateString1 = '2020-01-11 12:46:16.234';
164
164
 
165
165
  let dateString2 = '2020月1月11日';
166
166
 

6

実装の経過を反映

2020/04/16 12:52

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,8 @@
92
92
 
93
93
  function _formatCheckHelper (str, fmt) {
94
94
 
95
+ if (!(this instanceof _formatCheckHelper)) { return void(0); } // メソッドとしてコールされた場合は何もせず返却
96
+
95
97
  this.checkedString = str;
96
98
 
97
99
  this.checkedFormat = fmt;
@@ -100,7 +102,55 @@
100
102
 
101
103
  }
102
104
 
105
+ // 指定できるフォーマットのキーリスト
106
+
103
107
  _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'];
108
+
109
+ _formatCheckHelper.prototype._jp = {
110
+
111
+ // MMMで使用する日本の月名の配列
112
+
113
+ month: ['睦月', '如月', '弥生', '卯月', '皐月', '水無月', '文月', '葉月', '長月', '神無月', '霜月', '師走'],
114
+
115
+ // DDD、DDで使用する曜日に使用する配列
116
+
117
+ day: ['日', '月', '火', '水', '木', '金', '土'],
118
+
119
+ // TTで使用する配列
120
+
121
+ noonJp: ['午前', '午後'],
122
+
123
+ // ttで使用する配列
124
+
125
+ noonEn: ['AM', 'PM']
126
+
127
+ };
128
+
129
+ _formatCheckHelper.prototype._eras = {
130
+
131
+ M: {name: 'M', firstDate: 18691023, lastDate: 19120729, year: 0, yearDeff: 1867, longName: '明治', shortName: '明', alphaName: 'M'},
132
+
133
+ T: {name: 'T', firstDate: 19120730, lastDate: 19261224, year: 0, yearDeff: 1911, longName: '大正', shortName: '大', alphaName: 'T'},
134
+
135
+ S: {name: 'S', firstDate: 19261225, lastDate: 19890107, year: 0, yearDeff: 1925, longName: '昭和', shortName: '昭', alphaName: 'S'},
136
+
137
+ H: {name: 'H', firstDate: 19890108, lastDate: 20190430, year: 0, yearDeff: 1988, longName: '平成', shortName: '平', alphaName: 'H'},
138
+
139
+ R: {name: 'R', firstDate: 20190501, lastDate: 99999999, year: 0, yearDeff: 2018, longName: '令和', shortName: '令', alphaName: 'R'},
140
+
141
+ AD: {name: 'AD', firstDate: 0, lastDate: 99999999, year: 0, yearDeff: 0, longName: '西暦', shortName: '西', alphaName: 'AD'}
142
+
143
+ };
144
+
145
+ _formatCheckHelper.prototype.padSlice = function (val, num) {
146
+
147
+ val = (val == void(0)) ? 0 : val;
148
+
149
+ num = (num == void(0)) ? 2 : num;
150
+
151
+ return (new Array(num).join('0') + val).slice(num * -1);
152
+
153
+ };
104
154
 
105
155
  })(String);
106
156
 
@@ -122,7 +172,11 @@
122
172
 
123
173
  };
124
174
 
175
+ console.log(window._TEST_RESULT);
176
+
125
177
  })();
178
+
179
+
126
180
 
127
181
  ```
128
182
 

5

ソースコードを修正

2020/04/16 12:37

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -38,15 +38,35 @@
38
38
 
39
39
 
40
40
 
41
- // 定義
41
+ (function (Str) {
42
42
 
43
- (function (Str) {
43
+ Str.prototype.format = format;
44
44
 
45
45
  Str.prototype.formatCheck = formatCheck;
46
46
 
47
47
  Str.prototype._formatCheckHelper = _formatCheckHelper;
48
48
 
49
49
 
50
+
51
+ function format () {
52
+
53
+ let _str = this;
54
+
55
+ // 'My name is {0} !'.format('Tama Shiro'); -> output 'My name is Tama Shiro !'
56
+
57
+ return (!arguments || arguments.length <= 0 ) ? _str : (function (str, args) {
58
+
59
+ for (var idx = 0, len = args.length; idx < len; idx++) {
60
+
61
+ str = str.replace(new RegExp('\{' + idx + '\}', 'g'), args[idx]);
62
+
63
+ }
64
+
65
+ return str;
66
+
67
+ })('' + _str, arguments);
68
+
69
+ }
50
70
 
51
71
  function formatCheck (fmt) {
52
72
 
@@ -60,17 +80,27 @@
60
80
 
61
81
  // チェック処理 <- ここのアルゴリズムを検討したい
62
82
 
63
- return s._formatCheckHelper(f).test(s);// こんな感じか??...フォーマットを分割や予め定義するべきか?
83
+ // return s._formatCheckHelper(f).test(s);// こんな感じか??
84
+
85
+ let _helper = new s._formatCheckHelper(s, f);
86
+
87
+ return false;
64
88
 
65
89
  }
66
90
 
67
91
  }
68
92
 
69
- function _formatCheckHelper (format) {
93
+ function _formatCheckHelper (str, fmt) {
70
94
 
95
+ this.checkedString = str;
96
+
97
+ this.checkedFormat = fmt;
98
+
71
- return new RegExp(); // 受け取ったformatを正規表現化して返す..とか
99
+ return this; // 受け取ったformatを正規表現化して返す..とか
72
100
 
73
101
  }
102
+
103
+ _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'];
74
104
 
75
105
  })(String);
76
106
 
@@ -80,7 +110,7 @@
80
110
 
81
111
  (function () {
82
112
 
83
- let dateString1 = '2020-01-11 12:46:17.234';
113
+ let dateString1 = '2020-01-11 12:46:78.234';
84
114
 
85
115
  let dateString2 = '2020月1月11日';
86
116
 
@@ -91,8 +121,6 @@
91
121
  dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
92
122
 
93
123
  };
94
-
95
- console.log(window._TEST_RESULT);
96
124
 
97
125
  })();
98
126
 

4

タイトルが冗長なので修正

2020/04/16 12:16

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
@@ -1 +1 @@
1
- [JavaScript]文字列定義された日付のフォーマットチェック機能の実装
1
+ 文字列定義された日付のフォーマットチェック機能のレシピ案
test CHANGED
File without changes

3

実行すると「Uncaught SyntaxError: Unexpected identifier」エラーなので、修正、カンマが抜けてた

2020/04/16 11:47

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -86,7 +86,7 @@
86
86
 
87
87
  window._TEST_RESULT = {
88
88
 
89
- dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff')// <- 期待は"True"(一致したよ)
89
+ dateString1Check: dateString1.formatCheck('yyyy/MM/dd hh24:mm:ss.fff'),// <- 期待は"True"(一致したよ)
90
90
 
91
91
  dateString2Check: dateString2.formatCheck('yyyy/MM/dd')// <- 期待は"False"(一致しないよ)
92
92
 

2

秒の部分があり得ない数値だったので修正

2020/04/16 11:43

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -80,7 +80,7 @@
80
80
 
81
81
  (function () {
82
82
 
83
- let dateString1 = '2020-01-11 12:46:78.234';
83
+ let dateString1 = '2020-01-11 12:46:17.234';
84
84
 
85
85
  let dateString2 = '2020月1月11日';
86
86
 

1

誤字の修正

2020/04/16 11:41

投稿

tama_yn0815
tama_yn0815

スコア143

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
  Str.prototype.formatCheck = formatCheck;
46
46
 
47
- Str.prototype._formatCheckHelper = formatCheckHelper;
47
+ Str.prototype._formatCheckHelper = _formatCheckHelper;
48
48
 
49
49
 
50
50
 
@@ -66,7 +66,7 @@
66
66
 
67
67
  }
68
68
 
69
- function formatCheckHelper (format) {
69
+ function _formatCheckHelper (format) {
70
70
 
71
71
  return new RegExp(); // 受け取ったformatを正規表現化して返す..とか
72
72