@fkui/logic 6.14.0 → 6.16.0

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.
package/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2021 Försäkringskassan
1
+ Copyright 2025 Försäkringskassan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/lib/cjs/index.js CHANGED
@@ -1,12 +1,5 @@
1
1
  'use strict';
2
2
 
3
- /**
4
- * @public
5
- */
6
- const configLogic = {
7
- production: true,
8
- };
9
-
10
3
  /**
11
4
  * Determine if a value is empty.
12
5
  *
@@ -41,6 +34,106 @@ function isString(value) {
41
34
  return typeof value === "string" || value instanceof String;
42
35
  }
43
36
 
37
+ /**
38
+ * An error indicating that a mandatory value is not set.
39
+ *
40
+ * @public
41
+ */
42
+ class MissingValueError extends Error {
43
+ constructor(message) {
44
+ super(message);
45
+ Object.setPrototypeOf(this, MissingValueError.prototype);
46
+ }
47
+ }
48
+ /**
49
+ * Assert that a value is set and throw a {@link MissingValueError} if it is not,
50
+ * i.e., both null and undefined values will throw an error. This can be nice to
51
+ * use when transforming data between view model and rest api model.
52
+ *
53
+ * @public
54
+ * @param value - the value asserted to be set.
55
+ * @param message - an optional exception message to use if the check fails.
56
+ * @returns the value unambiguously defined.
57
+ * @throws {@link MissingValueError} if value is not set.
58
+ */
59
+ function ensureSet(value, message = "") {
60
+ if (!isSet(value)) {
61
+ throw new MissingValueError(message);
62
+ }
63
+ return value;
64
+ }
65
+
66
+ /**
67
+ * [Assertion function][ts-handbook] to assert that a nullable Vue ref holds a
68
+ * non-null value. Typically used with template refs but any nullable ref can be
69
+ * used.
70
+ *
71
+ * Throws {@link MissingValueError} if the ref holds `null` or `undefined`, or
72
+ * the ref itself is `null` or `undefined`.
73
+ *
74
+ * [ts-handbook]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
75
+ *
76
+ * @example
77
+ *
78
+ * ```ts
79
+ * const element = useTemplateRef("my-template-ref");
80
+ * // ^? Readonly<ShallowRef<HTMLElement | null>>
81
+ *
82
+ * assertRef(element);
83
+ *
84
+ * // element is now guaranteed to be a HTMLElement ref
85
+ * ```
86
+ *
87
+ * @public
88
+ * @since v6.15.0
89
+ * @param ref - the value asserted to be a ref.
90
+ * @param message - an optional exception message to use if the check fails.
91
+ * @throws {@link MissingValueError} if value is not set.
92
+ */
93
+ function assertRef(ref, message = "Expected ref to have a non-null value, but it did not") {
94
+ if (!isSet(ref?.value)) {
95
+ throw new MissingValueError(message);
96
+ }
97
+ }
98
+
99
+ /**
100
+ * [Assertion function][ts-handbook] to assert that a value is set.
101
+ * Any value that is not null or undefined is considered set including falsy values such as 0 or "".
102
+ *
103
+ * Throws {@link MissingValueError} if value is not set.
104
+ *
105
+ * [ts-handbook]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
106
+ *
107
+ * @example
108
+ *
109
+ * ```ts
110
+ * const value = getValue();
111
+ * // ^? string | null
112
+ *
113
+ * assertSet(value);
114
+ *
115
+ * // value is now guaranteed to be non-null
116
+ * ```
117
+ *
118
+ * @public
119
+ * @since v6.15.0
120
+ * @param value - the value asserted to be set.
121
+ * @param message - an optional exception message to use if the check fails.
122
+ * @throws {@link MissingValueError} if value is not set.
123
+ */
124
+ function assertSet(value, message = "Expected value to be set, but it was not") {
125
+ if (!isSet(value)) {
126
+ throw new MissingValueError(message);
127
+ }
128
+ }
129
+
130
+ /**
131
+ * @public
132
+ */
133
+ const configLogic = {
134
+ production: true,
135
+ };
136
+
44
137
  /**
45
138
  * Implementation of Object.fromEntries() until we run a recent enough version
46
139
  * of NodeJS.
@@ -1987,35 +2080,6 @@ function deepClone(value) {
1987
2080
  return cloneDeep(value);
1988
2081
  }
1989
2082
 
1990
- /**
1991
- * An error indicating that a mandatory value is not set.
1992
- *
1993
- * @public
1994
- */
1995
- class MissingValueError extends Error {
1996
- constructor(message) {
1997
- super(message);
1998
- Object.setPrototypeOf(this, MissingValueError.prototype);
1999
- }
2000
- }
2001
- /**
2002
- * Assert that a value is set and throw a {@link MissingValueError} if it is not,
2003
- * i.e., both null and undefined values will throw an error. This can be nice to
2004
- * use when transforming data between view model and rest api model.
2005
- *
2006
- * @public
2007
- * @param value - the value asserted to be set.
2008
- * @param message - an optional exception message to use if the check fails.
2009
- * @returns the value unambiguously defined.
2010
- * @throws {@link MissingValueError} if value is not set.
2011
- */
2012
- function ensureSet(value, message = "") {
2013
- if (!isSet(value)) {
2014
- throw new MissingValueError(message);
2015
- }
2016
- return value;
2017
- }
2018
-
2019
2083
  /**
2020
2084
  * Takes an optionally nested textobject structure and convert to a flat
2021
2085
  * structure. Nested keys are joined with a dot ".". Deep nesting is supported.
@@ -4043,7 +4107,7 @@ class ValidationServiceImpl {
4043
4107
  elementValidatorsReferences = {};
4044
4108
  validationErrorMessages = {};
4045
4109
  constructor() {
4046
- this.addValidationErrorMessages(getErrorMessages());
4110
+ this.setErrorMessages(getErrorMessages());
4047
4111
  }
4048
4112
  getElementsAndValidators() {
4049
4113
  return this.elementValidatorsReferences;
@@ -4052,10 +4116,18 @@ class ValidationServiceImpl {
4052
4116
  return Object.values(this.validationStates).some((item) => item.touched === true);
4053
4117
  }
4054
4118
  addValidationErrorMessages(validationErrorMessages) {
4055
- this.validationErrorMessages = {
4056
- ...this.validationErrorMessages,
4057
- ...validationErrorMessages,
4058
- };
4119
+ this.setErrorMessages(validationErrorMessages);
4120
+ }
4121
+ setErrorMessages(messages, options = {}) {
4122
+ const { clear = false } = options;
4123
+ if (clear) {
4124
+ this.clearErrorMessages();
4125
+ }
4126
+ const current = this.validationErrorMessages;
4127
+ this.validationErrorMessages = { ...current, ...messages };
4128
+ }
4129
+ clearErrorMessages() {
4130
+ this.validationErrorMessages = {};
4059
4131
  }
4060
4132
  registerValidator(validator) {
4061
4133
  const { name } = validator;
@@ -5143,6 +5215,8 @@ exports.ValidationErrorMessageBuilder = ValidationErrorMessageBuilder;
5143
5215
  exports.ValidationService = ValidationService;
5144
5216
  exports.addFocusListener = addFocusListener;
5145
5217
  exports.alertScreenReader = alertScreenReader;
5218
+ exports.assertRef = assertRef;
5219
+ exports.assertSet = assertSet;
5146
5220
  exports.availableValidators = availableValidators;
5147
5221
  exports.configLogic = configLogic;
5148
5222
  exports.debounce = debounce;
package/lib/esm/index.js CHANGED
@@ -1,10 +1,3 @@
1
- /**
2
- * @public
3
- */
4
- const configLogic = {
5
- production: true,
6
- };
7
-
8
1
  /**
9
2
  * Determine if a value is empty.
10
3
  *
@@ -39,6 +32,106 @@ function isString(value) {
39
32
  return typeof value === "string" || value instanceof String;
40
33
  }
41
34
 
35
+ /**
36
+ * An error indicating that a mandatory value is not set.
37
+ *
38
+ * @public
39
+ */
40
+ class MissingValueError extends Error {
41
+ constructor(message) {
42
+ super(message);
43
+ Object.setPrototypeOf(this, MissingValueError.prototype);
44
+ }
45
+ }
46
+ /**
47
+ * Assert that a value is set and throw a {@link MissingValueError} if it is not,
48
+ * i.e., both null and undefined values will throw an error. This can be nice to
49
+ * use when transforming data between view model and rest api model.
50
+ *
51
+ * @public
52
+ * @param value - the value asserted to be set.
53
+ * @param message - an optional exception message to use if the check fails.
54
+ * @returns the value unambiguously defined.
55
+ * @throws {@link MissingValueError} if value is not set.
56
+ */
57
+ function ensureSet(value, message = "") {
58
+ if (!isSet(value)) {
59
+ throw new MissingValueError(message);
60
+ }
61
+ return value;
62
+ }
63
+
64
+ /**
65
+ * [Assertion function][ts-handbook] to assert that a nullable Vue ref holds a
66
+ * non-null value. Typically used with template refs but any nullable ref can be
67
+ * used.
68
+ *
69
+ * Throws {@link MissingValueError} if the ref holds `null` or `undefined`, or
70
+ * the ref itself is `null` or `undefined`.
71
+ *
72
+ * [ts-handbook]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
73
+ *
74
+ * @example
75
+ *
76
+ * ```ts
77
+ * const element = useTemplateRef("my-template-ref");
78
+ * // ^? Readonly<ShallowRef<HTMLElement | null>>
79
+ *
80
+ * assertRef(element);
81
+ *
82
+ * // element is now guaranteed to be a HTMLElement ref
83
+ * ```
84
+ *
85
+ * @public
86
+ * @since v6.15.0
87
+ * @param ref - the value asserted to be a ref.
88
+ * @param message - an optional exception message to use if the check fails.
89
+ * @throws {@link MissingValueError} if value is not set.
90
+ */
91
+ function assertRef(ref, message = "Expected ref to have a non-null value, but it did not") {
92
+ if (!isSet(ref?.value)) {
93
+ throw new MissingValueError(message);
94
+ }
95
+ }
96
+
97
+ /**
98
+ * [Assertion function][ts-handbook] to assert that a value is set.
99
+ * Any value that is not null or undefined is considered set including falsy values such as 0 or "".
100
+ *
101
+ * Throws {@link MissingValueError} if value is not set.
102
+ *
103
+ * [ts-handbook]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
104
+ *
105
+ * @example
106
+ *
107
+ * ```ts
108
+ * const value = getValue();
109
+ * // ^? string | null
110
+ *
111
+ * assertSet(value);
112
+ *
113
+ * // value is now guaranteed to be non-null
114
+ * ```
115
+ *
116
+ * @public
117
+ * @since v6.15.0
118
+ * @param value - the value asserted to be set.
119
+ * @param message - an optional exception message to use if the check fails.
120
+ * @throws {@link MissingValueError} if value is not set.
121
+ */
122
+ function assertSet(value, message = "Expected value to be set, but it was not") {
123
+ if (!isSet(value)) {
124
+ throw new MissingValueError(message);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * @public
130
+ */
131
+ const configLogic = {
132
+ production: true,
133
+ };
134
+
42
135
  /**
43
136
  * Implementation of Object.fromEntries() until we run a recent enough version
44
137
  * of NodeJS.
@@ -1985,35 +2078,6 @@ function deepClone(value) {
1985
2078
  return cloneDeep(value);
1986
2079
  }
1987
2080
 
1988
- /**
1989
- * An error indicating that a mandatory value is not set.
1990
- *
1991
- * @public
1992
- */
1993
- class MissingValueError extends Error {
1994
- constructor(message) {
1995
- super(message);
1996
- Object.setPrototypeOf(this, MissingValueError.prototype);
1997
- }
1998
- }
1999
- /**
2000
- * Assert that a value is set and throw a {@link MissingValueError} if it is not,
2001
- * i.e., both null and undefined values will throw an error. This can be nice to
2002
- * use when transforming data between view model and rest api model.
2003
- *
2004
- * @public
2005
- * @param value - the value asserted to be set.
2006
- * @param message - an optional exception message to use if the check fails.
2007
- * @returns the value unambiguously defined.
2008
- * @throws {@link MissingValueError} if value is not set.
2009
- */
2010
- function ensureSet(value, message = "") {
2011
- if (!isSet(value)) {
2012
- throw new MissingValueError(message);
2013
- }
2014
- return value;
2015
- }
2016
-
2017
2081
  /**
2018
2082
  * Takes an optionally nested textobject structure and convert to a flat
2019
2083
  * structure. Nested keys are joined with a dot ".". Deep nesting is supported.
@@ -4041,7 +4105,7 @@ class ValidationServiceImpl {
4041
4105
  elementValidatorsReferences = {};
4042
4106
  validationErrorMessages = {};
4043
4107
  constructor() {
4044
- this.addValidationErrorMessages(getErrorMessages());
4108
+ this.setErrorMessages(getErrorMessages());
4045
4109
  }
4046
4110
  getElementsAndValidators() {
4047
4111
  return this.elementValidatorsReferences;
@@ -4050,10 +4114,18 @@ class ValidationServiceImpl {
4050
4114
  return Object.values(this.validationStates).some((item) => item.touched === true);
4051
4115
  }
4052
4116
  addValidationErrorMessages(validationErrorMessages) {
4053
- this.validationErrorMessages = {
4054
- ...this.validationErrorMessages,
4055
- ...validationErrorMessages,
4056
- };
4117
+ this.setErrorMessages(validationErrorMessages);
4118
+ }
4119
+ setErrorMessages(messages, options = {}) {
4120
+ const { clear = false } = options;
4121
+ if (clear) {
4122
+ this.clearErrorMessages();
4123
+ }
4124
+ const current = this.validationErrorMessages;
4125
+ this.validationErrorMessages = { ...current, ...messages };
4126
+ }
4127
+ clearErrorMessages() {
4128
+ this.validationErrorMessages = {};
4057
4129
  }
4058
4130
  registerValidator(validator) {
4059
4131
  const { name } = validator;
@@ -5129,5 +5201,5 @@ if (typeof document !== "undefined") {
5129
5201
  createScreenReaderWrapper({ assertive: false });
5130
5202
  }
5131
5203
 
5132
- export { DecoratedError, index as DomUtils, ElementIdService, MissingValueError, PersistenceService, SCREEN_READER_DELAY, SimplePersistenceService, TranslationService, ValidationErrorMessageBuilder, ValidationService, addFocusListener, alertScreenReader, availableValidators, configLogic, debounce, deepClone, deleteCookie, documentOrderComparator, ensureSet, findCookie, findTabbableElements, flatten, focus, focusFirst, focusLast, formatClearingNumberForBackend, formatNumber, formatPercent, formatPersonnummer, formatPersonnummerToDate, formatPostalCode, getErrorMessages, handleTab, isEmpty, isFocusable, isInvalidDatesConfig, isInvalidWeekdaysConfig, isRadiobuttonOrCheckbox, isSet, isString, isTabbable, isValidatableFormElement, isValidatableHTMLElement, isVisible, isVisibleInViewport, normalizeDateFormat, parseBankAccountNumber, parseBankgiro, parseClearingNumber, parseDate, parseNumber, parseOrganisationsnummer, parsePercent, parsePersonnummer, parsePersonnummerLuhn, parsePlusgiro, parsePostalCode, popFocus, pushFocus, removeFocusListener, restoreFocus, saveFocus, scrollTo, setCookie, stripNull, stripWhitespace, testLuhnChecksum, validChecksum, validLimit, waitForScreenReader };
5204
+ export { DecoratedError, index as DomUtils, ElementIdService, MissingValueError, PersistenceService, SCREEN_READER_DELAY, SimplePersistenceService, TranslationService, ValidationErrorMessageBuilder, ValidationService, addFocusListener, alertScreenReader, assertRef, assertSet, availableValidators, configLogic, debounce, deepClone, deleteCookie, documentOrderComparator, ensureSet, findCookie, findTabbableElements, flatten, focus, focusFirst, focusLast, formatClearingNumberForBackend, formatNumber, formatPercent, formatPersonnummer, formatPersonnummerToDate, formatPostalCode, getErrorMessages, handleTab, isEmpty, isFocusable, isInvalidDatesConfig, isInvalidWeekdaysConfig, isRadiobuttonOrCheckbox, isSet, isString, isTabbable, isValidatableFormElement, isValidatableHTMLElement, isVisible, isVisibleInViewport, normalizeDateFormat, parseBankAccountNumber, parseBankgiro, parseClearingNumber, parseDate, parseNumber, parseOrganisationsnummer, parsePercent, parsePersonnummer, parsePersonnummerLuhn, parsePlusgiro, parsePostalCode, popFocus, pushFocus, removeFocusListener, restoreFocus, saveFocus, scrollTo, setCookie, stripNull, stripWhitespace, testLuhnChecksum, validChecksum, validLimit, waitForScreenReader };
5133
5205
  //# sourceMappingURL=index.js.map