Skip to content

Commit 2347659

Browse files
committed
Fix some bugs
1 parent 4b60a4d commit 2347659

File tree

5 files changed

+156
-111
lines changed

5 files changed

+156
-111
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# SharpRegex
1+
# sharp-regex
22

3-
This class gets all matches, with start and end position, within the string, for a given regexp.
3+
This class gets all matches, with start and end position, within the string, for a given regex.
44

55
From high level the source code is:
66

dist/SharpRegex.d.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,18 @@ export declare class SharpRegex {
1414
* Get all detail of full match and all groups details
1515
* @param {string} subject The subject string
1616
*/
17-
getFullDetails(subject: string): {
18-
match: string;
19-
start: number;
20-
end: number;
21-
group: number;
22-
}[][];
17+
getFullDetails(subject: string): SharpRegexDetailsContract[][];
2318
/**
2419
* Get all groups details
2520
* @param {string} subject The subject string
2621
*/
27-
getGroupsDetails(subject: string): {
28-
match: string;
29-
start: number;
30-
end: number;
31-
group: number;
32-
}[][];
22+
getGroupsDetails(subject: string): SharpRegexDetailsContract[][];
3323
/**
3424
* Get details of given group
3525
* @param {string} subject The subject string
3626
* @param {number} group The group number
3727
*/
38-
getGroupDetails(subject: string, group: number): {
39-
match: string;
40-
start: number;
41-
end: number;
42-
}[];
28+
getGroupDetails(subject: string, group: number): SharpRegexDetailsContract[];
4329
/**
4430
* Adds brackets before and after a part of string
4531
* @param str string the hole regex string
@@ -73,3 +59,9 @@ export declare class SharpRegex {
7359
};
7460
};
7561
}
62+
export interface SharpRegexDetailsContract {
63+
match: string;
64+
start: number;
65+
end: number;
66+
group?: number;
67+
}

dist/SharpRegex.js

+54-33
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,30 @@ var SharpRegex = /** @class */ (function () {
1515
* @param {string} subject The subject string
1616
*/
1717
SharpRegex.prototype.getFullDetails = function (subject) {
18-
var matches, firstIndex, indexMapper = Object.assign({ 0: 0 }, this.groupIndexMapper), previousGroups = Object.assign({ 0: [] }, this.previousGroupsForGroup), regexClone = this.regexp, result = [];
19-
while ((matches = regexClone.exec(subject)) !== null) {
20-
firstIndex = matches.index;
21-
result.push(Object.keys(indexMapper).map(function (group) {
22-
var mapped = indexMapper[group], start = firstIndex + previousGroups[group].reduce(function (sum, i) { return sum + (matches[i] ? matches[i].length : 0); }, 0);
23-
return {
24-
match: matches[mapped],
25-
start: start,
26-
end: start + (matches[mapped] ? matches[mapped].length : 0),
27-
group: parseInt(group)
28-
};
29-
}));
30-
if (regexClone.lastIndex == matches.index) {
18+
var matches, indexMapper = Object.assign({ 0: 0 }, this.groupIndexMapper), previousGroups = Object.assign({ 0: [] }, this.previousGroupsForGroup), regexClone = this.regexp, result = [];
19+
var _loop_1 = function () {
20+
var firstIndex = matches.index, localResult = [];
21+
Object.keys(indexMapper).forEach(function (group) {
22+
var mapped = indexMapper[group], start = firstIndex + previousGroups[group].reduce(function (sum, i) { return sum + (matches[i] ? matches[i].length : 0); }, 0), end = start + (matches[mapped] ? matches[mapped].length : 0);
23+
if (group === "0" || end > start) {
24+
localResult.push({
25+
match: matches[mapped],
26+
start: start,
27+
end: end,
28+
group: parseInt(group)
29+
});
30+
}
31+
});
32+
result.push(localResult);
33+
/**
34+
* Prevent from infinite loop
35+
*/
36+
if (regexClone.lastIndex == firstIndex) {
3137
regexClone.lastIndex++;
3238
}
39+
};
40+
while ((matches = regexClone.exec(subject)) !== null) {
41+
_loop_1();
3342
}
3443
return result;
3544
};
@@ -38,21 +47,30 @@ var SharpRegex = /** @class */ (function () {
3847
* @param {string} subject The subject string
3948
*/
4049
SharpRegex.prototype.getGroupsDetails = function (subject) {
41-
var matches = this.regexp.exec(subject), indexMapper = this.groupIndexMapper, previousGroups = this.previousGroupsForGroup, firstIndex, regexClone = this.regexp, result = [];
42-
while ((matches = regexClone.exec(subject)) !== null) {
43-
firstIndex = matches.index;
44-
result.push(Object.keys(indexMapper).map(function (group) {
45-
var mapped = indexMapper[group], start = firstIndex + previousGroups[group].reduce(function (sum, i) { return sum + (matches[i] ? matches[i].length : 0); }, 0);
46-
return {
47-
match: matches[mapped],
48-
start: start,
49-
end: start + (matches[mapped] ? matches[mapped].length : 0),
50-
group: parseInt(group)
51-
};
52-
}));
53-
if (regexClone.lastIndex == matches.index) {
50+
var matches = this.regexp.exec(subject), indexMapper = this.groupIndexMapper, previousGroups = this.previousGroupsForGroup, regexClone = this.regexp, result = [];
51+
var _loop_2 = function () {
52+
var firstIndex = matches.index, localResult = [];
53+
Object.keys(indexMapper).forEach(function (group) {
54+
var mapped = indexMapper[group], start = firstIndex + previousGroups[group].reduce(function (sum, i) { return sum + (matches[i] ? matches[i].length : 0); }, 0), end = start + (matches[mapped] ? matches[mapped].length : 0);
55+
if (group === "0" || end > start) {
56+
localResult.push({
57+
match: matches[mapped],
58+
start: start,
59+
end: start + (matches[mapped] ? matches[mapped].length : 0),
60+
group: parseInt(group)
61+
});
62+
}
63+
});
64+
result.push(localResult);
65+
/**
66+
* Prevent from infinite loop
67+
*/
68+
if (regexClone.lastIndex == firstIndex) {
5469
regexClone.lastIndex++;
5570
}
71+
};
72+
while ((matches = regexClone.exec(subject)) !== null) {
73+
_loop_2();
5674
}
5775
return result;
5876
};
@@ -73,7 +91,10 @@ var SharpRegex = /** @class */ (function () {
7391
start: startIndex,
7492
end: endIndex,
7593
});
76-
if (regexClone.lastIndex == matches.index) {
94+
/**
95+
* Prevent from infinite loop
96+
*/
97+
if (regexClone.lastIndex == firstIndex) {
7798
regexClone.lastIndex++;
7899
}
79100
}
@@ -108,15 +129,15 @@ var SharpRegex = /** @class */ (function () {
108129
*/
109130
SharpRegex.prototype._fillGroups = function (regex) {
110131
var regexString = regex.source, modifier = regex.flags, tester = /(\\\()|(\\\))|(\(\?)|(\()|(\)(?:{\d+,?\d*}|[*+?])?\??)/g, modifiedRegex = regexString, lastGroupStartPosition = -1, lastGroupEndPosition = -1, lastNonGroupStartPosition = -1, lastNonGroupEndPosition = -1, groupsAdded = 0, groupCount = 0, matchArr, nonGroupPositions = [], groupPositions = [], groupNumber = [], currentLengthIndexes = [], groupIndexMapper = {}, previousGroupsForGroup = {};
111-
var _loop_1 = function () {
112-
if (matchArr[1] || matchArr[2]) {
132+
var _loop_3 = function () {
133+
if (matchArr[1] || matchArr[2]) { // ignore escaped brackets \(, \)
113134
}
114-
if (matchArr[3]) {
135+
if (matchArr[3]) { // non capturing group (?
115136
var index = matchArr.index + matchArr[0].length - 1;
116137
lastNonGroupStartPosition = index;
117138
nonGroupPositions.push(index);
118139
}
119-
else if (matchArr[4]) {
140+
else if (matchArr[4]) { // capturing group (
120141
var index = matchArr.index + matchArr[0].length - 1, lastGroupPosition = Math.max(lastGroupStartPosition, lastGroupEndPosition);
121142
// if a (? is found add ) before it
122143
if (lastNonGroupStartPosition > lastGroupPosition) {
@@ -163,7 +184,7 @@ var SharpRegex = /** @class */ (function () {
163184
groupIndexMapper[groupCount] = groupCount + groupsAdded;
164185
previousGroupsForGroup[groupCount] = currentLengthIndexes.slice();
165186
}
166-
else if (matchArr[5]) {
187+
else if (matchArr[5]) { // closing bracket ), )+, )+?, ){1,}?, ){1,1111}?
167188
var index = matchArr.index + matchArr[0].length - 1;
168189
if ((groupPositions.length && !nonGroupPositions.length) ||
169190
groupPositions[groupPositions.length - 1] > nonGroupPositions[nonGroupPositions.length - 1]) {
@@ -187,7 +208,7 @@ var SharpRegex = /** @class */ (function () {
187208
};
188209
var this_1 = this;
189210
while ((matchArr = tester.exec(regexString)) !== null) {
190-
_loop_1();
211+
_loop_3();
191212
}
192213
return { regexp: new RegExp(modifiedRegex, modifier), groupIndexMapper: groupIndexMapper, previousGroupsForGroup: previousGroupsForGroup };
193214
};

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sharp-regex",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Implements advance feature for regex",
55
"main": "dist/public_api.js",
66
"types": "dist/public_api.d.ts",
@@ -9,17 +9,17 @@
99
},
1010
"repository": {
1111
"type": "git",
12-
"url": "git+https://github.com/Lavaei/SharpRegex.git"
12+
"url": "git+https://github.com/Lavaei/sharp-regex.git"
1313
},
1414
"keywords": [
1515
"Regex"
1616
],
1717
"author": "Mostafa Lavaei",
1818
"license": "MIT",
1919
"bugs": {
20-
"url": "https://github.com/Lavaei/SharpRegex/issues"
20+
"url": "https://github.com/Lavaei/sharp-regex/issues"
2121
},
22-
"homepage": "https://github.com/Lavaei/SharpRegex#readme",
22+
"homepage": "https://github.com/Lavaei/sharp-regex#readme",
2323
"devDependencies": {
2424
"typescript": "2.5.2"
2525
},

0 commit comments

Comments
 (0)