@brandlab-digital/site-core 3.7.0 → 3.9.0

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.
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The site strategy record (3.8.0). One literal per site, in `src/data/strategy.ts`, that the checks read.
3
+ *
4
+ * This release holds what check D4 (same-trade distinctness) needs: the site's own look levers and the same-trade
5
+ * competitors with theirs. `navigation` (3.9.0) is what Romie recorded for the menu: an exact order, labels outside the
6
+ * closed list, whether Areas is a dropdown. Later releases add phrases, tiers and the wave schedule to the same record.
7
+ *
8
+ * The levers are recorded values, not measurements: the person building the look writes them down, and D4 compares
9
+ * the records. A shared display font counts as the same lever, and an action hue within 20 degrees counts as the same.
10
+ * Card style, photo treatment and the home sections are the same only when every part of them matches.
11
+ */
12
+ /** The seven look levers D4 compares (Site System v2 §7.7), in the order the record lists them. */
13
+ export const SITE_LOOK_LEVERS = [
14
+ 'displayFont',
15
+ 'actionHue',
16
+ 'heroVariant',
17
+ 'headerVariant',
18
+ 'cardStyle',
19
+ 'photoTreatment',
20
+ 'homeSections',
21
+ ];
22
+ const DOMAIN = /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/;
23
+ function assertText(value, field, maximum = 160) {
24
+ if (typeof value !== 'string' ||
25
+ !value.trim() ||
26
+ value.trim() !== value ||
27
+ value.length > maximum ||
28
+ /[\u0000-\u001f\u007f]/.test(value)) {
29
+ throw new Error(`${field} must be bounded, trimmed text.`);
30
+ }
31
+ }
32
+ function assertKnownKeys(value, allowed, field) {
33
+ if (!value ||
34
+ typeof value !== 'object' ||
35
+ Array.isArray(value) ||
36
+ ![Object.prototype, null].includes(Object.getPrototypeOf(value))) {
37
+ throw new Error(`${field} must be a plain object.`);
38
+ }
39
+ const unknown = Object.keys(value).find((key) => !allowed.includes(key));
40
+ if (unknown)
41
+ throw new Error(`${field}.${unknown} is not a supported strategy key.`);
42
+ }
43
+ function assertExactKeys(value, allowed, field, optional = []) {
44
+ assertKnownKeys(value, allowed, field);
45
+ const missing = allowed.find((key) => !optional.includes(key) && !Object.hasOwn(value, key));
46
+ if (missing)
47
+ throw new Error(`${field}.${missing} is required.`);
48
+ }
49
+ function assertTextList(values, field, minimum) {
50
+ if (!Array.isArray(values) || values.length < minimum) {
51
+ throw new Error(`${field} must be a list${minimum > 0 ? ' with at least one entry' : ''}.`);
52
+ }
53
+ values.forEach((value, index) => assertText(value, `${field}[${index}]`, 80));
54
+ const duplicate = values.find((value, index) => values.indexOf(value) !== index);
55
+ if (duplicate !== undefined)
56
+ throw new Error(`${field} lists "${duplicate}" more than once.`);
57
+ }
58
+ function assertLook(look, field) {
59
+ assertExactKeys(look, SITE_LOOK_LEVERS, field);
60
+ assertText(look.displayFont, `${field}.displayFont`);
61
+ if (typeof look.actionHue !== 'number' || !Number.isFinite(look.actionHue) || look.actionHue < 0 || look.actionHue >= 360) {
62
+ throw new Error(`${field}.actionHue must be a hue in degrees from 0 up to, but not including, 360.`);
63
+ }
64
+ assertText(look.heroVariant, `${field}.heroVariant`);
65
+ assertText(look.headerVariant, `${field}.headerVariant`);
66
+ assertExactKeys(look.cardStyle, ['radius', 'border', 'shadow'], `${field}.cardStyle`);
67
+ for (const token of ['radius', 'border', 'shadow'])
68
+ assertText(look.cardStyle[token], `${field}.cardStyle.${token}`);
69
+ assertExactKeys(look.photoTreatment, ['cropRatio', 'scrim', 'frame'], `${field}.photoTreatment`);
70
+ for (const token of ['cropRatio', 'scrim', 'frame'])
71
+ assertText(look.photoTreatment[token], `${field}.photoTreatment.${token}`);
72
+ assertExactKeys(look.homeSections, ['sectionOrder', 'signatures'], `${field}.homeSections`);
73
+ assertTextList(look.homeSections.sectionOrder, `${field}.homeSections.sectionOrder`, 1);
74
+ assertTextList(look.homeSections.signatures, `${field}.homeSections.signatures`, 0);
75
+ }
76
+ const RECORDED_DATE = /^\d{4}-\d{2}-\d{2}$/;
77
+ function assertNavigation(navigation, field) {
78
+ const allowed = ['order', 'labels', 'areas', 'setBy', 'recordedOn'];
79
+ assertExactKeys(navigation, allowed, field, ['order', 'labels', 'areas']);
80
+ assertText(navigation.setBy, `${field}.setBy`);
81
+ if (typeof navigation.recordedOn !== 'string' || !RECORDED_DATE.test(navigation.recordedOn) || Number.isNaN(Date.parse(navigation.recordedOn))) {
82
+ throw new Error(`${field}.recordedOn must be the date it was recorded, as YYYY-MM-DD.`);
83
+ }
84
+ if (navigation.order !== undefined)
85
+ assertTextList(navigation.order, `${field}.order`, 1);
86
+ if (navigation.labels !== undefined) {
87
+ assertExactKeys(navigation.labels, navigation.labels && typeof navigation.labels === 'object' ? Object.keys(navigation.labels) : [], `${field}.labels`);
88
+ for (const [key, label] of Object.entries(navigation.labels)) {
89
+ assertText(key, `${field}.labels key`, 80);
90
+ assertText(label, `${field}.labels.${key}`, 80);
91
+ }
92
+ }
93
+ if (navigation.areas !== undefined && navigation.areas !== 'link' && navigation.areas !== 'menu') {
94
+ throw new Error(`${field}.areas must be 'link' or 'menu'.`);
95
+ }
96
+ }
97
+ /** Validates a site strategy record and returns it with its literal types kept. */
98
+ export function defineSiteStrategy(strategy) {
99
+ assertKnownKeys(strategy, ['competitors', 'look', 'navigation'], 'strategy');
100
+ if (strategy.navigation !== undefined)
101
+ assertNavigation(strategy.navigation, 'strategy.navigation');
102
+ if (strategy.look !== undefined)
103
+ assertLook(strategy.look, 'strategy.look');
104
+ if (strategy.competitors === undefined)
105
+ return strategy;
106
+ if (!Array.isArray(strategy.competitors))
107
+ throw new Error('strategy.competitors must be a list.');
108
+ strategy.competitors.forEach((competitor, index) => {
109
+ const field = `strategy.competitors[${index}]`;
110
+ assertExactKeys(competitor, ['name', 'domain', 'trade', 'look'], field);
111
+ assertText(competitor.name, `${field}.name`);
112
+ assertText(competitor.trade, `${field}.trade`);
113
+ if (typeof competitor.domain !== 'string' || !DOMAIN.test(competitor.domain)) {
114
+ throw new Error(`${field}.domain must be a bare lowercase domain such as "example.com.au".`);
115
+ }
116
+ assertLook(competitor.look, `${field}.look`);
117
+ });
118
+ const domains = strategy.competitors.map((competitor) => competitor.domain);
119
+ const duplicate = domains.find((domain, index) => domains.indexOf(domain) !== index);
120
+ if (duplicate)
121
+ throw new Error(`strategy.competitors lists "${duplicate}" more than once.`);
122
+ return strategy;
123
+ }
124
+ //# sourceMappingURL=strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.js","sourceRoot":"","sources":["../src/strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,mGAAmG;AACnG,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,aAAa;IACb,WAAW;IACX,aAAa;IACb,eAAe;IACf,WAAW;IACX,gBAAgB;IAChB,cAAc;CACN,CAAA;AAsEV,MAAM,MAAM,GAAG,mDAAmD,CAAA;AAElE,SAAS,UAAU,CAAC,KAAc,EAAE,KAAa,EAAE,OAAO,GAAG,GAAG;IAC9D,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CAAC,IAAI,EAAE;QACb,KAAK,CAAC,IAAI,EAAE,KAAK,KAAK;QACtB,KAAK,CAAC,MAAM,GAAG,OAAO;QACtB,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,EACnC,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,iCAAiC,CAAC,CAAA;IAC5D,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,KAAc,EAAE,OAA0B,EAAE,KAAa;IAChF,IACE,CAAC,KAAK;QACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACpB,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,EAChE,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,0BAA0B,CAAC,CAAA;IACrD,CAAC;IACD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y record (3.8.0). One literal per site, in `src/data/strategy.ts`, that the checks read.\n *\n * This release holds what check D4 (same-trade distinctness) needs: the site's own look levers and the same-trade\n * competitors with theirs. `navigation` (3.9.0) is what Romie recorded for the menu: an exact order, labels outside the\n * closed list, whether Areas is a dropdown. Later releases add phrases, tiers and the wave schedule to the same record.\n *\n * The levers are recorded values, not measurements: the person building the look writes them down, and D4 compares\n * the records. A shared display font counts as the same lever, and an action hue within 20 degrees counts as the same.\n * Card style, photo treatment and the home sections are the same only when every part of them matches.\n */\n\nimport type { BrandlabRecordedNavigation } from './navigation.js'\n\n/** The seven look levers D4 compares (Site System v2 §7.7), in the order the record lists them. */\nexport const SITE_LOOK_LEVERS = [\n 'displayFont',\n 'actionHue',\n 'heroVariant',\n 'headerVariant',\n 'cardStyle',\n 'photoTreatment',\n 'homeSections',\n] as const\n\nexport type SiteLookLever = (typeof SITE_LOOK_LEVERS)[number]\n\n/** The card tokens the site's cards use. Two card styles are the same only when all three match. */\nexport interface SiteCardStyle {\n /** The corner radius token value, for example \"16px\" or \"0\". */\n radius: string\n /** The border token value, for example \"1px solid line\" or \"none\". */\n border: string\n /** The shadow token value, for example \"soft\" or \"none\". */\n shadow: string\n}\n\n/** How the site treats photographs. Two treatments are the same only when all three match. */\nexport interface SitePhotoTreatment {\n /** The crop ratio, for example \"4:3\" or \"16:9\". */\n cropRatio: string\n /** The scrim over photographs, for example \"light gradient\" or \"none\". */\n scrim: string\n /** The frame around photographs, for example \"rounded\" or \"square\". */\n frame: string\n}\n\n/** The home page's section order and its signature sections. */\nexport interface SiteHomeSections {\n /** The home page's sections in order, by section id. */\n sectionOrder: readonly string[]\n /** The sections or elements that make the home page recognisable, for example \"grass-edge divider\". May be empty. */\n signatures: readonly string[]\n}\n\n/** One site's look, as recorded. */\nexport interface SiteLook {\n /** The display (heading) font family, for example \"Plus Jakarta Sans\". */\n displayFont: string\n /** The hue of the main action colour in degrees: 0 up to, not including, 360. */\n actionHue: number\n /** The hero variant the site uses, as its skin names it. */\n heroVariant: string\n /** The header variant the site uses, as its skin names it. */\n headerVariant: string\n cardStyle: SiteCardStyle\n photoTreatment: SitePhotoTreatment\n homeSections: SiteHomeSections\n}\n\n/** A same-trade business with an overlapping service area, and its recorded look. */\nexport interface SiteCompetitor {\n name: string\n /** The bare domain, lowercase, for example \"example.com.au\". */\n domain: string\n /** The trade it shares with this site, for example \"tree services\". */\n trade: string\n look: SiteLook\n}\n\n/**\n * Both fields are optional so a site can start the record before its look is settled; check D4 still fails a new\n * build or redesign whose record lacks either.\n */\nexport interface SiteStrategy {\n /** Same-trade competitors with overlapping regions, fellow Brandlab clients first. */\n competitors?: readonly SiteCompetitor[]\n /** This site's own look. */\n look?: SiteLook\n /** The menu order, labels or Areas dropdown Romie recorded, when he recorded one (N1 reproduces it exactly). */\n navigation?: BrandlabRecordedNavigation\n}\n\nconst DOMAIN = /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z]{2,}$/\n\nfunction assertText(value: unknown, field: string, maximum = 160) {\n if (\n typeof value !== 'string' ||\n !value.trim() ||\n value.trim() !== value ||\n value.length > maximum ||\n /[\\u0000-\\u001f\\u007f]/.test(value)\n ) {\n throw new Error(`${field} must be bounded, trimmed text.`)\n }\n}\n\nfunction assertKnownKeys(value: unknown, allowed: readonly string[], field: string) {\n if (\n !value ||\n typeof value !== 'object' ||\n Array.isArray(value) ||\n ![Object.prototype, null].includes(Object.getPrototypeOf(value))\n ) {\n throw new Error(`${field} must be a plain object.`)\n }\n const unknown = Object.keys(value).find((key) => !allowed.includes(key))\n if (unknown) throw new Error(`${field}.${unknown} is not a supported strategy key.`)\n}\n\nfunction assertExactKeys(value: unknown, allowed: readonly string[], field: string, optional: readonly string[] = []) {\n assertKnownKeys(value, allowed, field)\n const missing = allowed.find((key) => !optional.includes(key) && !Object.hasOwn(value as object, key))\n if (missing) throw new Error(`${field}.${missing} is required.`)\n}\n\nfunction assertTextList(values: unknown, field: string, minimum: number) {\n if (!Array.isArray(values) || values.length < minimum) {\n throw new Error(`${field} must be a list${minimum > 0 ? ' with at least one entry' : ''}.`)\n }\n values.forEach((value, index) => assertText(value, `${field}[${index}]`, 80))\n const duplicate = values.find((value, index) => values.indexOf(value) !== index)\n if (duplicate !== undefined) throw new Error(`${field} lists \"${duplicate}\" more than once.`)\n}\n\nfunction assertLook(look: SiteLook, field: string) {\n assertExactKeys(look, SITE_LOOK_LEVERS, field)\n assertText(look.displayFont, `${field}.displayFont`)\n if (typeof look.actionHue !== 'number' || !Number.isFinite(look.actionHue) || look.actionHue < 0 || look.actionHue >= 360) {\n throw new Error(`${field}.actionHue must be a hue in degrees from 0 up to, but not including, 360.`)\n }\n assertText(look.heroVariant, `${field}.heroVariant`)\n assertText(look.headerVariant, `${field}.headerVariant`)\n assertExactKeys(look.cardStyle, ['radius', 'border', 'shadow'], `${field}.cardStyle`)\n for (const token of ['radius', 'border', 'shadow'] as const) assertText(look.cardStyle[token], `${field}.cardStyle.${token}`)\n assertExactKeys(look.photoTreatment, ['cropRatio', 'scrim', 'frame'], `${field}.photoTreatment`)\n for (const token of ['cropRatio', 'scrim', 'frame'] as const) assertText(look.photoTreatment[token], `${field}.photoTreatment.${token}`)\n assertExactKeys(look.homeSections, ['sectionOrder', 'signatures'], `${field}.homeSections`)\n assertTextList(look.homeSections.sectionOrder, `${field}.homeSections.sectionOrder`, 1)\n assertTextList(look.homeSections.signatures, `${field}.homeSections.signatures`, 0)\n}\n\nconst RECORDED_DATE = /^\\d{4}-\\d{2}-\\d{2}$/\n\nfunction assertNavigation(navigation: BrandlabRecordedNavigation, field: string) {\n const allowed = ['order', 'labels', 'areas', 'setBy', 'recordedOn']\n assertExactKeys(navigation, allowed, field, ['order', 'labels', 'areas'])\n assertText(navigation.setBy, `${field}.setBy`)\n if (typeof navigation.recordedOn !== 'string' || !RECORDED_DATE.test(navigation.recordedOn) || Number.isNaN(Date.parse(navigation.recordedOn))) {\n throw new Error(`${field}.recordedOn must be the date it was recorded, as YYYY-MM-DD.`)\n }\n if (navigation.order !== undefined) assertTextList(navigation.order, `${field}.order`, 1)\n if (navigation.labels !== undefined) {\n assertExactKeys(navigation.labels, navigation.labels && typeof navigation.labels === 'object' ? Object.keys(navigation.labels) : [], `${field}.labels`)\n for (const [key, label] of Object.entries(navigation.labels)) {\n assertText(key, `${field}.labels key`, 80)\n assertText(label, `${field}.labels.${key}`, 80)\n }\n }\n if (navigation.areas !== undefined && navigation.areas !== 'link' && navigation.areas !== 'menu') {\n throw new Error(`${field}.areas must be 'link' or 'menu'.`)\n }\n}\n\n/** Validates a site strategy record and returns it with its literal types kept. */\nexport function defineSiteStrategy<const T extends SiteStrategy>(strategy: T): T {\n assertKnownKeys(strategy, ['competitors', 'look', 'navigation'], 'strategy')\n if (strategy.navigation !== undefined) assertNavigation(strategy.navigation, 'strategy.navigation')\n if (strategy.look !== undefined) assertLook(strategy.look, 'strategy.look')\n if (strategy.competitors === undefined) return strategy\n if (!Array.isArray(strategy.competitors)) throw new Error('strategy.competitors must be a list.')\n strategy.competitors.forEach((competitor, index) => {\n const field = `strategy.competitors[${index}]`\n assertExactKeys(competitor, ['name', 'domain', 'trade', 'look'], field)\n assertText(competitor.name, `${field}.name`)\n assertText(competitor.trade, `${field}.trade`)\n if (typeof competitor.domain !== 'string' || !DOMAIN.test(competitor.domain)) {\n throw new Error(`${field}.domain must be a bare lowercase domain such as \"example.com.au\".`)\n }\n assertLook(competitor.look, `${field}.look`)\n })\n const domains = strategy.competitors.map((competitor) => competitor.domain)\n const duplicate = domains.find((domain, index) => domains.indexOf(domain) !== index)\n if (duplicate) throw new Error(`strategy.competitors lists \"${duplicate}\" more than once.`)\n return strategy\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brandlab-digital/site-core",
3
- "version": "3.7.0",
3
+ "version": "3.9.0",
4
4
  "license": "UNLICENSED",
5
5
  "repository": {
6
6
  "type": "git",
@@ -118,6 +118,14 @@
118
118
  "./blog": {
119
119
  "types": "./dist/blog.d.ts",
120
120
  "import": "./dist/blog.js"
121
+ },
122
+ "./navigation": {
123
+ "types": "./dist/navigation.d.ts",
124
+ "import": "./dist/navigation.js"
125
+ },
126
+ "./strategy": {
127
+ "types": "./dist/strategy.d.ts",
128
+ "import": "./dist/strategy.js"
121
129
  }
122
130
  }
123
131
  }