UNPKG

react-native-unit-components

Version:

Unit React Native components

71 lines (67 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFontName = void 0; var _shared = require("../types/shared"); var _UnitSdkManager = require("../unitSdkManager/UnitSdkManager"); const getFontWeightName = (familyFonts, fontWeight) => { /** * Given family variants and the required weight * Return the closet font that defined by the client. */ if (!fontWeight || fontWeight === 'normal') return 'Regular'; if (fontWeight === 'bold') return 'Bold'; const fontWeightNumber = Number(fontWeight); // Select the closet variant let selectedFontWeight = _shared.FontWeight.Regular.valueOf(); let minDistance = 1000; familyFonts.forEach(fontData => { const currDistance = Math.abs(fontData.fontWeight - fontWeightNumber); if (currDistance < minDistance) { minDistance = currDistance; selectedFontWeight = fontData.fontWeight.valueOf(); } }); // FontWeight to String switch (selectedFontWeight) { case _shared.FontWeight.Thin: return 'Thin'; case _shared.FontWeight.ExtraLight: return 'ExtraLight'; case _shared.FontWeight.Light: return 'Light'; case _shared.FontWeight.Regular: return 'Regular'; case _shared.FontWeight.Medium: return 'Medium'; case _shared.FontWeight.SemiBold: return 'SemiBold'; case _shared.FontWeight.Bold: return 'Bold'; case _shared.FontWeight.ExtraBold: return 'ExtraBold'; case _shared.FontWeight.Black: return 'Black'; default: return 'Regular'; } }; const getFontName = (fontFamily, fontWeight) => { /* * Get the font family name and the font weight name from the THEME object. * Then, return a possible font. */ const fontsInApp = _UnitSdkManager.UnitSDK.getFonts(); if (!fontsInApp) { throw new Error('Fonts are not found in the app'); } const familyFonts = fontsInApp[fontFamily]; if (!familyFonts) { throw new Error(`Font family ${fontFamily} not found in the app's fonts`); } const fontWeightName = getFontWeightName(familyFonts, fontWeight); return `${fontFamily}-${fontWeightName}`; }; exports.getFontName = getFontName; //# sourceMappingURL=fonts.js.map