@zohodesk/i18n 1.0.0-exp.5 → 1.0.0-exp.6

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.
Files changed (41) hide show
  1. package/es/components/DateTimeDiffFormat.js +5 -19
  2. package/es/components/FormatText.js +2 -2
  3. package/es/components/HOCI18N.js +2 -10
  4. package/es/components/I18N.js +2 -13
  5. package/es/components/I18NProvider.js +0 -9
  6. package/es/components/PluralFormat.js +3 -5
  7. package/es/components/UserTimeDiffFormat.js +0 -3
  8. package/es/components/__tests__/DateTimeDiffFormat.spec.js +46 -89
  9. package/es/components/__tests__/FormatText.spec.js +2 -2
  10. package/es/components/__tests__/HOCI18N.spec.js +2 -4
  11. package/es/components/__tests__/I18N.spec.js +6 -4
  12. package/es/components/__tests__/I18NProvider.spec.js +4 -4
  13. package/es/components/__tests__/PluralFormat.spec.js +2 -2
  14. package/es/components/__tests__/UserTimeDiffFormat.spec.js +90 -117
  15. package/es/utils/__tests__/jsxTranslations.spec.js +0 -3
  16. package/es/utils/errorReporter.js +0 -3
  17. package/es/utils/index.js +20 -62
  18. package/es/utils/jsxTranslations.js +5 -17
  19. package/lib/I18NContext.js +1 -5
  20. package/lib/components/DateTimeDiffFormat.js +41 -79
  21. package/lib/components/FormatText.js +14 -34
  22. package/lib/components/HOCI18N.js +17 -50
  23. package/lib/components/I18N.js +20 -55
  24. package/lib/components/I18NProvider.js +22 -54
  25. package/lib/components/PluralFormat.js +20 -43
  26. package/lib/components/UserTimeDiffFormat.js +38 -65
  27. package/lib/components/__tests__/DateTimeDiffFormat.spec.js +119 -189
  28. package/lib/components/__tests__/FormatText.spec.js +3 -10
  29. package/lib/components/__tests__/HOCI18N.spec.js +3 -14
  30. package/lib/components/__tests__/I18N.spec.js +4 -12
  31. package/lib/components/__tests__/I18NProvider.spec.js +7 -23
  32. package/lib/components/__tests__/PluralFormat.spec.js +3 -11
  33. package/lib/components/__tests__/UserTimeDiffFormat.spec.js +202 -271
  34. package/lib/index.js +3 -20
  35. package/lib/utils/__tests__/jsxTranslations.spec.js +1 -12
  36. package/lib/utils/errorReporter.js +0 -5
  37. package/lib/utils/index.js +45 -117
  38. package/lib/utils/jsxTranslations.js +61 -99
  39. package/package.json +1 -1
  40. package/src/utils/index.js +3 -0
  41. package/src/utils/jsxTranslations.js +3 -0
@@ -8,10 +8,8 @@ export default class DateTimeDiffFormat extends React.Component {
8
8
  super(props);
9
9
  this.getSuffix = this.getSuffix.bind(this);
10
10
  }
