@cellaware/utils 7.2.3 → 7.2.5
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.
- package/dist/util.d.ts +16 -0
- package/dist/util.js +140 -0
- package/package.json +1 -1
package/dist/util.d.ts
CHANGED
|
@@ -10,3 +10,19 @@ export declare function getCurrentDayInMonth(timeZone?: string): number;
|
|
|
10
10
|
export declare function getDaysInYear(timeZone?: string): 366 | 365;
|
|
11
11
|
export declare function getCurrentMonth(timeZone?: string): number;
|
|
12
12
|
export declare function getCurrentYear(timeZone?: string): number;
|
|
13
|
+
/**
|
|
14
|
+
* NOTE: `stopIdx` represents last character index of word
|
|
15
|
+
* ex: ABC -> `startIdx`: 0 `stopIdx`: 2
|
|
16
|
+
*/
|
|
17
|
+
export interface QueryRegexMatch {
|
|
18
|
+
word: string;
|
|
19
|
+
startIdx: number;
|
|
20
|
+
stopIdx: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Will only return matches that **are not** contained in single quotes,
|
|
24
|
+
* double quotes, single line, or multiline comments.
|
|
25
|
+
*/
|
|
26
|
+
export declare function getQueryMatches(query: string, regex: RegExp): QueryRegexMatch[];
|
|
27
|
+
export declare function removeQueryMatches(query: string, matches: QueryRegexMatch[]): string;
|
|
28
|
+
export declare function replaceQueryMatches(query: string, matches: QueryRegexMatch[], replacement: string): string;
|
package/dist/util.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Miscellaneous --------------------------------------------------------------------------
|
|
1
2
|
export async function sleep(ms) {
|
|
2
3
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
3
4
|
}
|
|
@@ -22,6 +23,7 @@ export function removePrefixIndicators(input, prefixes) {
|
|
|
22
23
|
}
|
|
23
24
|
return output;
|
|
24
25
|
}
|
|
26
|
+
// Dates --------------------------------------------------------------------------
|
|
25
27
|
export function initDate(timeZone) {
|
|
26
28
|
const date = new Date();
|
|
27
29
|
if (!!timeZone) {
|
|
@@ -70,3 +72,141 @@ export function getCurrentYear(timeZone) {
|
|
|
70
72
|
let date = initDate(timeZone);
|
|
71
73
|
return date.getFullYear();
|
|
72
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Will only return matches that **are not** contained in single quotes,
|
|
77
|
+
* double quotes, single line, or multiline comments.
|
|
78
|
+
*/
|
|
79
|
+
export function getQueryMatches(query, regex) {
|
|
80
|
+
let matches = [];
|
|
81
|
+
// Find quote ranges first.
|
|
82
|
+
let singleQuoteRanges = [];
|
|
83
|
+
{
|
|
84
|
+
let open = false;
|
|
85
|
+
let startIdx = -1;
|
|
86
|
+
for (let i = 0; i < query.length; i++) {
|
|
87
|
+
if (query[i] === "'") {
|
|
88
|
+
if (open) {
|
|
89
|
+
singleQuoteRanges.push({ startIdx, stopIdx: i });
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
startIdx = i;
|
|
93
|
+
}
|
|
94
|
+
open = !open;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let doubleQuoteRanges = [];
|
|
99
|
+
{
|
|
100
|
+
let open = false;
|
|
101
|
+
let startIdx = -1;
|
|
102
|
+
for (let i = 0; i < query.length; i++) {
|
|
103
|
+
if (query[i] === '"') {
|
|
104
|
+
if (open) {
|
|
105
|
+
doubleQuoteRanges.push({ startIdx, stopIdx: i });
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
startIdx = i;
|
|
109
|
+
}
|
|
110
|
+
open = !open;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
let singleLineCommentRanges = [];
|
|
115
|
+
{
|
|
116
|
+
let open = false;
|
|
117
|
+
let startIdx = -1;
|
|
118
|
+
for (let i = 0; i < query.length; i++) {
|
|
119
|
+
if (!open && query[i] === '-' && (i < query.length - 1 && query[i + 1] === '-')) {
|
|
120
|
+
open = true;
|
|
121
|
+
startIdx = i;
|
|
122
|
+
}
|
|
123
|
+
else if (open && (query[i] === '\n' || query[i] === '\r')) {
|
|
124
|
+
open = false;
|
|
125
|
+
singleLineCommentRanges.push({ startIdx, stopIdx: i });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
let multilineCommentRanges = [];
|
|
130
|
+
{
|
|
131
|
+
let open = false;
|
|
132
|
+
let startIdx = -1;
|
|
133
|
+
for (let i = 0; i < query.length; i++) {
|
|
134
|
+
if (!open && query[i] === '/' && (i < query.length - 1 && query[i + 1] === '*')) {
|
|
135
|
+
open = true;
|
|
136
|
+
startIdx = i;
|
|
137
|
+
}
|
|
138
|
+
else if (open && query[i] === '/' && (i > 0 && query[i - 1] === '*')) {
|
|
139
|
+
open = false;
|
|
140
|
+
multilineCommentRanges.push({ startIdx, stopIdx: i });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Now find matches and filter out the ones that are contained in quotes.
|
|
145
|
+
{
|
|
146
|
+
let match;
|
|
147
|
+
while ((match = regex.exec(query)) != null) {
|
|
148
|
+
let startIdx = match.index;
|
|
149
|
+
let stopIdx = match.index + match[0].length - 1;
|
|
150
|
+
let contained = false;
|
|
151
|
+
for (const quoteRange of singleQuoteRanges) {
|
|
152
|
+
if (quoteRange.startIdx <= startIdx && quoteRange.stopIdx >= stopIdx) {
|
|
153
|
+
contained = true;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!contained) {
|
|
158
|
+
for (const quoteRange of doubleQuoteRanges) {
|
|
159
|
+
if (quoteRange.startIdx <= startIdx && quoteRange.stopIdx >= stopIdx) {
|
|
160
|
+
contained = true;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (!contained) {
|
|
166
|
+
for (const singleLineCommentRange of singleLineCommentRanges) {
|
|
167
|
+
if (singleLineCommentRange.startIdx <= startIdx && singleLineCommentRange.stopIdx >= stopIdx) {
|
|
168
|
+
contained = true;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!contained) {
|
|
174
|
+
for (const multilineCommentRange of multilineCommentRanges) {
|
|
175
|
+
if (multilineCommentRange.startIdx <= startIdx && multilineCommentRange.stopIdx >= stopIdx) {
|
|
176
|
+
contained = true;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!contained) {
|
|
182
|
+
matches.push({ word: match[0], startIdx, stopIdx });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return matches;
|
|
187
|
+
}
|
|
188
|
+
export function removeQueryMatches(query, matches) {
|
|
189
|
+
let adjQuery = query;
|
|
190
|
+
let offset = 0;
|
|
191
|
+
for (const match of matches) {
|
|
192
|
+
const start = match.startIdx - offset;
|
|
193
|
+
const stop = match.stopIdx + 1 - offset;
|
|
194
|
+
adjQuery = adjQuery.substring(0, start) + adjQuery.substring(stop);
|
|
195
|
+
offset += match.word.length;
|
|
196
|
+
}
|
|
197
|
+
return adjQuery;
|
|
198
|
+
}
|
|
199
|
+
export function replaceQueryMatches(query, matches, replacement) {
|
|
200
|
+
let adjQuery = query;
|
|
201
|
+
let offset = 0;
|
|
202
|
+
for (const match of matches) {
|
|
203
|
+
const start = match.startIdx - offset;
|
|
204
|
+
const stop = match.stopIdx + 1 - offset;
|
|
205
|
+
adjQuery =
|
|
206
|
+
adjQuery.substring(0, start) +
|
|
207
|
+
replacement +
|
|
208
|
+
adjQuery.substring(stop);
|
|
209
|
+
offset += (match.word.length - replacement.length);
|
|
210
|
+
}
|
|
211
|
+
return adjQuery;
|
|
212
|
+
}
|