-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance: add more action rule templating helpers #4243
Conversation
✅ Deploy Preview for actualbudget ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Bundle Stats — desktop-clientHey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle. As this PR is updated, I'll keep you updated on how the bundle size is impacted. Total
Changeset
View detailed bundle breakdownAdded No assets were added Removed No assets were removed Bigger
Smaller
Unchanged
|
Bundle Stats — loot-coreHey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle. As this PR is updated, I'll keep you updated on how the bundle size is impacted. Total
Changeset
View detailed bundle breakdownAdded No assets were added Removed No assets were removed Bigger
Smaller No assets were smaller Unchanged No assets were unchanged |
WalkthroughThe pull request introduces new test cases and helper functions in the Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (6)
packages/loot-core/src/server/accounts/rules.ts (3)
Line range hint
54-79
: Consider enhancing type safety and error handling in regexHelper.The implementation is well-structured, but could benefit from:
- TypeScript type annotations for the function parameters
- Try-catch block for regex compilation
- Validation for empty strings
function regexHelper( - mapRegex: (regex: string, flags: string) => string | RegExp, - mapNonRegex: (value: string) => string | RegExp, - apply: (value: string, regex: string | RegExp, replace: string) => string, + mapRegex: (regex: string, flags: string) => RegExp, + mapNonRegex: (value: string) => string | RegExp, + apply: (value: string, regex: string | RegExp, replace: string) => string, ) { return (value: unknown, regex: unknown, replace: unknown) => { if (value == null) { return null; } - if (typeof regex !== 'string' || typeof replace !== 'string') { + if (!regex || typeof regex !== 'string' || !replace || typeof replace !== 'string') { return ''; } let regexp: string | RegExp; const match = regexTest.exec(regex); // Regex is in format /regex/flags if (match) { - regexp = mapRegex(match[1], match[2]); + try { + regexp = mapRegex(match[1], match[2]); + } catch (e) { + console.warn('Invalid regex pattern:', e); + return String(value); + } } else { regexp = mapNonRegex(regex); } return apply(String(value), regexp, replace); }; }
81-96
: Add JSDoc documentation for the regex-based helpers.Consider adding JSDoc documentation to improve IDE support and code clarity:
const helpers = { + /** + * Replaces text using regex or literal string patterns. + * @param {string} value - The input string + * @param {string} regex - Pattern in /regex/flags format or literal string + * @param {string} replace - Replacement string + * @returns {string} The modified string + */ regex: regexHelper( (regex, flags) => new RegExp(regex, flags), value => new RegExp(value), (value, regex, replace) => value.replace(regex, replace), ), + /** + * Replaces first occurrence using regex or literal string. + * @param {string} value - The input string + * @param {string} regex - Pattern in /regex/flags format or literal string + * @param {string} replace - Replacement string + * @returns {string} The modified string + */ replace: regexHelper( (regex, flags) => new RegExp(regex, flags), value => value, (value, regex, replace) => value.replace(regex, replace), ), + /** + * Replaces all occurrences using regex or literal string. + * @param {string} value - The input string + * @param {string} regex - Pattern in /regex/flags format or literal string + * @param {string} replace - Replacement string + * @returns {string} The modified string + */ replaceAll: regexHelper( (regex, flags) => new RegExp(regex, flags), value => value, (value, regex, replace) => value.replaceAll(regex, replace), ),
153-153
: Enhance the concat helper with types and documentation.Add type safety and documentation to improve code clarity:
+ /** + * Concatenates multiple values into a single string. + * @param {...unknown[]} args - Values to concatenate + * @returns {string} The concatenated string + */ - concat: (...args: unknown[]) => args.slice(0, -1).join(''), + concat: (...args: unknown[]): string => { + const values = args.slice(0, -1); + return values.map(String).join(''); + },packages/loot-core/src/server/accounts/rules.test.ts (3)
362-374
: Add more test cases for regex helpers.Consider adding tests for:
- Invalid regex patterns
- Empty strings
- Special characters in replacement
// Add these test cases testHelper('{{replace notes "/[/" "a"}}', 'Sarah Condition'); // Invalid regex testHelper('{{replace notes "" "a"}}', 'Sarah Condition'); // Empty pattern testHelper('{{replace notes "a" "$&"}}', 'S$&r$&h Condition'); // Special replacement
430-441
: Add edge cases for date manipulation helpers.Consider adding tests for:
- Invalid dates
- Month/year boundaries
- Leap years
// Add these test cases testHelper('{{addDays "invalid" 5}}', ''); // Invalid date testHelper('{{addDays "2002-01-31" 1}}', '2002-02-01'); // Month boundary testHelper('{{addMonths "2002-01-31" 1}}', '2002-02-28'); // Date overflow testHelper('{{addDays "2004-02-28" 1}}', '2004-02-29'); // Leap year testHelper('{{addDays "2004-02-29" 1}}', '2004-03-01'); // Leap year boundary
453-454
: Add edge cases for concat helper.Consider adding tests for:
- Non-string values
- Empty strings
- Null/undefined values
// Add these test cases testHelper('{{concat "A" 123 true null undefined}}', 'A123true'); // Mixed types testHelper('{{concat "" "" ""}}', ''); // Empty strings testHelper('{{concat null undefined}}', 'nullundefined'); // Null/undefined
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
upcoming-release-notes/4243.md
is excluded by!**/*.md
📒 Files selected for processing (2)
packages/loot-core/src/server/accounts/rules.test.ts
(2 hunks)packages/loot-core/src/server/accounts/rules.ts
(4 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/loot-core/src/server/accounts/rules.test.ts (1)
Learnt from: jfdoming
PR: actualbudget/actual#3641
File: packages/loot-core/src/server/accounts/rules.test.ts:524-536
Timestamp: 2024-11-10T16:45:25.627Z
Learning: In `packages/loot-core/src/server/accounts/rules.test.ts`, prefer explicit action definitions over refactoring similar actions into loops or helper functions, even when actions are similar.
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: Visual regression
- GitHub Check: Functional
- GitHub Check: build (windows-latest)
- GitHub Check: build (ubuntu-latest)
Part of #3606
Adds the following helpers:
replace value pattern replacement
mimics js replace. When pattern is not as/regex/flags
it just uses raw value as opposed to{{regex ...
replaceAll value pattern replacement
mimics js replaceAlladdDays date days
add x days to the daysubDays date days
addWeeks date weeks
subWeeks date weeks
addMonths date months
subMonths date months
addYears date years
subYears date years
setDay date day
Overflows are handled, 0 will set to last day of month beforeconcat args..
joins all arguments together