From 1a33d40fa14dc8e8904c91e9f166405a1eb2fe5b Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 11 Jul 2015 09:51:54 -0400 Subject: [PATCH] Add browser(ify) friendly version --- browser.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 9 ++++++++ 2 files changed, 69 insertions(+) create mode 100644 browser.js diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..2409d27 --- /dev/null +++ b/browser.js @@ -0,0 +1,60 @@ +/** + * Copyright 2014-2015, 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 this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +/** + * Similar to invariant but only logs a warning if the condition is not met. + * This can be used to log issues in development environments in critical + * paths. Removing the logging code for production environments will keep the + * same logic and follow the same code paths. + */ + +var warning = function() {}; + +if (process.env.NODE_ENV !== 'production') { + warning = function(condition, format, args) { + var len = arguments.length; + args = new Array(len > 2 ? len - 2 : 0); + for (var key = 2; key < len; key++) { + args[key - 2] = arguments[key]; + } + if (format === undefined) { + throw new Error( + '`warning(condition, format, ...args)` requires a warning ' + + 'message argument' + ); + } + + if (format.length < 10 || (/^[s\W]*$/).test(format)) { + throw new Error( + 'The warning format should be able to uniquely identify this ' + + 'warning. Please, use a more descriptive format than: ' + format + ); + } + + if (!condition) { + var argIndex = 0; + var message = 'Warning: ' + + format.replace(/%s/g, function() { + return args[argIndex++]; + }); + if (typeof console !== 'undefined') { + console.error(message); + } + try { + // This error was thrown as a convenience so that you can use this stack + // to find the callsite that caused this warning to fire. + throw new Error(message); + } catch(x) {} + } + }; +} + +module.exports = warning; diff --git a/package.json b/package.json index f9b4aad..71b53fd 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,18 @@ "version": "1.0.2", "description": "A mirror of Facebook's Warning", "main": "warning.js", + "browser": "browser.js", + "browserify": { + "transform": [ + "envify" + ] + }, "scripts": { "test": "echo 'you've been warned'" }, + "dependencies": { + "envify": "^3.0.0" + }, "repository": { "type": "git", "url": "/~https://github.com/r3dm/warning.git"