@stimulus-plumbers/controllers 0.2.8 → 0.2.9

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.
@@ -32,7 +32,7 @@ const VALID_CARD_LENGTH = /^\d{13,19}$/;
32
32
  /** Captures groups of up to 4 characters followed by at least one more character */
33
33
  const GROUP_FOUR_DIGITS = /(.{4})(?=.)/g;
34
34
 
35
- export const CreditCardInputFormatter = {
35
+ export const CreditCardFormatter = {
36
36
  /**
37
37
  * Converts raw input to the canonical stored form: digits only, no separators.
38
38
  * e.g. '4242 4242 4242 4242' → '4242424242424242'
@@ -4,7 +4,7 @@ const STRIP_NON_NUMERIC = /[^\d.,-]/g;
4
4
  /** Matches a valid canonical amount: optional negative sign, digits, optional decimal part */
5
5
  const VALID_AMOUNT_PATTERN = /^-?\d+(\.\d+)?$/;
6
6
 
7
- export const CurrencyInputFormatter = {
7
+ export const CurrencyFormatter = {
8
8
  /**
9
9
  * Converts raw input to the canonical stored form: a plain decimal number string.
10
10
  * Handles US format (1,234.56), European format (1.234,56), and integers ($1,000).
@@ -7,7 +7,7 @@ const SEPARATED_DATE_PATTERN = /^(\d{1,4})[/\-.](\d{1,2})[/\-.](\d{1,4})$/;
7
7
  /** Strips all non-digit characters (used when extracting 8-digit compact dates) */
8
8
  const STRIP_NON_DIGITS = /\D/g;
9
9
 
10
- export const DateInputFormatter = {
10
+ export const DateFormatter = {
11
11
  /**
12
12
  * Converts raw input to the canonical stored form: ISO 8601 YYYY-MM-DD.
13
13
  * Accepts a variety of common date formats:
@@ -61,7 +61,7 @@ export const DateInputFormatter = {
61
61
  */
62
62
  validate(value) {
63
63
  if (typeof value !== 'string') return false;
64
- const iso = DateInputFormatter.normalize(value);
64
+ const iso = DateFormatter.normalize(value);
65
65
  if (!ISO_DATE_PATTERN.test(iso)) return false;
66
66
  const date = new Date(`${iso}T00:00:00Z`);
67
67
  return !isNaN(date.getTime()) && date.toISOString().startsWith(iso);
@@ -18,7 +18,7 @@ const STRIP_NON_DIGITS = /\D/g;
18
18
  /** Matches an E.164 international phone number: + followed by 7–15 digits */
19
19
  const E164_PATTERN = /^\+\d{7,15}$/;
20
20
 
21
- export const PhoneInputFormatter = {
21
+ export const PhoneFormatter = {
22
22
  /**
23
23
  * Converts raw input to canonical form.
24
24
  * If input starts with '+', produces E.164 (+digits); otherwise strips to digits only.
@@ -1,4 +1,4 @@
1
- export const PlainInputFormatter = {
1
+ export const PlainFormatter = {
2
2
  normalize(raw) {
3
3
  if (typeof raw !== 'string') return '';
4
4
  return raw;
@@ -1,7 +1,7 @@
1
1
  /** Matches a 24-hour time: HH:MM */
2
2
  const H24_PATTERN = /^([01]?\d|2[0-3]):([0-5]\d)$/;
3
3
 
4
- export const TimeInputFormatter = {
4
+ export const TimeFormatter = {
5
5
  /**
6
6
  * Converts raw input to canonical 24-hour form: HH:MM.
7
7
  * Accepts HH:MM (24h) and h:mm AM/PM (12h).
@@ -38,7 +38,7 @@ export const TimeInputFormatter = {
38
38
  * @returns {boolean}
39
39
  */
40
40
  validate(value) {
41
- return TimeInputFormatter.normalize(value) !== '';
41
+ return TimeFormatter.normalize(value) !== '';
42
42
  },
43
43
 
44
44
  /**
@@ -7,6 +7,6 @@ export { initComboboxDropdown } from './combobox_dropdown';
7
7
  export { attachContentLoader } from './content_loader';
8
8
  export { attachDismisser } from './dismisser';
9
9
  export { attachFlipper } from './flipper';
10
- export { attachInputFormat } from './input_format';
10
+ export { attachFormatter } from './formatter';
11
11
  export { attachShifter } from './shifter';
12
12
  export { attachVisibility } from './visibility';
@@ -1,90 +0,0 @@
1
- import Plumber from '../plumber';
2
- import { PlainInputFormatter } from './formatters/plain';
3
- import { CreditCardInputFormatter } from './formatters/credit_card';
4
- import { PhoneInputFormatter } from './formatters/phone';
5
- import { CurrencyInputFormatter } from './formatters/currency';
6
- import { DateInputFormatter } from './formatters/date';
7
- import { TimeInputFormatter } from './formatters/time';
8
-
9
- export { PlainInputFormatter } from './formatters/plain';
10
- export { CreditCardInputFormatter } from './formatters/credit_card';
11
- export { PhoneInputFormatter } from './formatters/phone';
12
- export { CurrencyInputFormatter } from './formatters/currency';
13
- export { DateInputFormatter } from './formatters/date';
14
- export { TimeInputFormatter } from './formatters/time';
15
-
16
- export const FORMATTER_TYPES = {
17
- PLAIN: 'plain',
18
- CREDIT_CARD: 'creditCard',
19
- PHONE: 'phone',
20
- CURRENCY: 'currency',
21
- DATE: 'date',
22
- TIME: 'time',
23
- };
24
-
25
- const registry = new Map([
26
- [FORMATTER_TYPES.PLAIN, PlainInputFormatter],
27
- [FORMATTER_TYPES.CREDIT_CARD, CreditCardInputFormatter],
28
- [FORMATTER_TYPES.PHONE, PhoneInputFormatter],
29
- [FORMATTER_TYPES.CURRENCY, CurrencyInputFormatter],
30
- [FORMATTER_TYPES.DATE, DateInputFormatter],
31
- [FORMATTER_TYPES.TIME, TimeInputFormatter],
32
- ]);
33
-
34
- const defaultOptions = {
35
- type: FORMATTER_TYPES.PLAIN,
36
- options: {},
37
- };
38
-
39
- export class InputFormat extends Plumber {
40
- /**
41
- * Registers a custom input formatter for a given type identifier.
42
- * @param {string} type - The type identifier (e.g. 'iban')
43
- * @param {Object} formatter - Object with normalize, validate, and optionally format/mask methods
44
- */
45
- static register(type, formatter) {
46
- registry.set(type, formatter);
47
- }
48
-
49
- /**
50
- * Creates a new InputFormat plumber instance.
51
- * @param {Object} controller - Stimulus controller instance
52
- * @param {Object} [options] - Configuration options
53
- * @param {string} [options.type='plain'] - Formatter type identifier
54
- * @param {Object} [options.options={}] - Type-specific options (e.g. locale, currency)
55
- */
56
- constructor(controller, options = {}) {
57
- super(controller, options);
58
- this.type = options.type ?? defaultOptions.type;
59
- this.options = options.options ?? defaultOptions.options;
60
- this.enhance();
61
- }
62
-
63
- enhance() {
64
- const context = this;
65
- const formatter = registry.get(context.type) ?? registry.get(FORMATTER_TYPES.PLAIN);
66
-
67
- const helpers = {
68
- normalize: (raw) => formatter.normalize?.(raw, context.options) ?? (typeof raw === 'string' ? raw : ''),
69
- validate: (value) => formatter.validate?.(value, context.options) ?? true,
70
- format: (value) => formatter.format?.(value, context.options) ?? (typeof value === 'string' ? value : ''),
71
- mask: (value) => formatter.mask?.(value, context.options) ?? null,
72
- maskable: () => typeof formatter.mask === 'function',
73
- };
74
-
75
- Object.defineProperty(this.controller, 'inputFormat', {
76
- get() {
77
- return helpers;
78
- },
79
- configurable: true,
80
- });
81
- }
82
- }
83
-
84
- /**
85
- * Factory function to create and attach an InputFormat plumber to a controller.
86
- * @param {Object} controller - Stimulus controller instance
87
- * @param {Object} [options] - Configuration options
88
- * @returns {InputFormat} InputFormat plumber instance
89
- */
90
- export const attachInputFormat = (controller, options) => new InputFormat(controller, options);