@guardian/commercial-core 0.32.0 → 0.33.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.
@@ -16,8 +16,9 @@ function adElementBlocked(ad) {
16
16
  ad.offsetTop === 0 ||
17
17
  ad.offsetWidth === 0 ||
18
18
  ad.clientHeight === 0 ||
19
- ad.clientWidth === 0)
19
+ ad.clientWidth === 0) {
20
20
  return true;
21
+ }
21
22
  const adStyles = window.getComputedStyle(ad);
22
23
  if (adStyles.getPropertyValue('display') === 'none')
23
24
  return true;
@@ -0,0 +1,28 @@
1
+ declare type ValidTargeting<T, K> = '' extends T ? never : [] extends T ? never : [''] extends T ? never : T extends boolean ? never : T extends NonNullable<T> ? K : never;
2
+ declare type DefinedKeys<T> = {
3
+ [K in keyof T]-?: ValidTargeting<T[K], K>;
4
+ }[keyof T];
5
+ declare type ObjectWithDefinedValues<T> = Pick<T, DefinedKeys<T>>;
6
+ /**
7
+ * Picks only keys with targeting values from an object.
8
+ * A targeting values is defined as either:
9
+ * - a non-empty string
10
+ * - an array of non-empty strings
11
+ *
12
+ * If you object is read-only, you can safely access properties on the result.
13
+ * For example:
14
+ *
15
+ * ```ts
16
+ * dirty = {
17
+ * valid: 'real',
18
+ * invalid: undefined,
19
+ * } as const;
20
+ *
21
+ * clean = pickDefinedValues(dirty);
22
+ *
23
+ * // @ts-expect-error -- you can’t access this property
24
+ * clean.invalid
25
+ * ```
26
+ */
27
+ export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => ObjectWithDefinedValues<T>;
28
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pickTargetingValues = void 0;
4
+ const libs_1 = require("@guardian/libs");
5
+ const isTargetingString = (string) => (0, libs_1.isString)(string) && string !== '';
6
+ const isTargetingArray = (array) => Array.isArray(array) && array.filter(isTargetingString).length > 0;
7
+ const isValidTargeting = (value) => {
8
+ if (isTargetingString(value))
9
+ return true;
10
+ if (isTargetingArray(value))
11
+ return true;
12
+ return false;
13
+ };
14
+ /**
15
+ * Picks only keys with targeting values from an object.
16
+ * A targeting values is defined as either:
17
+ * - a non-empty string
18
+ * - an array of non-empty strings
19
+ *
20
+ * If you object is read-only, you can safely access properties on the result.
21
+ * For example:
22
+ *
23
+ * ```ts
24
+ * dirty = {
25
+ * valid: 'real',
26
+ * invalid: undefined,
27
+ * } as const;
28
+ *
29
+ * clean = pickDefinedValues(dirty);
30
+ *
31
+ * // @ts-expect-error -- you can’t access this property
32
+ * clean.invalid
33
+ * ```
34
+ */
35
+ const pickTargetingValues = (obj) => {
36
+ const initialValue = {};
37
+ return Object.entries(obj).reduce((valid, [key, value]) => {
38
+ if (isValidTargeting(value)) {
39
+ // @ts-expect-error -- isValidTargeting checks this
40
+ valid[key] = value;
41
+ }
42
+ return valid;
43
+ }, initialValue);
44
+ };
45
+ exports.pickTargetingValues = pickTargetingValues;
@@ -44,8 +44,9 @@ const experimentsTargeting = ({ clientSideParticipations, serverSideParticipatio
44
44
  const serverSideExperiments = Object.entries(serverSideParticipations)
45
45
  .map((test) => testToParams(...test))
46
46
  .filter(libs_1.isString);
47
- if (clientSideExperiment.length + serverSideExperiments.length === 0)
47
+ if (clientSideExperiment.length + serverSideExperiments.length === 0) {
48
48
  return null;
49
+ }
49
50
  return [...clientSideExperiment, ...serverSideExperiments];
50
51
  };
51
52
  /* -- Targeting -- */
@@ -13,8 +13,9 @@ function adElementBlocked(ad) {
13
13
  ad.offsetTop === 0 ||
14
14
  ad.offsetWidth === 0 ||
15
15
  ad.clientHeight === 0 ||
16
- ad.clientWidth === 0)
16
+ ad.clientWidth === 0) {
17
17
  return true;
18
+ }
18
19
  const adStyles = window.getComputedStyle(ad);