11
-
12
11
  getSuffix(min) {
13
12
  let suffix;
14
-
15
13
  if (this.props.ago && min < 0) {
16
14
  suffix = this.props.ago || '';
17
15
  } else if (this.props.later || min > 0) {
@@ -19,10 +17,8 @@ export default class DateTimeDiffFormat extends React.Component {
19
17
  } else {
20
18
  suffix = '';
21
19
  }
22
-
23
20
  return suffix;
24
21
  }
25
-
26
22
  render() {
27
23
  const {
28
24
  type,
@@ -95,9 +91,10 @@ export default class DateTimeDiffFormat extends React.Component {
95
91
  timeFormat: timeFormat,
96
92
  datePattern: datePattern,
97
93
  dateTimePattern: `${datePattern} ${timeFormat}`
98
- }; //In if condition we'll remove year and set date format if the current year is not required
99
- //In else part we'll set the date format as it is
94
+ };
100
95
 
96
+ //In if condition we'll remove year and set date format if the current year is not required
97
+ //In else part we'll set the date format as it is
101
98
  if (isEnabledCurrentYear === true && diffObj1.years === 0 && diffObj1.tYear === diffObj1.crntYear) {
102
99
  let dateFormat = getDatePatternWithoutYear(datePattern);
103
100
  diffObj1.dateFormat = dateFormat;
@@ -106,12 +103,10 @@ export default class DateTimeDiffFormat extends React.Component {
106
103
  diffObj1.dateFormat = datePattern;
107
104
  diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
108
105
  }
109
-
110
106
  let key = '';
111
107
  let values = [];
112
108
  let text = null;
113
109
  let isSuffixEnable = false;
114
-
115
110
  if (format) {
116
111
  let years, months, days, hours, minutes, seconds;
117
112
  years = diffObj1.years > 1 ? '2' : diffObj1.years;
@@ -128,15 +123,12 @@ export default class DateTimeDiffFormat extends React.Component {
128
123
  } else {
129
124
  res = res + next;
130
125
  }
131
-
132
126
  return res;
133
127
  }, '');
134
128
  let value = format(diffObj1, pattern);
135
-
136
129
  if (value && typeof value === 'object') {
137
130
  key = value.key;
138
131
  values = getValues(value.params, diffObj);
139
-
140
132
  if (pattern.indexOf('00000') === 0) {
141
133
  //suffix ignore for second hook
142
134
  isSuffixEnable = false;
@@ -149,32 +141,28 @@ export default class DateTimeDiffFormat extends React.Component {
149
141
  } else {
150
142
  let dateObj = new Date(toDateObj);
151
143
  let curDateObj = new Date(fromDateObj);
152
- let diffDayType = diffObj1.yDays; //In this condition, to calculate different days we have copied it from live --> diffDayType
144
+ let diffDayType = diffObj1.yDays;
153
145
 
146
+ //In this condition, to calculate different days we have copied it from live --> diffDayType
154
147
  if (isOverdue && dateObj.getDate() < curDateObj.getDate() && diffObj1.yDays == 0) {
155
148
  diffDayType = -1;
156
149
  }
157
-
158
150
  if (!isOverdue) {
159
151
  let diffHr = dateObj.getHours() - curDateObj.getHours();
160
-
161
152
  if (diffHr < 0) {
162
153
  diffDayType += 1;
163
154
  } else if (diffHr == 0) {
164
155
  let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
165
-
166
156
  if (diffMins < 0) {
167
157
  diffDayType += 1;
168
158
  } else if (diffMins == 0) {
169
159
  let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
170
-
171
160
  if (diffSec < 0) {
172
161
  diffDayType += 1;
173
162
  }
174
163
  }
175
164
  }
176
165
  }
177
-
178
166
  if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
179
167
  if (dateObj.getDate() === curDateObj.getDate()) {
180
168
  var value = today && today(diffObj1) || others(diffObj1);
@@ -191,7 +179,6 @@ export default class DateTimeDiffFormat extends React.Component {
191
179
  text = formatDate(toDateObj, value);
192
180
  }
193
181
  }
194
-
195
182
  return text ? /*#__PURE__*/React.createElement("span", {
196
183
  className: className,
197
184
  "data-title": title,
@@ -204,7 +191,6 @@ export default class DateTimeDiffFormat extends React.Component {
204
191
  "data-title": title
205
192
  });
206
193
  }
207
-
208
194
  }
209
195
  DateTimeDiffFormat.propTypes = {
210
196
  ago: PropTypes.string,
@@ -3,10 +3,10 @@ import PropTypes from 'prop-types';
3
3
  import I18N from "./I18N";
4
4
  export default class FormatText extends React.Component {
5
5
  render() {
6
- return /*#__PURE__*/React.createElement(I18N, { ...this.props
6
+ return /*#__PURE__*/React.createElement(I18N, {
7
+ ...this.props
7
8
  });
8
9
  }
9
-
10
10
  }
11
11
  FormatText.propTypes = {
12
12
  i18NKey: PropTypes.string.isRequired,
@@ -10,40 +10,32 @@ export default (function () {
10
10
  super(props);
11
11
  this.getI18NValue = this.getI18NValue.bind(this);
12
12
  }
13
-
14
13
  getI18NValue(key) {
15
14
  const {
16
15
  i18n
17
16
  } = this.context || {};
18
-
19
17
  if (typeof i18n === 'undefined') {
20
18
  return key;
21
19
  }
22
-
23
20
  let i18nStr = i18n[key];
24
-
25
21
  if (i18nStr === undefined) {
26
22
  return key;
27
23
  }
28
-
29
24
  return unescapeUnicode(i18nStr);
30
25
  }
31
-
32
26
  render() {
33
27
  let i18nProps = i18NKeys.reduce((result, key) => {
34
28
  if (this.props[key]) {
35
29
  result[key] = this.getI18NValue(this.props[key]);
36
30
  }
37
-
38
31
  return result;
39
32
  }, {});
40
- return /*#__PURE__*/React.createElement(Component, { ...this.props,
33
+ return /*#__PURE__*/React.createElement(Component, {
34
+ ...this.props,
41
35
  ...i18nProps
42
36
  });
43
37
  }
44
-
45
38
  }
46
-
47
39
  HOCI18N.contextType = I18NContext;
48
40
  return HOCI18N;
49
41
  };
@@ -9,7 +9,6 @@ export default class I18N extends React.Component {
9
9
  this.getI18NValue = this.getI18NValue.bind(this);
10
10
  this.createElement = this.createElement.bind(this);
11
11
  }
12
-
13
12
  getI18NValue() {
14
13
  const {
15
14
  i18NKey: key,
@@ -18,35 +17,28 @@ export default class I18N extends React.Component {
18
17
  const {
19
18
  i18n
20
19
  } = this.context || {};
21
-
22
20
  if (typeof i18n === 'undefined') {
23
21
  return key;
24
22
  }
25
-
26
23
  let i18nStr = i18n[key];
27
-
28
24
  if (i18nStr === undefined) {
29
25
  return key;
30
26
  }
31
-
32
27
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
33
28
  return unescapeUnicode(i18nStr);
34
29
  }
35
-
36
30
  createElement() {
37
31
  const props = Object.keys(this.props).reduce((result, nextKey) => {
38
32
  if (nextKey != 'i18NKey' && nextKey != 'tag' && nextKey != 'values' && nextKey != 'isHtml' && nextKey != 'dataId') {
39
33
  result[nextKey] = this.props[nextKey];
40
34
  }
41
-
42
35
  return result;
43
- }, {}); //const child=this.getI18NValue();
44
-
36
+ }, {});
37
+ //const child=this.getI18NValue();
45
38
  if (this.props.dataId) {
46
39
  props['data-id'] = this.props.dataId;
47
40
  props['data-test-id'] = this.props.dataId;
48
41
  }
49
-
50
42
  if (this.props.isHtml) {
51
43
  let dangerouslySetInnerHTML = {
52
44
  __html: HTMLPurifier.sanitize(this.getI18NValue())
@@ -55,14 +47,11 @@ export default class I18N extends React.Component {
55
47
  dangerouslySetInnerHTML
56
48
  }));
57
49
  }
58
-
59
50
  return /*#__PURE__*/React.createElement(this.props.tag, props, this.getI18NValue());
60
51
  }
61
-
62
52
  render() {
63
53
  return this.createElement();
64
54
  }
65
-
66
55
  }
67
56
  I18N.propTypes = {
68
57
  i18NKey: PropTypes.string.isRequired,
@@ -4,9 +4,7 @@ import { getI18NValue, userDateFormat } from "../utils";
4
4
  import { getI18NComponent } from "../utils/jsxTranslations";
5
5
  import { I18NContext } from "../I18NContext";
6
6
  const emptyObj = {};
7
-
8
7
  const dummy = (key, values) => key;
9
-
10
8
  export const i18NProviderUtils = {
11
9
  getI18NValue: dummy,
12
10
  userDateFormat: dummy,
@@ -17,12 +15,10 @@ export default class I18NProvider extends React.Component {
17
15
  super(props, context);
18
16
  i18NProviderUtils.getI18NValue = getI18NValue(props.i18n);
19
17
  i18NProviderUtils.getI18NComponent = getI18NComponent(props.i18n);
20
-
21
18
  if (props.tzData) {
22
19
  i18NProviderUtils.userDateFormat = userDateFormat(i18NProviderUtils.getI18NValue, props.tzData, props.timeFormat, props.datePattern, props.isEnabledCurrentYear);
23
20
  }
24
21
  }
25
-
26
22
  componentDidUpdate(prevProps) {
27
23
  let {
28
24
  i18n,
@@ -34,7 +30,6 @@ export default class I18NProvider extends React.Component {
34
30
  tzData,
35
31
  isEnabledCurrentYear
36
32
  } = this.props;
37
-
38
33
  if (i18n !== prevProps.i18n || timeZone !== prevProps.timeZone || datePattern !== prevProps.datePattern || timeFormat !== prevProps.timeFormat || direction !== prevProps.direction || tzData !== prevProps.tzData || isEnabledCurrentYear !== prevProps.isEnabledCurrentYear) {
39
34
  this.promise = new Promise((res, rej) => {
40
35
  this.resolve = res;
@@ -42,11 +37,9 @@ export default class I18NProvider extends React.Component {
42
37
  }).then(() => {
43
38
  i18NProviderUtils.getI18NValue = getI18NValue(this.props.i18n);
44
39
  i18NProviderUtils.getI18NComponent = getI18NComponent(this.props.i18n);
45
-
46
40
  if (this.props.tzData) {
47
41
  i18NProviderUtils.userDateFormat = userDateFormat(i18NProviderUtils.getI18NValue, this.props.tzData, this.props.timeFormat, this.props.datePattern, this.props.isEnabledCurrentYear);
48
42
  }
49
-
50
43
  this.promise = null;
51
44
  }, () => {
52
45
  this.promise = null;
@@ -54,7 +47,6 @@ export default class I18NProvider extends React.Component {
54
47
  onChange && onChange(this.resolve, this.reject);
55
48
  }
56
49
  }
57
-
58
50
  render() {
59
51
  return /*#__PURE__*/React.createElement(I18NContext.Provider, {
60
52
  value: {
@@ -68,7 +60,6 @@ export default class I18NProvider extends React.Component {
68
60
  }
69
61
  }, this.props.children);
70
62
  }
71
-
72
63
  }
73
64
  I18NProvider.defaultProps = {
74
65
  i18n: emptyObj,
@@ -10,8 +10,7 @@ export default class PluralFormat extends React.Component {
10
10
  value
11
11
  } = this.props;
12
12
  let key = '',
13
- values = '';
14
-
13
+ values = '';
15
14
  if (value > 1) {
16
15
  key = many;
17
16
  } else if (value == 1) {
@@ -19,9 +18,9 @@ export default class PluralFormat extends React.Component {
19
18
  } else if (value == 0) {
20
19
  key = zero;
21
20
  }
22
-
23
21
  values = `${value}`;
24
- return /*#__PURE__*/React.createElement(FormatText, { ...this.props,
22
+ return /*#__PURE__*/React.createElement(FormatText, {
23
+ ...this.props,
25
24
  i18NKey: key,
26
25
  values: values,
27
26
  one: null,
@@ -29,7 +28,6 @@ export default class PluralFormat extends React.Component {
29
28
  zero: null
30
29
  });
31
30
  }
32
-
33
31
  }
34
32
  PluralFormat.propTypes = {
35
33
  many: PropTypes.string,
@@ -31,7 +31,6 @@ export default class UserTimeDiffFormat extends Component {
31
31
  datePattern,
32
32
  isEnabledCurrentYear
33
33
  } = this.context || {};
34
-
35
34
  if (!format && !others) {
36
35
  format = _ref => {
37
36
  let {
@@ -42,7 +41,6 @@ export default class UserTimeDiffFormat extends Component {
42
41
  displayType === 'dateTime' ? dateTimeFormat : displayType === 'date' ? dateFormat : timeFormat;
43
42
  };
44
43
  }
45
-
46
44
  return /*#__PURE__*/React.createElement(DateTimeDiffFormat, {
47
45
  type: type,
48
46
  page: page,
@@ -68,7 +66,6 @@ export default class UserTimeDiffFormat extends Component {
68
66
  isDateField: isDateField
69
67
  });
70
68
  }
71
-
72
69
  }
73
70
  UserTimeDiffFormat.propTypes = {
74
71
  ago: PropTypes.string,