@onesy/utils
Version:
39 lines (38 loc) • 1.65 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = __importDefault(require("./is"));
/**
* Example:
* Input:
* getGoogleFontsURL([
* { name: 'Roboto', weights: [400, 700] },
* { name: 'Source Sans 3', weights: ['italic 200', 400, 700] },
* ]);
* Output:
* 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Source+Sans+3:ital,wght@0,200;0,700;1,200&display=swap'
*/
const getGoogleFontsURL = (value) => {
if ((0, is_1.default)('array', value)) {
let googleFontsLink = 'https://fonts.googleapis.com/css2?';
for (const font of value) {
if (!font.weights)
font.weights = [];
const hasItalic = font.weights.some((weight) => (0, is_1.default)('string', weight) && weight.indexOf('italic ') === 0);
if (!font.weights.length)
font.weights.push(400);
font.weights = font.weights.map((weight) => {
if ((0, is_1.default)('string', weight) && weight.indexOf('italic') === 0)
return weight.replace('italic ', '1,').replace(/\s/g, '');
return `${hasItalic ? '0,' : ''}${String(weight)}`;
});
font.weights.sort();
googleFontsLink += `family=${font.name.replace(/\s/g, '+')}:${hasItalic ? 'ital,' : ''}wght@${font.weights.join(';')}&`;
}
googleFontsLink += `display=swap`;
return googleFontsLink;
}
};
exports.default = getGoogleFontsURL;
;