19
20
  if (adStyles.getPropertyValue('display') === 'none')
20
21
  return true;
@@ -0,0 +1,28 @@
1
+ declare type ValidTargeting<T, K> = '' extends T ? never : [] extends T ? never : [''] extends T ? never : T extends boolean ? never : T extends NonNullable<T> ? K : never;
2
+ declare type DefinedKeys<T> = {
3
+ [K in keyof T]-?: ValidTargeting<T[K], K>;
4
+ }[keyof T];
5
+ declare type ObjectWithDefinedValues<T> = Pick<T, DefinedKeys<T>>;
6
+ /**
7
+ * Picks only keys with targeting values from an object.
8
+ * A targeting values is defined as either:
9
+ * - a non-empty string
10
+ * - an array of non-empty strings
11
+ *
12
+ * If you object is read-only, you can safely access properties on the result.
13
+ * For example:
14
+ *
15
+ * ```ts
16
+ * dirty = {
17
+ * valid: 'real',
18
+ * invalid: undefined,
19
+ * } as const;
20
+ *
21
+ * clean = pickDefinedValues(dirty);
22
+ *
23
+ * // @ts-expect-error -- you can’t access this property
24
+ * clean.invalid
25
+ * ```
26
+ */
27
+ export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => ObjectWithDefinedValues<T>;
28
+ export {};
@@ -0,0 +1,41 @@
1
+ import { isString } from '@guardian/libs';
2
+ const isTargetingString = (string) => isString(string) && string !== '';
3
+ const isTargetingArray = (array) => Array.isArray(array) && array.filter(isTargetingString).length > 0;
4
+ const isValidTargeting = (value) => {
5
+ if (isTargetingString(value))
6
+ return true;
7
+ if (isTargetingArray(value))
8
+ return true;
9
+ return false;
10
+ };
11
+ /**
12
+ * Picks only keys with targeting values from an object.
13
+ * A targeting values is defined as either:
14
+ * - a non-empty string
15
+ * - an array of non-empty strings
16
+ *
17
+ * If you object is read-only, you can safely access properties on the result.
18
+ * For example:
19
+ *
20
+ * ```ts
21
+ * dirty = {
22
+ * valid: 'real',
23
+ * invalid: undefined,
24
+ * } as const;
25
+ *
26
+ * clean = pickDefinedValues(dirty);
27
+ *
28
+ * // @ts-expect-error -- you can’t access this property
29
+ * clean.invalid
30
+ * ```
31
+ */
32
+ export const pickTargetingValues = (obj) => {
33
+ const initialValue = {};
34
+ return Object.entries(obj).reduce((valid, [key, value]) => {
35
+ if (isValidTargeting(value)) {
36
+ // @ts-expect-error -- isValidTargeting checks this
37
+ valid[key] = value;
38
+ }
39
+ return valid;
40
+ }, initialValue);
41
+ };
@@ -41,8 +41,9 @@ const experimentsTargeting = ({ clientSideParticipations, serverSideParticipatio
41
41
  const serverSideExperiments = Object.entries(serverSideParticipations)
42
42
  .map((test) => testToParams(...test))
43
43
  .filter(isString);
44
- if (clientSideExperiment.length + serverSideExperiments.length === 0)
44
+ if (clientSideExperiment.length + serverSideExperiments.length === 0) {
45
45
  return null;
46
+ }
46
47
  return [...clientSideExperiment, ...serverSideExperiments];
47
48
  };
48
49
  /* -- Targeting -- */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {
@@ -57,7 +57,7 @@
57
57
  "@typescript-eslint/parser": "^5.5.0",
58
58
  "commitizen": "^4.2.4",
59
59
  "cz-conventional-changelog": "^3.3.0",
60
- "eslint": "^8.3.0",
60
+ "eslint": "^8.4.1",
61
61
  "eslint-config-prettier": "^8.3.0",
62
62
  "eslint-plugin-eslint-comments": "^3.2.0",
63
63
  "eslint-plugin-import": "^2.25.3",