tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

parse-single-number.js (1576B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 const accepted = {
      7  "0": "2000-01-01T00:00:00",
      8  "1": "2001-01-01T00:00:00",
      9  "12": "2001-12-01T00:00:00",
     10  "32": "2032-01-01T00:00:00",
     11  "49": "2049-01-01T00:00:00",
     12  "50": "1950-01-01T00:00:00",
     13  "99": "1999-01-01T00:00:00",
     14  "100": "0100-01-01T00:00:00",
     15  "999": "0999-01-01T00:00:00",
     16  "1000": "1000-01-01T00:00:00Z",
     17  "1000-": "1000-01-01T00:00:00",
     18  "20009": "+020009-01-01T00:00:00",
     19  "+20009": "+020009-01-01T00:00:00",
     20 
     21  // Rejecting e.g. S22 (see rejected patterns below) shouldn't
     22  // reject mday directly after month name
     23  "Sep26 1995": "1995-09-26T00:00:00",
     24 };
     25 const rejected = [
     26  "S22",
     27  "5C",
     28  "Sep26 foo 1995",
     29 ];
     30 
     31 for (const [test, expected] of Object.entries(accepted)) {
     32  const testDate = new Date(test);
     33  const expectedDate = new Date(expected);
     34 
     35  assertEq(
     36    false, isNaN(testDate),
     37    `${test} should be accepted.`
     38  );
     39 
     40  assertEq(
     41    testDate.getTime(), expectedDate.getTime(),
     42    `"${test}" should be ${expectedDate} (got ${testDate}).`
     43  );
     44 }
     45 
     46 for (let i = 13; i <= 31; ++i) {
     47  assertEq(
     48    true, isNaN(new Date(`${i}`)),
     49    `"${i}" should be rejected.`
     50  );
     51 }
     52 for (const reject of rejected) {
     53  assertEq(
     54    true, isNaN(new Date(reject)),
     55    `"${reject}" should be rejected.`
     56  );
     57 }
     58 
     59 if (typeof reportCompare === "function")
     60  reportCompare(true, true);