11
11
12
12
goog . provide ( 'jspb.asserts' ) ;
13
13
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
-
37
14
/**
38
15
* Throws an exception with the given message and "Assertion failed" prefixed
39
16
* onto it.
@@ -43,7 +20,7 @@ function subs(pattern, subs) {
43
20
* @param {!Array<*> } givenArgs The substitution arguments for givenMessage.
44
21
* @throws {Error } When the value is not a number.
45
22
*/
46
- function doAssertFailure ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
23
+ jspb . asserts . doAssertFailure = function ( defaultMessage , defaultArgs , givenMessage , givenArgs ) {
47
24
let message = 'Assertion failed' ;
48
25
let args ;
49
26
if ( givenMessage ) {
@@ -72,7 +49,7 @@ function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
72
49
73
50
jspb . asserts . assert = function ( condition , opt_message , ...args ) {
74
51
if ( ! condition ) {
75
- doAssertFailure ( '' , null , opt_message , args ) ;
52
+ jspb . asserts . doAssertFailure ( '' , null , opt_message , args ) ;
76
53
}
77
54
return condition ;
78
55
} ;
@@ -88,7 +65,7 @@ jspb.asserts.assert = function(condition, opt_message, ...args) {
88
65
*/
89
66
jspb . asserts . assertString = function ( value , opt_message , ...args ) {
90
67
if ( typeof value !== 'string' ) {
91
- doAssertFailure (
68
+ jspb . asserts . doAssertFailure (
92
69
'Expected string but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
93
70
opt_message , args ) ;
94
71
}
@@ -106,7 +83,7 @@ jspb.asserts.assertString = function(value, opt_message, ...args) {
106
83
*/
107
84
jspb . asserts . assertArray = function ( value , opt_message , ...args ) {
108
85
if ( ! Array . isArray ( value ) ) {
109
- doAssertFailure (
86
+ jspb . asserts . doAssertFailure (
110
87
'Expected array but got %s: %s.' , [ goog . typeOf ( value ) , value ] ,
111
88
opt_message , args ) ;
112
89
}
@@ -155,7 +132,7 @@ jspb.asserts.fail = function(opt_message, ...args) {
155
132
*/
156
133
jspb . asserts . assertInstanceof = function ( value , type , opt_message , ...args ) {
157
134
if ( ! ( value instanceof type ) ) {
158
- doAssertFailure (
135
+ jspb . assert . doAssertFailure (
159
136
'Expected instanceof %s but got %s.' , [ getType ( type ) , getType ( value ) ] ,
160
137
opt_message , args ) ;
161
138
}
0 commit comments