Skip to content

Commit 29b3d03

Browse files
author
Berkeley Martinez
committed
Merge pull request #2 from zertosh/browser-friendliness
Browser(ify) friendliness
2 parents 3b16be8 + 3c1a900 commit 29b3d03

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

browser.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2014-2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
'use strict';
11+
12+
/**
13+
* Similar to invariant but only logs a warning if the condition is not met.
14+
* This can be used to log issues in development environments in critical
15+
* paths. Removing the logging code for production environments will keep the
16+
* same logic and follow the same code paths.
17+
*/
18+
19+
var warning = function() {};
20+
21+
if (process.env.NODE_ENV !== 'production') {
22+
warning = function(condition, format, args) {
23+
var len = arguments.length;
24+
args = new Array(len > 2 ? len - 2 : 0);
25+
for (var key = 2; key < len; key++) {
26+
args[key - 2] = arguments[key];
27+
}
28+
if (format === undefined) {
29+
throw new Error(
30+
'`warning(condition, format, ...args)` requires a warning ' +
31+
'message argument'
32+
);
33+
}
34+
35+
if (format.length < 10 || (/^[s\W]*$/).test(format)) {
36+
throw new Error(
37+
'The warning format should be able to uniquely identify this ' +
38+
'warning. Please, use a more descriptive format than: ' + format
39+
);
40+
}
41+
42+
if (!condition) {
43+
var argIndex = 0;
44+
var message = 'Warning: ' +
45+
format.replace(/%s/g, function() {
46+
return args[argIndex++];
47+
});
48+
if (typeof console !== 'undefined') {
49+
console.error(message);
50+
}
51+
try {
52+
// This error was thrown as a convenience so that you can use this stack
53+
// to find the callsite that caused this warning to fire.
54+
throw new Error(message);
55+
} catch(x) {}
56+
}
57+
};
58+
}
59+
60+
module.exports = warning;

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
"version": "1.0.2",
44
"description": "A mirror of Facebook's Warning",
55
"main": "warning.js",
6+
"browser": "browser.js",
7+
"browserify": {
8+
"transform": [
9+
"envify"
10+
]
11+
},
612
"scripts": {
713
"test": "echo 'you've been warned'"
814
},
15+
"dependencies": {
16+
"envify": "^3.0.0"
17+
},
918
"repository": {
1019
"type": "git",
1120
"url": "https://github.com/r3dm/warning.git"
@@ -17,7 +26,7 @@
1726
"invariant"
1827
],
1928
"author": "Berkeley Martinez <[email protected]> (http://r3dm.com)",
20-
"license": "BSD",
29+
"license": "BSD-2-Clause",
2130
"bugs": {
2231
"url": "https://github.com/r3dm/warning/issues"
2332
},

0 commit comments

Comments
 (0)