diff --git a/lib/node_modules/@stdlib/string/code-point-at/README.md b/lib/node_modules/@stdlib/string/code-point-at/README.md new file mode 100644 index 000000000000..33296f358967 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/README.md @@ -0,0 +1,190 @@ + + +# codePointAt + +> Return a Unicode [code point][code-point] from a string at a specified position. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var codePointAt = require( '@stdlib/string/code-point-at' ); +``` + +#### codePointAt( string, position\[, backward] ) + +Returns a Unicode [code point][code-point] from a string at a specified position. + +```javascript +var out = codePointAt( 'last man standing', 4 ); +// returns 32 +``` + +The function supports providing a `boolean` argument for performing backward iteration for low surrogates. + +```javascript +var out = codePointAt( '🌷', 1, true ); +// returns 127799 +``` + +
+ + + + + +
+ +## Notes + +This function differs from [`String.codePointAt`][mdn-string-codepointat] in the following ways: + +- The function supports providing a `boolean` argument for performing backward iteration for low surrogates. [`String.codePointAt`][mdn-string-codepointat] simply returns the low surrogate value if no [UTF-16][utf-16] surrogate pair begins at the specified position. If invoked with `backward` set to `true`, this function will return the code point after aggregating with the preceding high surrogate, if the specified position does not mark the start of a surrogate pair. + +
+ + + + + +
+ +## Examples + + + +```javascript +var codePointAt = require( '@stdlib/string/code-point-at' ); + +console.log( codePointAt( 'last man standing', 4 ) ); +// => 32 + +console.log( codePointAt( 'presidential election', 8, true ) ); +// => 116 + +console.log( codePointAt( 'अनुच्छेद', 2 ) ); +// => 2369 + +console.log( codePointAt( '🌷', 1, true ) ); +// => 127799 +``` + +
+ + + + + +* * * + +
+ +## CLI + + + +
+ +### Usage + +```text +Usage: code-point-at [options] [] --pos= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + -b, --backward Backward iteration for low surrogates. + --pos index Position in string. +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```bash +$ code-point-at --pos=2 अनुच्छेद +2369 +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'अनुच्छेद' | code-point-at --pos=2 +2369 +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/lib/node_modules/@stdlib/string/code-point-at/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/code-point-at/benchmark/benchmark.js new file mode 100644 index 000000000000..9e03d10f3b42 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/benchmark/benchmark.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var UNICODE_MAX = require( '@stdlib/constants/string/unicode-max' ); +var pkg = require( './../package.json' ).name; +var codePointAt = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': ( typeof String.prototype.codePointAt !== 'function' ) +}; + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var out; + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = floor( randu() * UNICODE_MAX ); + out = codePointAt( fromCodePoint( x ), 0 ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', opts, function benchmark( b ) { + var out; + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = floor( randu() * UNICODE_MAX ); + out = String.prototype.codePointAt.call( fromCodePoint( x ), 0 ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/code-point-at/bin/cli b/lib/node_modules/@stdlib/string/code-point-at/bin/cli new file mode 100755 index 000000000000..dcd139e845f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/bin/cli @@ -0,0 +1,87 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var codePointAt = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + var pos; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + pos = parseInt( flags.pos, 10 ); + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( process.stdin.isTTY ) { + return console.log( codePointAt( args[ 0 ], pos, flags.backward ) ); // eslint-disable-line no-console + } + return stdin( onRead ); + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + if ( error ) { + return cli.error( error ); + } + console.log( codePointAt( data.toString(), pos, flags.backward ) ); // eslint-disable-line no-console + } +} + +main(); diff --git a/lib/node_modules/@stdlib/string/code-point-at/docs/repl.txt b/lib/node_modules/@stdlib/string/code-point-at/docs/repl.txt new file mode 100644 index 000000000000..ca79247e8722 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( str, idx[, backward] ) + Returns a Unicode code point from a string at a specified position. + + Parameters + ---------- + str: string + Input string. + + idx: integer + Position. + + backward: boolean (optional) + Backward iteration for low surrogates. + + Returns + ------- + out: integer + Unicode code point. + + Examples + -------- + > var out = {{alias}}( 'last man standing', 4 ) + 32 + > out = {{alias}}( 'presidential election', 8, true ) + 116 + > out = {{alias}}( 'अनुच्छेद', 2 ) + 2369 + > out = {{alias}}( '🌷', 1, true ) + 127799 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/string/code-point-at/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/code-point-at/docs/types/index.d.ts new file mode 100644 index 000000000000..1fc6118fef13 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 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: 2.0 + +/** +* Returns a Unicode code point from a string at specified position. +* +* @param str - input string +* @param idx - position +* @param backward - backward iteration for low surrogates (default: false) +* @throws first argument must be a string +* @throws second argument must be a nonnegative integer +* @throws third argument must be a boolean +* @throws position must be a valid index in string +* @returns code point +* +* @example +* var str = codePointAt( 'अनुच्छेद', 2 ); +* // returns 2369 +* +* str = codePointAt( '🌷', 1, true ); +* // returns 127799 +*/ +declare function codePointAt( str: string, idx: number, backward?: boolean ): number; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = codePointAt; diff --git a/lib/node_modules/@stdlib/string/code-point-at/docs/types/test.ts b/lib/node_modules/@stdlib/string/code-point-at/docs/types/test.ts new file mode 100644 index 000000000000..493a2cd6586f --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/docs/types/test.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 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 codePointAt = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + codePointAt( 'last man standing', 4 ); // $ExpectType number + codePointAt( 'presidential election', 8, true ); // $ExpectType number + codePointAt( 'अनुच्छेद', 2 ); // $ExpectType number + codePointAt( '🌷', 1, true ); // $ExpectType number +} + +// The function does not compile if provided incorrect arguments... +{ + codePointAt( false, 3 ); // $ExpectError + codePointAt( {}, 3 ); // $ExpectError + codePointAt( ( x: number ): number => x, 3 ); // $ExpectError + + codePointAt( 'string', true ); // $ExpectError + codePointAt( 'string', false ); // $ExpectError + codePointAt( 'string', {} ); // $ExpectError + codePointAt( 'string', ( x: number ): number => x ); // $ExpectError + + codePointAt( 'string', 2, {} ); // $ExpectError + codePointAt( 'string', 2, ( x: number ): number => x ); // $ExpectError +} + +// The function does not compile if provided insufficient arguments... +{ + codePointAt(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/code-point-at/docs/usage.txt b/lib/node_modules/@stdlib/string/code-point-at/docs/usage.txt new file mode 100644 index 000000000000..7f67b3974731 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/docs/usage.txt @@ -0,0 +1,9 @@ + +Usage: code-point-at [options] [] --pos= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + -b, --backward Backward iteration for low surrogates. + --pos index Position in string. diff --git a/lib/node_modules/@stdlib/string/code-point-at/etc/cli_opts.json b/lib/node_modules/@stdlib/string/code-point-at/etc/cli_opts.json new file mode 100644 index 000000000000..755174f8622b --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/etc/cli_opts.json @@ -0,0 +1,21 @@ +{ + "string": [ + "pos" + ], + "boolean": [ + "help", + "version", + "backward" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ], + "backward": [ + "b" + ] + } +} diff --git a/lib/node_modules/@stdlib/string/code-point-at/examples/index.js b/lib/node_modules/@stdlib/string/code-point-at/examples/index.js new file mode 100644 index 000000000000..787f5c0081ee --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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'; + +var codePointAt = require( './../lib' ); + +console.log( codePointAt( 'last man standing', 4 ) ); +// => 32 + +console.log( codePointAt( 'presidential election', 8, true ) ); +// => 116 + +console.log( codePointAt( 'अनुच्छेद', 2 ) ); +// => 2369 + +console.log( codePointAt( '🌷', 1, true ) ); +// => 127799 diff --git a/lib/node_modules/@stdlib/string/code-point-at/lib/index.js b/lib/node_modules/@stdlib/string/code-point-at/lib/index.js new file mode 100644 index 000000000000..56b0a15dbcbd --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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'; + +/** +* Return a Unicode code point from a string at a specified position. +* +* @module @stdlib/string/code-point-at +* +* @example +* var codePointAt = require( '@stdlib/string/code-point-at' ); +* +* var out = codePointAt( 'अनुच्छेद', 2 ); +* // returns 2369 +* +* out = codePointAt( '🌷', 1, true ); +* // returns 127799 +*/ + +// MODULES // + +var codePointAt = require( './main.js' ); + + +// EXPORTS // + +module.exports = codePointAt; diff --git a/lib/node_modules/@stdlib/string/code-point-at/lib/main.js b/lib/node_modules/@stdlib/string/code-point-at/lib/main.js new file mode 100644 index 000000000000..3b0fad8885b3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/lib/main.js @@ -0,0 +1,130 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; + + +// VARIABLES // + +// Factors for converting individual surrogates +var Ox10000 = 0x10000|0; // 65536 +var Ox400 = 0x400|0; // 1024 + +// Range for a high surrogate +var OxD800 = 0xD800|0; // 55296 +var OxDBFF = 0xDBFF|0; // 56319 + +// Range for a low surrogate +var OxDC00 = 0xDC00|0; // 56320 +var OxDFFF = 0xDFFF|0; // 57343 + + +// MAIN // + +/** +* Returns a Unicode code point from a string at a specified position. +* +* ## Notes +* +* - UTF-16 encoding uses one 16-bit unit for non-surrogates (U+0000 to U+D7FF and U+E000 to U+FFFF). +* - UTF-16 encoding uses two 16-bit units (surrogate pairs) for U+10000 to U+10FFFF and encodes U+10000-U+10FFFF by subtracting 0x10000 from the code point, expressing the result as a 20-bit binary, and splitting the 20 bits of 0x0-0xFFFFF as upper and lower 10-bits. The respective 10-bits are stored in two 16-bit words: a high and a low surrogate. +* +* +* @param {string} str - input string +* @param {NonNegativeInteger} idx - position +* @param {boolean} [backward=false] - backward iteration for low surrogates +* @throws {TypeError} first argument must be a string primitive +* @throws {TypeError} second argument must be a number primitive having a nonnegative integer +* @throws {TypeError} third argument must be a boolean primitive +* @throws {RangeError} position must be a valid index in string +* @returns {NonNegativeInteger} code point +* +* @example +* var out = codePointAt( 'last man standing', 4 ); +* // returns 32 +* +* @example +* var out = codePointAt( 'presidential election', 8, true ); +* // returns 116 +* +* @example +* var out = codePointAt( 'अनुच्छेद', 2 ); +* // returns 2369 +* +* @example +* var out = codePointAt( '🌷', 1, true ); +* // returns 127799 +*/ +function codePointAt( str, idx, backward ) { + var code; + var FLG; + var low; + var hi; + + if ( !isString( str ) ) { + throw new TypeError( 'invalid argument. Must provide a string. Value: `' + str + '`.' ); + } + if ( !isNonNegativeInteger( idx ) ) { + throw new TypeError( 'invalid argument. Must provide a valid position (nonnegative integer). Value: `' + idx + '`.' ); + } + if ( idx >= str.length ) { + throw new RangeError( 'invalid argument. Must provide a valid position (within string bounds). Value: `' + idx + '`.' ); + } + if ( arguments.length > 2 ) { + if ( !isBoolean( backward ) ) { + throw new TypeError( 'invalid argument. Third argument must be a boolean. Value: `' + backward + '`.' ); + } + FLG = backward; + } else { + FLG = false; + } + code = str.charCodeAt( idx ); + + // High surrogate + if ( code >= OxD800 && code <= OxDBFF && idx < str.length - 1 ) { + hi = code; + low = str.charCodeAt( idx+1 ); + if ( OxDC00 <= low && low <= OxDFFF ) { + return ( ( hi - OxD800 ) * Ox400 ) + ( low - OxDC00 ) + Ox10000; + } + return hi; + } + // Low surrogate - support only if backward iteration is desired + if ( FLG ) { + if ( code >= OxDC00 && code <= OxDFFF && idx >= 1 ) { + hi = str.charCodeAt( idx-1 ); + low = code; + if ( OxD800 <= hi && hi <= OxDBFF ) { + return ( ( hi - OxD800 ) * Ox400 ) + ( low - OxDC00 ) + Ox10000; + } + return low; + } + } + return code; +} + + +// EXPORTS // + +module.exports = codePointAt; diff --git a/lib/node_modules/@stdlib/string/code-point-at/package.json b/lib/node_modules/@stdlib/string/code-point-at/package.json new file mode 100644 index 000000000000..66ff3b06e661 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/string/code-point-at", + "version": "0.0.0", + "description": "Return a Unicode code point from a string at a specified position.", + "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" + } + ], + "bin": { + "from-code-point": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "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", + "stdstring", + "string", + "str", + "utilities", + "utility", + "utils", + "util", + "code", + "point", + "pt", + "unicode", + "utf-16", + "utf16", + "surrogate", + "astral" + ] +} diff --git a/lib/node_modules/@stdlib/string/code-point-at/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/string/code-point-at/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..030cd465f279 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/test/fixtures/stdin_error.js.txt @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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. +*/ + +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +process.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/lib/node_modules/@stdlib/string/code-point-at/test/test.cli.js b/lib/node_modules/@stdlib/string/code-point-at/test/test.cli.js new file mode 100644 index 000000000000..367dd912ed52 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/test/test.cli.js @@ -0,0 +1,259 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + process.execPath, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + process.execPath, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + process.execPath, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + process.execPath, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface returns code point at a specified string position', opts, function test( t ) { + var cmd = [ + process.execPath, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--pos=4\'; process.argv[ 3 ] = \'last man standing\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '32\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports backward iteration for low surrogates when invoked with `--backward` flag', opts, function test( t ) { + var cmd = [ + process.execPath, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--pos=1\'; process.argv[ 3 ] = \'--backward\'; process.argv[ 4 ] = \'🌷\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '127799\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports backward iteration for low surrogates when invoked with `-b` flag', opts, function test( t ) { + var cmd = [ + process.execPath, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--pos=1\'; process.argv[ 3 ] = \'--backward\'; process.argv[ 4 ] = \'🌷\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '127799\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf \'अनुच्छेद\'', + '|', + process.execPath, + fpath, + '--pos=2' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '2369\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + process.execPath, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/string/code-point-at/test/test.js b/lib/node_modules/@stdlib/string/code-point-at/test/test.js new file mode 100644 index 000000000000..979b719f7016 --- /dev/null +++ b/lib/node_modules/@stdlib/string/code-point-at/test/test.js @@ -0,0 +1,166 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 codePointAt = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof codePointAt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not a string primitive', function test( t ) { + var values; + var i; + + values = [ + -5, + 3.14, + -1.0, + NaN, + true, + false, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + codePointAt( value, 0 ); + }; + } +}); + +tape( 'the function throws an error if the second argument is not a nonnegative integer', function test( t ) { + var values; + var i; + + values = [ + 'bar', + -5, + 3.14, + -1.0, + NaN, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + codePointAt( 'foo', value ); + }; + } +}); + +tape( 'the function throws an error if the third argument is not a boolean primitive', function test( t ) { + var values; + var i; + + values = [ + 'bar', + -5, + 3.14, + -1.0, + NaN, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + codePointAt( 'foo', 0, value ); + }; + } +}); + +tape( 'the function throws an error if a provided position is not a valid index in the provided string', function test( t ) { + var values; + var i; + + values = [ + [ 'bar', 3 ], + [ 'string', 7 ], + [ '', 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + codePointAt( value[ 0 ], value[ 1 ] ); + }; + } +}); + +tape( 'the arity of the function is 3', function test( t ) { + t.strictEqual( codePointAt.length, 3, 'has length 3' ); + t.end(); +}); + +tape( 'the function returns the code point at a specified string position', function test( t ) { + var out; + + out = codePointAt( 'last man standing', 4 ); + t.strictEqual( out, 0x20, 'returns expected value' ); + + out = codePointAt( 'presidential election', 8, true ); + t.strictEqual( out, 0x74, 'returns expected value' ); + + out = codePointAt( 'अनुच्छेद', 2 ); + t.strictEqual( out, 0x941, 'returns expected value' ); + + out = codePointAt( '🌷', 0, true ); + t.strictEqual( out, 0x1F337, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/code_point_at.js b/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/code_point_at.js deleted file mode 100644 index c8728b9eb3bf..000000000000 --- a/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/code_point_at.js +++ /dev/null @@ -1,78 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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'; - -// MAIN // - -/** -* Returns the Unicode code point of the character located at a specified position. -* -* @private -* @param {string} str - input string -* @param {NonNegativeInteger} idx - position -* @returns {NonNegativeInteger} code point -* -* @example -* var out = codePointAt( 'last man standing', 4 ); -* // returns 32 -* -* @example -* var out = codePointAt( 'presidential election', 8 ); -* // returns 116 -* -* @example -* var out = codePointAt( 'अनुच्छेद', 2 ); -* // returns 2369 -* -* @example -* var out = codePointAt( '🌷', 0 ); -* // returns 127799 -*/ -function codePointAt( str, idx ) { - var code; - var low; - var hi; - - code = str.charCodeAt( idx ); - - // High surrogate - if ( code >= 0xD800 && code <= 0xDBFF && idx < str.length - 1 ) { - hi = code; - low = str.charCodeAt( idx+1 ); - if ( 0xDC00 <= low && low <= 0xDFFF ) { - return ( ( hi - 0xD800 ) * 0x400 ) + ( low - 0xDC00 ) + 0x10000; - } - return hi; - } - // Low surrogate - if ( code >= 0xDC00 && code <= 0xDFFF && idx >= 1 ) { - hi = str.charCodeAt( idx-1 ); - low = code; - if ( 0xD800 <= hi && hi <= 0xDBFF ) { - return ( ( hi - 0xD800 ) * 0x400 ) + ( low - 0xDC00 ) + 0x10000; - } - return low; - } - return code; -} - - -// EXPORTS // - -module.exports = codePointAt; diff --git a/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/next_break.js b/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/next_break.js index d177668a150c..e4949b61c18a 100644 --- a/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/next_break.js +++ b/lib/node_modules/@stdlib/string/num-grapheme-clusters/lib/next_break.js @@ -20,7 +20,7 @@ // MODULES // -var codePointAt = require( './code_point_at.js' ); +var codePointAt = require( '@stdlib/string/code-point-at' ); var isSurrogate = require( './is_surrogate.js' ); var shouldBreak = require( './should_break.js' ); var getGraphemeBreakProperty = require( './get_grapheme_break_property.js' ); diff --git a/lib/node_modules/@stdlib/string/num-grapheme-clusters/test/test.code_point_at.js b/lib/node_modules/@stdlib/string/num-grapheme-clusters/test/test.code_point_at.js deleted file mode 100644 index 00232a0008a4..000000000000 --- a/lib/node_modules/@stdlib/string/num-grapheme-clusters/test/test.code_point_at.js +++ /dev/null @@ -1,51 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 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 codePointAt = require( './../lib/code_point_at.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof codePointAt, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns the code point at a specified string position', function test( t ) { - var out; - - out = codePointAt( 'last man standing', 4 ); - t.strictEqual( out, 0x20, 'returns expected value' ); - - out = codePointAt( 'presidential election', 8 ); - t.strictEqual( out, 0x74, 'returns expected value' ); - - out = codePointAt( 'अनुच्छेद', 2 ); - t.strictEqual( out, 0x941, 'returns expected value' ); - - out = codePointAt( '🌷', 0 ); - t.strictEqual( out, 0x1F337, 'returns expected value' ); - - t.end(); -});