UNPKG

@vini-wine/core-utils

Version:

Core package for Vini frontend utils.

1 lines 9.47 kB
{"version":3,"sources":["../src/price-formatter/index.ts","../src/number-formatter/index.ts","../src/wine-name-convention/index.ts","../src/vintage-name-convention/index.ts","../src/vintage-product-name-convention/index.ts"],"sourcesContent":["import { CurrencyCodeEnum, LocaleEnum } from \"@vini-wine/core-enums\";\n\nexport const priceFormatter = (\n price: {\n priceMicros: number;\n currencyCode: CurrencyCodeEnum;\n locale?: LocaleEnum;\n },\n showDecimals: boolean,\n roundPrice: boolean,\n) => {\n // Extracting data from the price object\n const { priceMicros, currencyCode, locale } = price;\n\n let priceFloat = priceMicrosToFloat(priceMicros, roundPrice);\n\n // Formatting price based on locale, currency, and amount\n let formattedPrice = new Intl.NumberFormat(locale ?? \"nl\", {\n style: \"currency\",\n currency: currencyCode,\n }).format(priceFloat);\n\n if (!showDecimals) {\n formattedPrice = formattedPrice.replace(/[.,]00$/, \"\");\n }\n return formattedPrice;\n};\n\nexport const priceMicrosToFloat = (price: number, roundPrice: boolean) => {\n if (roundPrice === true) {\n return Math.round(price / 1000000);\n }\n return price / 1000000;\n};\n","export const numberFormatter = (\n number: number,\n config: {\n locale?: string;\n suffix?: string | undefined;\n prefix?: string | undefined;\n minimumFractionDigits?: number;\n maximumFractionDigits?: number;\n millionSuffix?: string | undefined;\n thousandSuffix?: string | undefined;\n showAsMillion?: boolean;\n showAsThousand?: boolean;\n } = {\n locale: \"nl\",\n suffix: undefined,\n prefix: undefined,\n minimumFractionDigits: 0,\n maximumFractionDigits: 0,\n millionSuffix: \"M\",\n thousandSuffix: \"K\",\n showAsMillion: false,\n showAsThousand: false,\n },\n) => {\n let formattedNumber: string = \"\";\n\n // Function to format the number based on the configuration\n const formatNumber = (value: number): string => {\n return new Intl.NumberFormat(config.locale, {\n minimumFractionDigits: config.minimumFractionDigits,\n maximumFractionDigits: config.maximumFractionDigits,\n }).format(value);\n };\n\n // Check if showing as million is enabled and the number is greater than or equal to 1 million\n if (config.showAsMillion && Math.abs(number) >= 1e6) {\n formattedNumber =\n formatNumber(Math.round(number / 1e6)) + (config.millionSuffix || \"M\");\n }\n // Check if showing as thousand is enabled and the number is greater than or equal to 1 thousand\n else if (config.showAsThousand && Math.abs(number) >= 1e3) {\n formattedNumber =\n formatNumber(Math.round(number / 1e3)) + (config.thousandSuffix || \"K\");\n }\n // Otherwise, format the number as is\n else {\n formattedNumber = formatNumber(number);\n }\n\n // Append prefix and suffix if provided\n if (config.prefix !== undefined) {\n formattedNumber = `${config.prefix}${formattedNumber}`;\n }\n\n if (config.suffix !== undefined) {\n formattedNumber = `${formattedNumber}${config.suffix}`;\n }\n\n return formattedNumber;\n};\n","import { WineModel as AdminWineModel } from \"@vini-wine/admin-core-models\";\nimport { WineModel as AnalyticWineModel } from \"@vini-wine/analytic-core-models\";\nimport { WineModel as OfferWineModel } from \"@vini-wine/offer-core-models\";\nimport { WineModel as ContentWineModel } from \"@vini-wine/content-core-models\";\n\ntype WineModels =\n | AdminWineModel\n | AnalyticWineModel\n | OfferWineModel\n | ContentWineModel;\nexport const getWineNameConvention = (wine: WineModels): string => {\n const wineRegion = wine.region;\n let wineRegionCountry = undefined;\n // if the country is present in the wine region\n if (wineRegion && \"country\" in wineRegion) {\n wineRegionCountry = wineRegion.country;\n }\n\n const wineryName = wine?.winery?.name;\n const wineName = wine.name;\n const wineTypeName = wine?.wineType?.name;\n const regionName = wineRegion?.name;\n\n const countryName = wineRegionCountry ? `, ${wineRegionCountry.name}` : \"\";\n\n return `${wineryName} ${wineName} ${wineTypeName}, ${regionName}${countryName}`;\n};\n","import { VintageModel as AdminVintageModel } from \"@vini-wine/admin-core-models\";\nimport { VintageModel as OfferVintageModel } from \"@vini-wine/offer-core-models\";\nimport { VintageModel as ContentVintageModel } from \"@vini-wine/content-core-models\";\nimport { VintageModel as AnalyticVintageModel } from \"@vini-wine/analytic-core-models\";\n\ntype VintageModels =\n | AdminVintageModel\n | OfferVintageModel\n | ContentVintageModel\n | AnalyticVintageModel;\n\nexport const getVintageNameConvention = (vintage: VintageModels): string => {\n const vintageWine = vintage.wine;\n const vintageRegion = vintageWine?.region;\n let vintageWineRegionCountry = undefined;\n if (vintageRegion && \"country\" in vintageRegion) {\n vintageWineRegionCountry = vintageRegion?.country;\n }\n\n const wineryName = vintageWine?.winery?.name;\n const wineName = vintageWine?.name;\n const wineTypeName = vintageWine?.wineType?.name;\n const vintageYear = vintage.year;\n const wineRegionName = vintageRegion?.name;\n const countryName = vintageWineRegionCountry\n ? `, ${vintageWineRegionCountry.name}`\n : \"\";\n\n return `${wineryName} ${wineName} ${wineTypeName} ${vintageYear}, ${wineRegionName}${countryName}`;\n};\n","import { VintageProductModel as AdminVintageProductModel } from \"@vini-wine/admin-core-models\";\nimport { VintageProductModel as OfferVintageProductModel } from \"@vini-wine/offer-core-models\";\nimport { VintageProductModel as AnalyticVintageProductModel } from \"@vini-wine/analytic-core-models\";\n\ntype VintageProductModels =\n | AdminVintageProductModel\n | OfferVintageProductModel\n | AnalyticVintageProductModel;\nexport const getVintageProductNameConvention = (\n vintageProduct: VintageProductModels,\n): string => {\n const vintageProductVintage = vintageProduct.vintage;\n const vintageProductVintageWine = vintageProductVintage?.wine;\n const vintageProductVintageWineRegion = vintageProductVintage?.wine?.region;\n let vintageProductVintageWineRegionCountry = undefined;\n if (\n vintageProductVintageWineRegion &&\n \"country\" in vintageProductVintageWineRegion\n ) {\n vintageProductVintageWineRegionCountry =\n vintageProductVintageWineRegion.country;\n }\n\n const wineryName = vintageProductVintageWine?.winery?.name;\n const wineName = vintageProductVintageWine?.name;\n const wineTypeName = vintageProductVintageWine?.wineType?.name;\n const vintageYear = vintageProductVintage?.year;\n // size units formatter here\n const regionName = vintageProductVintageWineRegion?.name;\n const countryName = vintageProductVintageWineRegionCountry\n ? `, ${vintageProductVintageWineRegionCountry.name}`\n : \"\";\n return `${wineryName} ${wineName} ${wineTypeName} ${vintageYear} size, ${regionName}${countryName}`;\n};\n"],"mappings":";AAEO,IAAM,iBA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