w-dwdata-tweqod 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintignore +3 -0
  3. package/.eslintrc.js +55 -0
  4. package/.jsdoc +25 -0
  5. package/LICENSE +21 -0
  6. package/README.md +24 -0
  7. package/SECURITY.md +5 -0
  8. package/babel.config.js +16 -0
  9. package/dist/w-dwdata-tweqod.umd.js +8 -0
  10. package/dist/w-dwdata-tweqod.umd.js.map +1 -0
  11. package/docs/WDwdataTweqod.mjs.html +445 -0
  12. package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  13. package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  14. package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  15. package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  16. package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  17. package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  18. package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  19. package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  20. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  21. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
  22. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  23. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  24. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  25. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  26. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
  27. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  28. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  29. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  30. package/docs/global.html +779 -0
  31. package/docs/index.html +84 -0
  32. package/docs/scripts/collapse.js +39 -0
  33. package/docs/scripts/commonNav.js +28 -0
  34. package/docs/scripts/linenumber.js +25 -0
  35. package/docs/scripts/nav.js +12 -0
  36. package/docs/scripts/polyfill.js +4 -0
  37. package/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  38. package/docs/scripts/prettify/lang-css.js +2 -0
  39. package/docs/scripts/prettify/prettify.js +28 -0
  40. package/docs/scripts/search.js +99 -0
  41. package/docs/styles/jsdoc.css +776 -0
  42. package/docs/styles/prettify.css +80 -0
  43. package/g.downloadEqs.mjs +21 -0
  44. package/g.mjs +60 -0
  45. package/package.json +33 -0
  46. package/script.txt +17 -0
  47. package/src/WDwdataTweqod.mjs +373 -0
  48. package/src/downloadEqs.mjs +168 -0
  49. package/test/WDwdataTweq.test.mjs +137 -0
  50. package/toolg/addVersion.mjs +4 -0
  51. package/toolg/cleanFolder.mjs +4 -0
  52. package/toolg/gDistRollup.mjs +26 -0
  53. package/toolg/modifyReadme.mjs +4 -0
