Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 30, 2025

Extracts repetitive code patterns from /src/types into reusable helpers in /src/lib/helpers.js to enforce DRY principles and simplify maintenance.

Changes

Parsing helpers

  • parseTime(str) - Consolidates time parsing logic from time.type.js
    • Supports HH:mm, HH:mm:ss, HHmm, HHmmss formats
  • parseDateTime(str) - Consolidates datetime parsing from datetime-local.type.js
    • Supports ISO 8601, compact formats (YYYYMMDDTHHmmss), with/without seconds

Validation helper

  • validateInputType(node, expectedType, errorCode, errorMessage) - Unifies type validation across 6 input types
    • Replaces duplicate 8-line validation blocks in time, datetime-local, date, number, radio, color
    • Handles tag validation, type checking, and autofill

Attribute helper

  • setTabIndex(target, value) - Standardizes tabindex management
    • Only sets if not explicitly defined by user

Example

Before:

// Repeated in 6 type files
const targetTag = me.targetFieldNode.tagName;
const targetType = me.targetFieldNode.getAttribute("type");
if (targetTag != "INPUT" || (targetType || "time").toLowerCase() != "time") {
    throw me.renderError('NOT_A_TIME_FIELD', `Time inputs require...`);
}
if (!targetType) me.targetFieldNode.type = "time";

After:

try {
    validateInputType(me.targetFieldNode, "time", 'NOT_A_TIME_FIELD', `Time inputs require...`);
} catch (error) {
    throw me.renderError(error.code, error.message);
}

Notes

Evaluated but skipped extracting loadTemplates and updateTriggers - these are tightly coupled to their component contexts and have no reuse cases outside their modules.

Original prompt

Problem Statement

The /src/types directory of this project contains repetitive and redundant code that can be unified into utility functions or abstractions located in /src/lib/helpers.js. This will simplify maintenance, enforce DRY (Don't Repeat Yourself) principles, and make the repository more modular.

Proposed Changes

1. Common Parsing Logic (Commit 1):

Unify string-based time and datetime parsing (parseTimeStr, parseDateTimeStr) from time.type.js and datetime-local.type.js into helper functions. These functions will be named parseTime and parseDateTime respectively.

2. Handling of Event Handlers (Skip):

No changes here, as components such as color.type.js already inherit hooks properly from their parent components (e.g., input.type.js).

3. Repeated Node Attributes (Commit 2):

Unify common operations like tabindex management, style attribute writing, and aria-labelledby handling into helpers (e.g., setTabIndex, ensureAriaLabel in /src/lib/helpers.js).

4. Template and Children Processing (Commit 3):

Abstract duplicate template-loading mechanisms from list.type.js (loadTemplates) and form.type.js (me.children) into helpers/functions. These methods could generalize template validation, role-based assignment, and object collection responsibilities.

5. Decorator & Trigger Improvements (Commit 4):

For decorators like smartdisabling in smartdisabling.deco.js and sortable, generalize methods that operate on DOM elements (e.g., updating triggers or sorting). Abstract these behaviors into reusable helpers that the decorators call.

6. Centralized Validation (Commit 5):

Unify repetitive validation checks (e.g., NOT_A_*_FIELD from radio.type.js, time.type.js) by extracting validateType or equivalent methods that enforce input, node types, or children consistency.


Each of these suggested changes should be implemented in separate commits to allow a more surgical approach for review, rollback, or cherry-picking downstream if needed.

Let me know if clarifications are required!

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 31, 2025 00:03
Co-authored-by: bitifet <1643647+bitifet@users.noreply.github.com>
Co-authored-by: bitifet <1643647+bitifet@users.noreply.github.com>
Co-authored-by: bitifet <1643647+bitifet@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor repetitive code in types directory into helpers Refactor types: extract common parsing, validation, and attribute helpers Dec 31, 2025
Copilot AI requested a review from bitifet December 31, 2025 00:12
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.

2 participants