@quenty/textfilterutils 6.7.0 → 6.8.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [6.8.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/textfilterutils@6.7.0...@quenty/textfilterutils@6.8.0) (2023-08-01)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add TextFilterUtils.countFilteredCharacters() ([f156822](https://github.com/Quenty/NevermoreEngine/commit/f1568223d2da7051e24558a094ad7fe29c8a2d5e))
12
+
13
+
14
+
15
+
16
+
6
17
  # [6.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/textfilterutils@6.6.0...@quenty/textfilterutils@6.7.0) (2023-07-28)
7
18
 
8
19
  **Note:** Version bump only for package @quenty/textfilterutils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/textfilterutils",
3
- "version": "6.7.0",
3
+ "version": "6.8.0",
4
4
  "description": "Utility functions for filtering text",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "539b6bcf1266fb18e143059d08381654cca200bd"
34
+ "gitHead": "839b4d929e4d6154aadf719f1ecfb9add4c8b247"
35
35
  }
@@ -186,4 +186,115 @@ function TextFilterUtils.hasNonFilteredText(text: string): boolean
186
186
  return string.find(text, "[^#%s]") ~= nil
187
187
  end
188
188
 
189
+ local WHITESPACE = {
190
+ ["\r"] = true;
191
+ ["\n"] = true;
192
+ [" "] = true;
193
+ ["\t"] = true;
194
+ }
195
+
196
+ --[=[
197
+ Computes proportional text that is filtered ignoring whitespace.
198
+
199
+ @param text string
200
+ @return number
201
+ ]=]
202
+ function TextFilterUtils.getProportionFiltered(text: string)
203
+ local filteredChars, unfilteredChars = TextFilterUtils.countFilteredCharacters(text)
204
+ local total = unfilteredChars + filteredChars
205
+ if total == 0 then
206
+ return 0
207
+ end
208
+
209
+ return filteredChars/total
210
+ end
211
+
212
+ --[=[
213
+ Gets the number of filtered characters in the text string
214
+
215
+ @param text string
216
+ @return number -- filtered characters
217
+ @return number -- Unfiltered characters
218
+ @return number -- White space characters
219
+ ]=]
220
+ function TextFilterUtils.countFilteredCharacters(text: string)
221
+ local filteredChars = 0
222
+ local unfilteredChars = 0
223
+ local whitespaceCharacters = 0
224
+ for i=1, #text do
225
+ local textChar = string.sub(text, i, i)
226
+ if textChar == "#" then
227
+ filteredChars = filteredChars + 1
228
+ elseif WHITESPACE[textChar] then
229
+ whitespaceCharacters = whitespaceCharacters + 1
230
+ else
231
+ unfilteredChars = unfilteredChars + 1
232
+ end
233
+ end
234
+
235
+ return filteredChars, unfilteredChars, whitespaceCharacters
236
+ end
237
+
238
+
239
+ --[=[
240
+ Adds in new lines and whitespace to the text
241
+
242
+ @param text string
243
+ @param filteredText string
244
+ @return number
245
+ ]=]
246
+ function TextFilterUtils.addBackInNewLinesAndWhitespace(text, filteredText)
247
+ assert(type(text) == "string", "Bad text")
248
+ assert(type(filteredText) == "string", "Bad filteredText")
249
+
250
+ if text == filteredText then
251
+ return text
252
+ end
253
+
254
+ -- Assume that any missing characters are actually our newlines.
255
+ local missingCharacters = math.max(0, #text - #filteredText)
256
+
257
+ -- TODO: Not all this GC
258
+ local newString = ""
259
+ local filteredTextIndex = 1
260
+
261
+ local textIndex = 1
262
+ while filteredTextIndex <= #filteredText or textIndex <= #text do
263
+ local textChar = string.sub(text, textIndex, textIndex)
264
+ local filteredChar = string.sub(filteredText, filteredTextIndex, filteredTextIndex)
265
+
266
+ if textChar == "\n" then
267
+ if missingCharacters > 0 then
268
+ missingCharacters = missingCharacters - 1
269
+ newString = newString .. "\n"
270
+ else
271
+ newString = newString .. "\n"
272
+ filteredTextIndex = filteredTextIndex + 1
273
+ end
274
+ elseif textChar == " " and filteredChar == "#" then
275
+ newString = newString .. " "
276
+ elseif textChar == "\t" and filteredChar == "#" then
277
+ newString = newString .. "\t"
278
+ elseif textChar == "\r" and filteredChar == "#" then
279
+ newString = newString .. "\r"
280
+ else
281
+ if filteredChar == "" then
282
+ if missingCharacters > 0 then
283
+ missingCharacters = missingCharacters - 1
284
+ newString = newString .. "\n"
285
+ end
286
+ else
287
+ newString = newString .. filteredChar
288
+ end
289
+
290
+ filteredTextIndex = filteredTextIndex + 1
291
+ end
292
+
293
+ textIndex = textIndex + 1
294
+ end
295
+
296
+ return newString
297
+ end
298
+
299
+
189
300
  return TextFilterUtils