1(function( global, factory ) { 2 if ( typeof module === "object" && typeof module.exports === "object" ) { 3 // CMD 4 module.exports = factory(require('moment')); 5 } else if ( typeof define === 'function' && define.amd ) { 6 // AMD. Register as parrot 7 // TODO how to define the jquery plugin here? 8 define(['moment'], factory); 9 } else { 10 // in browser, assump moment is loaded 11 global.moment = factory(moment); 12 } 13}(typeof window !== "undefined" ? window : this, function(moment) { 14 // utils 15 var taiwan = { 16 /* 17 Converts a Gregorian date to Taiwan. 18 */ 19 toTaiwan: function(gYear, month, day) { 20 return { 21 year: gYear <= 1911 ? (gYear - 1912) : (gYear - 1911), 22 month: month, 23 day: day 24 }; 25 }, 26 /* 27 Converts a Taiwan date to Gregorian. 28 */ 29 toGregorian: function(tYear, month, day) { 30 return { 31 year: utils.calYear(tYear), 32 month: month, 33 day: day 34 }; 35 }, 36 /** 37 * Checks whether a Taiwan date is valid or not. 38 */ 39 isValidDate: function(tYear, month, day) { 40 return month >= 1 && month <= 12 && 41 day >= 1 && day <= utils.monthLength(tYear, month); 42 }, 43 /** 44 * check the Gregorian year is leap or not 45 * @param gy 46 * @returns {boolean} 47 */ 48 isLeapYear: function isLeapYear(gy) { 49 return ((gy % 4 == 0) && (gy % 100 != 0)) || (gy % 400 == 0); 50 }, 51 /** 52 * Is this a leap year or not? 53 * @returns {boolean} 54 */ 55 isLeapTaiwanYear: function isLeapTaiwanYear(tYear) { 56 return utils.twCal(tYear).leap; 57 }, 58 /** 59 * Number of days in a given month in a Taiwan year. 60 * @returns {number} days of month 61 */ 62 monthLength: function(tYear, month) { 63 switch (month) { 64 case 4: 65 case 6: 66 case 9: 67 case 11: 68 return 30; 69 case 2: 70 return utils.isLeapYear(utils.calYear(tYear)) ? 29 : 28; 71 default: 72 return 31; 73 } 74 }, 75 /** 76 * 77 * @param tYear taiwan year 78 * @returns {{leap: boolean, gy: number}} 79 */ 80 twCal: function twCal(tYear) { 81 var gy = calYear(tYear); 82 return {leap: isLeapYear(gy), gy: gy}; 83 }, 84 /** 85 * calculate the Taiwan year to Gregorian year 86 * @param tYear 87 * @returns {number} 88 */ 89 calYear: function calYear(tYear) { 90 // 1 -> 1912, 2 -> 1913, etc 91 // 0 -> 1912. 92 // -1 -> 1911, -2 -> 1910, etc 93 return tYear == 0 ? 1912 : (tYear > 0) ? (tYear + 1911) : (tYear + 1912); 94 } 95 }; 96 97 /************************************ 98 Constants 99 ************************************/ 100 var formattingTokens = /tYY|YY(YY)?|Q|MM?|MMMM?|DD?|Do|DDDD?|X|x|gg(gg)?|ww?|e|dddd?|GG(GG)?|WW?|E|HH?|hh?|a|A|mm?|ss?|SS?S?S?|ZZ?|./g; 101 //var formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|w[o|w]?|YYYYY|YYYY|YY|tYY|gg(ggg?)?|)|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|tYY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g, 102 var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g, 103 104 parseTokenOneOrTwoDigits = /\d\d?/, 105 parseTokenOneToThreeDigits = /\d{1,3}/, 106 parseTokenThreeDigits = /\d{3}/, 107 parseTokenFourDigits = /\d{1,4}/, 108 parseTokenSixDigits = /[+\-]?\d{1,6}/, 109 parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i, 110 parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, 111 parseTokenT = /T/i, 112 parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, 113 114 formatFunctions = {}, 115 formatTokenFunctions = { 116 tYY: function () { 117 return this.twYear() + ''; 118 } 119 }; 120 121 /************************************ 122 Helpers 123 ************************************/ 124 var extend = function(a, b) { 125 var key; 126 for (key in b) { 127 if (b.hasOwnProperty(key)) { 128 a[key] = b[key]; 129 } 130 } 131 return a; 132 }; 133 134 var setDate = function(m, year, month, date) { 135 var d = m._d; 136 if (m._isUTC) { 137 /*eslint-disable new-cap*/ 138 m._d = new Date(Date.UTC(year, month, date, 139 d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds())); 140 /*eslint-enable new-cap*/ 141 } else { 142 m._d = new Date(year, month, date, 143 d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()); 144 } 145 }; 146 147 var objectCreate = function(parent) { 148 function F() { 149 } 150 151 F.prototype = parent; 152 return new F(); 153 }; 154 155 /************************************ 156 Formatting 157 ************************************/ 158 var makeFormatFunction = function(format) { 159 var array = format.match(formattingTokens); 160 var length = array.length; 161 var i; 162 163 array.forEach(function (element, index) { 164 if (formatTokenFunctions[element]) { 165 array[index] = formatTokenFunctions[element]; 166 } 167 }); 168 169 return function (mom) { 170 var output = ''; 171 for (i = 0; i < length; i += 1) { 172 output += array[i] instanceof Function ? '[' + array[i].call(mom, format) + ']' : array[i]; 173 } 174 return output; 175 }; 176 }; 177 178 /************************************ 179 Parsing 180 ************************************/ 181 /** 182 * get parse regex for token 183 * @param token 184 * @param config 185 * @returns {*} 186 */ 187 var getParseRegexForToken = function(token, config) { 188 switch (token) { 189 case 'tYY': 190 return parseTokenOneToThreeDigits; 191 case 'DDDD': 192 return parseTokenThreeDigits; 193 case 'YYYY': 194 return parseTokenFourDigits; 195 case 'YYYYY': 196 return parseTokenSixDigits; 197 case 'S': 198 case 'SS': 199 case 'SSS': 200 case 'DDD': 201 return parseTokenOneToThreeDigits; 202 case 'MMM': 203 case 'MMMM': 204 case 'dd': 205 case 'ddd': 206 case 'dddd': 207 return parseTokenWord; 208 case 'a': 209 case 'A': 210 return moment.localeData(config.lang)._meridiemParse; 211 case 'X': 212 return parseTokenTimestampMs; 213 case 'Z': 214 case 'ZZ': 215 return parseTokenTimezone; 216 case 'T': 217 return parseTokenT; 218 case 'MM': 219 case 'DD': 220 case 'YY': 221 case 'HH': 222 case 'hh': 223 case 'mm': 224 case 'ss': 225 case 'M': 226 case 'D': 227 case 'd': 228 case 'H': 229 case 'h': 230 case 'm': 231 case 's': 232 return parseTokenOneOrTwoDigits; 233 default: 234 return new RegExp(token.replace('\\', '')); 235 } 236 }; 237 238 var addTimeToArrayFromToken = function(token, input, config) { 239 switch (token) { 240 case 'tYY': 241 config._twYear = ~~input; 242 break; 243 } 244 if (input == null) { 245 config._isValid = false; 246 } 247 }; 248 249 /** 250 * get taiwan year from config 251 * @param config 252 * @returns {*} 253 */ 254 var yearFromConfig = function(config) { 255 return config._twYear; 256 }; 257 258 /** 259 * 260 * @param config 261 * @returns {[]} 262 */ 263 var makeDateFromStringAndFormat = function(config) { 264 var tokens = config.format.match(formattingTokens); 265 var string = config.input + ''; 266 267 config._a = []; 268 269 tokens.forEach(function (token) { 270 var parsedInput = (getParseRegexForToken(token, config).exec(string) || [])[0]; 271 if (parsedInput) {
272 string = string.slice(string.indexOf(parsedInput) + parsedInput.length); 273 } 274 if (formatTokenFunctions[token]) { 275 addTimeToArrayFromToken(token, parsedInput, config); 276 } 277 }); 278 if (string) { 279 config._tw_leftLength = string; 280 } 281 return yearFromConfig(config); 282 }; 283 284 /** 285 * create moment by given configuration with multiple formats. 286 * score of moment is left length of formatting. return moment object with minimum score. 287 * @param config 288 * @param utc 289 * @returns {*} 290 */ 291 var makeDateFromStringAndArray = function(config, utc) { 292 if (config.format.length === 0) { 293 return makeMoment(new Date(NaN)); 294 } 295 296 var moments = config.format.map(function (format) { 297 var currentScore = 0; 298 var tempMoment = makeMoment(config.input, format, config.lang, config.strict, utc); 299 if (tempMoment.isValid()) { 300 if (tempMoment._tw_leftLength) { 301 currentScore += tempMoment._tw_leftLength.length; 302 } 303 } 304 return { 305 score: currentScore, 306 moment: tempMoment 307 }; 308 }); 309 moments.sort(function (m1, m2) { 310 return m1.score - m2.score; 311 }); 312 return moments[0].moment; 313 }; 314 315 var removeParsedTokens = function(config) { 316 var string = config.input + ''; 317 var input = ''; 318 var format = ''; 319 var array = config.format.match(formattingTokens); 320 321 array.forEach(function (match) { 322 var parsed = (getParseRegexForToken(match, config).exec(string) || [])[0]; 323 if (parsed) { 324 string = string.slice(string.indexOf(parsed) + parsed.length); 325 } 326 if (!(formatTokenFunctions[match] instanceof Function)) { 327 format += match; 328 if (parsed) { 329 input += parsed; 330 } 331 } 332 }); 333 config.input = input; 334 var orgFormat = config.format; 335 config.format = format; 336 return orgFormat != format; 337 }; 338 339 /************************************ 340 Top Level Functions 341 ************************************/ 342 var makeMoment = function(input, format, lang, strict, utc) { 343 if (typeof lang === 'boolean') { 344 utc = strict; 345 strict = lang; 346 lang = undefined; 347 } 348 var config = { 349 input: input, 350 format: format, 351 lang: lang, 352 strict: strict, 353 utc: utc 354 }; 355 var origInput = input; 356 var origFormat = format; 357 358 359 if (format) { 360 if (Array.isArray(format)) { 361 // create moment by given multiple formats 362 return makeDateFromStringAndArray(config, utc); 363 } else { 364 // create moment by given single format 365 var year = makeDateFromStringAndFormat(config); 366 var removed = removeParsedTokens(config); 367 if (removed) { 368 format = 'YYYY-' + config.format; 369 // has tw format 370 if (typeof year === 'undefined') { 371 // no year parsed, let it be invalid 372 input = 'ABCD-' + config.input; 373 } else { 374 input = taiwan.calYear(year) + '-' + config.input; 375 } 376 } else { 377 // keep original 378 format = origFormat; 379 input = origInput; 380 } 381 // format = 'YYYY-' + config.format; 382 // input = taiwan.calYear(year) + '-' + config.input; 383 } 384 } 385 386 var orgMoment; 387 if (utc) 388 orgMoment = moment.utc(input, format, lang, strict); 389 else 390 orgMoment = moment(input, format, lang, strict); 391 if (config._isValid === false) { 392 orgMoment._isValid = false; 393 } 394 var newMoment = objectCreate(twMoment.fn); 395 extend(newMoment, orgMoment); 396 if (strict && newMoment.isValid()) { 397 newMoment._isValid = newMoment.format(origFormat) === origInput; 398 } 399 return newMoment; 400 }; 401 402 var twMoment = function(input, format, lang, strict) { 403 return makeMoment(input, format, lang, strict, false); 404 }; 405 406 extend(twMoment, moment);
407 twMoment.fn = objectCreate(moment.fn); 408 409 twMoment.utc = function (input, format, lang, strict) { 410 return makeMoment(input, format, lang, strict, true); 411 }; 412 413 twMoment.unix = function (input) { 414 return makeMoment(input * 1000); 415 }; 416 417 /************************************ 418 twMoment Prototype 419 ************************************/ 420 421 twMoment.fn.format = function (format) { 422 var i, replace, me = this; 423 if (format) { 424 i = 5; 425 replace = function (input) { 426 return me.localeData().longDateFormat(input) || input; 427 }; 428 while (i > 0 && localFormattingTokens.test(format)) { 429 i -= 1; 430 format = format.replace(localFormattingTokens, replace); 431 } 432 if (!formatFunctions[format]) { 433 formatFunctions[format] = makeFormatFunction(format); 434 } 435 format = formatFunctions[format](this); 436 } 437 return moment.fn.format.call(this, format); 438 }; 439 440 twMoment.fn.twYear = function (input) { 441 var lastDay, tw, gregorian; 442 if (typeof input === 'number') { 443 tw = toTaiwan(this.year(), this.month(), this.date()); 444 lastDay = Math.min(tw.year, twMoment.twDaysInMonth(input, tw.month)); 445 gregorian = toGregorian(input, tw.month, lastDay); 446 setDate(this, gregorian.year, gregorian.month, gregorian.day); 447 moment.updateOffset(this); 448 return this; 449 } else { 450 return toTaiwan(this.year(), this.month(), this.date()).year; 451 } 452 }; 453 454 twMoment.fn.clone = function () { 455 return twMoment(this); 456 }; 457 458 /************************************ 459 twMoment Statics 460 ************************************/ 461 twMoment.twDaysInMonth = function (year, month) { 462 year += div(month, 12); 463 month = mod(month, 12); 464 if (month < 0) { 465 month += 12; 466 year -= 1; 467 } 468 return taiwan.monthLength(year, month); 469 }; 470 471 twMoment.twIsLeapYear = taiwan.isLeapYear; 472 473 twMoment.twConvert = { 474 toTaiwan: toTaiwan, 475 toGregorian: toGregorian 476 }; 477 478 /************************************ 479 Taiwan Conversion 480 ************************************/ 481 var toTaiwan = function(gy, gm, gd) { 482 var date = taiwan.toTaiwan(gy, gm + 1, gd); 483 date.month -= 1; 484 return date; 485 }; 486 487 var toGregorian = function(jy, jm, jd) { 488 var date = taiwan.toGregorian(jy, jm + 1, jd); 489 date.month -= 1; 490 return date; 491 }; 492 493 /* 494 Utility helper functions. 495 */ 496 497 var div = function(a, b) { 498 return ~~(a / b); 499 }; 500 501 var mod = function(a, b) { 502 return a - ~~(a / b) * b; 503 }; 504 505 return twMoment; 506}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.