spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: dist/Utilities/DateRegExBuilder.js
-rw-r--r--
8267
  1"use strict";
  2exports.__esModule = true;
  3/**
  4 * Build a regular expression step by step, to make it easier to build and read the resulting regular expressions.
  5 */
  6var DateRegExBuilder = /** @class */ (function () {
  7    function DateRegExBuilder() {
  8        this.regexString = "";
  9    }
 10    DateRegExBuilder.DateRegExBuilder = function () {
 11        return new DateRegExBuilder();
 12    };
 13    /**
 14     * Start the regular expression builder by matching the start of a line and zero or more spaces.
 15     * @returns {DateRegExBuilder} builder
 16     */
 17    DateRegExBuilder.prototype.start = function () {
 18        this.regexString += "^" + DateRegExBuilder.ZERO_OR_MORE_SPACES;
 19        return this;
 20    };
 21    /**
 22     * End the regular expression builder by matching the end of the line.
 23     * @returns {DateRegExBuilder} builder
 24     */
 25    DateRegExBuilder.prototype.end = function () {
 26        this.regexString += "$";
 27        return this;
 28    };
 29    /**
 30     * Capture all month full name and short names to the regular expression.
 31     * @returns {DateRegExBuilder} builder
 32     */
 33    DateRegExBuilder.prototype.MONTHNAME = function () {
 34        this.regexString += "(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)";
 35        return this;
 36    };
 37    /**
 38     * Capture all month full name and short names to the regular expression, in addition to any followed by one or more
 39     * spaces.
 40     * @returns {DateRegExBuilder} builder
 41     * @constructor
 42     */
 43    DateRegExBuilder.prototype.MONTHNAME_W_SPACE = function () {
 44        this.regexString += "(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec|january\\s+|february\\s+|march\\s+|april\\s+|may\\s+|june\\s+|july\\s+|august\\s+|september\\s+|october\\s+|november\\s+|december\\s+|jan\\s+|feb\\s+|mar\\s+|apr\\s+|jun\\s+|jul\\s+|aug\\s+|sep\\s+|oct\\s+|nov\\s+|dec\\s+)";
 45        return this;
 46    };
 47    /**
 48     * Add capture group for optionally capturing day names.
 49     * @returns {DateRegExBuilder} builder
 50     * @constructor
 51     */
 52    DateRegExBuilder.prototype.OPTIONAL_DAYNAME = function () {
 53        this.regexString += "(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tue|wed|thu|fri|sat)?";
 54        return this;
 55    };
 56    /**
 57     * Add capture group for optionally capturing a comma followed by one or more spaces.
 58     * @returns {DateRegExBuilder} builder
 59     * @constructor
 60     */
 61    DateRegExBuilder.prototype.OPTIONAL_COMMA = function () {
 62        this.regexString += "(,?\\s+)?";
 63        return this;
 64    };
 65    /**
 66     * Add capture group for capturing month digits between 01 and 12, inclusively.
 67     * @returns {DateRegExBuilder} builder
 68     * @constructor
 69     */
 70    DateRegExBuilder.prototype.MM = function () {
 71        this.regexString += "([1-9]|0[1-9]|1[0-2])";
 72        return this;
 73    };
 74    /**
 75     * Add capture group for capturing month digits between 01 and 12, inclusively, in addition to any followed by one or
 76     * more spaces.
 77     * @returns {DateRegExBuilder} builder
 78     * @constructor
 79     */
 80    DateRegExBuilder.prototype.MM_W_SPACE = function () {
 81        this.regexString += "([1-9]|0[1-9]|1[0-2]|[1-9]\\s+|0[1-9]\\s+|1[0-2]\\s+)";
 82        return this;
 83    };
 84    /**
 85     * Add capture group for capturing day digits between 01 and 31, inclusively.
 86     * @returns {DateRegExBuilder} builder
 87     * @constructor
 88     */
 89    DateRegExBuilder.prototype.DD = function () {
 90        this.regexString += "(0?[0-9]|1[0-9]|2[0-9]|3[0-1])";
 91        return this;
 92    };
 93    /**
 94     * Add capture group for capturing day digits between 01 and 31, inclusively, in addition to any followed by one or
 95     * more spaces.
 96     * @returns {DateRegExBuilder} builder
 97     * @constructor
 98     */
 99    DateRegExBuilder.prototype.DD_W_SPACE = function () {
100        this.regexString += "(0?[0-9]|1[0-9]|2[0-9]|3[0-1]|0?[0-9]\\s+|1[0-9]\\s+|2[0-9]\\s+|3[0-1]\\s+)";
101        return this;
102    };
103    /**
104     * Add capture group for capturing 4 digits or 3 digits starting with 0-9.
105     * @returns {DateRegExBuilder} builder
106     * @constructor
107     */
108    DateRegExBuilder.prototype.YYYY = function () {
109        this.regexString += "([0-9]{4}|[1-9][0-9][0-9])";
110        return this;
111    };
112    /**
113     * Add capture group for capturing 1 through 4 digits.
114     * @returns {DateRegExBuilder} builder
115     * @constructor
116     */
117    DateRegExBuilder.prototype.YYYY14 = function () {
118        this.regexString += "([0-9]{1,4})";
119        return this;
120    };
121    /**
122     * Add capture group for capturing 1 through 4 digits, in addition to any followed by one or more spaces.
123     * @returns {DateRegExBuilder} builder
124     * @constructor
125     */
126    DateRegExBuilder.prototype.YYYY14_W_SPACE = function () {
127        this.regexString += "([0-9]{1,4}|[0-9]{1,4}\\s+)";
128        return this;
129    };
130    DateRegExBuilder.prototype.YYYY2_OR_4_W_SPACE = function () {
131        this.regexString += "([0-9]{2}|[0-9]{4}|[0-9]{2}\\s+|[0-9]{4}\\s+)";
132        return this;
133    };
134    /**
135     * Add capture group for a flexible delimiter, including ", ", " ", ". ", "\", "-".
136     * @returns {DateRegExBuilder} builder
137     * @constructor
138     */
139    DateRegExBuilder.prototype.FLEX_DELIMITER = function () {
140        // this.regexString += "(,?\\s+|\\s*-?\\.?-?\\/?\\s+)";// close to being right
141        this.regexString += "(,?\\s+|\\s*\\.\\s+|\\s*-\\s*|\\s*\\/\\s*)";
142        return this;
143    };
144    /**
145     * Add capture group for a flexible delimiter, including ", ", " ", ".", "\", "-". Different from FLEX_DELIMITER
146     * in that it will match periods with zero or more spaces on either side.
147     * For reference: https://regex101.com/r/q1fp1z/1/
148     * @returns {DateRegExBuilder} builder
149     * @constructor
150     */
151    DateRegExBuilder.prototype.FLEX_DELIMITER_LOOSEDOT = function () {
152        // this.regexString += "(,?\\s+|\\s*-?\\.?-?\\/?\\s+)";// close to being right
153        this.regexString += "(,?\\s+|\\s*\\.\\s*|\\s*-\\s*|\\s*\\/\\s*)";
154        return this;
155    };
156    /**
157     * Add an optional capture group for capturing timestamps including: "10am", "10:10", "10:10pm", "10:10:10",
158     * "10:10:10am", along with zero or more spaces after semi colons, AM or PM, and unlimited number of digits per unit.
159     * @returns {DateRegExBuilder} builder
160     * @constructor
161     */
162    DateRegExBuilder.prototype.OPTIONAL_TIMESTAMP_CAPTURE_GROUP = function () {
163        this.regexString += "((\\s+[0-9]+\\s*am\\s*$|[0-9]+\\s*pm\\s*$)|(\\s+[0-9]+:\\s*[0-9]+\\s*$)|(\\s+[0-9]+:\\s*[0-9]+\\s*am\\s*$|\\s+[0-9]+:\\s*[0-9]+\\s*pm\\s*$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*$)|(\\s+[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*am\\s*$|[0-9]+:\\s*[0-9]+:\\s*[0-9]+\\s*pm\\s*$))?";
164        return this;
165    };
166    /**
167     * Add a capture group for capturing timestamps including: "10am", "10:10", "10:10pm", "10:10:10",
168     * "10:10:10am", along with zero or more spaces after semi colons, AM or PM, and unlimited number of digits per unit.
169     * See https://regex101.com/r/0bmj5n/1/ for more information of 9-digit maximum. One series, "12:00001989198298am",
170     * has a maximum of 10 digits: "0*(?:[1-9]{1}[0-9]{0,9})?"
171     * @returns {DateRegExBuilder} builder
172     * @constructor
173     */
174    DateRegExBuilder.prototype.TIMESTAMP_UNITS_CAPTURE_GROUP = function () {
175        this.regexString += "(\\s*(0*(?:[1-9]{1}[0-9]{0,8})?)()()\\s*(am|pm)\\s*$)|(\\s*(0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,8})?)()()\\s*$)|(\\s*((0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,9})?)()\\s*(am|pm))\\s*$)|(\\s*((0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,8})?)())\\s*$)|(\\s*((0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,8})?):\\s*(0*(?:[1-9]{1}[0-9]{0,8})?)\\s*(am|pm))\\s*$)";
176        return this;
177    };
178    /**
179     * Build the regular expression and ignore case.
180     * @returns {RegExp}
181     */
182    DateRegExBuilder.prototype.build = function () {
183        return new RegExp(this.regexString, 'i');
184    };
185    DateRegExBuilder.ZERO_OR_MORE_SPACES = "\\s*";
186    return DateRegExBuilder;
187}());
188exports.DateRegExBuilder = DateRegExBuilder;