bv-ui-core 0.1.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.
Files changed (122) hide show
  1. package/.eslintrc +40 -0
  2. package/.travis.yml +5 -0
  3. package/CONTRIBUTING.md +98 -0
  4. package/LICENSE.md +13 -0
  5. package/OWNERS +2 -0
  6. package/README.md +63 -0
  7. package/catalog-info.yaml +30 -0
  8. package/karma.conf.js +130 -0
  9. package/lib/README.md +61 -0
  10. package/lib/api/.babelrc +3 -0
  11. package/lib/api/.eslintrc +9 -0
  12. package/lib/api/README.md +134 -0
  13. package/lib/api/index.js +15 -0
  14. package/lib/api/statistics.js +148 -0
  15. package/lib/application/README.md +96 -0
  16. package/lib/application/index.js +171 -0
  17. package/lib/body/README.md +13 -0
  18. package/lib/body/index.js +19 -0
  19. package/lib/bvFetch/README.md +101 -0
  20. package/lib/bvFetch/index.js +383 -0
  21. package/lib/checkHighContrast/README.md +26 -0
  22. package/lib/checkHighContrast/index.js +36 -0
  23. package/lib/cookie/README.md +29 -0
  24. package/lib/cookie/index.js +111 -0
  25. package/lib/cookieConsent/README.md +41 -0
  26. package/lib/cookieConsent/index.js +227 -0
  27. package/lib/cssLoadChecker/README.md +19 -0
  28. package/lib/cssLoadChecker/index.js +72 -0
  29. package/lib/date.now/README.md +11 -0
  30. package/lib/date.now/index.js +40 -0
  31. package/lib/domainPolice/README.md +66 -0
  32. package/lib/domainPolice/index.js +95 -0
  33. package/lib/evented/README.md +42 -0
  34. package/lib/evented/index.js +135 -0
  35. package/lib/getOriginalConstructor/README.md +16 -0
  36. package/lib/getOriginalConstructor/index.js +52 -0
  37. package/lib/global/README.md +10 -0
  38. package/lib/global/index.js +48 -0
  39. package/lib/ie/README.md +13 -0
  40. package/lib/ie/index.js +25 -0
  41. package/lib/loader/README.md +73 -0
  42. package/lib/loader/index.js +358 -0
  43. package/lib/logger/README.md +82 -0
  44. package/lib/logger/index.js +180 -0
  45. package/lib/messageFormat/README.md +10 -0
  46. package/lib/messageFormat/index.js +42 -0
  47. package/lib/namespacer/README.md +57 -0
  48. package/lib/namespacer/index.js +98 -0
  49. package/lib/performance/README.md +35 -0
  50. package/lib/performance/entries.js +5 -0
  51. package/lib/performance/getEntries.js +42 -0
  52. package/lib/performance/getEntriesByName.js +61 -0
  53. package/lib/performance/getEntriesByType.js +55 -0
  54. package/lib/performance/index.js +11 -0
  55. package/lib/performance/mark.js +97 -0
  56. package/lib/performance/measure.js +106 -0
  57. package/lib/performance/now.js +63 -0
  58. package/lib/pixelsDisplayed/README.md +19 -0
  59. package/lib/pixelsDisplayed/index.js +113 -0
  60. package/lib/polyfills/README.md +39 -0
  61. package/lib/polyfills/fetch/Headers.js +3 -0
  62. package/lib/polyfills/fetch/Request.js +3 -0
  63. package/lib/polyfills/fetch/Response.js +3 -0
  64. package/lib/polyfills/fetch/fetch.js +3 -0
  65. package/lib/polyfills/fetch/index.js +57 -0
  66. package/lib/polyfills/promise.js +265 -0
  67. package/lib/queryShadowDom/README.md +50 -0
  68. package/lib/queryShadowDom/index.js +93 -0
  69. package/lib/region/README.md +64 -0
  70. package/lib/region/europe/index.js +94 -0
  71. package/lib/region/europe/territories.js +40 -0
  72. package/lib/region/index.js +123 -0
  73. package/lib/staticAssetLoader/README.md +116 -0
  74. package/lib/staticAssetLoader/index.js +248 -0
  75. package/lib/waitForBody/README.md +21 -0
  76. package/lib/waitForBody/index.js +36 -0
  77. package/mkdocs.yaml +5 -0
  78. package/package.json +55 -0
  79. package/test/.eslintrc +5 -0
  80. package/test/fixtures/lib.loader.document-is-wrong-in-ie8.js +5 -0
  81. package/test/fixtures/lib.loader.loadscript-attribute.js +3 -0
  82. package/test/fixtures/lib.loader.loadscript.js +3 -0
  83. package/test/fixtures/lib.loader.loadstylesheet.css +3 -0
  84. package/test/unit/api/.eslintrc +6 -0
  85. package/test/unit/api/index.spec.js +37 -0
  86. package/test/unit/api/response.json +67 -0
  87. package/test/unit/api/statistics.spec.js +218 -0
  88. package/test/unit/application/index.spec.js +313 -0
  89. package/test/unit/body/index.spec.js +15 -0
  90. package/test/unit/bvFetch/index.spec.js +246 -0
  91. package/test/unit/checkHighContrast/index.spec.js +16 -0
  92. package/test/unit/cookie/index.spec.js +191 -0
  93. package/test/unit/cookieConsent/index.spec.js +119 -0
  94. package/test/unit/cssLoadChecker/index.spec.js +75 -0
  95. package/test/unit/date.now/index.spec.js +21 -0
  96. package/test/unit/domainPolice/index.spec.js +141 -0
  97. package/test/unit/evented/index.spec.js +59 -0
  98. package/test/unit/getOriginalConstructor/index.spec.js +60 -0
  99. package/test/unit/global/index.spec.js +84 -0
  100. package/test/unit/ie/index.spec.js +14 -0
  101. package/test/unit/loader/index.spec.js +644 -0
  102. package/test/unit/logger/index.spec.js +141 -0
  103. package/test/unit/messageFormat/index.spec.js +31 -0
  104. package/test/unit/mochaInit.js +8 -0
  105. package/test/unit/namespacer/index.spec.js +100 -0
  106. package/test/unit/performance/getEntries.spec.js +30 -0
  107. package/test/unit/performance/getEntriesByName.spec.js +88 -0
  108. package/test/unit/performance/getEntriesByType.spec.js +48 -0
  109. package/test/unit/performance/index.spec.js +35 -0
  110. package/test/unit/performance/mark.spec.js +40 -0
  111. package/test/unit/performance/measure.spec.js +50 -0
  112. package/test/unit/performance/now.spec.js +21 -0
  113. package/test/unit/pixelsDisplayed/index.spec.js +271 -0
  114. package/test/unit/polyfills/fetch/Headers.js +12 -0
  115. package/test/unit/polyfills/fetch/Request.js +12 -0
  116. package/test/unit/polyfills/fetch/Response.js +12 -0
  117. package/test/unit/polyfills/fetch/fetch.js +12 -0
  118. package/test/unit/polyfills/fetch/index.js +61 -0
  119. package/test/unit/polyfills/promise.spec.js +48 -0
  120. package/test/unit/region/europe/index.spec.js +70 -0
  121. package/test/unit/region/index.spec.js +113 -0
  122. package/test/unit/staticAssetLoader/index.spec.js +215 -0
