Skip to content

Commit 0f3838c

Browse files
feat: add assert/is-well-formed-string
PR-URL: #1388 Closes: #1065 Signed-off-by: GUNJ JOSHI <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 272f91b commit 0f3838c

File tree

16 files changed

+1340
-0
lines changed

16 files changed

+1340
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isWellFormedString
22+
23+
> Test if a string is well-formed.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' );
31+
```
32+
33+
#### isWellFormedString( str )
34+
35+
Tests if a `string` is well-formed.
36+
37+
<!-- eslint-disable no-new-wrappers -->
38+
39+
```javascript
40+
var bool = isWellFormedString( '' );
41+
// returns true
42+
43+
bool = isWellFormedString( new String( '' ) );
44+
// returns true
45+
46+
bool = isWellFormedString( '\uDBFF' );
47+
// returns false
48+
49+
bool = isWellFormedString( '\uDBFFFF\uDBFF' );
50+
// returns false
51+
52+
bool = isWellFormedString( [] );
53+
// returns false
54+
55+
bool = isWellFormedString( '-5' );
56+
// returns true
57+
58+
bool = isWellFormedString( null );
59+
// returns false
60+
```
61+
62+
#### isWellFormedString.isPrimitive( str )
63+
64+
Tests if a `string` is a well-formed `string` primitive.
65+
66+
<!-- eslint-disable no-new-wrappers -->
67+
68+
```javascript
69+
var bool = isWellFormedString.isPrimitive( '' );
70+
// returns true
71+
72+
bool = isWellFormedString.isPrimitive( new String( '' ) );
73+
// returns false
74+
```
75+
76+
#### isWellFormedString.isObject( str )
77+
78+
Tests if a `string` is a well-formed `String` object.
79+
80+
<!-- eslint-disable no-new-wrappers -->
81+
82+
```javascript
83+
var bool = isWellFormedString.isObject( '' );
84+
// returns false
85+
86+
bool = isWellFormedString.isObject( new String( '' ) );
87+
// returns true
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint-disable no-new-wrappers -->
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' );
104+
105+
var bool = isWellFormedString( '' );
106+
// returns true
107+
108+
bool = isWellFormedString( new String( '' ) );
109+
// returns true
110+
111+
bool = isWellFormedString( '\uDBFF' );
112+
// returns false
113+
114+
bool = isWellFormedString( '\uDBFF\uDBFF' );
115+
// returns false
116+
117+
bool = isWellFormedString( [] );
118+
// returns false
119+
120+
bool = isWellFormedString( '-5' );
121+
// returns true
122+
123+
bool = isWellFormedString( null );
124+
// returns false
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
<!-- <related-links> -->
145+
146+
147+
<!-- </related-links> -->
148+
149+
</section>
150+
151+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var bench = require( '@stdlib/bench' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var isWellFormedString = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::primitives', function benchmark( b ) {
34+
var strs;
35+
var bool;
36+
var i;
37+
38+
strs = [
39+
'',
40+
'\uDBFF',
41+
'Hello World',
42+
'3.14',
43+
'\uDBFF\uDBFF',
44+
-4.0,
45+
NaN,
46+
true,
47+
false,
48+
null,
49+
undefined
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
bool = isWellFormedString( strs[ i % strs.length ] );
55+
if ( typeof bool !== 'boolean' ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isBoolean( bool ) ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
66+
67+
bench( pkg+'::objects', function benchmark( b ) {
68+
var strs;
69+
var bool;
70+
var i;
71+
72+
strs = [
73+
[],
74+
{},
75+
function noop() {},
76+
new String( '' ),
77+
new String( '\uDBFF' ),
78+
new String( '\uDBFFFF\uDBFF' )
79+
];
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
bool = isWellFormedString( strs[ i % strs.length ] );
84+
if ( typeof bool !== 'boolean' ) {
85+
b.fail( 'should return a boolean' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isBoolean( bool ) ) {
90+
b.fail( 'should return a boolean' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
95+
96+
bench( pkg+'::primitives:isPrimitive', function benchmark( b ) {
97+
var strs;
98+
var bool;
99+
var i;
100+
101+
strs = [
102+
'',
103+
'\uDBFF',
104+
'Hello World',
105+
'3.14',
106+
'\uDBFF\uDBFF',
107+
-4.0,
108+
NaN,
109+
true,
110+
false,
111+
null,
112+
undefined
113+
];
114+
115+
b.tic();
116+
for ( i = 0; i < b.iterations; i++ ) {
117+
bool = isWellFormedString.isPrimitive( strs[ i % strs.length ] );
118+
if ( typeof bool !== 'boolean' ) {
119+
b.fail( 'should return a boolean' );
120+
}
121+
}
122+
b.toc();
123+
if ( !isBoolean( bool ) ) {
124+
b.fail( 'should return a boolean' );
125+
}
126+
b.pass( 'benchmark finished' );
127+
b.end();
128+
});
129+
130+
bench( pkg+'::objects:isPrimitive', function benchmark( b ) {
131+
var strs;
132+
var bool;
133+
var i;
134+
135+
strs = [
136+
[],
137+
{},
138+
function noop() {},
139+
new String( '' ),
140+
new String( '\uDBFF' ),
141+
new String( '\uDBFFFF\uDBFF' )
142+
];
143+
144+
b.tic();
145+
for ( i = 0; i < b.iterations; i++ ) {
146+
bool = isWellFormedString.isPrimitive( strs[ i % strs.length ] );
147+
if ( typeof bool !== 'boolean' ) {
148+
b.fail( 'should return a boolean' );
149+
}
150+
}
151+
b.toc();
152+
if ( !isBoolean( bool ) ) {
153+
b.fail( 'should return a boolean' );
154+
}
155+
b.pass( 'benchmark finished' );
156+
b.end();
157+
});
158+
159+
bench( pkg+'::primitives:isObject', function benchmark( b ) {
160+
var strs;
161+
var bool;
162+
var i;
163+
164+
strs = [
165+
'',
166+
'\uDBFF',
167+
'Hello World',
168+
'3.14',
169+
'\uDBFF\uDBFF',
170+
-4.0,
171+
NaN,
172+
true,
173+
false,
174+
null,
175+
undefined
176+
];
177+
178+
b.tic();
179+
for ( i = 0; i < b.iterations; i++ ) {
180+
bool = isWellFormedString.isObject( strs[ i % strs.length ] );
181+
if ( typeof bool !== 'boolean' ) {
182+
b.fail( 'should return a boolean' );
183+
}
184+
}
185+
b.toc();
186+
if ( !isBoolean( bool ) ) {
187+
b.fail( 'should return a boolean' );
188+
}
189+
b.pass( 'benchmark finished' );
190+
b.end();
191+
});
192+
193+
bench( pkg+'::objects:isObject', function benchmark( b ) {
194+
var strs;
195+
var bool;
196+
var i;
197+
198+
strs = [
199+
[],
200+
{},
201+
function noop() {},
202+
new String( '' ),
203+
new String( '\uDBFF' ),
204+
new String( '\uDBFFFF\uDBFF' )
205+
];
206+
207+
b.tic();
208+
for ( i = 0; i < b.iterations; i++ ) {
209+
bool = isWellFormedString.isObject( strs[ i % strs.length ] );
210+
if ( typeof bool !== 'boolean' ) {
211+
b.fail( 'should return a boolean' );
212+
}
213+
}
214+
b.toc();
215+
if ( !isBoolean( bool ) ) {
216+
b.fail( 'should return a boolean' );
217+
}
218+
b.pass( 'benchmark finished' );
219+
b.end();
220+
});

0 commit comments

Comments
 (0)