@messagevisor/module-interpolation 0.0.1 → 0.2.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.2.0](https://github.com/messagevisor/messagevisor/compare/v0.1.0...v0.2.0) (2026-06-02)
7
+
8
+ **Note:** Version bump only for package @messagevisor/module-interpolation
9
+
10
+
11
+
12
+
13
+
14
+ # [0.1.0](https://github.com/messagevisor/messagevisor/compare/v0.0.2...v0.1.0) (2026-05-31)
15
+
16
+ **Note:** Version bump only for package @messagevisor/module-interpolation
17
+
18
+
19
+
20
+
21
+
22
+ ## 0.0.2 (2026-05-31)
23
+
24
+ **Note:** Version bump only for package @messagevisor/module-interpolation
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Fahad Heylaal (https://fahad19.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @messagevisor/module-interpolation
2
+
3
+ Visit [https://messagevisor.com/docs/modules/interpolation/](https://messagevisor.com/docs/modules/interpolation/) for more information.
4
+
5
+ ## License
6
+
7
+ MIT © [Fahad Heylaal](https://fahad19.com)
package/jest.config.js ADDED
@@ -0,0 +1,12 @@
1
+ const base = require("../../jest.config");
2
+
3
+ module.exports = {
4
+ ...base,
5
+ rootDir: "../..",
6
+ testRegex: undefined,
7
+ testMatch: ["<rootDir>/packages/module-interpolation/src/**/*.spec.ts"],
8
+ moduleNameMapper: {
9
+ "^@messagevisor/sdk$": "<rootDir>/packages/sdk/src/index.ts",
10
+ "^@messagevisor/module-icu$": "<rootDir>/packages/module-icu/src/index.ts",
11
+ },
12
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { MessagevisorModule } from "@messagevisor/sdk";
2
+ export interface InterpolationModuleOptions {
3
+ name?: string;
4
+ pattern?: RegExp;
5
+ }
6
+ export declare function createInterpolationModule(options?: InterpolationModuleOptions): MessagevisorModule;
package/lib/index.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createInterpolationModule = createInterpolationModule;
4
+ const DEFAULT_PATTERN = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
5
+ function toGlobalPattern(pattern = DEFAULT_PATTERN) {
6
+ const flags = Array.from(new Set(`${pattern.flags}g`.split(""))).join("");
7
+ return new RegExp(pattern.source, flags);
8
+ }
9
+ function isInterpolatableValue(value) {
10
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
11
+ }
12
+ function createInterpolationModule(options = {}) {
13
+ const pattern = toGlobalPattern(options.pattern);
14
+ return {
15
+ name: options.name || "interpolation",
16
+ format(payload) {
17
+ if (typeof payload.translation !== "string") {
18
+ return;
19
+ }
20
+ return payload.translation.replace(pattern, (match, variableName) => {
21
+ var _a;
22
+ if (typeof variableName !== "string") {
23
+ return match;
24
+ }
25
+ const value = (_a = payload.values) === null || _a === void 0 ? void 0 : _a[variableName];
26
+ return isInterpolatableValue(value) ? String(value) : match;
27
+ });
28
+ },
29
+ };
30
+ }
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAkBA,8DAuBC;AAlCD,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAExD,SAAS,eAAe,CAAC,UAAkB,eAAe;IACxD,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,GAAG,OAAO,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1E,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAC3C,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAc;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,SAAS,CAAC;AAC9F,CAAC;AAED,SAAgB,yBAAyB,CACvC,UAAsC,EAAE;IAExC,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEjD,OAAO;QACL,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,eAAe;QACrC,MAAM,CAAC,OAAkC;YACvC,IAAI,OAAO,OAAO,CAAC,WAAW,KAAK,QAAQ,EAAE,CAAC;gBAC5C,OAAO;YACT,CAAC;YAED,OAAO,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,YAAqB,EAAE,EAAE;;gBAC3E,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE,CAAC;oBACrC,OAAO,KAAK,CAAC;gBACf,CAAC;gBAED,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,MAAM,0CAAG,YAAY,CAAC,CAAC;gBAE7C,OAAO,qBAAqB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YAC9D,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,15 +1,44 @@
1
1
  {
2
- "name": "@messagevisor/module-interpolation",
3
- "version": "0.0.1",
4
- "description": "Messagevisor package",
5
- "license": "ISC",
6
- "author": "",
7
- "type": "commonjs",
8
- "main": "index.js",
9
- "publishConfig": {
10
- "access": "public"
11
- },
12
- "scripts": {
13
- "test": "echo \"Error: no test specified\" && exit 1"
14
- }
2
+ "name": "@messagevisor/module-interpolation",
3
+ "version": "0.2.0",
4
+ "description": "String interpolation module for Messagevisor",
5
+ "main": "lib/index.js",
6
+ "module": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "require": "./lib/index.js",
12
+ "import": "./lib/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "scripts": {
17
+ "transpile": "rimraf lib && tsc --project tsconfig.cjs.json",
18
+ "dist": "echo 'Nothing to dist here'",
19
+ "build": "npm run transpile",
20
+ "test": "jest --config jest.config.js --verbose",
21
+ "typecheck": "tsc --noEmit --skipLibCheck -p tsconfig.typecheck.json"
22
+ },
23
+ "author": {
24
+ "name": "Fahad Heylaal",
25
+ "url": "https://fahad19.com"
26
+ },
27
+ "homepage": "https://messagevisor.com",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/messagevisor/messagevisor.git"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/messagevisor/messagevisor/issues"
38
+ },
39
+ "license": "MIT",
40
+ "dependencies": {
41
+ "@messagevisor/sdk": "0.2.0"
42
+ },
43
+ "gitHead": "53cfef1a5ae28724b24226dd3a32ff813b063ce3"
15
44
  }
@@ -0,0 +1,169 @@
1
+ import type { DatafileContent } from "@messagevisor/types";
2
+ import { createMessagevisor } from "@messagevisor/sdk";
3
+ import { createICUModule } from "@messagevisor/module-icu";
4
+
5
+ import { createInterpolationModule } from "./index";
6
+
7
+ const datafile: DatafileContent = {
8
+ schemaVersion: "1",
9
+ messagevisorVersion: "0.0.1",
10
+ revision: "1",
11
+ target: "web",
12
+ locale: "en-US",
13
+ formats: {
14
+ number: {
15
+ decimalFixed: { style: "decimal", minimumFractionDigits: 2, maximumFractionDigits: 2 },
16
+ },
17
+ },
18
+ segments: {},
19
+ messages: {
20
+ greeting: {},
21
+ repeated: {},
22
+ customPrefix: {},
23
+ mixedFormatting: {},
24
+ },
25
+ translations: {
26
+ greeting: "Hello {name}",
27
+ repeated: "{name} and {name} and {_value_2}",
28
+ customPrefix: "Hello %{name}",
29
+ mixedFormatting: "Total %{currency}{amount, number, decimalFixed}",
30
+ },
31
+ };
32
+
33
+ describe("@messagevisor/module-interpolation", function () {
34
+ it("interpolates default placeholders for translate and formatMessage", function () {
35
+ const m = createMessagevisor({
36
+ datafile,
37
+ modules: [createInterpolationModule()],
38
+ });
39
+
40
+ expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello Ada");
41
+ expect(m.formatMessage("Hi {name}", { name: "Lin" })).toEqual("Hi Lin");
42
+ });
43
+
44
+ it("replaces repeated placeholders and supports uppercase, lowercase, digits, and underscores", function () {
45
+ const m = createMessagevisor({
46
+ datafile,
47
+ modules: [createInterpolationModule()],
48
+ });
49
+
50
+ expect(
51
+ m.translate("repeated", {
52
+ name: "Ada",
53
+ _value_2: "OK",
54
+ }),
55
+ ).toEqual("Ada and Ada and OK");
56
+ expect(
57
+ m.formatMessage("Vars {USER_1} {user_2}", {
58
+ USER_1: "A",
59
+ user_2: "B",
60
+ }),
61
+ ).toEqual("Vars A B");
62
+ });
63
+
64
+ it("leaves unknown, null, undefined, object, array, date, and function placeholders untouched", function () {
65
+ const m = createMessagevisor({
66
+ datafile,
67
+ modules: [createInterpolationModule()],
68
+ });
69
+
70
+ expect(m.formatMessage("Hello {name}", {})).toEqual("Hello {name}");
71
+ expect(m.formatMessage("Hello {name}", { name: null as any })).toEqual("Hello {name}");
72
+ expect(m.formatMessage("Hello {name}", { name: undefined as any })).toEqual("Hello {name}");
73
+ expect(m.formatMessage("Hello {name}", { name: { first: "Ada" } as any })).toEqual(
74
+ "Hello {name}",
75
+ );
76
+ expect(m.formatMessage("Hello {name}", { name: ["Ada"] as any })).toEqual("Hello {name}");
77
+ expect(m.formatMessage("Hello {name}", { name: new Date() as any })).toEqual("Hello {name}");
78
+ expect(
79
+ m.formatMessage("Hello {name}", {
80
+ name: (() => "Ada") as any,
81
+ }),
82
+ ).toEqual("Hello {name}");
83
+ });
84
+
85
+ it("interpolates string, number, and boolean values", function () {
86
+ const m = createMessagevisor({
87
+ datafile,
88
+ modules: [createInterpolationModule()],
89
+ });
90
+
91
+ expect(m.formatMessage("Name {name}", { name: "Ada" })).toEqual("Name Ada");
92
+ expect(m.formatMessage("Count {count}", { count: 12 })).toEqual("Count 12");
93
+ expect(m.formatMessage("Flag {enabled}", { enabled: false })).toEqual("Flag false");
94
+ });
95
+
96
+ it("skips processing when the incoming translation is not a string", function () {
97
+ const m = createMessagevisor({
98
+ datafile,
99
+ modules: [
100
+ {
101
+ name: "object-first",
102
+ format() {
103
+ return { value: "not-a-string" };
104
+ },
105
+ },
106
+ createInterpolationModule(),
107
+ ],
108
+ });
109
+
110
+ expect(m.translate("greeting", { name: "Ada" })).toEqual({ value: "not-a-string" });
111
+ });
112
+
113
+ it("supports custom regex patterns such as %{name}", function () {
114
+ const m = createMessagevisor({
115
+ datafile,
116
+ modules: [
117
+ createInterpolationModule({
118
+ pattern: /%\{([A-Za-z_][A-Za-z0-9_]*)\}/,
119
+ }),
120
+ ],
121
+ });
122
+
123
+ expect(m.translate("customPrefix", { name: "Ada" })).toEqual("Hello Ada");
124
+ });
125
+
126
+ it("composes with module-icu through module ordering", function () {
127
+ const m = createMessagevisor({
128
+ datafile,
129
+ modules: [
130
+ createInterpolationModule({
131
+ pattern: /%\{([A-Za-z_][A-Za-z0-9_]*)\}/,
132
+ }),
133
+ createICUModule(),
134
+ ],
135
+ });
136
+
137
+ expect(
138
+ m.translate("mixedFormatting", {
139
+ currency: "EUR",
140
+ amount: 12,
141
+ }),
142
+ ).toEqual("Total EUR12.00");
143
+ });
144
+
145
+ it("does not mutate payload values", function () {
146
+ const values = { name: "Ada" };
147
+ const m = createMessagevisor({
148
+ datafile,
149
+ modules: [createInterpolationModule()],
150
+ });
151
+
152
+ m.translate("greeting", values);
153
+
154
+ expect(values).toEqual({ name: "Ada" });
155
+ });
156
+
157
+ it("can be removed by its default name", function () {
158
+ const m = createMessagevisor({
159
+ datafile,
160
+ modules: [createInterpolationModule()],
161
+ });
162
+
163
+ expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello Ada");
164
+
165
+ m.removeModule("interpolation");
166
+
167
+ expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello {name}");
168
+ });
169
+ });
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { MessagevisorFormatPayload, MessagevisorModule } from "@messagevisor/sdk";
2
+
3
+ export interface InterpolationModuleOptions {
4
+ name?: string;
5
+ pattern?: RegExp;
6
+ }
7
+
8
+ const DEFAULT_PATTERN = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
9
+
10
+ function toGlobalPattern(pattern: RegExp = DEFAULT_PATTERN) {
11
+ const flags = Array.from(new Set(`${pattern.flags}g`.split(""))).join("");
12
+ return new RegExp(pattern.source, flags);
13
+ }
14
+
15
+ function isInterpolatableValue(value: unknown): value is string | number | boolean {
16
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
17
+ }
18
+
19
+ export function createInterpolationModule(
20
+ options: InterpolationModuleOptions = {},
21
+ ): MessagevisorModule {
22
+ const pattern = toGlobalPattern(options.pattern);
23
+
24
+ return {
25
+ name: options.name || "interpolation",
26
+ format(payload: MessagevisorFormatPayload) {
27
+ if (typeof payload.translation !== "string") {
28
+ return;
29
+ }
30
+
31
+ return payload.translation.replace(pattern, (match, variableName: unknown) => {
32
+ if (typeof variableName !== "string") {
33
+ return match;
34
+ }
35
+
36
+ const value = payload.values?.[variableName];
37
+
38
+ return isInterpolatableValue(value) ? String(value) : match;
39
+ });
40
+ },
41
+ };
42
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../tsconfig.cjs.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib",
5
+ "rootDir": "./src",
6
+ "moduleResolution": "node",
7
+ "esModuleInterop": true,
8
+ "target": "es2018",
9
+ "lib": ["es2021", "es2021.intl", "dom"],
10
+ "skipLibCheck": true
11
+ },
12
+ "include": ["./src/**/*.ts"],
13
+ "exclude": ["./src/**/*.spec.ts"]
14
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.cjs.json",
3
+ "exclude": []
4
+ }