Skip to content

Commit

Permalink
feat: add support for complex and boolean typed arrays
Browse files Browse the repository at this point in the history
---
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
---
  • Loading branch information
kgryte committed Jan 20, 2025
1 parent d0e60c2 commit 7a0ea1d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' );


Expand Down Expand Up @@ -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 += ' ]';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -203,15 +205,15 @@ 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 = '<Number>';
t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' );

val = Object( Symbol( '' ) );
expected = '<Symbol>';
t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' );

val = new Boolean( false ); // eslint-disable-line no-new-wrappers
val = new Boolean( false );
expected = '<Boolean>';
t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' );

Expand Down Expand Up @@ -307,6 +309,20 @@ tape( 'the function creates a deep instance equality annotation value for typed
expected = '<Float32Array>[ ~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 = '<Complex64Array>[ 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 = '<BooleanArray>[ true, false, true, false ]';
t.strictEqual( createAnnotationValue( val, opts ), expected, 'returns expected value' );

t.end();
});

Expand Down Expand Up @@ -384,6 +400,10 @@ tape( 'the function creates a type annotation for class instances', function tes
expected = '<Complex128>';
t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' );

actual = new BooleanArray( [ true, false, true, false ] );
expected = '<BooleanArray>';
t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' );

actual = noop;
expected = '<Function>';
t.strictEqual( createAnnotationValue( actual, opts ), expected, 'returns expected value' );
Expand Down

0 comments on commit 7a0ea1d

Please sign in to comment.