Skip to content

Visualize Your Ideas With Code

feat(2d): modifies to match exact strings when passed a plain string (#1153)

Henrique Inonhe2eb64a4ac36a
diff --git a/package-lock.json b/package-lock.json
index 36698570..a3d76bf9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25436,6 +25436,7 @@
"@lezer/highlight": "^1.2.0",
"@motion-canvas/core": "^3.17.2",
"code-fns": "^0.8.2",
+ "escape-string-regexp": "^5.0.0",
"mathjax-full": "^3.2.2",
"parse-svg-path": "^0.1.2"
},
@@ -25466,6 +25467,18 @@
"@wooorm/starry-night": "^1.2.0"
}
},
+ "packages/2d/node_modules/escape-string-regexp": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
+ "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
"packages/core": {
"name": "@motion-canvas/core",
"version": "3.17.2",
@@ -29664,6 +29677,7 @@
"@preact/signals": "^1.2.1",
"clsx": "^2.0.0",
"code-fns": "^0.8.2",
+ "escape-string-regexp": "^5.0.0",
"jsdom": "^22.1.0",
"mathjax-full": "^3.2.2",
"parse-svg-path": "^0.1.2",
@@ -29684,6 +29698,11 @@
"requires": {
"@wooorm/starry-night": "^1.2.0"
}
+ },
+ "escape-string-regexp": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
+ "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw=="
}
}
},
diff --git a/packages/2d/package.json b/packages/2d/package.json
index 69954a68..2e09d04d 100644
--- a/packages/2d/package.json
+++ b/packages/2d/package.json
@@ -42,6 +42,7 @@
"@lezer/highlight": "^1.2.0",
"@motion-canvas/core": "^3.17.2",
"code-fns": "^0.8.2",
+ "escape-string-regexp": "^5.0.0",
"mathjax-full": "^3.2.2",
"parse-svg-path": "^0.1.2"
}
diff --git a/packages/2d/src/lib/code/CodeRange.test.ts b/packages/2d/src/lib/code/CodeRange.test.ts
index 6baf7258..28b1ed09 100644
--- a/packages/2d/src/lib/code/CodeRange.test.ts
+++ b/packages/2d/src/lib/code/CodeRange.test.ts
@@ -1,6 +1,7 @@
import {describe, expect, test} from 'vitest';
import {
consolidateCodeRanges,
+ findAllCodeRanges,
inverseCodeRange,
isPointInCodeRange,
pointToPoint,
@@ -71,4 +72,42 @@ describe('CodeRange', () => {
pointToPoint(0, 0, Infinity, Infinity),
]);
});
+
+ test('should treat pattern as an exact match when passing a string to findAllCodeRanges', () => {
+ const code = [
+ `const fn = () => {`,
+ ` const x = 10;`,
+ ` return x;`,
+ `};`,
+ `const fn2 = () => {`,
+ ` // NO OP`,
+ `};`,
+ ].join('\n');
+
+ expect(findAllCodeRanges(code, '()')).toEqual([
+ pointToPoint(0, 11, 0, 13),
+ pointToPoint(4, 12, 4, 14),
+ ]);
+ });
+
+ test('should treat pattern as a regular expression (with special characters) when passing a RegExp to findAllCodeRanges', () => {
+ const code = [
+ `const fn = () => {`,
+ ` const x = 10;`,
+ ` return x;`,
+ `};`,
+ `const fn2 = () => {`,
+ ` // NO OP`,
+ `};`,
+ ].join('\n');
+
+ expect(findAllCodeRanges(code, new RegExp('\\(\\)'))).toEqual([
+ pointToPoint(0, 11, 0, 13),
+ pointToPoint(4, 12, 4, 14),
+ ]);
+ expect(findAllCodeRanges(code, /\(\)/)).toEqual([
+ pointToPoint(0, 11, 0, 13),
+ pointToPoint(4, 12, 4, 14),
+ ]);
+ });
});
diff --git a/packages/2d/src/lib/code/CodeRange.ts b/packages/2d/src/lib/code/CodeRange.ts
index bde3a7bf..8d74b6a1 100644
--- a/packages/2d/src/lib/code/CodeRange.ts
+++ b/packages/2d/src/lib/code/CodeRange.ts
@@ -1,3 +1,5 @@
+import escapeRegExpString from 'escape-string-regexp';
+
export type CodePoint = [number, number];
function isCodePoint(value: unknown): value is CodePoint {
@@ -169,6 +171,8 @@ export function inverseCodeRange(ranges: CodeRange[]): CodeRange[] {
*
* @param code - The code to search in.
* @param pattern - Either a string or a regular expression to search for.
+ * When a string is passed, it looks for **exact matches**.
+ * When a RegExp object is passed, it will match against it.
* @param limit - An optional limit on the number of ranges to find.
*/
export function findAllCodeRanges(
@@ -177,6 +181,8 @@ export function findAllCodeRanges(
limit = Infinity,
): CodeRange[] {
if (typeof pattern === 'string') {
+ pattern = new RegExp(escapeRegExpString(pattern), 'g');
+ } else {
pattern = new RegExp(pattern, 'g');
}
diff --git a/packages/2d/src/lib/components/Code.ts b/packages/2d/src/lib/components/Code.ts
index 14e116bb..70a264a4 100644
--- a/packages/2d/src/lib/components/Code.ts
+++ b/packages/2d/src/lib/components/Code.ts
@@ -323,7 +323,9 @@ export class Code extends Shape {
/**
* Find all code ranges that match the given pattern.
*
- * @param pattern - Either a string or a regular expression to match.
+ * @param pattern - Either a string or a regular expression to search for.
+ * When a string is passed, it looks for **exact matches**.
+ * When a RegExp object is passed, it will match against it.
*/
public findAllRanges(pattern: string | RegExp): CodeRange[] {
return findAllCodeRanges(this.parsed(), pattern);
@@ -332,7 +334,9 @@ export class Code extends Shape {
/**
* Find the first code range that matches the given pattern.
*
- * @param pattern - Either a string or a regular expression to match.
+ * @param pattern - Either a string or a regular expression to search for.
+ * When a string is passed, it looks for **exact matches**.
+ * When a RegExp object is passed, it will match against it.
*/
public findFirstRange(pattern: string | RegExp): CodeRange {
return (
@@ -346,7 +350,9 @@ export class Code extends Shape {
/**
* Find the last code range that matches the given pattern.
*
- * @param pattern - Either a string or a regular expression to match.
+ * @param pattern - Either a string or a regular expression to search for.
+ * When a string is passed, it looks for **exact matches**.
+ * When a RegExp object is passed, it will match against it.
*/
public findLastRange(pattern: string | RegExp): CodeRange {
return (