99 * invariants runtime.
1010 */
1111
12- goog . provide ( 'jspb.asserts' ) ;
12+ goog . module ( 'jspb.asserts' ) ;
13+ goog . module . declareLegacyNamespace ( ) ;
1314
1415/**
1516 * Throws an exception with the given message and "Assertion failed" prefixed
@@ -20,7 +21,7 @@ goog.provide('jspb.asserts');
2021 * @param {!Array<*> } givenArgs The substitution arguments for givenMessage.
2122 * @throws {Error } When the value is not a number.
2223 */
23- jspb . asserts . doAssertFailure = function ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
24+ function doAssertFailure ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
2425 let message = 'Assertion failed' ;
2526 let args ;
2627 if ( givenMessage ) {
@@ -46,13 +47,12 @@ jspb.asserts.doAssertFailure = function(defaultMessage, defaultArgs, givenMessag
4647 * @return {T } The value of the condition.
4748 * @throws {Error } When the condition evaluates to false.
4849 */
49-
50- jspb . asserts . assert = function ( condition , opt_message , ...args ) {
50+ function assert ( condition , opt_message , ...args ) {
5151 if ( ! condition ) {
52- jspb . asserts . doAssertFailure ( '' , null , opt_message , args ) ;
52+ doAssertFailure ( '' , null , opt_message , args ) ;
5353 }
5454 return condition ;
55- } ;
55+ }
5656
5757
5858/**
@@ -63,14 +63,14 @@ jspb.asserts.assert = function(condition, opt_message, ...args) {
6363 * @return {string } The value, guaranteed to be a string when asserts enabled.
6464 * @throws {Error } When the value is not a string.
6565 */
66- jspb . asserts . assertString = function ( value , opt_message , ...args ) {
66+ function assertString ( value , opt_message , ...args ) {
6767 if ( typeof value !== 'string' ) {
68- jspb . asserts . doAssertFailure (
68+ doAssertFailure (
6969 'Expected string but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
7070 opt_message , args ) ;
7171 }
7272 return /** @type {string } */ ( value ) ;
73- } ;
73+ }
7474
7575
7676/**
@@ -81,14 +81,14 @@ jspb.asserts.assertString = function(value, opt_message, ...args) {
8181 * @return {!Array<?> } The value, guaranteed to be a non-null array.
8282 * @throws {Error } When the value is not an array.
8383 */
84- jspb . asserts . assertArray = function ( value , opt_message , ...args ) {
84+ function assertArray ( value , opt_message , ...args ) {
8585 if ( ! Array . isArray ( value ) ) {
86- jspb . asserts . doAssertFailure (
86+ doAssertFailure (
8787 'Expected array but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
8888 opt_message , args ) ;
8989 }
9090 return /** @type {!Array<?> } */ ( value ) ;
91- } ;
91+ }
9292
9393/**
9494 * Triggers a failure. This function is useful in case when we want to add a
@@ -98,7 +98,7 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
9898 * switch(type) {
9999 * case FOO: doSomething(); break;
100100 * case BAR: doSomethingElse(); break;
101- * default: jspb.asserts.JspbFail ('Unrecognized type: ' + type);
101+ * default: jspb.asserts.fail ('Unrecognized type: ' + type);
102102 * // We have only 2 types - "default:" section is unreachable code.
103103 * }
104104 * </pre>
@@ -108,11 +108,11 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
108108 * @return {void }
109109 * @throws {Error } Failure.
110110 */
111- jspb . asserts . fail = function ( opt_message , ...args ) {
111+ function fail ( opt_message , ...args ) {
112112 throw new Error (
113113 'Failure' + ( opt_message ? ': ' + opt_message : '' ) ,
114114 args ) ;
115- } ;
115+ }
116116
117117/**
118118 * Checks if the value is an instance of the user-defined type.
@@ -130,15 +130,15 @@ jspb.asserts.fail = function(opt_message, ...args) {
130130 * @return {T }
131131 * @template T
132132 */
133- jspb . asserts . assertInstanceof = function ( value , type , opt_message , ...args ) {
133+ function assertInstanceof ( value , type , opt_message , ...args ) {
134134 if ( ! ( value instanceof type ) ) {
135- jspb . asserts . doAssertFailure (
135+ doAssertFailure (
136136 'Expected instanceof %s but got %s.' ,
137- [ jspb . asserts . getType ( type ) , jspb . asserts . getType ( value ) ] ,
137+ [ getType ( type ) , getType ( value ) ] ,
138138 opt_message , args ) ;
139139 }
140140 return value ;
141- } ;
141+ }
142142
143143/**
144144 * Returns the type of a value. If a constructor is passed, and a suitable
@@ -147,7 +147,7 @@ jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
147147 * @return {string } The best display name for the value, or 'unknown type name'.
148148 * @private
149149 */
150- jspb . asserts . getType = function ( value ) {
150+ function getType ( value ) {
151151 if ( value instanceof Function ) {
152152 return value . displayName || value . name || 'unknown type name' ;
153153 } else if ( value instanceof Object ) {
@@ -157,3 +157,13 @@ jspb.asserts.getType = function(value) {
157157 return value === null ? 'null' : typeof value ;
158158 }
159159}
160+
161+ exports = {
162+ doAssertFailure,
163+ assert,
164+ assertString,
165+ assertArray,
166+ fail,
167+ assertInstanceof,
168+ getType
169+ } ;
0 commit comments