@webround/core 0.3.2 → 0.3.3

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.
package/dist/theme.d.ts CHANGED
@@ -19,4 +19,10 @@ export declare function wrThemeHasDark(css: string): boolean;
19
19
  export declare function wrThemeStartsFromSystem(css: string): boolean;
20
20
  /** Whether the page's stylesheets have a `.dark` rule: the same definition as `wrThemeHasDark`, read from the page the visitor has (the toggle is only there when it can do something). */
21
21
  export declare function wrThemeSiteHasDark(doc: Document): boolean;
22
+ /** The weights as they are written into the theme: only what is a weight, once each, in order; nothing left is `undefined` (Google's own default). */
23
+ export declare function wrGoogleWeights(weights: string | undefined): string | undefined;
24
+ /** The `family=…` part of the address of Google's stylesheet for one family: `family=Inter:wght@400;700`, and with an italic one `family=Inter:ital,wght@0,400;1,400`. */
25
+ export declare function wrGoogleFamilyParam(family: string, weights?: string): string;
26
+ /** The weights of a family in the address of a stylesheet (what follows the `:` of `family=Inter:ital,wght@0,400;1,400`), as the theme says them. */
27
+ export declare function wrGoogleWeightsOfParam(axes: string | undefined): string | undefined;
22
28
  //# sourceMappingURL=theme.d.ts.map
package/dist/theme.js CHANGED
@@ -42,4 +42,50 @@ export function wrThemeSiteHasDark(doc) {
42
42
  return false;
43
43
  } });
44
44
  }
45
+ const GOOGLE_WEIGHT = /^(\d{1,4})(?:\.\.(\d{1,4}))?(i?)$/;
46
+ function wrGoogleWeightsParsed(weights) {
47
+ const found = new Map();
48
+ for (const part of (weights ?? '').split(';')) {
49
+ const match = GOOGLE_WEIGHT.exec(part.trim());
50
+ if (!match)
51
+ continue;
52
+ const from = Number(match[1]);
53
+ const to = match[2] === undefined ? null : Number(match[2]);
54
+ if (from < 1 || from > 1000 || (to !== null && (to > 1000 || to < from)))
55
+ continue;
56
+ const weight = { from, to: to === from ? null : to, italic: match[3] === 'i' };
57
+ found.set(wrGoogleWeightText(weight), weight);
58
+ }
59
+ // Upright first, then italic, each by weight: the order Google asks for
60
+ return [...found.values()].sort((a, b) => Number(a.italic) - Number(b.italic) || a.from - b.from);
61
+ }
62
+ const wrGoogleWeightValue = ({ from, to }) => (to === null ? `${from}` : `${from}..${to}`);
63
+ const wrGoogleWeightText = (weight) => `${wrGoogleWeightValue(weight)}${weight.italic ? 'i' : ''}`;
64
+ /** The weights as they are written into the theme: only what is a weight, once each, in order; nothing left is `undefined` (Google's own default). */
65
+ export function wrGoogleWeights(weights) {
66
+ const parsed = wrGoogleWeightsParsed(weights);
67
+ return parsed.length > 0 ? parsed.map(wrGoogleWeightText).join(';') : undefined;
68
+ }
69
+ /** The `family=…` part of the address of Google's stylesheet for one family: `family=Inter:wght@400;700`, and with an italic one `family=Inter:ital,wght@0,400;1,400`. */
70
+ export function wrGoogleFamilyParam(family, weights) {
71
+ const name = encodeURIComponent(family.trim()).replace(/%20/g, '+');
72
+ const parsed = wrGoogleWeightsParsed(weights);
73
+ if (parsed.length === 0)
74
+ return `family=${name}`;
75
+ if (parsed.some(weight => weight.italic))
76
+ return `family=${name}:ital,wght@${parsed.map(weight => `${weight.italic ? 1 : 0},${wrGoogleWeightValue(weight)}`).join(';')}`;
77
+ return `family=${name}:wght@${parsed.map(wrGoogleWeightValue).join(';')}`;
78
+ }
79
+ /** The weights of a family in the address of a stylesheet (what follows the `:` of `family=Inter:ital,wght@0,400;1,400`), as the theme says them. */
80
+ export function wrGoogleWeightsOfParam(axes) {
81
+ if (!axes)
82
+ return undefined;
83
+ if (axes.startsWith('wght@'))
84
+ return wrGoogleWeights(axes.slice('wght@'.length));
85
+ if (axes.startsWith('ital,wght@')) {
86
+ const parts = axes.slice('ital,wght@'.length).split(';').map(tuple => tuple.split(','));
87
+ return wrGoogleWeights(parts.map(([ital, value]) => `${value}${ital === '1' ? 'i' : ''}`).join(';'));
88
+ }
89
+ return undefined;
90
+ }
45
91
  //# sourceMappingURL=theme.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webround/core",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",