@nejs/basic-extensions 2.2.1 → 2.4.0

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.
@@ -1,4 +1,5 @@
1
1
  import { Patch } from '@nejs/extension';
2
+ const parenthesisPair = ['(', ')'];
2
3
  /**
3
4
  * `StringExtensions` is a patch for the JavaScript built-in `String` class. It
4
5
  * adds utility methods to the `String` class without modifying the global namespace
@@ -21,5 +22,132 @@ export const StringExtensions = new Patch(String, {
21
22
  }
22
23
  return false;
23
24
  },
25
+ /**
26
+ * A getter property that returns a pair of parentheses as an array.
27
+ * This property can be used when operations require a clear distinction
28
+ * between the opening and closing parentheses, such as parsing or
29
+ * matching balanced expressions in strings.
30
+ *
31
+ * @returns {[string, string]} An array containing a pair of strings: the
32
+ * opening parenthesis '(' as the first element, and the closing parenthesis
33
+ * ')' as the second element.
34
+ */
35
+ get parenthesisPair() {
36
+ return ['(', ')'];
37
+ },
38
+ /**
39
+ * A getter property that returns a pair of square brackets as an array.
40
+ * This property is particularly useful for operations that require a clear
41
+ * distinction between the opening and closing square brackets, such as
42
+ * parsing arrays in strings or matching balanced expressions within
43
+ * square brackets.
44
+ *
45
+ * @returns {[string, string]} An array containing a pair of strings: the
46
+ * opening square bracket '[' as the first element, and the closing square
47
+ * bracket ']' as the second element.
48
+ */
49
+ get squareBracketsPair() {
50
+ return ['[', ']'];
51
+ },
52
+ /**
53
+ * A getter property that returns a pair of curly brackets as an array.
54
+ * This property is particularly useful for operations that require a clear
55
+ * distinction between the opening and closing curly brackets, such as
56
+ * parsing objects in strings or matching balanced expressions within
57
+ * curly brackets. The returned array consists of the opening curly bracket
58
+ * '{' as the first element, and the closing curly bracket '}' as the
59
+ * second element.
60
+ *
61
+ * @returns {[string, string]} An array containing a pair of strings: the
62
+ * opening curly bracket '{' as the first element, and the closing curly
63
+ * bracket '}' as the second element.
64
+ */
65
+ get curlyBracketsPair() {
66
+ return ['{', '}'];
67
+ },
68
+ });
69
+ /**
70
+ * `StringPrototypeExtensions` provides a set of utility methods that are
71
+ * added to the `String` prototype. This allows all string instances to
72
+ * access new functionality directly, enhancing their capabilities beyond
73
+ * the standard `String` class methods. These extensions are applied using
74
+ * the `Patch` class from '@nejs/extension', ensuring that they do not
75
+ * interfere with the global namespace or existing properties.
76
+ *
77
+ * The extensions include methods for extracting substrings based on
78
+ * specific tokens, checking the presence of certain patterns, and more,
79
+ * making string manipulation tasks more convenient and expressive.
80
+ */
81
+ export const StringPrototypeExtensions = new Patch(String.prototype, {
82
+ /**
83
+ * Extracts a substring from the current string, starting at a given offset
84
+ * and bounded by specified opening and closing tokens. This method is
85
+ * particularly useful for parsing nested structures or quoted strings,
86
+ * where the level of nesting or the presence of escape characters must
87
+ * be considered.
88
+ *
89
+ * @param {number} offset The position in the string from which to start the
90
+ * search for the substring.
91
+ * @param {[string, string]} tokens An array containing two strings: the
92
+ * opening and closing tokens that define the boundaries of the substring
93
+ * to be extracted.
94
+ * @returns {Object} An object with two properties: `extracted`, the
95
+ * extracted substring, and `newOffset`, the position in the original
96
+ * string immediately after the end of the extracted substring. If no
97
+ * substring is found, `extracted` is `null` and `newOffset` is the same
98
+ * as the input offset.
99
+ */
100
+ extractSubstring(offset = 0, tokens = parenthesisPair) {
101
+ let [openToken, closeToken] = tokens;
102
+ let depth = 0;
103
+ let start = -1;
104
+ let end = -1;
105
+ let leadingToken = '';
106
+ let firstToken = 0;
107
+ for (let i = offset; i < this.length; i++) {
108
+ const char = this[i];
109
+ if (char === openToken) {
110
+ depth++;
111
+ if (start === -1)
112
+ start = i;
113
+ }
114
+ else if (char === closeToken) {
115
+ depth--;
116
+ if (depth === 0) {
117
+ end = i;
118
+ break;
119
+ }
120
+ }
121
+ }
122
+ let lRange = [
123
+ Math.max(0, start - 100),
124
+ start
125
+ ];
126
+ let leading = [...this.substring(lRange[0], lRange[1])].reverse().join('');
127
+ let reversedLeadingToken;
128
+ try {
129
+ reversedLeadingToken = /([^ \,\"\'\`]+)/.exec(leading)[1] ?? '';
130
+ leadingToken = [...reversedLeadingToken].reverse().join('');
131
+ }
132
+ catch (ignored) { }
133
+ if (start !== -1 && end !== -1) {
134
+ const sliceRange = [start, end + 1];
135
+ const extracted = this.slice(sliceRange[0], sliceRange[1]);
136
+ return {
137
+ extracted,
138
+ range: [start, end],
139
+ newOffset: end + 1,
140
+ leadingToken,
141
+ };
142
+ }
143
+ else {
144
+ return {
145
+ extracted: null,
146
+ range: [start, end],
147
+ newOffset: offset,
148
+ leadingToken,
149
+ };
150
+ }
151
+ },
24
152
  });
25
153
  //# sourceMappingURL=stringextensions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stringextensions.js","sourceRoot":"","sources":["../../src/stringextensions.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAExC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE;IAChD;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK;QACZ,IAAI,KAAK,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,MAAM,CAAC,EAAE,CAAC;YACpE,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAA;QACzB,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;CACF,CAAC,CAAC"}
1
+ {"version":3,"file":"stringextensions.js","sourceRoot":"","sources":["../../src/stringextensions.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAExC,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE;IAChD;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK;QACZ,IAAI,KAAK,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK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