@tuspe/components 1.6.26 → 1.7.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/README.md CHANGED
@@ -201,3 +201,54 @@ A simple `Modal` component that displays a popup with customizable content.
201
201
  title?: string
202
202
  }
203
203
  ```
204
+
205
+ ## Functions
206
+
207
+ ### calculatePreTax
208
+
209
+ Computes the pre-tax amount by subtracting the tax from the total, using the specified VAT percentage.
210
+
211
+ ### calculateTax
212
+
213
+ Calculates the tax portion of a total amount based on a given VAT percentage (default: 25.5%), ensuring precision.
214
+
215
+ ### fixNumber
216
+
217
+ Converts a number or string to a rounded number with two decimal places, ensuring precision.
218
+
219
+ ### formatPrice
220
+
221
+ Formats a number or string as a price with two decimal places, spaces as thousand separators, optional comma as a decimal separator, and an appended currency symbol (default: €).
222
+
223
+ ### handleCache
224
+
225
+ Retrieves or stores values in the cache; keys are slugified, and values are updated or returned if they exist. This ensures that `+page.ts` and other pages only retrieve data from the backend once and that the WordPress frontend uses the product list data on the product page without retrieving the same page again.
226
+
227
+ ## preventDefault
228
+
229
+ Previously, Svelte handled form submission without reloading the page. However, the `preventDefault` function is no longer supported by Svelte v5, so this function handles form submission without loading the page.
230
+
231
+ ### slugify
232
+
233
+ Converts a string to a URL-friendly format by replacing special characters, removing quotes, and replacing non-alphanumeric characters with hyphens.
234
+
235
+
236
+ ### summary
237
+
238
+ This function removes any HTML tags from a given string, then trims it to a maximum of 160 characters. If possible, it truncates at the last occurrence of a sentence-ending character (".", "!", "?"); otherwise, it appends "..." to indicate truncation.
239
+
240
+ ### validateArray
241
+
242
+ Checks if a value is an array and contains more than a specified number of items (default: 0).
243
+
244
+ ### validateEmail
245
+
246
+ Validates if a string is a properly formatted email address.
247
+
248
+ ### validateSlug
249
+
250
+ Checks if a given string is a valid slug by ensuring it matches its slugified version.
251
+
252
+ ### validateString
253
+
254
+ Ensures a string contains only allowed characters (letters, numbers, spaces, and certain symbols).
package/dist/index.d.ts CHANGED
@@ -55,3 +55,7 @@ export declare const handleCache: (key: string, value?: any) => any;
55
55
  * PREVENT DEFAULT
56
56
  */
57
57
  export declare const preventDefault: (fn: Function) => (this: any, event: any) => void;
58
+ /**
59
+ * SUMMARY
60
+ */
61
+ export declare const summary: (text: string, num?: number) => string;
package/dist/index.js CHANGED
@@ -103,3 +103,31 @@ export const preventDefault = (fn) => {
103
103
  fn.call(this, event);
104
104
  };
105
105
  };
106
+ /**
107
+ * SUMMARY
108
+ */
109
+ export const summary = (text, num = 160) => {
110
+ // Remove HTML tags
111
+ let sum = text.replace(/<[^>]*>/g, '').trim();
112
+ // If the summary is within the limit, return it directly.
113
+ if (sum.length <= num) {
114
+ return sum;
115
+ }
116
+ // Truncate the summary to the specified number of characters.
117
+ let truncatedSummary = sum.substring(0, num);
118
+ // Define end characters to find the last occurrence for truncation.
119
+ const endChars = ['.', '!', '?'];
120
+ // Variable to track if an end character is found.
121
+ let foundEndChar = false;
122
+ // Find the last occurrence of any end character within the truncated summary.
123
+ for (const char of endChars) {
124
+ const lastIndex = truncatedSummary.lastIndexOf(char);
125
+ if (lastIndex !== -1) {
126
+ truncatedSummary = truncatedSummary.substring(0, lastIndex + 1);
127
+ foundEndChar = true;
128
+ break; // Exit loop after finding the first end character.
129
+ }
130
+ }
131
+ // Return the summary. Append ellipsis only if no end character was found.
132
+ return truncatedSummary.trim() + (foundEndChar ? '' : '...');
133
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuspe/components",
3
- "version": "1.6.26",
3
+ "version": "1.7.0",
4
4
  "description": "A reusable SvelteKit component library for form elements, breadcrumbs, prices and images.",
5
5
  "keywords": [
6
6
  "svelteKit",