@lingui/core 4.7.0 → 4.7.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.
package/dist/index.cjs CHANGED
@@ -3,10 +3,6 @@
3
3
  const unraw = require('unraw');
4
4
  const compileMessage = require('@lingui/message-utils/compileMessage');
5
5
 
6
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
-
8
- const unraw__default = /*#__PURE__*/_interopDefaultCompat(unraw);
9
-
10
6
  const isString = (s) => typeof s === "string";
11
7
  const isFunction = (f) => typeof f === "function";
12
8
 
@@ -66,6 +62,7 @@ const formats = {
66
62
  };
67
63
 
68
64
  const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
65
+ const OCTOTHORPE_PH = "%__lingui_octothorpe__%";
69
66
  const getDefaultFormats = (locale, passedLocales, formats = {}) => {
70
67
  const locales = passedLocales || locale;
71
68
  const style = (format) => {
@@ -74,7 +71,7 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
74
71
  const replaceOctothorpe = (value, message) => {
75
72
  const numberFormat = Object.keys(formats).length ? style("number") : void 0;
76
73
  const valueStr = number(locales, value, numberFormat);
77
- return message.replace("#", valueStr);
74
+ return message.replace(new RegExp(OCTOTHORPE_PH, "g"), valueStr);
78
75
  };
79
76
  return {
80
77
  plural: (value, cases) => {
@@ -89,40 +86,50 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
89
86
  },
90
87
  select: selectFormatter,
91
88
  number: (value, format) => number(locales, value, style(format)),
92
- date: (value, format) => date(locales, value, style(format)),
93
- undefined: undefinedFormatter
89
+ date: (value, format) => date(locales, value, style(format))
94
90
  };
95
91
  };
96
92
  const selectFormatter = (value, rules) => rules[value] ?? rules.other;
97
- const undefinedFormatter = (value) => value;
98
93
  function interpolate(translation, locale, locales) {
99
94
  return (values = {}, formats) => {
100
95
  const formatters = getDefaultFormats(locale, locales, formats);
101
- const formatMessage = (message) => {
102
- if (!Array.isArray(message))
103
- return message;
104
- return message.reduce((message2, token) => {
105
- if (isString(token))
106
- return message2 + token;
96
+ const formatMessage = (tokens) => {
97
+ if (!Array.isArray(tokens))
98
+ return tokens;
99
+ return tokens.reduce((message, token) => {
100
+ if (token === "#") {
101
+ return message + OCTOTHORPE_PH;
102
+ }
103
+ if (isString(token)) {
104
+ return message + token;
105
+ }
107
106
  const [name, type, format] = token;
108
107
  let interpolatedFormat = {};
109
- if (format != null && typeof format === "object") {
110
- Object.entries(format).forEach(([key, value2]) => {
111
- interpolatedFormat[key] = formatMessage(value2);
112
- });
108
+ if (type === "plural" || type === "selectordinal" || type === "select") {
109
+ Object.entries(format).forEach(
110
+ ([key, value2]) => {
111
+ interpolatedFormat[key] = formatMessage(value2);
112
+ }
113
+ );
113
114
  } else {
114
115
  interpolatedFormat = format;
115
116
  }
116
- const formatter = formatters[type];
117
- const value = formatter(values[name], interpolatedFormat);
118
- if (value == null)
119
- return message2;
120
- return message2 + value;
117
+ let value;
118
+ if (type) {
119
+ const formatter = formatters[type];
120
+ value = formatter(values[name], interpolatedFormat);
121
+ } else {
122
+ value = values[name];
123
+ }
124
+ if (value == null) {
125
+ return message;
126
+ }
127
+ return message + value;
121
128
  }, "");
122
129
  };
123
130
  const result = formatMessage(translation);
124
131
  if (isString(result) && UNICODE_REGEX.test(result)) {
125
- return unraw__default(result.trim());
132
+ return unraw.unraw(result.trim());
126
133
  }
127
134
  if (isString(result))
128
135
  return result.trim();
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import unraw from 'unraw';
1
+ import { unraw } from 'unraw';
2
2
  import { compileMessage } from '@lingui/message-utils/compileMessage';
3
3
 
4
4
  const isString = (s) => typeof s === "string";
@@ -60,6 +60,7 @@ const formats = {
60
60
  };
61
61
 
62
62
  const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
63
+ const OCTOTHORPE_PH = "%__lingui_octothorpe__%";
63
64
  const getDefaultFormats = (locale, passedLocales, formats = {}) => {
64
65
  const locales = passedLocales || locale;
65
66
  const style = (format) => {
@@ -68,7 +69,7 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
68
69
  const replaceOctothorpe = (value, message) => {
69
70
  const numberFormat = Object.keys(formats).length ? style("number") : void 0;
70
71
  const valueStr = number(locales, value, numberFormat);
71
- return message.replace("#", valueStr);
72
+ return message.replace(new RegExp(OCTOTHORPE_PH, "g"), valueStr);
72
73
  };
73
74
  return {
74
75
  plural: (value, cases) => {
@@ -83,35 +84,45 @@ const getDefaultFormats = (locale, passedLocales, formats = {}) => {
83
84
  },
84
85
  select: selectFormatter,
85
86
  number: (value, format) => number(locales, value, style(format)),
86
- date: (value, format) => date(locales, value, style(format)),
87
- undefined: undefinedFormatter
87
+ date: (value, format) => date(locales, value, style(format))
88
88
  };
89
89
  };
90
90
  const selectFormatter = (value, rules) => rules[value] ?? rules.other;
91
- const undefinedFormatter = (value) => value;
92
91
  function interpolate(translation, locale, locales) {
93
92
  return (values = {}, formats) => {
94
93
  const formatters = getDefaultFormats(locale, locales, formats);
95
- const formatMessage = (message) => {
96
- if (!Array.isArray(message))
97
- return message;
98
- return message.reduce((message2, token) => {
99
- if (isString(token))
100
- return message2 + token;
94
+ const formatMessage = (tokens) => {
95
+ if (!Array.isArray(tokens))
96
+ return tokens;
97
+ return tokens.reduce((message, token) => {
98
+ if (token === "#") {
99
+ return message + OCTOTHORPE_PH;
100
+ }
101
+ if (isString(token)) {
102
+ return message + token;
103
+ }
101
104
  const [name, type, format] = token;
102
105
  let interpolatedFormat = {};
103
- if (format != null && typeof format === "object") {
104
- Object.entries(format).forEach(([key, value2]) => {
105
- interpolatedFormat[key] = formatMessage(value2);
106
- });
106
+ if (type === "plural" || type === "selectordinal" || type === "select") {
107
+ Object.entries(format).forEach(
108
+ ([key, value2]) => {
109
+ interpolatedFormat[key] = formatMessage(value2);
110
+ }
111
+ );
107
112
  } else {
108
113
  interpolatedFormat = format;
109
114
  }
110
- const formatter = formatters[type];
111
- const value = formatter(values[name], interpolatedFormat);
112
- if (value == null)
113
- return message2;
114
- return message2 + value;
115
+ let value;
116
+ if (type) {
117
+ const formatter = formatters[type];
118
+ value = formatter(values[name], interpolatedFormat);
119
+ } else {
120
+ value = values[name];
121
+ }
122
+ if (value == null) {
123
+ return message;
124
+ }
125
+ return message + value;
115
126
  }, "");
116
127
  };
117
128
  const result = formatMessage(translation);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/core",
3
- "version": "4.7.0",
3
+ "version": "4.7.1",
4
4
  "sideEffects": false,
5
5
  "description": "I18n tools for javascript",
6
6
  "main": "./dist/index.cjs",
@@ -54,12 +54,12 @@
54
54
  ],
55
55
  "dependencies": {
56
56
  "@babel/runtime": "^7.20.13",
57
- "@lingui/message-utils": "4.7.0",
57
+ "@lingui/message-utils": "4.7.1",
58
58
  "unraw": "^3.0.0"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@lingui/jest-mocks": "*",
62
62
  "unbuild": "2.0.0"
63
63
  },
64
- "gitHead": "ebb6353aef59d1b1a29a10ba6ffe94eb42c51a77"
64
+ "gitHead": "49647102dbbd7046a2c318ba0525c1392e370076"
65
65
  }