From 7a0ea1d180b0a523f68be7584ad7d603c725e9e9 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 19 Jan 2025 23:40:49 -0800 Subject: [PATCH] feat: add support for complex and boolean typed arrays --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../create-annotation-value/lib/main.js | 40 +++++++++++++------ .../create-annotation-value/test/test.js | 24 ++++++++++- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js index 8ba4676ad4b8..50d00828fab5 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js @@ -20,27 +20,33 @@ // MODULES // +// Note: keep in alphabetical order +var isArray = require( '@stdlib/assert/is-array' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ); +var isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); var isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); -var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); +var isError = require( '@stdlib/assert/is-error' ); var isInfinite = require( '@stdlib/assert/is-infinite' ); -var isPrimitive = require( '@stdlib/assert/is-primitive' ); -var isBoolean = require( '@stdlib/assert/is-boolean' ); var isInteger = require( '@stdlib/assert/is-integer' ); +var isnan = require( '@stdlib/assert/is-nan' ); +var isNull = require( '@stdlib/assert/is-null' ); var isNumber = require( '@stdlib/assert/is-number' ); -var isString = require( '@stdlib/assert/is-string' ); -var isArray = require( '@stdlib/assert/is-array' ); -var isError = require( '@stdlib/assert/is-error' ); var isObject = require( '@stdlib/assert/is-object' ); +var isPrimitive = require( '@stdlib/assert/is-primitive' ); var isRegExp = require( '@stdlib/assert/is-regexp' ); +var isString = require( '@stdlib/assert/is-string' ); +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); var isUndefined = require( '@stdlib/assert/is-undefined' ); -var isNull = require( '@stdlib/assert/is-null' ); -var isnan = require( '@stdlib/assert/is-nan' ); + var capitalize = require( '@stdlib/string/capitalize' ); var roundn = require( '@stdlib/math/base/special/roundn' ); var floor = require( '@stdlib/math/base/special/floor' ); var constructorName = require( '@stdlib/utils/constructor-name' ); var objectKeys = require( '@stdlib/utils/keys' ); var typeOf = require( '@stdlib/utils/type-of' ); +var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' ); +var copyArray = require( '@stdlib/array/base/copy' ); var validate = require( './validate.js' ); @@ -131,35 +137,43 @@ function genericAnnotation( actual, opts ) { var keys; var out; var end; + var f; var i; if ( isPrimitive( actual ) ) { return primitiveAnnotation( actual, opts ); } - if ( isTypedArray( actual ) ) { + if ( isTypedArray( actual ) || isComplexTypedArray( actual ) || isBooleanArray( actual ) ) { out = '<'+actual.constructor.name+'>'; if ( actual.length === 0 ) { out += '[]'; return out; } + f = numberAnnotation; + if ( isComplexTypedArray( actual ) ) { + actual = reinterpretComplex( actual, 0 ); + } else if ( isBooleanArray( actual ) ) { + actual = copyArray( actual ); + f = genericAnnotation; + } out += '[ '; - out += numberAnnotation( actual[ 0 ], opts ); + out += f( actual[ 0 ], opts ); if ( actual.length > opts.numel ) { end = floor( opts.numel / 2 ); for ( i = 1; i < end; i++ ) { out += ', '; - out += numberAnnotation( actual[ i ], opts ); + out += f( actual[ i ], opts ); } out += ', ...'; for ( i = actual.length - end; i < actual.length; i++ ) { out += ', '; - out += numberAnnotation( actual[ i ], opts ); + out += f( actual[ i ], opts ); } } else { for ( i = 1; i < actual.length; i++ ) { out += ', '; - out += numberAnnotation( actual[ i ], opts ); + out += f( actual[ i ], opts ); } } out += ' ]'; diff --git a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js index f57e20fa3471..0030afdb4f45 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js +++ b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js @@ -32,12 +32,14 @@ var Complex64 = require( '@stdlib/complex/float32/ctor' ); var Complex128 = require( '@stdlib/complex/float64/ctor' ); var Complex64Array = require( '@stdlib/array/complex64' ); var Complex128Array = require( '@stdlib/array/complex128' ); +var BooleanArray = require( '@stdlib/array/bool' ); var createAnnotationValue = require( './../lib' ); // TESTS // tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); t.strictEqual( typeof createAnnotationValue, 'function', 'main export is a function' ); t.end(); }); @@ -203,7 +205,7 @@ tape( 'the function creates a type annotation value for boxed primitives', funct 'type': true }; - val = new Number( 1/3 ); // eslint-disable-line no-new-wrappers + val = new Number( 1/3 ); expected = ''; t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); @@ -211,7 +213,7 @@ tape( 'the function creates a type annotation value for boxed primitives', funct expected = ''; t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); - val = new Boolean( false ); // eslint-disable-line no-new-wrappers + val = new Boolean( false ); expected = ''; t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); @@ -307,6 +309,20 @@ tape( 'the function creates a deep instance equality annotation value for typed expected = '[ ~0.333, 0.5, 1 ]'; t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); + val = new Complex64Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); + opts = { + 'numel': 6 + }; + expected = '[ 1, 2, 3, ..., 6, 7, 8 ]'; + t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); + + val = new BooleanArray( [ true, false, true, false ] ); + opts = { + 'numel': 6 + }; + expected = '[ true, false, true, false ]'; + t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' ); + t.end(); }); @@ -384,6 +400,10 @@ tape( 'the function creates a type annotation for class instances', function tes expected = ''; t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' ); + actual = new BooleanArray( [ true, false, true, false ] ); + expected = ''; + t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' ); + actual = noop; expected = ''; t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' );