@stackedapp/utils 0.26.1

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,3 @@
1
+ import { StackedCompletionDynamicTracker } from '@stackedapp/types';
2
+ export declare const dynamicTrackerToPrimitive: (dynaTrack: Record<string, StackedCompletionDynamicTracker>) => Record<string, string | number | boolean>;
3
+ //# sourceMappingURL=dynamic.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dynamicTrackerToPrimitive = void 0;
4
+ const dynamicTrackerToPrimitive = (dynaTrack) => {
5
+ const primitive = {};
6
+ for (const key in dynaTrack) {
7
+ primitive[key] = dynaTrack[key].value || 0;
8
+ }
9
+ return primitive;
10
+ };
11
+ exports.dynamicTrackerToPrimitive = dynamicTrackerToPrimitive;
12
+ //# sourceMappingURL=dynamic.js.map
@@ -0,0 +1,5 @@
1
+ export * from './conditions';
2
+ export * from './template';
3
+ export * from './dynamic';
4
+ export * from './blockchain_utils';
5
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./conditions"), exports);
18
+ __exportStar(require("./template"), exports);
19
+ __exportStar(require("./dynamic"), exports);
20
+ __exportStar(require("./blockchain_utils"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,21 @@
1
+ import { StackedDynamicCondition } from '@stackedapp/types';
2
+ export declare function extractTemplateKeys(template: string | undefined): Set<string>;
3
+ /**
4
+ * This replaces {keyName} keys from the template with corresponding values from the dynamic object.
5
+ */
6
+ export declare function renderTemplate(template: string | undefined, dynamic: Record<string, string | number | boolean> | undefined | null): string;
7
+ /**
8
+ * This replaces {{keyName}} in dynamic condition keys with corresponding values from
9
+ * the PlayerOffer.trackers
10
+ *
11
+ * eg. a condition high_score_pet-{{surfacerPlayerId}} with high_score_pet-12345
12
+ */
13
+ export declare function replaceDynamicConditionKey(key: string, trackers: Record<string, any>): string;
14
+ /** this replaces all of the dynamic conditions.keys by calling replaceDynamicConditionKey */
15
+ export declare function replaceDynamicConditionKeys(conditions: Array<StackedDynamicCondition>, trackers: Record<string, any>): {
16
+ key: string;
17
+ compareTo: string | number | boolean;
18
+ operator: "==" | "!=" | ">" | ">=" | "<" | "<=" | "has" | "not_has";
19
+ intervalHr?: number;
20
+ }[];
21
+ //# sourceMappingURL=template.d.ts.map
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractTemplateKeys = extractTemplateKeys;
4
+ exports.renderTemplate = renderTemplate;
5
+ exports.replaceDynamicConditionKey = replaceDynamicConditionKey;
6
+ exports.replaceDynamicConditionKeys = replaceDynamicConditionKeys;
7
+ const keyPattern = /\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g;
8
+ // extract {key} placeholders from template string
9
+ // e.g. "Hello {playerName}, you have {coins} coins!" → Set { "playerName", "coins" }
10
+ function extractTemplateKeys(template) {
11
+ if (!template)
12
+ return new Set();
13
+ const keys = new Set();
14
+ for (const match of template.matchAll(keyPattern)) {
15
+ keys.add(match[1]);
16
+ }
17
+ return keys;
18
+ }
19
+ /**
20
+ * This replaces {keyName} keys from the template with corresponding values from the dynamic object.
21
+ */
22
+ function renderTemplate(template, dynamic) {
23
+ if (!template)
24
+ return '';
25
+ return template.replace(keyPattern, (_match, key) => {
26
+ if (dynamic && typeof dynamic[key] === 'boolean') {
27
+ return dynamic[key] ? '✓' : '✗';
28
+ }
29
+ if (dynamic && dynamic[key] !== undefined) {
30
+ return String(dynamic[key]);
31
+ }
32
+ return '{?}'; // indicate missing key
33
+ });
34
+ }
35
+ /**
36
+ * This replaces {{keyName}} in dynamic condition keys with corresponding values from
37
+ * the PlayerOffer.trackers
38
+ *
39
+ * eg. a condition high_score_pet-{{surfacerPlayerId}} with high_score_pet-12345
40
+ */
41
+ function replaceDynamicConditionKey(key, trackers) {
42
+ return key?.replace(/\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g, (match, p1) => {
43
+ const value = trackers[p1];
44
+ return value !== undefined ? String(value) : match;
45
+ });
46
+ }
47
+ /** this replaces all of the dynamic conditions.keys by calling replaceDynamicConditionKey */
48
+ function replaceDynamicConditionKeys(conditions, trackers) {
49
+ return conditions.map((condition) => ({
50
+ ...condition,
51
+ key: replaceDynamicConditionKey(condition.key, trackers),
52
+ }));
53
+ }
54
+ //# sourceMappingURL=template.js.map
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@stackedapp/utils",
3
+ "version": "0.26.1",
4
+ "description": "Public utilities for Stacked platform SDK",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*.js",
9
+ "dist/**/*.d.ts"
10
+ ],
11
+ "private": false,
12
+ "scripts": {
13
+ "prebuild": "npx rimraf dist",
14
+ "build": "tsc",
15
+ "clean": "npx rimraf dist"
16
+ },
17
+ "dependencies": {
18
+ "@stackedapp/types": "*"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "license": "ISC"
24
+ }