@@ -0,0 +1,445 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+
5
+ <meta charset="utf-8">
6
+ <title>WDwdataTweqod.mjs - Documentation</title>
7
+
8
+
9
+ <script src="scripts/prettify/prettify.js"></script>
10
+ <script src="scripts/prettify/lang-css.js"></script>
11
+ <!--[if lt IE 9]>
12
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
13
+ <![endif]-->
14
+ <link type="text/css" rel="stylesheet" href="styles/prettify.css">
15
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
16
+ <script src="scripts/nav.js" defer></script>
17
+
18
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
19
+ </head>
20
+ <body>
21
+
22
+ <input type="checkbox" id="nav-trigger" class="nav-trigger" />
23
+ <label for="nav-trigger" class="navicon-button x">
24
+ <div class="navicon"></div>
25
+ </label>
26
+
27
+ <label for="nav-trigger" class="overlay"></label>
28
+
29
+ <nav >
30
+
31
+
32
+ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#WDwdataTweqod">WDwdataTweqod</a></li></ul>
33
+
34
+ </nav>
35
+
36
+ <div id="main">
37
+
38
+ <h1 class="page-title">WDwdataTweqod.mjs</h1>
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+ <section>
47
+ <article>
48
+ <pre class="prettyprint source linenums"><code>import fs from 'fs'
49
+ import get from 'lodash-es/get.js'
50
+ import each from 'lodash-es/each.js'
51
+ import reverse from 'lodash-es/reverse.js'
52
+ import values from 'lodash-es/values.js'
53
+ import isestr from 'wsemi/src/isestr.mjs'
54
+ import isfun from 'wsemi/src/isfun.mjs'
55
+ import ispm from 'wsemi/src/ispm.mjs'
56
+ import haskey from 'wsemi/src/haskey.mjs'
57
+ import pmSeries from 'wsemi/src/pmSeries.mjs'
58
+ import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
59
+ import fsCopyFile from 'wsemi/src/fsCopyFile.mjs'
60
+ import fsCleanFolder from 'wsemi/src/fsCleanFolder.mjs'
61
+ import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
62
+ import fsDeleteFolder from 'wsemi/src/fsDeleteFolder.mjs'
63
+ import fsTreeFolder from 'wsemi/src/fsTreeFolder.mjs'
64
+ import fsGetFileXxHash from 'wsemi/src/fsGetFileXxHash.mjs'
65
+ import WDwdataBuilder from 'w-dwdata-builder/src/WDwdataBuilder.mjs'
66
+ import downloadEqs from './downloadEqs.mjs'
67
+ import fsCopyFolder from 'wsemi/src/fsCopyFolder.mjs'
68
+
69
+
70
+ /**
71
+ * 於OpenData下載台灣氣象署地震資料
72
+ *
73
+ * @param {String} token 輸入氣象署OpenData之API用token字串
74
+ * @param {Object} [opt={}] 輸入設定物件,預設{}
75
+ * @param {String} [opt.fdDwStorage] 輸入完整下載數據資料夾字串,預設'./_dwStorage'
76
+ * @param {String} [opt.fdDwAttime] 輸入當前下載供比對hash用之數據資料夾字串,預設'./_dwAttime'
77
+ * @param {String} [opt.fdDwCurrent] 輸入已下載供比對hash用之數據資料夾字串,預設'./_dwCurrent'
78
+ * @param {String} [opt.fdResult] 輸入已下載數據所連動生成數據資料夾字串,預設'./_result'
79
+ * @param {String} [opt.fdTaskCpSrc] 輸入任務狀態之來源端資料夾字串,預設'./_taskCpSrc'
80
+ * @param {Function} [opt.funDownloadEqs] 輸入自定義下載地震數據函數,回傳資料陣列
81
+ * @param {Function} [opt.funDownload] 輸入自定義當前下載之hash數據處理函數,回傳資料陣列
82
+ * @param {Function} [opt.funGetCurrent] 輸入自定義已下載之hash數據處理函數,回傳資料陣列
83
+ * @param {Function} [opt.funAdd] 輸入當有新資料時,需要連動處理之函數
84
+ * @param {Function} [opt.funModify] 輸入當有資料需更新時,需要連動處理之函數
85
+ * @param {Function} [opt.funRemove] 輸入當有資料需刪除時,需要連動處理之函數
86
+ * @returns {Object} 回傳事件物件,可呼叫函數on監聽change事件
87
+ * @example
88
+ *
89
+ * import fs from 'fs'
90
+ * import _ from 'lodash-es'
91
+ * import w from 'wsemi'
92
+ * import WDwdataTweqod from './src/WDwdataTweqod.mjs'
93
+ *
94
+ * let j = fs.readFileSync('../_data/settings.json', 'utf8')
95
+ * let st = JSON.parse(j)
96
+ * let token = _.get(st, 'token')
97
+ *
98
+ * //fdDwStorage
99
+ * let fdDwStorage = `./_dwStorage`
100
+ * w.fsCleanFolder(fdDwStorage)
101
+ *
102
+ * //fdDwAttime
103
+ * let fdDwAttime = `./_dwAttime`
104
+ * w.fsCleanFolder(fdDwAttime)
105
+ *
106
+ * //fdDwCurrent
107
+ * let fdDwCurrent = `./_dwCurrent`
108
+ * w.fsCleanFolder(fdDwCurrent)
109
+ *
110
+ * //fdResult
111
+ * let fdResult = './_result'
112
+ * w.fsCleanFolder(fdResult)
113
+ *
114
+ * let opt = {
115
+ * fdDwStorage,
116
+ * fdDwAttime,
117
+ * fdDwCurrent,
118
+ * fdResult,
119
+ * // funDownload,
120
+ * // funGetCurrent,
121
+ * // funRemove,
122
+ * // funAdd,
123
+ * // funModify,
124
+ * }
125
+ * let ev = await WDwdataTweqod(token, opt)
126
+ * .catch((err) => {
127
+ * console.log(err)
128
+ * })
129
+ * ev.on('change', (msg) => {
130
+ * delete msg.type
131
+ * console.log('change', msg)
132
+ * })
133
+ * // change { event: 'start', msg: 'running...' }
134
+ * // change { event: 'proc-callfun-download', msg: 'start...' }
135
+ * // change { event: 'proc-callfun-download', msg: 'done' }
136
+ * // change { event: 'proc-callfun-getCurrent', msg: 'start...' }
137
+ * // change { event: 'proc-callfun-getCurrent', msg: 'done' }
138
+ * // change { event: 'compare', msg: 'start...' }
139
+ * // change { event: 'compare', msg: 'done' }
140
+ * // change { event: 'proc-add-callfun-add', id: '114101', msg: 'start...' }
141
+ * // change { event: 'proc-add-callfun-add', id: '114101', msg: 'done' }
142
+ * // change { event: 'proc-add-callfun-add', id: '114102', msg: 'start...' }
143
+ * // change { event: 'proc-add-callfun-add', id: '114102', msg: 'done' }
144
+ * // ...
145
+ *
146
+ */
147
+ let WDwdataTweqod = async(token, opt = {}) => {
148
+
149
+ //fdDwStorage
150
+ let fdDwStorage = get(opt, 'fdDwStorage')
151
+ if (!isestr(fdDwStorage)) {
152
+ fdDwStorage = `./_dwStorage`
153
+ }
154
+ if (!fsIsFolder(fdDwStorage)) {
155
+ fsCreateFolder(fdDwStorage)
156
+ }
157
+
158
+ //fdDwAttime
159
+ let fdDwAttime = get(opt, 'fdDwAttime')
160
+ if (!isestr(fdDwAttime)) {
161
+ fdDwAttime = `./_dwAttime`
162
+ }
163
+ if (!fsIsFolder(fdDwAttime)) {
164
+ fsCreateFolder(fdDwAttime)
165
+ }
166
+
167
+ //fdDwCurrent
168
+ let fdDwCurrent = get(opt, 'fdDwCurrent')
169
+ if (!isestr(fdDwCurrent)) {
170
+ fdDwCurrent = `./_dwCurrent`
171
+ }
172
+ if (!fsIsFolder(fdDwCurrent)) {
173
+ fsCreateFolder(fdDwCurrent)
174
+ }
175
+
176
+ //fdResult
177
+ let fdResult = get(opt, 'fdResult')
178
+ if (!isestr(fdResult)) {
179
+ fdResult = './_result'
180
+ }
181
+ if (!fsIsFolder(fdResult)) {
182
+ fsCreateFolder(fdResult)
183
+ }
184
+
185
+ //fdTaskCpSrc
186
+ let fdTaskCpSrc = get(opt, 'fdTaskCpSrc')
187
+ if (!isestr(fdTaskCpSrc)) {
188
+ fdTaskCpSrc = './_taskCpSrc'
189
+ }
190
+ if (!fsIsFolder(fdTaskCpSrc)) {
191
+ fsCreateFolder(fdTaskCpSrc)
192
+ }
193
+
194
+ //funDownloadEqs
195
+ let funDownloadEqs = get(opt, 'funDownloadEqs')
196
+
197
+ //funDownload
198
+ let funDownload = get(opt, 'funDownload')
199
+
200
+ //funGetCurrent
201
+ let funGetCurrent = get(opt, 'funGetCurrent')
202
+
203
+ //funAdd
204
+ let funAdd = get(opt, 'funAdd')
205
+
206
+ //funModify
207
+ let funModify = get(opt, 'funModify')
208
+
209
+ //funRemove
210
+ let funRemove = get(opt, 'funRemove')
211
+
212
+ //treeFilesAndGetHashs
213
+ let treeFilesAndGetHashs = (fd) => {
214
+
215
+ //vfps
216
+ let vfps = fsTreeFolder(fd, 1)
217
+ // console.log('vfps', vfps)
218
+
219
+ //ltdtHash
220
+ let ltdtHash = []
221
+ each(vfps, (v) => {
222
+
223
+ let j = fs.readFileSync(v.path, 'utf8')
224
+ let o = JSON.parse(j)
225
+
226
+ ltdtHash.push(o)
227
+
228
+ })
229
+
230
+ return ltdtHash
231
+ }
232
+
233
+ //funDownloadDef
234
+ let funDownloadDef = async() => {
235
+
236
+ //eqs
237
+ let eqs = []
238
+ if (isfun(funDownloadEqs)) {
239
+ eqs = funDownloadEqs()
240
+ if (ispm(eqs)) {
241
+ eqs = await eqs
242
+ }
243
+ }
244
+ else {
245
+ eqs = await downloadEqs(token)
246
+ // console.log('eqs', eqs, size(eqs))
247
+ // eqs = [eqs[0], eqs[1]]
248
+ // fs.writeFileSync('./temp.json', JSON.stringify(eqs, null, 2), 'utf8')
249
+ }
250
+
251
+ //reverse
252
+ eqs = reverse(eqs)
253
+
254
+ //清空fdDwAttime
255
+ fsCleanFolder(fdDwAttime)
256
+
257
+ //複製已下載供比對hash用數據fdDwCurrent至fdDwAttime
258
+ fsCopyFolder(fdDwCurrent, fdDwAttime)
259
+
260
+ //ltdtHashOld, 數據來源為fdDwCurrent, 故為舊hash數據清單
261
+ let ltdtHashOld = treeFilesAndGetHashs(fdDwAttime)
262
+
263
+ //kpHash, 基於舊hash數據建構字典物件
264
+ let kpHash = {}
265
+ each(ltdtHashOld, (v) => {
266
+ kpHash[v.id] = v
267
+ })
268
+
269
+ //逐筆偵測與更新
270
+ await pmSeries(eqs, async(v) => {
271
+
272
+ //先儲存解析後數據至存放完整數據資料夾fdDwStorage
273
+ let fp = `${fdDwStorage}/${v.id}.json`
274
+ fs.writeFileSync(fp, JSON.stringify(v), 'utf8')
275
+
276
+ //計算檔案hash值, 為新hash
277
+ let hashNew = await fsGetFileXxHash(fp)
278
+
279
+ //check
280
+ if (haskey(kpHash, v.id)) {
281
+ //為已下載過之地震數據
282
+
283
+ //舊hash
284
+ let hashOld = kpHash[v.id].hash
285
+
286
+ //check
287
+ if (hashNew !== hashOld) {
288
+
289
+ //update
290
+ kpHash[v.id].hash = hashNew
291
+
292
+ //儲存更新後之新地震hash數據檔案
293
+ let fp = `${fdDwAttime}/${v.id}.json`
294
+ fs.writeFileSync(fp, JSON.stringify(kpHash[v.id]), 'utf8')
295
+
296
+ }
297
+ else {
298
+ //不用更新hash檔案
299
+ }
300
+
301
+ }
302
+ else {
303
+ //為新增之地震數據
304
+
305
+ //add
306
+ kpHash[v.id] = {
307
+ id: v.id,
308
+ hash: hashNew,
309
+ }
310
+
311
+ //儲存新地震hash數據檔案
312
+ let fp = `${fdDwAttime}/${v.id}.json`
313
+ fs.writeFileSync(fp, JSON.stringify(kpHash[v.id]), 'utf8')
314
+
315
+ }
316
+
317
+ })
318
+
319
+ //ltdtHashNew
320
+ let ltdtHashNew = values(kpHash)
321
+
322
+ return ltdtHashNew
323
+ }
324
+ if (!isfun(funDownload)) {
325
+ funDownload = funDownloadDef
326
+ }
327
+
328
+ //funGetCurrentDef
329
+ let funGetCurrentDef = async() => {
330
+
331
+ //vfps
332
+ let vfps = fsTreeFolder(fdDwCurrent, 1)
333
+ // console.log('vfps', vfps)
334
+
335
+ //ltdtHashOld
336
+ let ltdtHashOld = []
337
+ each(vfps, (v) => {
338
+
339
+ let j = fs.readFileSync(v.path, 'utf8')
340
+ let eq = JSON.parse(j)
341
+
342
+ ltdtHashOld.push(eq)
343
+
344
+ })
345
+
346
+ return ltdtHashOld
347
+ }
348
+ if (!isfun(funGetCurrent)) {
349
+ funGetCurrent = funGetCurrentDef
350
+ }
351
+
352
+ //funRemoveDef
353
+ let funRemoveDef = async(v) => {
354
+
355
+ let fd = `${fdResult}/${v.id}`
356
+
357
+ if (fsIsFolder(fd)) {
358
+ fsDeleteFolder(fd)
359
+ }
360
+
361
+ }
362
+ if (!isfun(funRemove)) {
363
+ funRemove = funRemoveDef
364
+ }
365
+
366
+ //funAddDef
367
+ let funAddDef = async(v) => {
368
+
369
+ let fd = `${fdResult}/${v.id}`
370
+
371
+ if (fsIsFolder(fd)) {
372
+ fsCleanFolder(fd)
373
+ }
374
+
375
+ let fpStorage = `${fdDwStorage}/${v.id}.json`
376
+ let fpResult = `${fd}/${v.id}.json`
377
+ fsCopyFile(fpStorage, fpResult)
378
+
379
+ }
380
+ if (!isfun(funAdd)) {
381
+ funAdd = funAddDef
382
+ }
383
+
384
+ //funModifyDef
385
+ let funModifyDef = async(v) => {
386
+
387
+ let fd = `${fdResult}/${v.id}`
388
+
389
+ if (fsIsFolder(fd)) {
390
+ fsCleanFolder(fd)
391
+ }
392
+
393
+ let fpStorage = `${fdDwStorage}/${v.id}.json`
394
+ let fpResult = `${fd}/${v.id}.json`
395
+ fsCopyFile(fpStorage, fpResult)
396
+
397
+ }
398
+ if (!isfun(funModify)) {
399
+ funModify = funModifyDef
400
+ }
401
+
402
+ //WDwdataBuilder
403
+ let optBdr = {
404
+ fdDwAttime,
405
+ fdDwCurrent,
406
+ fdResult,
407
+ fdTaskCpSrc,
408
+ funDownload,
409
+ funGetCurrent,
410
+ funRemove,
411
+ funAdd,
412
+ funModify,
413
+ }
414
+ let ev = await WDwdataBuilder(optBdr)
415
+
416
+ return ev
417
+ }
418
+
419
+
420
+ export default WDwdataTweqod
421
+ </code></pre>
422
+ </article>
423
+ </section>
424
+
425
+
426
+
427
+
428
+
429
+
430
+ </div>
431
+
432
+ <br class="clear">
433
+
434
+ <footer>
435
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.4</a> on Sun Aug 24 2025 13:42:17 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
436
+ </footer>
437
+
438
+ <script>prettyPrint();</script>
439
+ <script src="scripts/polyfill.js"></script>
440
+ <script src="scripts/linenumber.js"></script>
441
+
442
+
443
+
444
+ </body>
445
+ </html>