1111
1212goog . provide ( 'jspb.asserts' ) ;
1313
14- /**
15- * Does simple python-style string substitution.
16- * subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
17- * @param {string } pattern The string containing the pattern.
18- * @param {!Array<*> } subs The items to substitute into the pattern.
19- * @return {string } A copy of `str` in which each occurrence of
20- * `%s` has been replaced an argument from `var_args`.
21- */
22- function subs ( pattern , subs ) {
23- const splitParts = pattern . split ( '%s' ) ;
24- let returnString = '' ;
25-
26- // Replace up to the last split part. We are inserting in the
27- // positions between split parts.
28- const subLast = splitParts . length - 1 ;
29- for ( let i = 0 ; i < subLast ; i ++ ) {
30- // keep unsupplied as '%s'
31- const sub = ( i < subs . length ) ? subs [ i ] : '%s' ;
32- returnString += splitParts [ i ] + sub ;
33- }
34- return returnString + splitParts [ subLast ] ;
35- }
36-
3714/**
3815 * Throws an exception with the given message and "Assertion failed" prefixed
3916 * onto it.
@@ -43,7 +20,7 @@ function subs(pattern, subs) {
4320 * @param {!Array<*> } givenArgs The substitution arguments for givenMessage.
4421 * @throws {Error } When the value is not a number.
4522 */
46- function doAssertFailure ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
23+ jspb . asserts . doAssertFailure = function ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
4724 let message = 'Assertion failed' ;
4825 let args ;
4926 if ( givenMessage ) {
@@ -72,7 +49,7 @@ function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
7249
7350jspb . asserts . assert = function ( condition , opt_message , ...args ) {
7451 if ( ! condition ) {
75- doAssertFailure ( '' , null , opt_message , args ) ;
52+ jspb . asserts . doAssertFailure ( '' , null , opt_message , args ) ;
7653 }
7754 return condition ;
7855} ;
@@ -88,7 +65,7 @@ jspb.asserts.assert = function(condition, opt_message, ...args) {
8865 */
8966jspb . asserts . assertString = function ( value , opt_message , ...args ) {
9067 if ( typeof value !== 'string' ) {
91- doAssertFailure (
68+ jspb . asserts . doAssertFailure (
9269 'Expected string but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
9370 opt_message , args ) ;
9471 }
@@ -106,7 +83,7 @@ jspb.asserts.assertString = function(value, opt_message, ...args) {
10683 */
10784jspb . asserts . assertArray = function ( value , opt_message , ...args ) {
10885 if ( ! Array . isArray ( value ) ) {
109- doAssertFailure (
86+ jspb . asserts . doAssertFailure (
11087 'Expected array but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
11188 opt_message , args ) ;
11289 }
@@ -155,7 +132,7 @@ jspb.asserts.fail = function(opt_message, ...args) {
155132 */
156133jspb . asserts . assertInstanceof = function ( value , type , opt_message , ...args ) {
157134 if ( ! ( value instanceof type ) ) {
158- doAssertFailure (
135+ jspb . assert . doAssertFailure (
159136 'Expected instanceof %s but got %s.' , [ getType ( type ) , getType ( value ) ] ,
160137 opt_message , args ) ;
161138 }
0 commit comments