@worldware/msg 0.6.0 → 0.6.2

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/README.md CHANGED
@@ -160,6 +160,9 @@ const jsonWithoutNotes = resource.toJSON(true);
160
160
 
161
161
  // Get data object
162
162
  const data = resource.getData();
163
+
164
+ // Message objects in the output only include `attributes` when they differ from
165
+ // the resource's attributes, keeping the serialized data compact
163
166
  ```
164
167
 
165
168
  ## API Reference
@@ -187,7 +190,7 @@ const data = resource.getData();
187
190
  - `translate(data: MsgResourceData): MsgResource` - Create a translated version
188
191
  - `getTranslation(lang: string): Promise<MsgResource>` - Load and apply translations
189
192
  - `getProject(): MsgProject` - Returns the project instance associated with the resource
190
- - `getData(stripNotes?: boolean): MsgResourceData` - Get resource data
193
+ - `getData(stripNotes?: boolean): MsgResourceData` - Get resource data. Message objects in the output omit `attributes` when they match the resource's attributes (to avoid redundancy)
191
194
  - `toJSON(stripNotes?: boolean): string` - Serialize to JSON
192
195
 
193
196
  **Properties:**
@@ -1,20 +1,20 @@
1
1
  // src/classes/MsgMessage/MsgMessage.ts
2
2
  import { MessageFormat } from "messageformat";
3
3
  var DEFAULT_ATTRIBUTES = {
4
- lang: "",
5
- dir: "",
4
+ lang: "und",
5
+ dir: "auto",
6
6
  dnt: false
7
7
  };
8
8
  var MsgMessage = class _MsgMessage {
9
9
  _key;
10
10
  _value;
11
11
  _mf;
12
- _attributes = DEFAULT_ATTRIBUTES;
12
+ _attributes;
13
13
  _notes = [];
14
14
  constructor(key, value, attributes, notes) {
15
15
  this._key = key;
16
16
  this._value = value;
17
- this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
17
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
18
18
  if (notes) {
19
19
  notes.forEach((note) => this.addNote(note));
20
20
  }
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-OMRO6GAZ.mjs";
4
4
  import {
5
5
  MsgMessage
6
- } from "./chunk-GHRFKA7N.mjs";
6
+ } from "./chunk-ELDIN22M.mjs";
7
7
 
8
8
  // src/classes/MsgResource/MsgResource.ts
9
9
  var MsgResource = class _MsgResource extends Map {
@@ -33,6 +33,11 @@ var MsgResource = class _MsgResource extends Map {
33
33
  notes.forEach((note) => this.addNote(note));
34
34
  }
35
35
  }
36
+ hasMatchingAttributes(message) {
37
+ const res = this.attributes;
38
+ const msg = message.attributes;
39
+ return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
40
+ }
36
41
  get attributes() {
37
42
  return this._attributes;
38
43
  }
@@ -117,7 +122,13 @@ var MsgResource = class _MsgResource extends Map {
117
122
  }
118
123
  getData(stripNotes = false) {
119
124
  const messages = [];
120
- this.forEach((msg) => messages.push(msg.getData(stripNotes)));
125
+ this.forEach((msg) => {
126
+ if (this.hasMatchingAttributes(msg)) {
127
+ messages.push({ key: msg.key, value: msg.value });
128
+ } else {
129
+ messages.push(msg.getData(stripNotes));
130
+ }
131
+ });
121
132
  return {
122
133
  title: this.title,
123
134
  attributes: this.attributes,
@@ -25,20 +25,20 @@ __export(MsgMessage_exports, {
25
25
  module.exports = __toCommonJS(MsgMessage_exports);
26
26
  var import_messageformat = require("messageformat");
27
27
  var DEFAULT_ATTRIBUTES = {
28
- lang: "",
29
- dir: "",
28
+ lang: "und",
29
+ dir: "auto",
30
30
  dnt: false
31
31
  };
32
32
  var MsgMessage = class _MsgMessage {
33
33
  _key;
34
34
  _value;
35
35
  _mf;
36
- _attributes = DEFAULT_ATTRIBUTES;
36
+ _attributes;
37
37
  _notes = [];
38
38
  constructor(key, value, attributes, notes) {
39
39
  this._key = key;
40
40
  this._value = value;
41
- this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
41
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
42
42
  if (notes) {
43
43
  notes.forEach((note) => this.addNote(note));
44
44
  }
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MsgMessage
3
- } from "../../chunk-GHRFKA7N.mjs";
3
+ } from "../../chunk-ELDIN22M.mjs";
4
4
  export {
5
5
  MsgMessage
6
6
  };
@@ -45,6 +45,7 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
45
45
  private _project;
46
46
  static create(data: MsgResourceData, project: MsgProject): MsgResource;
47
47
  private constructor();
48
+ private hasMatchingAttributes;
48
49
  get attributes(): MsgAttributes;
49
50
  set attributes(attributes: MsgAttributes);
50
51
  get notes(): MsgNote[];
@@ -45,6 +45,7 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
45
45
  private _project;
46
46
  static create(data: MsgResourceData, project: MsgProject): MsgResource;
47
47
  private constructor();
48
+ private hasMatchingAttributes;
48
49
  get attributes(): MsgAttributes;
49
50
  set attributes(attributes: MsgAttributes);
50
51
  get notes(): MsgNote[];
@@ -27,20 +27,20 @@ module.exports = __toCommonJS(MsgResource_exports);
27
27
  // src/classes/MsgMessage/MsgMessage.ts
28
28
  var import_messageformat = require("messageformat");
29
29
  var DEFAULT_ATTRIBUTES = {
30
- lang: "",
31
- dir: "",
30
+ lang: "und",
31
+ dir: "auto",
32
32
  dnt: false
33
33
  };
34
34
  var MsgMessage = class _MsgMessage {
35
35
  _key;
36
36
  _value;
37
37
  _mf;
38
- _attributes = DEFAULT_ATTRIBUTES;
38
+ _attributes;
39
39
  _notes = [];
40
40
  constructor(key, value, attributes, notes) {
41
41
  this._key = key;
42
42
  this._value = value;
43
- this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
43
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
44
44
  if (notes) {
45
45
  notes.forEach((note) => this.addNote(note));
46
46
  }
@@ -128,6 +128,11 @@ var MsgResource = class _MsgResource extends Map {
128
128
  notes.forEach((note) => this.addNote(note));
129
129
  }
130
130
  }
131
+ hasMatchingAttributes(message) {
132
+ const res = this.attributes;
133
+ const msg = message.attributes;
134
+ return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
135
+ }
131
136
  get attributes() {
132
137
  return this._attributes;
133
138
  }
@@ -212,7 +217,13 @@ var MsgResource = class _MsgResource extends Map {
212
217
  }
213
218
  getData(stripNotes = false) {
214
219
  const messages = [];
215
- this.forEach((msg) => messages.push(msg.getData(stripNotes)));
220
+ this.forEach((msg) => {
221
+ if (this.hasMatchingAttributes(msg)) {
222
+ messages.push({ key: msg.key, value: msg.value });
223
+ } else {
224
+ messages.push(msg.getData(stripNotes));
225
+ }
226
+ });
216
227
  return {
217
228
  title: this.title,
218
229
  attributes: this.attributes,
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  MsgResource
3
- } from "../../chunk-4SWDABOO.mjs";
3
+ } from "../../chunk-ZV7S5BRP.mjs";
4
4
  import "../../chunk-OMRO6GAZ.mjs";
5
- import "../../chunk-GHRFKA7N.mjs";
5
+ import "../../chunk-ELDIN22M.mjs";
6
6
  export {
7
7
  MsgResource
8
8
  };
@@ -29,20 +29,20 @@ module.exports = __toCommonJS(classes_exports);
29
29
  // src/classes/MsgMessage/MsgMessage.ts
30
30
  var import_messageformat = require("messageformat");
31
31
  var DEFAULT_ATTRIBUTES = {
32
- lang: "",
33
- dir: "",
32
+ lang: "und",
33
+ dir: "auto",
34
34
  dnt: false
35
35
  };
36
36
  var MsgMessage = class _MsgMessage {
37
37
  _key;
38
38
  _value;
39
39
  _mf;
40
- _attributes = DEFAULT_ATTRIBUTES;
40
+ _attributes;
41
41
  _notes = [];
42
42
  constructor(key, value, attributes, notes) {
43
43
  this._key = key;
44
44
  this._value = value;
45
- this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
45
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
46
46
  if (notes) {
47
47
  notes.forEach((note) => this.addNote(note));
48
48
  }
@@ -130,6 +130,11 @@ var MsgResource = class _MsgResource extends Map {
130
130
  notes.forEach((note) => this.addNote(note));
131
131
  }
132
132
  }
133
+ hasMatchingAttributes(message) {
134
+ const res = this.attributes;
135
+ const msg = message.attributes;
136
+ return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
137
+ }
133
138
  get attributes() {
134
139
  return this._attributes;
135
140
  }
@@ -214,7 +219,13 @@ var MsgResource = class _MsgResource extends Map {
214
219
  }
215
220
  getData(stripNotes = false) {
216
221
  const messages = [];
217
- this.forEach((msg) => messages.push(msg.getData(stripNotes)));
222
+ this.forEach((msg) => {
223
+ if (this.hasMatchingAttributes(msg)) {
224
+ messages.push({ key: msg.key, value: msg.value });
225
+ } else {
226
+ messages.push(msg.getData(stripNotes));
227
+ }
228
+ });
218
229
  return {
219
230
  title: this.title,
220
231
  attributes: this.attributes,
@@ -4,11 +4,11 @@ import {
4
4
  } from "../chunk-XS43NAP2.mjs";
5
5
  import {
6
6
  MsgResource
7
- } from "../chunk-4SWDABOO.mjs";
7
+ } from "../chunk-ZV7S5BRP.mjs";
8
8
  import "../chunk-OMRO6GAZ.mjs";
9
9
  import {
10
10
  MsgMessage
11
- } from "../chunk-GHRFKA7N.mjs";
11
+ } from "../chunk-ELDIN22M.mjs";
12
12
  export {
13
13
  MsgMessage,
14
14
  MsgProject,
package/dist/index.cjs CHANGED
@@ -29,20 +29,20 @@ module.exports = __toCommonJS(index_exports);
29
29
  // src/classes/MsgMessage/MsgMessage.ts
30
30
  var import_messageformat = require("messageformat");
31
31
  var DEFAULT_ATTRIBUTES = {
32
- lang: "",
33
- dir: "",
32
+ lang: "und",
33
+ dir: "auto",
34
34
  dnt: false
35
35
  };
36
36
  var MsgMessage = class _MsgMessage {
37
37
  _key;
38
38
  _value;
39
39
  _mf;
40
- _attributes = DEFAULT_ATTRIBUTES;
40
+ _attributes;
41
41
  _notes = [];
42
42
  constructor(key, value, attributes, notes) {
43
43
  this._key = key;
44
44
  this._value = value;
45
- this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
45
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
46
46
  if (notes) {
47
47
  notes.forEach((note) => this.addNote(note));
48
48
  }
@@ -130,6 +130,11 @@ var MsgResource = class _MsgResource extends Map {
130
130
  notes.forEach((note) => this.addNote(note));
131
131
  }
132
132
  }
133
+ hasMatchingAttributes(message) {
134
+ const res = this.attributes;
135
+ const msg = message.attributes;
136
+ return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
137
+ }
133
138
  get attributes() {
134
139
  return this._attributes;
135
140
  }
@@ -214,7 +219,13 @@ var MsgResource = class _MsgResource extends Map {
214
219
  }
215
220
  getData(stripNotes = false) {
216
221
  const messages = [];
217
- this.forEach((msg) => messages.push(msg.getData(stripNotes)));
222
+ this.forEach((msg) => {
223
+ if (this.hasMatchingAttributes(msg)) {
224
+ messages.push({ key: msg.key, value: msg.value });
225
+ } else {
226
+ messages.push(msg.getData(stripNotes));
227
+ }
228
+ });
218
229
  return {
219
230
  title: this.title,
220
231
  attributes: this.attributes,
package/dist/index.mjs CHANGED
@@ -4,11 +4,11 @@ import {
4
4
  } from "./chunk-XS43NAP2.mjs";
5
5
  import {
6
6
  MsgResource
7
- } from "./chunk-4SWDABOO.mjs";
7
+ } from "./chunk-ZV7S5BRP.mjs";
8
8
  import "./chunk-OMRO6GAZ.mjs";
9
9
  import {
10
10
  MsgMessage
11
- } from "./chunk-GHRFKA7N.mjs";
11
+ } from "./chunk-ELDIN22M.mjs";
12
12
  export {
13
13
  MsgMessage,
14
14
  MsgProject,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Message localization tooling",
5
5
  "license": "MIT",
6
6
  "author": "Joel Sahleen",
@@ -11,9 +11,22 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/worldware-studios/msg/issues"
13
13
  },
14
- "main": "dist/index.js",
14
+ "main": "dist/index.cjs",
15
+ "module": "dist/index.mjs",
15
16
  "type": "module",
16
17
  "types": "dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ }
28
+ }
29
+ },
17
30
  "files": [
18
31
  "dist"
19
32
  ],