Skip to content

Commit fb97a2e

Browse files
committed
refactor: export native add-on if available
--- 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: passed - 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: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 6a778f7 commit fb97a2e

File tree

5 files changed

+219
-127
lines changed

5 files changed

+219
-127
lines changed

lib/node_modules/@stdlib/stats/base/ndarray/dmax/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2828
var pkg = require( './../package.json' ).name;
29-
var dmax = require( './../lib' );
29+
var dmax = require( './../lib/main.js' );
3030

3131

3232
// VARIABLES //

lib/node_modules/@stdlib/stats/base/ndarray/dmax/lib/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,23 @@
3737

3838
// MODULES //
3939

40+
var join = require( 'path' ).join;
41+
var tryRequire = require( '@stdlib/utils/try-require' );
42+
var isError = require( '@stdlib/assert/is-error' );
4043
var main = require( './main.js' );
4144

4245

46+
// MAIN //
47+
48+
var dmax;
49+
var tmp = tryRequire( join( __dirname, './native.js' ) );
50+
if ( isError( tmp ) ) {
51+
dmax = main;
52+
} else {
53+
dmax = tmp;
54+
}
55+
56+
4357
// EXPORTS //
4458

45-
module.exports = main;
59+
module.exports = dmax;

lib/node_modules/@stdlib/stats/base/ndarray/dmax/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
}
1515
],
1616
"main": "./lib",
17+
"browser": "./lib/main.js",
1718
"gypfile": true,
1819
"directories": {
1920
"benchmark": "./benchmark",
Lines changed: 29 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2025 The Stdlib Authors.
4+
* Copyright (c) 2026 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,28 +21,16 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25-
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
26-
var Float64Array = require( '@stdlib/array/float64' );
27-
var ndarray = require( '@stdlib/ndarray/base/ctor' );
24+
var proxyquire = require( 'proxyquire' );
25+
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
2826
var dmax = require( './../lib' );
2927

3028

31-
// FUNCTIONS //
29+
// VARIABLES //
3230

33-
/**
34-
* Returns a one-dimensional ndarray.
35-
*
36-
* @private
37-
* @param {Float64Array} buffer - underlying data buffer
38-
* @param {NonNegativeInteger} length - number of indexed elements
39-
* @param {integer} stride - stride length
40-
* @param {NonNegativeInteger} offset - index offset
41-
* @returns {ndarray} one-dimensional ndarray
42-
*/
43-
function vector( buffer, length, stride, offset ) {
44-
return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' );
45-
}
31+
var opts = {
32+
'skip': IS_BROWSER
33+
};
4634

4735

4836
// TESTS //
@@ -53,121 +41,37 @@ tape( 'main export is a function', function test( t ) {
5341
t.end();
5442
});
5543

56-
tape( 'the function has an arity of 1', function test( t ) {
57-
t.strictEqual( dmax.length, 1, 'has expected arity' );
58-
t.end();
59-
});
60-
61-
tape( 'the function calculates the maximum value of a one-dimensional ndarray', function test( t ) {
62-
var x;
63-
var v;
64-
65-
x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] );
66-
v = dmax( [ vector( x, 6, 1, 0 ) ] );
67-
t.strictEqual( v, 5.0, 'returns expected value' );
68-
69-
x = new Float64Array( [ -4.0, -5.0 ] );
70-
v = dmax( [ vector( x, 2, 1, 0 ) ] );
71-
t.strictEqual( v, -4.0, 'returns expected value' );
72-
73-
x = new Float64Array( [ -0.0, 0.0, -0.0 ] );
74-
v = dmax( [ vector( x, 3, 1, 0 ) ] );
75-
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
76-
77-
x = new Float64Array( [ NaN ] );
78-
v = dmax( [ vector( x, 1, 1, 0 ) ] );
79-
t.strictEqual( isnan( v ), true, 'returns expected value' );
80-
81-
x = new Float64Array( [ NaN, NaN ] );
82-
v = dmax( [ vector( x, 2, 1, 0 ) ] );
83-
t.strictEqual( isnan( v ), true, 'returns expected value' );
84-
85-
t.end();
86-
});
87-
88-
tape( 'if provided an empty ndarray, the function returns `NaN`', function test( t ) {
89-
var x;
90-
var v;
91-
92-
x = new Float64Array( [] );
93-
94-
v = dmax( [ vector( x, 0, 1, 0 ) ] );
95-
t.strictEqual( isnan( v ), true, 'returns expected value' );
96-
97-
t.end();
98-
});
99-
100-
tape( 'if provided an ndarray containing a single element, the function returns that element', function test( t ) {
101-
var x;
102-
var v;
103-
104-
x = new Float64Array( [ 1.0 ] );
105-
106-
v = dmax( [ vector( x, 1, 1, 0 ) ] );
107-
t.strictEqual( v, 1.0, 'returns expected value' );
44+
tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) {
45+
var dmax = proxyquire( './../lib', {
46+
'@stdlib/utils/try-require': tryRequire
47+
});
10848

49+
t.strictEqual( dmax, mock, 'returns expected value' );
10950
t.end();
110-
});
11151

112-
tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) {
113-
var x;
114-
var v;
52+
function tryRequire() {
53+
return mock;
54+
}
11555

116-
x = new Float64Array([
117-
1.0, // 0
118-
2.0,
119-
2.0, // 1
120-
-7.0,
121-
-2.0, // 2
122-
3.0,
123-
4.0, // 3
124-
2.0
125-
]);
126-
127-
v = dmax( [ vector( x, 4, 2, 0 ) ] );
128-
129-
t.strictEqual( v, 4.0, 'returns expected value' );
130-
t.end();
56+
function mock() {
57+
// Mock...
58+
}
13159
});
13260

133-
tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) {
134-
var x;
135-
var v;
61+
tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) {
62+
var dmax;
63+
var main;
13664

137-
x = new Float64Array([
138-
1.0, // 3
139-
2.0,
140-
2.0, // 2
141-
-7.0,
142-
-2.0, // 1
143-
3.0,
144-
4.0, // 0
145-
2.0
146-
]);
65+
main = require( './../lib/main.js' );
14766

148-
v = dmax( [ vector( x, 4, -2, 6 ) ] );
67+
dmax = proxyquire( './../lib', {
68+
'@stdlib/utils/try-require': tryRequire
69+
});
14970

150-
t.strictEqual( v, 4.0, 'returns expected value' );
71+
t.strictEqual( dmax, main, 'returns expected value' );
15172
t.end();
152-
});
15373

154-
tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) {
155-
var x;
156-
var v;
157-
158-
x = new Float64Array([
159-
2.0,
160-
1.0, // 0
161-
2.0,
162-
-2.0, // 1
163-
-2.0,
164-
2.0, // 2
165-
3.0,
166-
4.0 // 3
167-
]);
168-
169-
v = dmax( [ vector( x, 4, 2, 1 ) ] );
170-
t.strictEqual( v, 4.0, 'returns expected value' );
171-
172-
t.end();
74+
function tryRequire() {
75+
return new Error( 'Cannot find module' );
76+
}
17377
});

0 commit comments

Comments
 (0)