Skip to content

Commit 37d321c

Browse files
committed
fix: address review feedback from Planeshifter
- Revert lib/index.js to simple passthrough pattern - Create separate lib/enum.js and lib/str2enum.js modules - Update lib/native.js to use str2enum with proper spacing conventions - Fix test/test.native.js with tryRequire pattern and stdlib spacing - All tests passing (213/213 native, 2013/2013 standard) Follows pattern established in heavisidef as requested by @Planeshifter
1 parent fa12b7b commit 37d321c

5 files changed

Lines changed: 197 additions & 91 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Returns an object mapping supported function continuity type strings to enumeration constants.
25+
*
26+
* @private
27+
* @returns {Object} object mapping supported function continuity types to enumeration constants
28+
*
29+
* @example
30+
* var table = enumeration();
31+
* // returns <Object>
32+
*/
33+
function enumeration() {
34+
return {
35+
// Half-maximum:
36+
'half-maximum': 0,
37+
38+
// Left-continuous:
39+
'left-continuous': 1,
40+
41+
// Right-continuous:
42+
'right-continuous': 2,
43+
44+
// Discontinuous:
45+
'discontinuous': 3
46+
};
47+
}
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = enumeration;

lib/node_modules/@stdlib/math/base/special/heaviside/lib/index.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,9 @@
5050

5151
// MODULES //
5252

53-
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
54-
var main = require( './main.js' );
55-
var native = require( './native.js' );
56-
57-
58-
// MAIN //
59-
60-
/**
61-
* Evaluate the Heaviside function.
62-
*
63-
* @module @stdlib/math/base/special/heaviside
64-
*
65-
* @example
66-
* var heaviside = require( '@stdlib/math/base/special/heaviside' );
67-
*
68-
* var v = heaviside( 3.14 );
69-
* // returns 1.0
70-
*
71-
* v = heaviside( -3.14 );
72-
* // returns 0.0
73-
*
74-
* v = heaviside( 0.0 );
75-
* // returns NaN
76-
*
77-
* v = heaviside( 0.0, 'half-maximum' );
78-
* // returns 0.5
79-
*
80-
* v = heaviside( 0.0, 'left-continuous' );
81-
* // returns 0.0
82-
*
83-
* v = heaviside( 0.0, 'right-continuous' );
84-
* // returns 1.0
85-
*
86-
* v = heaviside( NaN );
87-
* // returns NaN
88-
*/
89-
var heaviside;
90-
if (native) {
91-
heaviside = native;
92-
} else {
93-
heaviside = main;
94-
}
95-
96-
setReadOnly(heaviside, 'native', native);
53+
var main = require('./main.js');
9754

9855

9956
// EXPORTS //
10057

101-
module.exports = heaviside;
58+
module.exports = main;

lib/node_modules/@stdlib/math/base/special/heaviside/lib/native.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,50 @@
2020

2121
// MODULES //
2222

23-
var addon = require( './../src/addon.node' );
24-
25-
26-
// VARIABLES //
27-
28-
var CONTINUITY = {
29-
'half-maximum': 0,
30-
'left-continuous': 1,
31-
'right-continuous': 2
32-
};
23+
var addon = require('./../src/addon.node');
24+
var str2enum = require('./str2enum.js');
3325

3426

3527
// MAIN //
3628

3729
/**
38-
* Evaluates the Heaviside function using native code.
30+
* Evaluates the Heaviside function.
3931
*
4032
* @private
4133
* @param {number} x - input value
4234
* @param {string} [continuity] - continuity option
4335
* @returns {number} function value
4436
*
4537
* @example
46-
* var v = heaviside( 3.14, 'half-maximum' );
38+
* var v = heaviside( 3.14 );
39+
* // returns 1.0
40+
*
41+
* @example
42+
* var v = heaviside( -3.14 );
43+
* // returns 0.0
44+
*
45+
* @example
46+
* var v = heaviside( 0.0, 'half-maximum' );
47+
* // returns 0.5
48+
*
49+
* @example
50+
* var v = heaviside( 0.0, 'left-continuous' );
51+
* // returns 0.0
52+
*
53+
* @example
54+
* var v = heaviside( 0.0, 'right-continuous' );
4755
* // returns 1.0
56+
*
57+
* @example
58+
* var v = heaviside( 0.0, 'discontinuous' );
59+
* // returns NaN
60+
*
61+
* @example
62+
* var v = heaviside( NaN, 'right-continuous' );
63+
* // returns NaN
4864
*/
4965
function heaviside(x, continuity) {
50-
var c = CONTINUITY[continuity];
51-
if (c === void 0) {
52-
c = 3; // Discontinuous
53-
}
54-
return addon(x, c);
66+
return addon(x, str2enum(continuity));
5567
}
5668

