@vini-wine/core-utils
Version:
Core package for Vini frontend utils.
148 lines (142 loc) • 6.42 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
getVintageNameConvention: () => getVintageNameConvention,
getVintageProductNameConvention: () => getVintageProductNameConvention,
getWineNameConvention: () => getWineNameConvention,
numberFormatter: () => numberFormatter,
priceFormatter: () => priceFormatter,
priceMicrosToFloat: () => priceMicrosToFloat
});
module.exports = __toCommonJS(src_exports);
// src/price-formatter/index.ts
var priceFormatter = (price, showDecimals, roundPrice) => {
const { priceMicros, currencyCode, locale } = price;
let priceFloat = priceMicrosToFloat(priceMicros, roundPrice);
let formattedPrice = new Intl.NumberFormat(locale != null ? locale : "nl", {
style: "currency",
currency: currencyCode
}).format(priceFloat);
if (!showDecimals) {
formattedPrice = formattedPrice.replace(/[.,]00$/, "");
}
return formattedPrice;
};
var priceMicrosToFloat = (price, roundPrice) => {
if (roundPrice === true) {
return Math.round(price / 1e6);
}
return price / 1e6;
};
// src/number-formatter/index.ts
var numberFormatter = (number, config = {
locale: "nl",
suffix: void 0,
prefix: void 0,
minimumFractionDigits: 0,
maximumFractionDigits: 0,
millionSuffix: "M",
thousandSuffix: "K",
showAsMillion: false,
showAsThousand: false
}) => {
let formattedNumber = "";
const formatNumber = (value) => {
return new Intl.NumberFormat(config.locale, {
minimumFractionDigits: config.minimumFractionDigits,
maximumFractionDigits: config.maximumFractionDigits
}).format(value);
};
if (config.showAsMillion && Math.abs(number) >= 1e6) {
formattedNumber = formatNumber(Math.round(number / 1e6)) + (config.millionSuffix || "M");
} else if (config.showAsThousand && Math.abs(number) >= 1e3) {
formattedNumber = formatNumber(Math.round(number / 1e3)) + (config.thousandSuffix || "K");
} else {
formattedNumber = formatNumber(number);
}
if (config.prefix !== void 0) {
formattedNumber = `${config.prefix}${formattedNumber}`;
}
if (config.suffix !== void 0) {
formattedNumber = `${formattedNumber}${config.suffix}`;
}
return formattedNumber;
};
// src/wine-name-convention/index.ts
var getWineNameConvention = (wine) => {
var _a, _b;
const wineRegion = wine.region;
let wineRegionCountry = void 0;
if (wineRegion && "country" in wineRegion) {
wineRegionCountry = wineRegion.country;
}
const wineryName = (_a = wine == null ? void 0 : wine.winery) == null ? void 0 : _a.name;
const wineName = wine.name;
const wineTypeName = (_b = wine == null ? void 0 : wine.wineType) == null ? void 0 : _b.name;
const regionName = wineRegion == null ? void 0 : wineRegion.name;
const countryName = wineRegionCountry ? `, ${wineRegionCountry.name}` : "";
return `${wineryName} ${wineName} ${wineTypeName}, ${regionName}${countryName}`;
};
// src/vintage-name-convention/index.ts
var getVintageNameConvention = (vintage) => {
var _a, _b;
const vintageWine = vintage.wine;
const vintageRegion = vintageWine == null ? void 0 : vintageWine.region;
let vintageWineRegionCountry = void 0;
if (vintageRegion && "country" in vintageRegion) {
vintageWineRegionCountry = vintageRegion == null ? void 0 : vintageRegion.country;
}
const wineryName = (_a = vintageWine == null ? void 0 : vintageWine.winery) == null ? void 0 : _a.name;
const wineName = vintageWine == null ? void 0 : vintageWine.name;
const wineTypeName = (_b = vintageWine == null ? void 0 : vintageWine.wineType) == null ? void 0 : _b.name;
const vintageYear = vintage.year;
const wineRegionName = vintageRegion == null ? void 0 : vintageRegion.name;
const countryName = vintageWineRegionCountry ? `, ${vintageWineRegionCountry.name}` : "";
return `${wineryName} ${wineName} ${wineTypeName} ${vintageYear}, ${wineRegionName}${countryName}`;
};
// src/vintage-product-name-convention/index.ts
var getVintageProductNameConvention = (vintageProduct) => {
var _a, _b, _c;
const vintageProductVintage = vintageProduct.vintage;
const vintageProductVintageWine = vintageProductVintage == null ? void 0 : vintageProductVintage.wine;
const vintageProductVintageWineRegion = (_a = vintageProductVintage == null ? void 0 : vintageProductVintage.wine) == null ? void 0 : _a.region;
let vintageProductVintageWineRegionCountry = void 0;
if (vintageProductVintageWineRegion && "country" in vintageProductVintageWineRegion) {
vintageProductVintageWineRegionCountry = vintageProductVintageWineRegion.country;
}
const wineryName = (_b = vintageProductVintageWine == null ? void 0 : vintageProductVintageWine.winery) == null ? void 0 : _b.name;
const wineName = vintageProductVintageWine == null ? void 0 : vintageProductVintageWine.name;
const wineTypeName = (_c = vintageProductVintageWine == null ? void 0 : vintageProductVintageWine.wineType) == null ? void 0 : _c.name;
const vintageYear = vintageProductVintage == null ? void 0 : vintageProductVintage.year;
const regionName = vintageProductVintageWineRegion == null ? void 0 : vintageProductVintageWineRegion.name;
const countryName = vintageProductVintageWineRegionCountry ? `, ${vintageProductVintageWineRegionCountry.name}` : "";
return `${wineryName} ${wineName} ${wineTypeName} ${vintageYear} size, ${regionName}${countryName}`;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getVintageNameConvention,
getVintageProductNameConvention,
getWineNameConvention,
numberFormatter,
priceFormatter,
priceMicrosToFloat
});
//# sourceMappingURL=index.js.map