diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2f33708d..527a868d 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -8,6 +8,7 @@ Amit Jimiwal Athan Reines Brendan Graetz Bruno Fenzl +Chinmay J <86140365+JawHawk@users.noreply.github.com> Christopher Dambamuromo Dan Rose Daniel Killenberger @@ -36,6 +37,7 @@ Ognjen Jevremović Philipp Burckhardt Prajwal Kulkarni Pranav Goswami +Praneki <97080887+PraneGIT@users.noreply.github.com> Pratik <97464067+Pratik772846@users.noreply.github.com> Ricky Reusser Robert Gislason @@ -44,6 +46,7 @@ Rutam <138517416+performant23@users.noreply.github.com> Ryan Seal Seyyed Parsa Neshaei Shraddheya Shendre +Shubham Spandan Barve <114365550+marsian83@users.noreply.github.com> Stephannie Jiménez Gacha Yernar Yergaziyev diff --git a/is-well-formed-string/README.md b/is-well-formed-string/README.md new file mode 100644 index 00000000..b667b2a9 --- /dev/null +++ b/is-well-formed-string/README.md @@ -0,0 +1,151 @@ + + +# isWellFormedString + +> Test if a string is well-formed. + +
+ +## Usage + +```javascript +var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' ); +``` + +#### isWellFormedString( str ) + +Tests if a `string` is well-formed. + + + +```javascript +var bool = isWellFormedString( '' ); +// returns true + +bool = isWellFormedString( new String( '' ) ); +// returns true + +bool = isWellFormedString( '\uDBFF' ); +// returns false + +bool = isWellFormedString( '\uDBFFFF\uDBFF' ); +// returns false + +bool = isWellFormedString( [] ); +// returns false + +bool = isWellFormedString( '-5' ); +// returns true + +bool = isWellFormedString( null ); +// returns false +``` + +#### isWellFormedString.isPrimitive( str ) + +Tests if a `string` is a well-formed `string` primitive. + + + +```javascript +var bool = isWellFormedString.isPrimitive( '' ); +// returns true + +bool = isWellFormedString.isPrimitive( new String( '' ) ); +// returns false +``` + +#### isWellFormedString.isObject( str ) + +Tests if a `string` is a well-formed `String` object. + + + +```javascript +var bool = isWellFormedString.isObject( '' ); +// returns false + +bool = isWellFormedString.isObject( new String( '' ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' ); + +var bool = isWellFormedString( '' ); +// returns true + +bool = isWellFormedString( new String( '' ) ); +// returns true + +bool = isWellFormedString( '\uDBFF' ); +// returns false + +bool = isWellFormedString( '\uDBFF\uDBFF' ); +// returns false + +bool = isWellFormedString( [] ); +// returns false + +bool = isWellFormedString( '-5' ); +// returns true + +bool = isWellFormedString( null ); +// returns false +``` + +
+ + + + + + + + + + + + + + diff --git a/is-well-formed-string/benchmark/benchmark.js b/is-well-formed-string/benchmark/benchmark.js new file mode 100644 index 00000000..e57bd36c --- /dev/null +++ b/is-well-formed-string/benchmark/benchmark.js @@ -0,0 +1,220 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( './../../is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isWellFormedString = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + '', + '\uDBFF', + 'Hello World', + '3.14', + '\uDBFF\uDBFF', + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + [], + {}, + function noop() {}, + new String( '' ), + new String( '\uDBFF' ), + new String( '\uDBFFFF\uDBFF' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + '', + '\uDBFF', + 'Hello World', + '3.14', + '\uDBFF\uDBFF', + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString.isPrimitive( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + [], + {}, + function noop() {}, + new String( '' ), + new String( '\uDBFF' ), + new String( '\uDBFFFF\uDBFF' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString.isPrimitive( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + '', + '\uDBFF', + 'Hello World', + '3.14', + '\uDBFF\uDBFF', + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString.isObject( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var strs; + var bool; + var i; + + strs = [ + [], + {}, + function noop() {}, + new String( '' ), + new String( '\uDBFF' ), + new String( '\uDBFFFF\uDBFF' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWellFormedString.isObject( strs[ i % strs.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-well-formed-string/docs/repl.txt b/is-well-formed-string/docs/repl.txt new file mode 100644 index 00000000..cc8c6602 --- /dev/null +++ b/is-well-formed-string/docs/repl.txt @@ -0,0 +1,82 @@ + +{{alias}}( str ) + Tests if a string is well-formed. + + Parameters + ---------- + str: any + String to test. + + Returns + ------- + bool: boolean + Boolean indicating whether str is well-formed. + + Examples + -------- + > var bool = {{alias}}( '' ) + true + + > bool = {{alias}}( new String( '' ) ) + true + + > bool = {{alias}}( '\uDBFF' ) + false + + > bool = {{alias}}( '\uDBFFFF\uDBFF' ) + false + + > bool = {{alias}}( [] ) + false + + > bool = {{alias}}( '-5' ) + true + + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( str ) + Tests if a string is a well-formed string primitive. + + Parameters + ---------- + str: any + String to test. + + Returns + ------- + bool: boolean + Boolean indicating whether str is a well-formed string primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( '' ) + true + > bool = {{alias}}.isPrimitive( new String( '' ) ) + false + + +{{alias}}.isObject( str ) + Tests if a string is a well-formed string object. + + Parameters + ---------- + str: any + String to test. + + Returns + ------- + bool: boolean + Boolean indicating whether str is a well-formed string object. + + Examples + -------- + > var bool = {{alias}}.isObject( '' ) + false + > bool = {{alias}}.isObject( new String( '' ) ) + true + + See Also + -------- + diff --git a/is-well-formed-string/docs/types/index.d.ts b/is-well-formed-string/docs/types/index.d.ts new file mode 100644 index 00000000..734f8ae0 --- /dev/null +++ b/is-well-formed-string/docs/types/index.d.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Interface defining `isWellFormedString` with methods for testing for primitives and objects, respectively. +*/ +interface isWellFormedString { + /** + * Tests if a string is well-formed. + * + * @param str - string to test + * @returns boolean indicating whether string is well-formed + * + * @example + * var bool = isWellFormedString( '' ); + * // returns true + * + * @example + * var bool = isWellFormedString( new String( '' ) ); + * // returns true + * + * @example + * var bool = isWellFormedString( '\uDBFF' ); + * // returns false + * + * @example + * var bool = isWellFormedString( '\uDBFFFF\uDBFF' ); + * // returns false + * + * @example + * var bool = isWellFormedString( [] ); + * // returns false + * + * @example + * var bool = isWellFormedString( '-5' ); + * // returns true + * + * @example + * var bool = isWellFormedString( null ); + * // returns false + */ + ( str: any ): str is string | String; + + /** + * Tests if a string is a well-formed string primitive. + * + * @param str - string to test + * @returns boolean indicating if a string is a well-formed string primitive. + * + * @example + * var bool = isWellFormedString.isPrimitive( '' ); + * // returns true + * + * @example + * var bool = isWellFormedString.isPrimitive( new String( '' ) ); + * // returns false + */ + isPrimitive( str: any ): str is string; + + /** + * Tests if a string is a well-formed string object. + * + * @param str - string to test + * @returns boolean indicating if a string is a well-formed string object + * + * @example + * var bool = isWellFormedString.isObject( '' ); + * // returns false + * + * @example + * var bool = isWellFormedString.isObject( new String( '' ) ); + * // returns true + */ + isObject( str: any ): str is Object; +} + +/** +* Tests if a string is well-formed. +* +* @param str - string to test +* @returns boolean indicating whether string is well-formed +* +* @example +* var bool = isWellFormedString( '' ); +* // returns true +* +* @example +* var bool = isWellFormedString( new String( '' ) ); +* // returns true +* +* @example +* var bool = isWellFormedString( '\uDBFF' ); +* // returns false +* +* @example +* var bool = isWellFormedString( '\uDBFFFF\uDBFF' ); +* // returns false +* +* @example +* var bool = isWellFormedString( [] ); +* // returns false +* +* @example +* var bool = isWellFormedString( '-5' ); +* // returns true +* +* @example +* var bool = isWellFormedString( null ); +* // returns false +*/ +declare var isWellFormedString: isWellFormedString; + + +// EXPORTS // + +export = isWellFormedString; diff --git a/is-well-formed-string/docs/types/test.ts b/is-well-formed-string/docs/types/test.ts new file mode 100644 index 00000000..41f6f568 --- /dev/null +++ b/is-well-formed-string/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWellFormedString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isWellFormedString( '' ); // $ExpectType boolean + isWellFormedString( 'Hello' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWellFormedString(); // $ExpectError + isWellFormedString( '', '\uDBFF' ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // eslint-disable-next-line no-new-wrappers + isWellFormedString.isPrimitive( new String( '' ) ); // $ExpectType boolean + isWellFormedString.isPrimitive( '' ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isWellFormedString.isPrimitive(); // $ExpectError + isWellFormedString.isPrimitive( '', '\uDBFF' ); // $ExpectError +} + +// Attached to main export is an isObject method which returns a boolean... +{ + // eslint-disable-next-line no-new-wrappers + isWellFormedString.isObject( new String( '' ) ); // $ExpectType boolean + isWellFormedString.isObject( '\uDBFF' ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isWellFormedString.isObject(); // $ExpectError + isWellFormedString.isObject( '', '\uDBFF' ); // $ExpectError +} diff --git a/is-well-formed-string/examples/index.js b/is-well-formed-string/examples/index.js new file mode 100644 index 00000000..bf730ead --- /dev/null +++ b/is-well-formed-string/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var isWellFormedString = require( './../lib' ); + +console.log( isWellFormedString( '' ) ); +// => true + +console.log( isWellFormedString( new String( '' ) ) ); +// => true + +console.log( isWellFormedString( '\uDBFF' ) ); +// => false + +console.log( isWellFormedString( '\uDBFFFF\uDBFF' ) ); +// => false + +console.log( isWellFormedString( [] ) ); +// => false + +console.log( isWellFormedString( '-5' ) ); +// => true + +console.log( isWellFormedString( null ) ); +// => false diff --git a/is-well-formed-string/lib/index.js b/is-well-formed-string/lib/index.js new file mode 100644 index 00000000..6c84767b --- /dev/null +++ b/is-well-formed-string/lib/index.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a string is well-formed. +* +* @module @stdlib/assert/is-well-formed-string +* +* @example +* var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' ); +* +* var bool = isWellFormedString( '' ); +* // returns true +* +* bool = isWellFormedString( new String( '' ) ); +* // returns true +* +* bool = isWellFormedString( '\uDBFF' ); +* // returns false +* +* bool = isWellFormedString( '\uDBFFFF\uDBFF' ); +* // returns false +* +* bool = isWellFormedString( [] ); +* // returns false +* +* bool = isWellFormedString( '-5' ); +* // returns true +* +* bool = isWellFormedString( null ); +* // returns false +* +* @example +* // Use interface to check for well-formed string primitives... +* var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' ).isPrimitive; +* +* var bool = isWellFormedString( '\uDC00' ); +* // returns false +* +* bool = isWellFormedString( new String( '' ) ); +* // returns false +* +* @example +* // Use interface to check for well-formed string objects... +* var isWellFormedString = require( '@stdlib/assert/is-well-formed-string' ).isObject; +* +* var bool = isWellFormedString( '\uDC00' ); +* // returns false +* +* bool = isWellFormedString( new String( '' ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( main, 'isPrimitive', isPrimitive ); +setReadOnly( main, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = main; diff --git a/is-well-formed-string/lib/iswellformed.js b/is-well-formed-string/lib/iswellformed.js new file mode 100644 index 00000000..11d01f42 --- /dev/null +++ b/is-well-formed-string/lib/iswellformed.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// VARIABLES // + +var RE_UTF16_LOW_SURROGATE = /[\uDC00-\uDFFF]/; +var RE_UTF16_HIGH_SURROGATE = /[\uD800-\uDBFF]/; + + +// MAIN // + +/** +* Tests if a string is a well-formed string. +* +* @param {string} str - input string +* @returns {boolean} boolean indicating if a value is a well-formed string primitive +* +* @example +* var bool = isWellFormed( '' ); +* // returns true +* +* @example +* var bool = isWellFormed( new String( '\uDC00' ) ); +* // returns false +*/ +function isWellFormed(str) { + var i; + for ( i = 0; i < str.length; i++ ) { + // Checking if a low surrogate is present at the beginning + if ( i === 0 && RE_UTF16_LOW_SURROGATE.test(str[i]) ) { + return false; + } + + // Checking if a high surrogate is present at the last position + if ( i === str.length - 1 && RE_UTF16_HIGH_SURROGATE.test(str[i]) ) { + return false; + } + + // Checking if there is no low surrogate after a high surrogate + if ( i < str.length - 1 && RE_UTF16_HIGH_SURROGATE.test(str[i]) ) { + if ( RE_UTF16_LOW_SURROGATE.test(str[i + 1]) ) { + i += 1; + } + else { + return false; + } + } + + // Checking if there is no high surrogate before a low surrogate + if ( i > 0 && RE_UTF16_LOW_SURROGATE.test(str[i]) ) { + if ( !RE_UTF16_HIGH_SURROGATE.test(str[i - 1]) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isWellFormed; diff --git a/is-well-formed-string/lib/main.js b/is-well-formed-string/lib/main.js new file mode 100644 index 00000000..5f4e0ec3 --- /dev/null +++ b/is-well-formed-string/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a string is well-formed. +* +* @param {string} str - input string +* @returns {boolean} boolean indicating whether a string is well-formed +* +* @example +* var bool = isWellFormedString( '' ); +* // returns true +* +* @example +* var bool = isWellFormedString( new String( '' ) ); +* // returns true +* +* @example +* var bool = isWellFormedString( '\uDBFF' ); +* // returns false +* +* @example +* var bool = isWellFormedString( '\uDBFFFF\uDBFF' ); +* // returns false +* +* @example +* var bool = isWellFormedString( [] ); +* // returns false +* +* @example +* var bool = isWellFormedString( '-5' ); +* // returns true +* +* @example +* var bool = isWellFormedString( null ); +* // returns false +*/ +function isWellFormedString( str ) { + return ( isPrimitive( str ) || isObject( str ) ); +} + + +// EXPORTS // + +module.exports = isWellFormedString; diff --git a/is-well-formed-string/lib/object.js b/is-well-formed-string/lib/object.js new file mode 100644 index 00000000..0a85e22b --- /dev/null +++ b/is-well-formed-string/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require('./../../is-string'); +var isWellFormed = require('./iswellformed.js'); + + +// MAIN // + +/** +* Tests if a string is a well-formed string object. +* +* @param {string} str - input string +* @returns {boolean} boolean indicating if a string is a well-formed string object +* +* @example +* var bool = isWellFormedString( '\uDC00' ); +* // returns false +* +* @example +* var bool = isWellFormedString( new String( '' ) ); +* // returns true +*/ +function isWellFormedString( str ) { + return ( + isString.isObject( str ) && + isWellFormed( str.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isWellFormedString; diff --git a/is-well-formed-string/lib/primitive.js b/is-well-formed-string/lib/primitive.js new file mode 100644 index 00000000..da4e82c9 --- /dev/null +++ b/is-well-formed-string/lib/primitive.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isWellFormed = require('./iswellformed.js'); + + +// MAIN // + +/** +* Tests if a string is a well-formed string primitive. +* +* @param {string} str - input string +* @returns {boolean} boolean indicating if a string is a well-formed string primitive +* +* @example +* var bool = isWellFormedString( '\uDC00' ); +* // returns false +* +* @example +* var bool = isWellFormedString( new String( '' ) ); +* // returns false +*/ +function isWellFormedString( str ) { + return ( + typeof str === 'string' && isWellFormed( str ) + ); +} + + +// EXPORTS // + +module.exports = isWellFormedString; diff --git a/is-well-formed-string/package.json b/is-well-formed-string/package.json new file mode 100644 index 00000000..f3757e85 --- /dev/null +++ b/is-well-formed-string/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-well-formed-string", + "version": "0.0.0", + "description": "Test if a string is a well-formed string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "negative", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-well-formed-string/test/test.js b/is-well-formed-string/test/test.js new file mode 100644 index 00000000..4c6566ba --- /dev/null +++ b/is-well-formed-string/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isWellFormedString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isWellFormedString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a well-formed string primitive', function test( t ) { + t.equal( typeof isWellFormedString.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a well-formed string object', function test( t ) { + t.equal( typeof isWellFormedString.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-well-formed-string/test/test.main.js b/is-well-formed-string/test/test.main.js new file mode 100644 index 00000000..eab84390 --- /dev/null +++ b/is-well-formed-string/test/test.main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isWellFormedString = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isWellFormedString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a well-formed string', function test( t ) { + t.equal( isWellFormedString( '' ), true, 'returns true' ); + t.equal( isWellFormedString( new String( '' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a well-formed string', function test( t ) { + var strs; + var i; + + strs = [ + '5\uDBFF', + '\uDBFF\uDBFF', + 'Hello \uDBFFWorld', + '\uDBFF', + '\uDBFFFFF', + '\uDC00\uDC00', + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < strs.length; i++ ) { + t.equal( isWellFormedString( strs[i] ), false, 'returns false when provided '+strs[i] ); + } + t.end(); +}); diff --git a/is-well-formed-string/test/test.object.js b/is-well-formed-string/test/test.object.js new file mode 100644 index 00000000..8914b0d8 --- /dev/null +++ b/is-well-formed-string/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isWellFormedString = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isWellFormedString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a well-formed string object', function test( t ) { + t.equal( isWellFormedString( new String( '' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a string primitive, even if it is well-formed', function test( t ) { + t.equal( isWellFormedString( '' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a well-formed string', function test( t ) { + var strs; + var i; + + strs = [ + '\uDBFF', + new String( '\uDBFF\uDBFF' ), // eslint-disable-line no-new-wrappers + new String( '\uD800' ), // eslint-disable-line no-new-wrappers + '\uDBFFFFFF', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < strs.length; i++ ) { + t.equal( isWellFormedString( strs[i] ), false, 'returns false when provided '+strs[i] ); + } + t.end(); +}); diff --git a/is-well-formed-string/test/test.primitive.js b/is-well-formed-string/test/test.primitive.js new file mode 100644 index 00000000..1ef40a7b --- /dev/null +++ b/is-well-formed-string/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isWellFormedString = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isWellFormedString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a well-formed string primitive', function test( t ) { + t.equal( isWellFormedString( '' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a string object, even if the string is well-formed', function test( t ) { + t.equal( isWellFormedString( new String( '' ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a well-formed string', function test( t ) { + var strs; + var i; + + strs = [ + '\uDBFFFFFF', + '\uD800', + new String( '\uDBFF' ), // eslint-disable-line no-new-wrappers + 3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < strs.length; i++ ) { + t.equal( isWellFormedString( strs[i] ), false, 'returns false when provided '+strs[i] ); + } + t.end(); +});