5769

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var enumeration = require('./enum.js');
24+
25+
26+
// VARIABLES //
27+
28+
var ENUM = enumeration();
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Returns the enumeration constant associated with a function continuity type string.
35+
*
36+
* @private
37+
* @param {string} ctype - function continuity type string
38+
* @returns {(integer|null)} integer value or null
39+
*
40+
* @example
41+
* var v = str2enum( 'left-continuous' );
42+
* // returns <number>
43+
*/
44+
function str2enum(ctype) {
45+
var v = ENUM[ctype];
46+
return (typeof v === 'number') ? v : -1; // note: we include this guard to prevent walking the prototype chain
47+
}
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = str2enum;

lib/node_modules/@stdlib/math/base/special/heaviside/test/test.native.js

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,84 @@
2020

2121
// MODULES //
2222

23-
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 PINF = require( '@stdlib/constants/float64/pinf' );
27-
var NINF = require( '@stdlib/constants/float64/ninf' );
28-
var EPS = require( '@stdlib/constants/float64/eps' );
29-
var randu = require( '@stdlib/random/base/randu' );
30-
var heaviside = require( './../lib/native.js' );
23+
var resolve = require('path').resolve;
24+
var tape = require('tape');
25+
var isnan = require('@stdlib/math/base/assert/is-nan');
26+
var isPositiveZero = require('@stdlib/math/base/assert/is-positive-zero');
27+
var PINF = require('@stdlib/constants/float64/pinf');
28+
var NINF = require('@stdlib/constants/float64/ninf');
29+
var EPS = require('@stdlib/constants/float64/eps');
30+
var randu = require('@stdlib/random/base/randu');
31+
var tryRequire = require('@stdlib/utils/try-require');
32+
33+
34+
// VARIABLES //
35+
36+
var heaviside = tryRequire(resolve(__dirname, './../lib/native.js'));
37+
var opts = {
38+
'skip': (heaviside instanceof Error)
39+
};
3140

3241

3342
// TESTS //
3443

