Created
January 30, 2017 02:13
-
-
Save cognitom/56d73f23e7671b40368f396cef47ddba to your computer and use it in GitHub Desktop.
openBDの日付フィールドのノーマライズ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function normalizeDate (raw) { | |
const patterns = [ | |
// 2017-01-30 | |
{re: /^\d{4}-\d{2}-\d{2}($|,)/, f: m => m[0]}, | |
// 2017-01 | |
{re: /^\d{4}-\d{2}($|,)/, f: m => m[0]}, | |
// 2017 | |
{re: /^\d{4}($|,)/, f: m => m[0]}, | |
// 20170130 | |
{re: /^(\d{4})(\d{2})(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}-${m[3]}`}, | |
// 201701 | |
{re: /^(\d{4})(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`}, | |
// c2017-01 | |
{re: /^c(\d{4})-(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`}, | |
// [2017]-01 | |
{re: /^\[(\d{4})\]-(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}`} | |
] | |
for (const pattern of patterns) { | |
const m = raw.match(pattern.re) | |
if (m) return pattern.f(m) | |
} | |
return 'unrecognized' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment