forked from formatjs/formatjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
114 lines (94 loc) · 2.58 KB
/
Copy pathutils.js
File metadata and controls
114 lines (94 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
HTML escaping and shallow-equals implementations are the same as React's
(on purpose.) Therefore, it has the following Copyright and Licensing:
Copyright 2013-2014, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the LICENSE
file in the root directory of React's source tree.
*/
import invariant from 'invariant';
import {intlConfigPropTypes} from './types';
const intlConfigPropNames = Object.keys(intlConfigPropTypes);
const ESCAPED_CHARS = {
'&': '&',
'>': '>',
'<': '<',
'"': '"',
"'": ''',
};
const UNSAFE_CHARS_REGEX = /[&><"']/g;
export function escape(str) {
return ('' + str).replace(UNSAFE_CHARS_REGEX, match => ESCAPED_CHARS[match]);
}
export function filterProps(props, whitelist, defaults = {}) {
return whitelist.reduce((filtered, name) => {
if (props.hasOwnProperty(name)) {
filtered[name] = props[name];
} else if (defaults.hasOwnProperty(name)) {
filtered[name] = defaults[name];
}
return filtered;
}, {});
}
export function invariantIntlContext({intl} = {}) {
invariant(
intl,
'[React Intl] Could not find required `intl` object. ' +
'<IntlProvider> needs to exist in the component ancestry.'
);
}
export function shallowEquals(objA, objB) {
if (objA === objB) {
return true;
}
if (
typeof objA !== 'object' ||
objA === null ||
typeof objB !== 'object' ||
objB === null
) {
return false;
}
let keysA = Object.keys(objA);
let keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
// Test for A's keys different from B.
let bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
for (let i = 0; i < keysA.length; i++) {
if (!bHasOwnProperty(keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
return false;
}
}
return true;
}
export function shouldIntlComponentUpdate(
{props, state, context = {}},
nextProps,
nextState,
nextContext = {}
) {
const {intl = {}} = context;
const {intl: nextIntl = {}} = nextContext;
return (
!shallowEquals(nextProps, props) ||
!shallowEquals(nextState, state) ||
!(
nextIntl === intl ||
shallowEquals(
filterProps(nextIntl, intlConfigPropNames),
filterProps(intl, intlConfigPropNames)
)
)
);
}
export function createError(message, exception) {
const eMsg = exception ? `\n${exception}` : '';
return `[React Intl] ${message}${eMsg}`;
}
export function defaultErrorHandler(error) {
if (process.env.NODE_ENV !== 'production') {
console.error(error);
}
}