@@ -0,0 +1,358 @@
1
+ /* eslint no-use-before-define: 0 */
2
+ /**
3
+ * @fileOverview
4
+ * Load JavaScript and CSS at runtime.
5
+ *
6
+ * This module is derived from yepnope.js 2.0.0:
7
+ * Copyright (c) 2014, Alex Sexton
8
+ * Copyright (c) 2014, Ralph Holzmann
9
+ * All rights reserved.
10
+ * New BSD license:
11
+ * https://github.com/SlexAxton/yepnope.js/blob/master/LICENSE.md
12
+ *
13
+ * @module
14
+ */
15
+
16
+ var global = require('../global');
17
+ var namespacer = require('../namespacer');
18
+ var doc = global.document;
19
+
20
+ var DEFAULT_TIMEOUT = 10000;
21
+ var loadedUrls = {};
22
+
23
+ function getFirstScript () {
24
+ return doc.getElementsByTagName('script')[0];
25
+ }
26
+
27
+ function isFileReady (readyState) {
28
+ // the file is ready if there is no readyState, or if it has reached a
29
+ // terminal state.
30
+ return !readyState ||
31
+ readyState === 'loaded' ||
32
+ readyState === 'complete' ||
33
+ readyState === 'uninitialized';
34
+ }
35
+
36
+ function validateArguments (url, options, callback) {
37
+ if (!url || typeof url !== 'string') {
38
+ throw new Error('`url` must be a string');
39
+ }
40
+
41
+ if (options.namespaceName && typeof options.namespaceName !== 'string') {
42
+ throw new Error('`options.namespaceName` must be a string');
43
+ }
44
+
45
+ if (options.forceLoad && typeof options.forceLoad !== 'boolean') {
46
+ throw new Error('`options.forceLoad` must be a boolean');
47
+ }
48
+
49
+ if (typeof options.timeout !== 'number') {
50
+ throw new Error('`options.timeout` must be a number');
51
+ }
52
+
53
+ if (callback && typeof callback !== 'function') {
54
+ throw new Error('`callback` must be a function');
55
+ }
56
+ }
57
+
58
+ module.exports = {
59
+ /**
60
+ * Clears the loaded URLs
61
+ *
62
+ * Clears any URLs that might have been stored in the loadedUrls hash, either
63
+ * locally or in the given namespace(optional). This is needed for testing so
64
+ * we don't need unique URLs for every test, but rather this can be used as a
65
+ * means of cleaning up.
66
+ *
67
+ * @param {string} [namespace] - contains the name of the namespace to clear
68
+ * the loadedUrls hash in.
69
+ */
70
+ _clearLoadedUrls: function (namespaceName) {
71
+ if (namespaceName) {
72
+ var namespace = namespacer.namespace(namespaceName);
73
+ namespace.loadedUrls = {};
74
+ }
75
+ else {
76
+ loadedUrls = {};
77
+ }
78
+ },
79
+
80
+ /**
81
+ * Load a script from a URL
82
+ *
83
+ * The script is loaded on a later turn of the event loop. If a callback is
84
+ * provided, it will be invoked on success in all supported browsers. The
85
+ * callback will execute after the script.
86
+ *
87
+ * In modern IE and other browsers, the callback will be invoked with an error
88
+ * on failure as well. A timeout in milliseconds may be provided as
89
+ * `options.timeout`. If this timeout expires before loading fails or
90
+ * succeeds, the callback will be invoked with an error.
91
+ *
92
+ * @param {string} url - URL of the script
93
+ * @param {object} [options]
94
+ * @param {object} options.attributes - an object containing key/value pairs
95
+ * to be used as attributes on the created `script` element (optional)
96
+ * @param {number} options.timeout - timeout in milliseconds (defaults to
97
+ * 10000ms)
98
+ * @param {boolean} options.forceLoad - whether to load the script at the
99
+ * specified url or not regardless of whether an earlier attempt to do so has
100
+ * already been made
101
+ * @param {string} options.namespaceName - the name of the namespace to check
102
+ * if the script has been previously loaded and store the loadedUrls hash
103
+ * @param {function} [callback] - node-style callback
104
+ */
105
+ loadScript: function (url, options, callback) {
106
+ var _loadedUrls = loadedUrls;
107
+ var namespace;
108
+
109
+ if (typeof options === 'function') {
110
+ callback = options;
111
+ options = null;
112
+ }
113
+
114
+ options = options || {};
115
+ options.timeout = options.timeout || DEFAULT_TIMEOUT;
116
+
117
+ validateArguments(url, options, callback);
118
+
119
+ /**
120
+ * The goal of using the namespacer is to allow multiple apps on the same
121
+ * page to share the properties within the namespace, namely loadedUrls. For
122
+ * this reason, we are forgoing the use of registerProperty to instantiate
123
+ * the list of loaded URLs. This will allow the loader module to be run in
124
+ * different apps but access the same namespace property.
125
+ */
126
+
127
+ if (options.namespaceName) {
128
+ namespace = namespacer.namespace(options.namespaceName);
129
+ _loadedUrls = namespace.loadedUrls || {};
130
+ }
131
+
132
+ if (!options.forceLoad && _loadedUrls[url]) {
133
+ // File at given url has already been loaded and option has not been set
134
+ // to reload it, so we'll just call the callback and return.
135
+ if (typeof callback === 'function') {
136
+ callback();
137
+ }
138
+ return;
139
+ }
140
+
141
+ var script = doc.createElement('script');
142
+ var done = false;
143
+ var attr;
144
+
145
+ if (options.attributes) {
146
+ for (attr in options.attributes) {
147
+ script.setAttribute(attr, options.attributes[attr]);
148
+ }
149
+ }
150
+
151
+ function cleanUp (err) {
152
+ done = true;
153
+ clearTimeout(timeoutHandle);
154
+ script.onload = script.onreadystatechange = script.onerror = null;
155
+ if (script && script.parentNode) {
156
+ script.parentNode.removeChild(script);
157
+ }
158
+ if (!err) {
159
+ _loadedUrls[url] = true;
160
+
161
+ if (options.namespaceName) {
162
+ namespace.loadedUrls = _loadedUrls;
163
+ }
164
+ else {
165
+ loadedUrls = _loadedUrls;
166
+ }
167
+ }
168
+
169
+ if (typeof callback === 'function') {
170
+ callback(err);
171
+ }
172
+ }
173
+
174
+ script.onreadystatechange = script.onload = function () {
175
+ if (done || !isFileReady(script.readyState)) {
176
+ return;
177
+ }
178
+
179
+ cleanUp();
180
+ };
181
+
182
+ script.onerror = function () {
183
+ if (done) {
184
+ return;
185
+ }
186
+
187
+ cleanUp(new Error('Error: could not load ' + url));
188
+ };
189
+
190
+ // We should be able to catch most errors with onerror, but for browsers
191
+ // that don't support onerror we provide the below timeout cb
192
+ var timeoutHandle = setTimeout(function () {
193
+ if (done) {
194
+ return;
195
+ }
196
+
197
+ cleanUp(new Error('Error: script timeout ' + url));
198
+ }, options.timeout);
199
+
200
+ // If a script is cached, IE may execute it immediately, which breaks
201
+ // JavaScript's run-to-completion semantics. So, don't try to load the
202
+ // script until a later turn of the event loop.
203
+ //
204
+ // See http://www.guypo.com/ies-premature-execution-problem/
205
+ setTimeout(function () {
206
+ script.src = url;
207
+
208
+ var firstScript = getFirstScript();
209
+
210
+ firstScript.parentNode.insertBefore(script, firstScript);
211
+ }, 0);
212
+ },
213
+
214
+ /**
215
+ * Load a stylesheet from a URL
216
+ *
217
+ * The stylesheet is loaded on a later turn of the event loop. If a callback
218
+ * is provided, it will be invoked on success in all supported browsers.
219
+ *
220
+ * In modern IE and other browsers, the callback will be invoked with an error
221
+ * on failure as well. A timeout in milliseconds may be provided as
222
+ * `options.timeout`. If this timeout expires before loading fails or
223
+ * succeeds, the callback will be invoked with an error.
224
+ *
225
+ * @param {string} url - URL of the stylesheet
226
+ * @param {object} [options]
227
+ * @param {object} options.attributes - an object containing key/value pairs
228
+ * to be used as attributes on the created `link` element (optional)
229
+ * @param {number} options.timeout - timeout in milliseconds (defaults to
230
+ * 1000ms)
231
+ * @param {boolean} options.forceLoad - whether to load the style sheet at the
232
+ * specified url or not regardless of whether an earlier attempt to do so has
233
+ * already been made
234
+ * @param {string} options.namespaceName - the name of the namespace to check
235
+ * if the stylesheet has been previously loaded and store the loadedUrls hash
236
+ * @param {element} options.injectionNode - node to inject link element
237
+ * into (defaults to parent node of the first script node)
238
+ * @param {function} [callback] - node-style callback
239
+ */
240
+ loadStyleSheet: function (url, options, callback) {
241
+ var _loadedUrls = loadedUrls;
242
+ var namespace;
243
+
244
+ if (typeof options === 'function') {
245
+ callback = options;
246
+ options = null;
247
+ }
248
+ options = options || {};
249
+ options.timeout = options.timeout || DEFAULT_TIMEOUT;
250
+
251
+ validateArguments(url, options, callback);
252
+
253
+ // This check doesn't happen in `validateArguments` because it's unique
254
+ // to `loadStyleSheet`.
255
+ if (
256
+ 'injectionNode' in options &&
257
+ !(options.injectionNode instanceof global.Element)
258
+ ) {
259
+ throw new Error('`options.injectionNode` must be a DOM node');
260
+ }
261
+
262
+ /**
263
+ * The goal of using the namespacer is to allow multiple apps on the same
264
+ * page to share the properties within the namespace, namely loadedUrls. For
265
+ * this reason, we are forgoing the use of registerProperty to instantiate
266
+ * the list of loaded URLs. This will allow the loader module to be run in
267
+ * different apps but access the same namespace property.
268
+ */
269
+
270
+ if (options.namespaceName) {
271
+ namespace = namespacer.namespace(options.namespaceName);
272
+ _loadedUrls = namespace.loadedUrls || {};
273
+ }
274
+
275
+ if (!options.forceLoad && _loadedUrls[url]) {
276
+ // File at given url has already been loaded and option has not been set
277
+ // to reload it, so we'll just call the callback and return.
278
+ if (typeof callback === 'function') {
279
+ callback();
280
+ }
281
+ return;
282
+ }
283
+
284
+ var link = doc.createElement('link');
285
+ var done = false;
286
+ var attr;
287
+
288
+ if (options.attributes) {
289
+ for (attr in options.attributes) {
290
+ link.setAttribute(attr, options.attributes[attr]);
291
+ }
292
+ }
293
+
294
+ function cleanUp (err) {
295
+ done = true;
296
+ clearTimeout(timeoutHandle);
297
+ link.onload = link.onreadystatechange = link.onerror = null;
298
+
299
+ if (!err) {
300
+ _loadedUrls[url] = true;
301
+
302
+ if (options.namespaceName) {
303
+ namespace.loadedUrls = _loadedUrls;
304
+ }
305
+ else {
306
+ loadedUrls = _loadedUrls;
307
+ }
308
+ }
309
+
310
+ if (typeof callback === 'function') {
311
+ callback(err);
312
+ }
313
+ }
314
+
315
+ link.onreadystatechange = link.onload = function () {
316
+ if (done || !isFileReady(link.readyState)) {
317
+ return;
318
+ }
319
+
320
+ cleanUp();
321
+ };
322
+
323
+ link.onerror = function () {
324
+ if (done) {
325
+ return;
326
+ }
327
+
328
+ cleanUp(new Error('Error: could not load ' + url));
329
+ };
330
+
331
+ var timeoutHandle = setTimeout(link.onerror, options.timeout);
332
+
333
+ // Inject the stylesheet on a later turn of the event loop
334
+ setTimeout(function () {
335
+ // technique to force non-blocking loading from:
336
+ // https://github.com/filamentgroup/loadCSS/blob/master/loadCSS.js#L20
337
+ link.media = 'only x';
338
+
339
+ link.rel = 'stylesheet';
340
+ link.type = 'text/css';
341
+ link.href = url;
342
+
343
+ // on next tick, set `media` back
344
+ setTimeout(function () {
345
+ link.media = 'all';
346
+ }, 0);
347
+
348
+ var injectionNode = options.injectionNode || getFirstScript().parentNode;
349
+
350
+ try {
351
+ injectionNode.appendChild(link);
352
+ }
353
+ catch (error) {
354
+ cleanUp(new Error('Error: could not append LINK element'));
355
+ }
356
+ }, 0);
357
+ }
358
+ };
@@ -0,0 +1,82 @@
1
+ # logger
2
+
3
+ The `logger` module provides an object that wraps various console logging methods safely. The default level is OFF, but it is recommended that you set this according to your environment either at build time or by a cookie in the browser in the format `_bvui_debug=loglevel:1`.
4
+
5
+ ## Usage
6
+
7
+ ```javascript
8
+ var Logger = require('bv-ui-core/lib/logger');
9
+ // <method> is one of the 5 types, ex: log
10
+ Logger.<method>('Hello, world!') // console.log('Hello, world!')
11
+ ```
12
+
13
+ ## Other Methods
14
+ ### Logger.setLogLevel
15
+
16
+ Set the log level to one of the following to prevent logging under the specified level.
17
+ ```
18
+ Logger.DEBUG
19
+ Logger.LOG
20
+ Logger.INFO
21
+ Logger.WARN
22
+ Logger.ERROR
23
+ Logger.OFF
24
+ ```
25
+
26
+ ```javascript
27
+ var Logger = require('bv-ui-core/lib/logger');
28
+
29
+ // Set's the default log level to one of the valid levels
30
+ // DEBUG, LOG, INFO, WARN, ERROR, OFF
31
+ Logger.setLogLevel(Logger.DEBUG)
32
+
33
+ Logger.log('Hello, World!') // Prints Hello, World!
34
+
35
+ Logger.setLogLevel(Logger.INFO)
36
+
37
+ Logger.log('Hello, World!') // Prints nothing because of logLevel
38
+ ```
39
+
40
+ If you have set the logLevel via cookie, any calls to `setLogLevel` in your application will be ignored unless you also pass a force param.
41
+
42
+ ```javascript
43
+ setLogLevel(Logger.LOG, true); // forces LogLevel to LOG
44
+ ```
45
+
46
+ ### Logger.group/Logger.groupEnd
47
+ https://developer.mozilla.org/en-US/docs/Web/API/console/group
48
+
49
+ ### Logger.time/Logger.timeEnd
50
+ https://developer.mozilla.org/en-US/docs/Web/API/console/time
51
+
52
+ ### Logger.assert
53
+
54
+ Assertions can be made if `logLevel` is not `Logger.OFF`. You can assert a function's result or a boolean value.
55
+
56
+ ```javascript
57
+ Logger.assert(() => {
58
+ return false;
59
+ }, 'The function failed!'
60
+ );
61
+
62
+ Logger.assert(true === false, 'Obviously, I failed.');
63
+ ```
64
+
65
+ ## Bookmarklet
66
+
67
+ You can use the following bookmarklet in a browser to specify your loglevel. Save the below script as a bookmark and click on it when you want to modify the stored cookie value.
68
+
69
+ ```javascript
70
+ javascript:(function() { var val = prompt('Enter log level (From -1 to 4)'); if (!val && val !== 0) { return; } document.cookie='_bvui_debug=logLevel:' + val; }());
71
+ ```
72
+
73
+ Here are the values for log level. This bookmarklet will show you everything above the log level you enter as well (so if you enter `1`, you will see values for log level 1, 2, and 3).
74
+
75
+ ```
76
+ DEBUG: -1, // very noisy
77
+ LOG: 0,
78
+ INFO: 1, // recommended
79
+ WARN: 2,
80
+ ERROR: 3,
81
+ OFF: 4, // don't log anything
82
+ ```
@@ -0,0 +1,180 @@
1
+ /*jshint expr:true */
2
+ /*global console:false */
3
+
4
+ /**
5
+ * @fileOverview Logger provides a wrapper around the console object. It should be used for creating
6
+ * messages that will be helpful for developers. Logger methods can safely be kept in the
7
+ * code -- that is, you don't need to remove them like you need to remove console.log statements.
8
+ *
9
+ * Logger should be used sparingly outside of framework files. When you use it, be thoughtful
10
+ * about the level of logging you choose. Most messages should be at the "debug" or "log" level, so
11
+ * developers can easily silence them. Messages at the info level will generally not be silenced
12
+ * during development, and messages at the warn and error level will almost never be silenced.
13
+ *
14
+ * Logger also provides the ability to make assertions. Assertions are useful when other
15
+ * developers may use your code in unpredictable ways. You can write an assertion so a fellow
16
+ * developer will know at a glance what your code expects, and so that they will get a useful
17
+ * error message if they violate those expectations.
18
+ *
19
+ * @type {Object}
20
+ */
21
+
22
+ var global = require('../global');
23
+ var ie = require('../ie')
24
+ var cookie = require('../cookie');
25
+
26
+ var logLevel = 4; // Default to OFF.
27
+
28
+ /**
29
+ * Set the logLevel to whatever the _bvui_debug cookie has stored.
30
+ */
31
+ var cookieObj = {};
32
+ var debugCookie = cookie.read('_bvui_debug');
33
+ if (debugCookie) {
34
+ var val;
35
+ var values = debugCookie.split(',');
36
+
37
+ for (var i = 0; i < values.length; i++) {
38
+ val = values[i].split(':');
39
+ cookieObj[ val[0] ] = val[1];
40
+ }
41
+ }
42
+
43
+ if (cookieObj.logLevel) {
44
+ logLevel = cookieObj.logLevel;
45
+ }
46
+
47
+ var Logger = {
48
+ // Assign these constants so we don't have to remember them.
49
+ DEBUG: -1, // very noisy
50
+ LOG: 0,
51
+ INFO: 1, // recommended
52
+ WARN: 2,
53
+ ERROR: 3,
54
+ OFF: 4, // don't log anything
55
+
56
+ getLogLevel: function () {
57
+ return logLevel;
58
+ },
59
+
60
+ /**
61
+ * Only sets the log level if there is no cookie or if passed a force param.
62
+ *
63
+ * @param {[type]} level logLevel
64
+ * @param {[type]} force arg to force setting level to the level arg
65
+ */
66
+ setLogLevel: function (level, force) {
67
+ logLevel = (!cookieObj.logLevel || force) ?
68
+ (level|| Logger.LOG) :
69
+ cookieObj.logLevel;
70
+ logger('log', [ 'Log level set to', logLevel ]);
71
+ },
72
+
73
+ debug: function () {
74
+ logLevel <= Logger.DEBUG && logger('log', arguments);
75
+ },
76
+
77
+ log: function () {
78
+ logLevel <= Logger.LOG && logger('log', arguments);
79
+ },
80
+
81
+ info: function () {
82
+ logLevel <= Logger.INFO && logger('info', arguments);
83
+ },
84
+
85
+ warn: function () {
86
+ logLevel <= Logger.WARN && logger('warn', arguments);
87
+ },
88
+
89
+ error: function () {
90
+ logLevel <= Logger.ERROR && logger('error', arguments);
91
+ },
92
+
93
+ count: function () {
94
+ logLevel < Logger.INFO && logger('count', arguments);
95
+ },
96
+
97
+ time: function () {
98
+ logLevel < Logger.WARN && logger('time', arguments);
99
+ },
100
+
101
+ timeEnd: function () {
102
+ logLevel < Logger.WARN && logger('timeEnd', arguments);
103
+ },
104
+
105
+ group: function (maxLevel) {
106
+ var args = Array.prototype.slice.call(arguments, 1);
107
+ if (logLevel <= maxLevel) {
108
+ logger('group', args);
109
+ logger('time', args);
110
+ }
111
+ },
112
+
113
+ groupEnd: function (maxLevel) {
114
+ var args = Array.prototype.slice.call(arguments, 1);
115
+ if (logLevel <= maxLevel) {
116
+ logger('groupEnd', args);
117
+ logger('timeEnd', args);
118
+ }
119
+ },
120
+
121
+ /**
122
+ * Assert that an expression is true, and throw the provided message if it is not.
123
+ *
124
+ * @param {Expression} assertion
125
+ * @param {String} message
126
+ */
127
+ assert: function (assertion, message) {
128
+ if (logLevel === Logger.OFF) {
129
+ return;
130
+ }
131
+
132
+ // If we want to assert something computationally expensive, we can pass a function
133
+ // as the sole argument to Logger.assert. The function should return an array with
134
+ // two items: the assertion, and the message.
135
+ if (typeof assertion === 'function') {
136
+ Logger.assert.apply(null, assertion());
137
+ return;
138
+ }
139
+
140
+ if (!assertion) {
141
+ throw new Error('Assertion failed: ' + message);
142
+ }
143
+ }
144
+ };
145
+
146
+ function logger (level, args) {
147
+ if (logLevel === Logger.OFF) {
148
+ return;
149
+ }
150
+ if (!global.console) {
151
+ return;
152
+ }
153
+
154
+ // If we want to log something that's computationally expensive, we can pass a
155
+ // function to a Logger method as its sole argument. The function should
156
+ // return the arguments to be logged; the function will only ever be executed
157
+ // in the development environment.
158
+ args = [].slice.call(args);
159
+
160
+ if (typeof args[0] === 'function') {
161
+ args = args[0]();
162
+ }
163
+
164
+ if (ie) {
165
+ global.console.log(args);
166
+ return;
167
+ }
168
+
169
+ try {
170
+ (global.console[level] || global.console.log).apply(global.console, args);
171
+ }
172
+ catch (e) {
173
+ // If we are unable to call, log then we cannot do anything but silently
174
+ // fail. In reality, you will either see your logs or you wont. And if you
175
+ // don't see the logs, there are only two cases: 1 - your logLevel is
176
+ // incorrect or 2 - logging is failing.
177
+ }
178
+ }
179
+
180
+ module.exports = Logger;
@@ -0,0 +1,10 @@
1
+ # messageFormat
2
+
3
+ Uses `INTL.DisplayNames` and uses a polyfill from `https://cdn.polyfill.io/v3/polyfill.min.js?features=Intl.DisplayNames`
4
+
5
+ # Usage
6
+
7
+ ```javascript
8
+ var messageFormat = require('bv-ui-core/lib/messageFormat');
9
+ messageFormat.generateCountryFromLocale('en_US') // United States
10
+ ```
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @fileOverview
3
+ * messageFormat provides utility functions to which use the INTL API which provides
4
+ * language sensitive string comparison, number formatting, and date and time formatting.
5
+ *
6
+ * messageFormat can have functions which are used in multiple places
7
+ *
8
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
9
+ */
10
+
11
+
12
+ var global = require('../global');
13
+ /**
14
+ * Generates the country name associated with the given locale
15
+ * @param {string} locale The locale code (e.g., 'en_US')
16
+ * @returns {string} The name of the country associated with the locale, or an empty string if the locale is not supported by Intl.DisplayNames
17
+ */
18
+ function generateCountryFromLocale (locale) {
19
+ if (!locale || locale.length === 0) {
20
+ return '';
21
+ }
22
+
23
+ // Split the locale code into language and region components
24
+ try {
25
+ var localeArr = locale.split('_');
26
+ if (localeArr.length < 2) {
27
+ return '';
28
+ }
29
+ // Get the display name for the region (i.e., the country name)
30
+ var regionNames = new global.Intl.DisplayNames(localeArr[0], { type: 'region' });
31
+ var countryName = regionNames.of(localeArr[1]);
32
+ }
33
+ catch (error) {
34
+ return '';
35
+ }
36
+ // Return the country name, or an empty string if the region is not valid
37
+ return countryName
38
+ }
39
+
40
+ module.exports = {
41
+ generateCountryFromLocale: generateCountryFromLocale
42
+ }