35-
tape('main export is a function', function test(t) {
44+
tape('main export is a function', opts, function test(t) {
3645
t.ok(true, __filename);
3746
t.strictEqual(typeof heaviside, 'function', 'main export is a function');
3847
t.end();
3948
});
4049

41-
tape('the function returns `0` if `x` is negative', function test(t) {
50+
tape('the function returns `0` if `x` is negative', opts, function test(t) {
4251
var x;
4352
var v;
4453
var i;
4554

46-
for (i = 0; i < 1e2; i++) {
55+
for (i = 0; i < 1e3; i++) {
4756
x = -(randu() * 100.0) - EPS;
48-
v = heaviside(x, 'left-continuous');
57+
v = heaviside(x);
4958
t.strictEqual(isPositiveZero(v), true, 'returns expected value when provided ' + x);
5059
}
5160
t.end();
5261
});
5362

54-
tape('the function returns `1` if `x` is positive', function test(t) {
63+
tape('the function returns `1` if `x` is positive', opts, function test(t) {
5564
var x;
5665
var v;
5766
var i;
5867

59-
for (i = 0; i < 1e2; i++) {
68+
for (i = 0; i < 1e3; i++) {
6069
x = (randu() * 100.0) + EPS;
61-
v = heaviside(x, 'right-continuous');
70+
v = heaviside(x);
6271
t.strictEqual(v, 1.0, 'returns expected value when provided ' + x);
6372
}
6473
t.end();
6574
});
6675

67-
tape('the function returns `0.5` if provided `+-0` and continuity is `half-maximum`', function test(t) {
76+
tape('by default, the function returns `NaN` if provided `+-0`', opts, function test(t) {
77+
var v;
78+
79+
v = heaviside(-0.0);
80+
t.strictEqual(isnan(v), true, 'returns expected value');
81+
82+
v = heaviside(+0.0);
83+
t.strictEqual(isnan(v), true, 'returns expected value');
84+
85+
t.end();
86+
});
87+
88+
tape('if the `continuity` option is `discontinuous`, the function returns `NaN` if provided `+-0`', opts, function test(t) {
89+
var v;
90+
91+
v = heaviside(-0.0, 'discontinuous');
92+
t.strictEqual(isnan(v), true, 'returns expected value');
93+
94+
v = heaviside(+0.0, 'discontinuous');
95+
t.strictEqual(isnan(v), true, 'returns expected value');
96+
97+
t.end();
98+
});
99+
100+
tape('if the `continuity` option is `half-maximum`, the function returns `0.5` if provided `+-0`', opts, function test(t) {
68101
var v;
69102

70103
v = heaviside(-0.0, 'half-maximum');
@@ -76,7 +109,7 @@ tape('the function returns `0.5` if provided `+-0` and continuity is `half-maxim
76109
t.end();
77110
});
78111

79-
tape('the function returns `0.0` if provided `+-0` and continuity is `left-continuous`', function test(t) {
112+
tape('if the `continuity` option is `left-continuous`, the function returns `0.0` if provided `+-0`', opts, function test(t) {
80113
var v;
81114

82115
v = heaviside(-0.0, 'left-continuous');
@@ -88,44 +121,44 @@ tape('the function returns `0.0` if provided `+-0` and continuity is `left-conti
88121
t.end();
89122
});
90123

91-
tape('the function returns `1.0` if provided `+-0` and continuity is `right-continuous`', function test(t) {
124+
tape('if the `continuity` option is `right-continuous`, the function returns `1` if provided `+-0`', opts, function test(t) {
92125
var v;
93126

94127
v = heaviside(-0.0, 'right-continuous');
95-
t.strictEqual(v, 1.0, 'returns expected value');
128+
t.strictEqual(v, 1, 'returns expected value');
96129

97130
v = heaviside(+0.0, 'right-continuous');
98-
t.strictEqual(v, 1.0, 'returns expected value');
131+
t.strictEqual(v, 1, 'returns expected value');
99132

100133
t.end();
101134
});
102135

103-
tape('by default, the function returns `NaN` if provided `+-0`', function test(t) {
136+
tape('if the `continuity` option is not valid, the function returns `NaN` if provided `+-0`', opts, function test(t) {
104137
var v;
105138

106-
v = heaviside(-0.0);
139+
v = heaviside(-0.0, 'foo');
107140
t.strictEqual(isnan(v), true, 'returns expected value');
108141

109-
v = heaviside(+0.0);
142+
v = heaviside(+0.0, 'bar');
110143
t.strictEqual(isnan(v), true, 'returns expected value');
111144

112145
t.end();
113146
});
114147

115-
tape('the function returns `NaN` if provided `NaN`', function test(t) {
116-
var v = heaviside(NaN);
148+
tape('the function returns `NaN` if provided `NaN`', opts, function test(t) {
149+
var v = heaviside(NaN, 'left-continuous');
117150
t.strictEqual(isnan(v), true, 'returns expected value');
118151
t.end();
119152
});
120153

121-
tape('the function returns `0` if provided `-infinity`', function test(t) {
122-
var v = heaviside(NINF);
154+
tape('the function returns `0` if provided `-infinity`', opts, function test(t) {
155+
var v = heaviside(NINF, 'left-continuous');
123156
t.strictEqual(v, 0.0, 'returns expected value');
124157
t.end();
125158
});
126159

127-
tape('the function returns `+1` if provided `+infinity`', function test(t) {
128-
var v = heaviside(PINF);
160+
tape('the function returns `+1` if provided `+infinity`', opts, function test(t) {
161+
var v = heaviside(PINF, 'left-continuous');
129162
t.strictEqual(v, 1.0, 'returns expected value');
130163
t.end();
131164
});

0 commit comments

Comments
 (0)