@streamscloud/kit 0.2.17 → 0.2.19

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.
@@ -10,6 +10,7 @@ export * from './html-helper';
10
10
  export * from './image-preloader';
11
11
  export * from './lazy-init';
12
12
  export * from './number-helper';
13
+ export * from './price-helper';
13
14
  export * from './string-generator';
14
15
  export * from './string-helper';
15
16
  export * from './url-helper';
@@ -10,6 +10,7 @@ export * from './html-helper';
10
10
  export * from './image-preloader';
11
11
  export * from './lazy-init';
12
12
  export * from './number-helper';
13
+ export * from './price-helper';
13
14
  export * from './string-generator';
14
15
  export * from './string-helper';
15
16
  export * from './url-helper';
@@ -0,0 +1,13 @@
1
+ type Currency = 'EUR' | 'NOK' | 'USD';
2
+ type CurrencyMode = 'none' | 'symbol' | 'code';
3
+ export type PriceRepresentationOptions = {
4
+ currencyMode?: CurrencyMode;
5
+ missingFractionMode?: 'hide' | 'currency-placeholder' | 'zeros' | 'zeros-or-placeholder';
6
+ locale?: string;
7
+ };
8
+ export declare const toPriceRepresentation: (data: {
9
+ amount: number;
10
+ currency: Currency;
11
+ options?: PriceRepresentationOptions;
12
+ }) => string;
13
+ export {};
@@ -0,0 +1,74 @@
1
+ import { Utils } from './utils';
2
+ export const toPriceRepresentation = (data) => {
3
+ const { amount, currency, options } = data;
4
+ const { currencyMode = 'symbol', missingFractionMode = 'zeros-or-placeholder', locale = 'nb-NO' } = options ?? {};
5
+ const cfg = currencyDefaults[currency];
6
+ // Helper to attach currency per mode
7
+ const decorateWithCurrency = (value) => {
8
+ switch (currencyMode) {
9
+ case 'symbol':
10
+ return `${cfg.symbol} ${value}`;
11
+ case 'code':
12
+ return `${value} ${cfg.code}`;
13
+ case 'none':
14
+ return `${value}`;
15
+ default:
16
+ Utils.assertUnreachable(currencyMode);
17
+ }
18
+ };
19
+ // If amount has a fractional part, show localized number with 2 fraction digits
20
+ if (hasFraction(amount)) {
21
+ const nf = new Intl.NumberFormat(locale, {
22
+ minimumFractionDigits: 2,
23
+ maximumFractionDigits: 2
24
+ });
25
+ return decorateWithCurrency(nf.format(amount));
26
+ }
27
+ // Fraction is missing: build numeric part according to the selected missingFractionMode
28
+ const formattedInteger = formatIntegerWithGrouping(amount, locale);
29
+ const decimalSeparator = getDecimalSeparator(locale);
30
+ switch (missingFractionMode) {
31
+ case 'hide':
32
+ return decorateWithCurrency(formattedInteger);
33
+ case 'currency-placeholder':
34
+ if (cfg.missingFractionPlaceholder) {
35
+ return decorateWithCurrency(`${formattedInteger}${decimalSeparator}${cfg.missingFractionPlaceholder}`);
36
+ }
37
+ else {
38
+ return decorateWithCurrency(formattedInteger);
39
+ }
40
+ case 'zeros':
41
+ return decorateWithCurrency(`${formattedInteger}${decimalSeparator}00`);
42
+ case 'zeros-or-placeholder':
43
+ if (cfg.missingFractionPlaceholder) {
44
+ return decorateWithCurrency(`${formattedInteger}${decimalSeparator}${cfg.missingFractionPlaceholder}`);
45
+ }
46
+ else {
47
+ return decorateWithCurrency(`${formattedInteger}${decimalSeparator}00`);
48
+ }
49
+ default:
50
+ Utils.assertUnreachable(missingFractionMode);
51
+ }
52
+ };
53
+ const currencyDefaults = {
54
+ ['NOK']: { symbol: 'kr', code: 'NOK', missingFractionPlaceholder: '-' },
55
+ ['USD']: { symbol: '$', code: 'USD' },
56
+ ['EUR']: { symbol: '€', code: 'EUR' }
57
+ };
58
+ const getDecimalSeparator = (locale) => {
59
+ const nf = new Intl.NumberFormat(locale);
60
+ const parts = nf.formatToParts(1.1);
61
+ const decimalPart = parts.find((p) => p.type === 'decimal');
62
+ return decimalPart ? decimalPart.value : '.';
63
+ };
64
+ const formatIntegerWithGrouping = (amount, locale) => {
65
+ const nf = new Intl.NumberFormat(locale, {
66
+ minimumFractionDigits: 0,
67
+ maximumFractionDigits: 0,
68
+ useGrouping: true
69
+ });
70
+ return nf.format(Math.trunc(amount));
71
+ };
72
+ const hasFraction = (amount) => {
73
+ return Math.abs(amount % 1) > Number.EPSILON;
74
+ };
@@ -1,5 +1,10 @@
1
1
  <script lang="ts">import { HtmlHelper } from '../../core/utils/html-helper';
2
+ import { onMount } from 'svelte';
2
3
  let { src, color = null } = $props();
4
+ let mounted = $state(false);
5
+ onMount(() => {
6
+ mounted = true;
7
+ });
3
8
  </script>
4
9
 
5
10
  {#if src?.startsWith('<svg')}
@@ -12,7 +17,9 @@ let { src, color = null } = $props();
12
17
  class:icon--red={color === 'red'}
13
18
  class:icon--orange={color === 'orange'}
14
19
  class:icon--blue={color === 'blue'}>
15
- {@html HtmlHelper.sanitizeSvg(src)}
20
+ {#if mounted}
21
+ {@html HtmlHelper.sanitizeSvg(src)}
22
+ {/if}
16
23
  </div>
17
24
  {:else}
18
25
  <i class="invalid-svg">Invalid SVG</i>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/kit",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "author": "StreamsCloud",
5
5
  "repository": {
6
6
  "type": "git",