Skip to content

feat(core): add Sugar#useTransform method#75

Merged
AsPulse merged 4 commits intomainfrom
devin/1749699495-connected-sugar-method
Jun 13, 2025
Merged

feat(core): add Sugar#useTransform method#75
AsPulse merged 4 commits intomainfrom
devin/1749699495-connected-sugar-method

Conversation

@devin-ai-integration
Copy link
Contributor

Add Sugar#useTransform method

This PR implements the Sugar#useTransform method requested in issue #14, which allows creating a new Sugar instance connected to an existing Sugar with bidirectional transformation functions.

Features

  • Bidirectional transformation: Support for async forward and backward transformation functions
  • Event propagation: Proper change/blur event propagation between original and transformed Sugar instances
  • Type safety: Full TypeScript support with generic type parameters
  • NullableStringInput example: Implements the specific use case from issue feat(core): Sugar#useTransform #14 where empty strings can be converted to null

API

const transformedSugar = sugar.useTransform<string>({
  forward: async (value: string | null) => value ?? '',
  backward: async (value: string) => value === '' ? null : value,
});

Implementation Details

  • Follows the same architectural pattern as useObject and useValidation
  • Creates an independent transformed Sugar instance that synchronizes with the original
  • Handles proper cleanup and event listener management
  • Supports template propagation with setTemplate functionality

Testing

  • Includes comprehensive test case demonstrating the NullableStringInput example
  • All existing tests continue to pass
  • Follows proper Sugar component attachment patterns for test reliability

Files Changed

  • packages/core/src/sugar/types.ts - Added type definitions
  • packages/core/src/sugar/useTransform.ts - Core implementation
  • packages/core/src/sugar/index.ts - Added method to SugarInner class
  • tests/core-unittest/src/useTransform-simple.spec.tsx - Test cases

Link to Devin run: https://app.devin.ai/sessions/87157fa55aed4027ac092aeb3a6a7ba7

- Add useTransform method to create connected sugars with bidirectional transformation
- Implement SugarTransformConfig and SugarUseTransform types
- Add useTransform to SugarInner class following useObject pattern
- Include NullableStringInput example test case from issue #14
- Support async forward/backward transformation functions
- Maintain proper event propagation between original and transformed sugars

- close #14

Co-Authored-By: あすぱる <contact@aspulse.dev>
@devin-ai-integration devin-ai-integration bot requested a review from AsPulse as a code owner June 12, 2025 04:20
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@codecov
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

Attention: Patch coverage is 63.63636% with 12 lines in your changes missing coverage. Please review.

Project coverage is 80.11%. Comparing base (60746d1) to head (685a5bf).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
packages/core/src/sugar/useTransform.ts 61.29% 12 Missing ⚠️
@@            Coverage Diff             @@
##             main      #75      +/-   ##
==========================================
- Coverage   81.15%   80.11%   -1.05%     
==========================================
  Files           7        8       +1     
  Lines         329      362      +33     
  Branches       80       85       +5     
==========================================
+ Hits          267      290      +23     
- Misses         62       72      +10     
Files with missing lines Coverage Δ
packages/core/src/sugar/index.ts 82.90% <100.00%> (+2.03%) ⬆️
packages/core/src/sugar/useTransform.ts 61.29% <61.29%> (ø)

@codecov
Copy link

codecov bot commented Jun 12, 2025

Bundle Report

Changes will increase total bundle size by 9.45kB (10.47%) ⬆️⚠️, exceeding the configured threshold of 5%.

Bundle name Size Change
all-@sugarform/core 70.56kB 6.69kB (10.47%) ⬆️⚠️
cjs-@sugarform/core 14.66kB 1.36kB (10.25%) ⬆️⚠️
esm-@sugarform/core 14.51kB 1.4kB (10.7%) ⬆️⚠️

Affected Assets, Files, and Routes:

view changes for bundle: esm-@sugarform/core

Assets Changed:

Asset Name Size Change Total Size Change (%)
lib.js 1.4kB 14.51kB 10.7% ⚠️
view changes for bundle: all-@sugarform/core

Assets Changed:

Asset Name Size Change Total Size Change (%)
lib.cjs 1.36kB 14.66kB 10.25% ⚠️
lib.js 1.4kB 14.51kB 10.7% ⚠️
lib.cjs.map 1.17kB 13.97kB 9.11% ⚠️
lib.js.map 1.16kB 12.19kB 10.51% ⚠️
sugar/types.d.ts.map 93 bytes 2.62kB 3.69%
sugar/types.d.ts 97 bytes 2.2kB 4.61%
sugar/index.d.ts.map 90 bytes 1.79kB 5.28% ⚠️
sugar/index.d.ts 92 bytes 1.76kB 5.52% ⚠️
sugar/useTransform.d.ts.map (New) 706 bytes 706 bytes 100.0% 🚀
sugar/useTransform.d.ts (New) 521 bytes 521 bytes 100.0% 🚀
view changes for bundle: cjs-@sugarform/core

Assets Changed:

Asset Name Size Change Total Size Change (%)
lib.cjs 1.36kB 14.66kB 10.25% ⚠️

Copy link
Member

@AsPulse AsPulse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ以外の部分はヨシ!! テストの内容も問題なし。

- Fix template initialization to use original sugar's template with proper casting
- Add Japanese explanatory comments for non-null assertions following useObject pattern
- Remove unnecessary async Promise-based initialization that caused test failures
- Ensure synchronous initialization for proper component attachment

Co-Authored-By: あすぱる <contact@aspulse.dev>
@AsPulse
Copy link
Member

AsPulse commented Jun 13, 2025

mainブランチの変更を取りこみましょう。templateの形が変わっています。
これに伴って、今まで「非同期にnew SugarInner({ template: await forward(template) });としたいができない」という状況が発生していたと思いますが、「new SugarInner({ template: { status: 'pending' } });」としてから、あとからsetTemplateで解決すればOKです。

devin-ai-integration bot and others added 2 commits June 13, 2025 03:21
Co-Authored-By: あすぱる <contact@aspulse.dev>
…mplate pattern

- Combine useTransform and useIsPending imports in index.ts and types.ts
- Update useTransform to use new { status: 'pending' } template initialization
- Add proper template synchronization with original sugar's template state
- Add explanatory comments for non-null assertions following codebase conventions

Co-Authored-By: あすぱる <contact@aspulse.dev>
@AsPulse AsPulse added this pull request to the merge queue Jun 13, 2025
Merged via the queue into main with commit cf78c9a Jun 13, 2025
9 checks passed
@AsPulse AsPulse deleted the devin/1749699495-connected-sugar-method branch June 13, 2025 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): Sugar#useTransform

1 participant