@bouko/ts 0.4.1 → 0.4.3

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,3 +1,10 @@
1
1
  export declare const shortenStr: (str: string) => string;
2
2
  export declare const shortenNum: (x: number) => string;
3
3
  export declare const cleanStr: (str: string) => string;
4
+ export declare const stripStr: (str: string, noise: string[]) => string;
5
+ export type CanonicalTermsMap = Record<string, string[]>;
6
+ export declare function getCanonicalMatch(input: string, map: CanonicalTermsMap): {
7
+ canonical: string;
8
+ matchedTerm: string;
9
+ } | undefined;
10
+ export declare function removeAll(input: string, needle: string, flags?: string): string;
@@ -16,3 +16,23 @@ export const cleanStr = (str) => str
16
16
  .replace(/[^\w!?.)]+$/g, "") // trailing junk
17
17
  .replace(/\s+/g, " ") // collapse spaces
18
18
  .trim();
19
+ export const stripStr = (str, noise) => {
20
+ for (const word of noise)
21
+ str = str.replace(new RegExp(word, "gi"), "");
22
+ return cleanStr(str.replace("()", ""));
23
+ };
24
+ export function getCanonicalMatch(input, map) {
25
+ const lower = input.toLowerCase();
26
+ for (const [canonical, terms] of Object.entries(map)) {
27
+ const searchTerms = [canonical, ...(terms ?? [])].filter(Boolean);
28
+ const match = searchTerms.find(t => lower.includes(t.toLowerCase()));
29
+ if (match)
30
+ return { canonical, matchedTerm: match };
31
+ }
32
+ }
33
+ export function removeAll(input, needle, flags = "gi") {
34
+ return needle ? input.replace(new RegExp(escapeRegExp(needle), flags), "") : input;
35
+ }
36
+ function escapeRegExp(s) {
37
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
38
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/ts",
4
- "version": "0.4.1",
4
+ "version": "0.4.3",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",