@stdlib/array-base-count-same-value 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -6
- package/dist/index.js +3 -3
- package/dist/index.js.map +3 -3
- package/docs/types/index.d.ts +10 -3
- package/lib/index.js +1 -1
- package/lib/main.js +70 -21
- package/package.json +15 -11
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ limitations under the License.
|
|
|
33
33
|
|
|
34
34
|
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
35
|
|
|
36
|
-
> Count the number of elements that are equal to a
|
|
36
|
+
> Count the number of elements in an array that are equal to a specified value.
|
|
37
37
|
|
|
38
38
|
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
39
39
|
|
|
@@ -65,7 +65,7 @@ var countSameValue = require( '@stdlib/array-base-count-same-value' );
|
|
|
65
65
|
|
|
66
66
|
#### countSameValue( x, value )
|
|
67
67
|
|
|
68
|
-
Counts the number of elements that are equal to a
|
|
68
|
+
Counts the number of elements in an array that are equal to a specified value.
|
|
69
69
|
|
|
70
70
|
```javascript
|
|
71
71
|
var x = [ 0, 1, 0, 1, 2 ];
|
|
@@ -74,6 +74,15 @@ var out = countSameValue( x, 1 );
|
|
|
74
74
|
// returns 2
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
+
In contrast to an implementation using the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value.
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
var x = [ NaN, NaN, NaN ];
|
|
81
|
+
|
|
82
|
+
var out = countSameValue( x, NaN );
|
|
83
|
+
// returns 3
|
|
84
|
+
```
|
|
85
|
+
|
|
77
86
|
</section>
|
|
78
87
|
|
|
79
88
|
<!-- /.usage -->
|
|
@@ -82,6 +91,10 @@ var out = countSameValue( x, 1 );
|
|
|
82
91
|
|
|
83
92
|
<section class="notes">
|
|
84
93
|
|
|
94
|
+
## Notes
|
|
95
|
+
|
|
96
|
+
- The function uses the [SameValue Algorithm][@stdlib/assert/is-same-value] as specified in ECMAScript 5.
|
|
97
|
+
|
|
85
98
|
</section>
|
|
86
99
|
|
|
87
100
|
<!-- /.notes -->
|
|
@@ -95,10 +108,12 @@ var out = countSameValue( x, 1 );
|
|
|
95
108
|
<!-- eslint no-undef: "error" -->
|
|
96
109
|
|
|
97
110
|
```javascript
|
|
98
|
-
var
|
|
111
|
+
var bernoulli = require( '@stdlib/random-array-bernoulli' );
|
|
99
112
|
var countSameValue = require( '@stdlib/array-base-count-same-value' );
|
|
100
113
|
|
|
101
|
-
var x =
|
|
114
|
+
var x = bernoulli( 10, 0.5, {
|
|
115
|
+
'dtype': 'generic'
|
|
116
|
+
});
|
|
102
117
|
console.log( x );
|
|
103
118
|
|
|
104
119
|
var n = countSameValue( x, 1 );
|
|
@@ -164,8 +179,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
164
179
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-base-count-same-value.svg
|
|
165
180
|
[npm-url]: https://npmjs.org/package/@stdlib/array-base-count-same-value
|
|
166
181
|
|
|
167
|
-
[test-image]: https://github.com/stdlib-js/array-base-count-same-value/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
168
|
-
[test-url]: https://github.com/stdlib-js/array-base-count-same-value/actions/workflows/test.yml?query=branch:v0.
|
|
182
|
+
[test-image]: https://github.com/stdlib-js/array-base-count-same-value/actions/workflows/test.yml/badge.svg?branch=v0.2.0
|
|
183
|
+
[test-url]: https://github.com/stdlib-js/array-base-count-same-value/actions/workflows/test.yml?query=branch:v0.2.0
|
|
169
184
|
|
|
170
185
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-base-count-same-value/main.svg
|
|
171
186
|
[coverage-url]: https://codecov.io/github/stdlib-js/array-base-count-same-value?branch=main
|
|
@@ -197,6 +212,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
197
212
|
|
|
198
213
|
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-base-count-same-value/main/LICENSE
|
|
199
214
|
|
|
215
|
+
[@stdlib/assert/is-same-value]: https://www.npmjs.com/package/@stdlib/assert-is-same-value
|
|
216
|
+
|
|
200
217
|
</section>
|
|
201
218
|
|
|
202
219
|
<!-- /.links -->
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";var f=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var
|
|
2
|
-
var c=require('@stdlib/assert-is-complex-like/dist'),q=require('@stdlib/
|
|
3
|
-
});var
|
|
1
|
+
"use strict";var f=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var s=f(function(G,v){
|
|
2
|
+
var c=require('@stdlib/assert-is-complex-like/dist'),q=require('@stdlib/assert-is-boolean/dist').isPrimitive,m=require('@stdlib/strided-base-reinterpret-complex/dist'),p=require('@stdlib/strided-base-reinterpret-boolean/dist'),g=require('@stdlib/array-base-assert-is-complex-typed-array/dist'),l=require('@stdlib/array-base-assert-is-booleanarray/dist'),h=require('@stdlib/array-base-assert-is-accessor-array/dist'),y=require('@stdlib/array-base-resolve-getter/dist'),u=require('@stdlib/assert-is-same-value/dist'),A=require('@stdlib/complex-float64-real/dist'),d=require('@stdlib/complex-float64-imag/dist');function B(e,r){var i,n;for(i=0,n=0;n<e.length;n++)u(e[n],r)&&(i+=1);return i}function w(e,r){var i,n,t;for(i=y(e),n=0,t=0;t<e.length;t++)u(i(e,t),r)&&(n+=1);return n}function C(e,r){var i,n,t,a,o;if(!c(r))return 0;for(n=A(r),t=d(r),i=m(e,0),a=0,o=0;o<i.length;o+=2)u(i[o],n)&&u(i[o+1],t)&&(a+=1);return a}function S(e,r){var i,n,t,a;if(!q(r))return 0;for(i=p(e,0),t=r?1:0,n=0,a=0;a<i.length;a++)i[a]===t&&(n+=1);return n}function V(e,r){return h(e,r)?g(e,r)?C(e,r):l(e,r)?S(e,r):w(e,r):B(e,r)}v.exports=V
|
|
3
|
+
});var b=s();module.exports=b;
|
|
4
4
|
/** @license Apache-2.0 */
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../lib/main.js", "../lib/index.js"],
|
|
4
|
-
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isComplexLike = require( '@stdlib/assert-is-complex-like' );\nvar
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAgB,QAAS,gCAAiC,EAC1DC,
|
|
6
|
-
"names": ["require_main", "__commonJSMin", "exports", "module", "isComplexLike", "
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isComplexLike = require( '@stdlib/assert-is-complex-like' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar reinterpret = require( '@stdlib/strided-base-reinterpret-complex' );\nvar reinterpretBoolean = require( '@stdlib/strided-base-reinterpret-boolean' );\nvar isComplexTypedArray = require( '@stdlib/array-base-assert-is-complex-typed-array' );\nvar isBooleanArray = require( '@stdlib/array-base-assert-is-booleanarray' );\nvar isAccessorArray = require( '@stdlib/array-base-assert-is-accessor-array' );\nvar resolveGetter = require( '@stdlib/array-base-resolve-getter' );\nvar isSameValue = require( '@stdlib/assert-is-same-value' );\nvar real = require( '@stdlib/complex-float64-real' );\nvar imag = require( '@stdlib/complex-float64-imag' );\n\n\n// FUNCTIONS //\n\n/**\n* Counts the number of elements in an array that are equal to a specified value.\n*\n* @private\n* @param {Collection} x - input array\n* @param {*} value - search value\n* @returns {NonNegativeInteger} number of elements that are equal to a specified value\n*\n* @example\n* var x = [ 0, 1, 0, 1, 1 ];\n*\n* var n = indexed( x, 1 );\n* // returns 3\n*/\nfunction indexed( x, value ) {\n\tvar n;\n\tvar i;\n\n\tn = 0;\n\tfor ( i = 0; i < x.length; i++ ) {\n\t\tif ( isSameValue( x[ i ], value ) ) {\n\t\t\tn += 1;\n\t\t}\n\t}\n\treturn n;\n}\n\n/**\n* Counts the number of elements in an accessor array that are equal to a specified value.\n*\n* @private\n* @param {Collection} x - input array\n* @param {*} value - search value\n* @returns {NonNegativeInteger} number of elements that are equal to a provided value\n*\n* @example\n* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );\n*\n* var x = toAccessorArray( [ 0, 1, 0, 1, 1 ] );\n*\n* var n = accessors( x, 1 );\n* // returns 3\n*/\nfunction accessors( x, value ) {\n\tvar get;\n\tvar n;\n\tvar i;\n\n\tget = resolveGetter( x );\n\n\tn = 0;\n\tfor ( i = 0; i < x.length; i++ ) {\n\t\tif ( isSameValue( get( x, i ), value ) ) {\n\t\t\tn += 1;\n\t\t}\n\t}\n\treturn n;\n}\n\n/**\n* Counts the number of elements in a complex array that are equal to a specified value.\n*\n* @private\n* @param {Collection} x - input array\n* @param {*} value - search value\n* @returns {NonNegativeInteger} number of elements that are equal to a specified value\n*\n* @example\n* var Complex128Array = require( '@stdlib/array-complex128' );\n* var Complex128 = require( '@stdlib/complex-float64-ctor' );\n*\n* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );\n*\n* var n = complex( x, new Complex128( 1.0, 2.0 ) );\n* // returns 1\n*/\nfunction complex( x, value ) {\n\tvar view;\n\tvar re;\n\tvar im;\n\tvar n;\n\tvar i;\n\n\tif ( !isComplexLike( value ) ) {\n\t\treturn 0;\n\t}\n\tre = real( value );\n\tim = imag( value );\n\n\tview = reinterpret( x, 0 );\n\n\tn = 0;\n\tfor ( i = 0; i < view.length; i += 2 ) {\n\t\tif ( isSameValue( view[ i ], re ) && isSameValue( view[ i+1 ], im ) ) {\n\t\t\tn += 1;\n\t\t}\n\t}\n\treturn n;\n}\n\n/**\n* Counts the number of elements in a boolean array that are equal to a specified value.\n*\n* @private\n* @param {Collection} x - input array\n* @param {*} value - search value\n* @returns {NonNegativeInteger} number of elements that are equal to a specified value\n*\n* @example\n* var BooleanArray = require( '@stdlib/array-bool' );\n*\n* var x = new BooleanArray( [ true, false, true, false, true ] );\n*\n* var n = boolean( x, true );\n* // returns 3\n*/\nfunction boolean( x, value ) {\n\tvar view;\n\tvar n;\n\tvar v;\n\tvar i;\n\n\tif ( !isBoolean( value ) ) {\n\t\treturn 0;\n\t}\n\tview = reinterpretBoolean( x, 0 );\n\n\tv = ( value ) ? 1 : 0;\n\tn = 0;\n\tfor ( i = 0; i < view.length; i++ ) {\n\t\tif ( view[ i ] === v ) {\n\t\t\tn += 1;\n\t\t}\n\t}\n\treturn n;\n}\n\n\n// MAIN //\n\n/**\n* Counts the number of elements in an array that are equal to a specified value.\n*\n* ## Notes\n*\n* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.\n* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.\n*\n* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12\n*\n* @param {Collection} x - input array\n* @param {*} value - search value\n* @returns {NonNegativeInteger} number of elements that are equal to a specified value\n*\n* @example\n* var x = [ 0, 1, 0, 1, 1 ];\n*\n* var n = countSameValue( x, 1 );\n* // returns 3\n*/\nfunction countSameValue( x, value ) {\n\tif ( isAccessorArray( x, value ) ) {\n\t\tif ( isComplexTypedArray( x, value ) ) {\n\t\t\treturn complex( x, value );\n\t\t}\n\t\tif ( isBooleanArray( x, value ) ) {\n\t\t\treturn boolean( x, value );\n\t\t}\n\t\treturn accessors( x, value );\n\t}\n\treturn indexed( x, value );\n}\n\n\n// EXPORTS //\n\nmodule.exports = countSameValue;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Count the number of elements in an array that are equal to a specified value.\n*\n* @module @stdlib/array-base-count-same-value\n*\n* @example\n* var countSameValue = require( '@stdlib/array-base-count-same-value' );\n*\n* var x = [ 0, 1, 0, 1, 1 ];\n*\n* var n = countSameValue( x, 1 );\n* // returns 3\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAgB,QAAS,gCAAiC,EAC1DC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAc,QAAS,0CAA2C,EAClEC,EAAqB,QAAS,0CAA2C,EACzEC,EAAsB,QAAS,kDAAmD,EAClFC,EAAiB,QAAS,2CAA4C,EACtEC,EAAkB,QAAS,6CAA8C,EACzEC,EAAgB,QAAS,mCAAoC,EAC7DC,EAAc,QAAS,8BAA+B,EACtDC,EAAO,QAAS,8BAA+B,EAC/CC,EAAO,QAAS,8BAA+B,EAmBnD,SAASC,EAASC,EAAGC,EAAQ,CAC5B,IAAIC,EACAC,EAGJ,IADAD,EAAI,EACEC,EAAI,EAAGA,EAAIH,EAAE,OAAQG,IACrBP,EAAaI,EAAGG,CAAE,EAAGF,CAAM,IAC/BC,GAAK,GAGP,OAAOA,CACR,CAkBA,SAASE,EAAWJ,EAAGC,EAAQ,CAC9B,IAAII,EACA,EACAF,EAKJ,IAHAE,EAAMV,EAAeK,CAAE,EAEvB,EAAI,EACEG,EAAI,EAAGA,EAAIH,EAAE,OAAQG,IACrBP,EAAaS,EAAKL,EAAGG,CAAE,EAAGF,CAAM,IACpC,GAAK,GAGP,OAAO,CACR,CAmBA,SAASK,EAASN,EAAGC,EAAQ,CAC5B,IAAIM,EACAC,EACAC,EACAP,EACAC,EAEJ,GAAK,CAACf,EAAea,CAAM,EAC1B,MAAO,GAQR,IANAO,EAAKX,EAAMI,CAAM,EACjBQ,EAAKX,EAAMG,CAAM,EAEjBM,EAAOjB,EAAaU,EAAG,CAAE,EAEzBE,EAAI,EACEC,EAAI,EAAGA,EAAII,EAAK,OAAQJ,GAAK,EAC7BP,EAAaW,EAAMJ,CAAE,EAAGK,CAAG,GAAKZ,EAAaW,EAAMJ,EAAE,CAAE,EAAGM,CAAG,IACjEP,GAAK,GAGP,OAAOA,CACR,CAkBA,SAASQ,EAASV,EAAGC,EAAQ,CAC5B,IAAIM,EACA,EACAI,EACAR,EAEJ,GAAK,CAACd,EAAWY,CAAM,EACtB,MAAO,GAMR,IAJAM,EAAOhB,EAAoBS,EAAG,CAAE,EAEhCW,EAAMV,EAAU,EAAI,EACpB,EAAI,EACEE,EAAI,EAAGA,EAAII,EAAK,OAAQJ,IACxBI,EAAMJ,CAAE,IAAMQ,IAClB,GAAK,GAGP,OAAO,CACR,CAyBA,SAASC,EAAgBZ,EAAGC,EAAQ,CACnC,OAAKP,EAAiBM,EAAGC,CAAM,EACzBT,EAAqBQ,EAAGC,CAAM,EAC3BK,EAASN,EAAGC,CAAM,EAErBR,EAAgBO,EAAGC,CAAM,EACtBS,EAASV,EAAGC,CAAM,EAEnBG,EAAWJ,EAAGC,CAAM,EAErBF,EAASC,EAAGC,CAAM,CAC1B,CAKAd,EAAO,QAAUyB,ICjLjB,IAAIC,EAAO,IAKX,OAAO,QAAUA",
|
|
6
|
+
"names": ["require_main", "__commonJSMin", "exports", "module", "isComplexLike", "isBoolean", "reinterpret", "reinterpretBoolean", "isComplexTypedArray", "isBooleanArray", "isAccessorArray", "resolveGetter", "isSameValue", "real", "imag", "indexed", "x", "value", "n", "i", "accessors", "get", "complex", "view", "re", "im", "boolean", "v", "countSameValue", "main"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -23,11 +23,18 @@
|
|
|
23
23
|
import { Collection } from '@stdlib/types/array';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
* Counts the number of elements that are equal to a
|
|
26
|
+
* Counts the number of elements in an array that are equal to a specified value.
|
|
27
|
+
*
|
|
28
|
+
* ## Notes
|
|
29
|
+
*
|
|
30
|
+
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
|
|
31
|
+
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
|
|
32
|
+
*
|
|
33
|
+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
|
|
27
34
|
*
|
|
28
35
|
* @param x - input array
|
|
29
|
-
* @param value -
|
|
30
|
-
* @returns number of elements that are equal to
|
|
36
|
+
* @param value - search value
|
|
37
|
+
* @returns number of elements that are equal to a specified value
|
|
31
38
|
*
|
|
32
39
|
* @example
|
|
33
40
|
* var x = [ 0, 1, 0, 1, 1 ];
|
package/lib/index.js
CHANGED
package/lib/main.js
CHANGED
|
@@ -21,30 +21,33 @@
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
23
|
var isComplexLike = require( '@stdlib/assert-is-complex-like' );
|
|
24
|
-
var
|
|
25
|
-
var imag = require( '@stdlib/complex-imag' );
|
|
24
|
+
var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
|
|
26
25
|
var reinterpret = require( '@stdlib/strided-base-reinterpret-complex' );
|
|
26
|
+
var reinterpretBoolean = require( '@stdlib/strided-base-reinterpret-boolean' );
|
|
27
27
|
var isComplexTypedArray = require( '@stdlib/array-base-assert-is-complex-typed-array' );
|
|
28
|
+
var isBooleanArray = require( '@stdlib/array-base-assert-is-booleanarray' );
|
|
28
29
|
var isAccessorArray = require( '@stdlib/array-base-assert-is-accessor-array' );
|
|
29
30
|
var resolveGetter = require( '@stdlib/array-base-resolve-getter' );
|
|
30
31
|
var isSameValue = require( '@stdlib/assert-is-same-value' );
|
|
32
|
+
var real = require( '@stdlib/complex-float64-real' );
|
|
33
|
+
var imag = require( '@stdlib/complex-float64-imag' );
|
|
31
34
|
|
|
32
35
|
|
|
33
36
|
// FUNCTIONS //
|
|
34
37
|
|
|
35
38
|
/**
|
|
36
|
-
* Counts the number of elements that are equal to a
|
|
39
|
+
* Counts the number of elements in an array that are equal to a specified value.
|
|
37
40
|
*
|
|
38
41
|
* @private
|
|
39
42
|
* @param {Collection} x - input array
|
|
40
|
-
* @param {*} value -
|
|
41
|
-
* @returns {NonNegativeInteger} number of elements that are equal to
|
|
43
|
+
* @param {*} value - search value
|
|
44
|
+
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
|
|
42
45
|
*
|
|
43
46
|
* @example
|
|
44
|
-
* var x = [ 0, 1, 0, 1 ];
|
|
47
|
+
* var x = [ 0, 1, 0, 1, 1 ];
|
|
45
48
|
*
|
|
46
49
|
* var n = indexed( x, 1 );
|
|
47
|
-
* // returns
|
|
50
|
+
* // returns 3
|
|
48
51
|
*/
|
|
49
52
|
function indexed( x, value ) {
|
|
50
53
|
var n;
|
|
@@ -60,20 +63,20 @@ function indexed( x, value ) {
|
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
/**
|
|
63
|
-
* Counts the number of elements that are equal to a
|
|
66
|
+
* Counts the number of elements in an accessor array that are equal to a specified value.
|
|
64
67
|
*
|
|
65
68
|
* @private
|
|
66
69
|
* @param {Collection} x - input array
|
|
67
|
-
* @param {*} value -
|
|
68
|
-
* @returns {NonNegativeInteger} number of elements that are equal to
|
|
70
|
+
* @param {*} value - search value
|
|
71
|
+
* @returns {NonNegativeInteger} number of elements that are equal to a provided value
|
|
69
72
|
*
|
|
70
73
|
* @example
|
|
71
74
|
* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
|
|
72
75
|
*
|
|
73
|
-
* var x = toAccessorArray( [ 0, 1, 0, 1 ] );
|
|
76
|
+
* var x = toAccessorArray( [ 0, 1, 0, 1, 1 ] );
|
|
74
77
|
*
|
|
75
78
|
* var n = accessors( x, 1 );
|
|
76
|
-
* // returns
|
|
79
|
+
* // returns 3
|
|
77
80
|
*/
|
|
78
81
|
function accessors( x, value ) {
|
|
79
82
|
var get;
|
|
@@ -92,16 +95,16 @@ function accessors( x, value ) {
|
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
/**
|
|
95
|
-
* Counts the number of elements that are equal to a
|
|
98
|
+
* Counts the number of elements in a complex array that are equal to a specified value.
|
|
96
99
|
*
|
|
97
100
|
* @private
|
|
98
101
|
* @param {Collection} x - input array
|
|
99
|
-
* @param {*} value -
|
|
100
|
-
* @returns {NonNegativeInteger} number of elements that are equal to
|
|
102
|
+
* @param {*} value - search value
|
|
103
|
+
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
|
|
101
104
|
*
|
|
102
105
|
* @example
|
|
103
|
-
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
104
106
|
* var Complex128Array = require( '@stdlib/array-complex128' );
|
|
107
|
+
* var Complex128 = require( '@stdlib/complex-float64-ctor' );
|
|
105
108
|
*
|
|
106
109
|
* var x = new Complex128Array( [ 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0 ] );
|
|
107
110
|
*
|
|
@@ -118,7 +121,6 @@ function complex( x, value ) {
|
|
|
118
121
|
if ( !isComplexLike( value ) ) {
|
|
119
122
|
return 0;
|
|
120
123
|
}
|
|
121
|
-
|
|
122
124
|
re = real( value );
|
|
123
125
|
im = imag( value );
|
|
124
126
|
|
|
@@ -126,7 +128,44 @@ function complex( x, value ) {
|
|
|
126
128
|
|
|
127
129
|
n = 0;
|
|
128
130
|
for ( i = 0; i < view.length; i += 2 ) {
|
|
129
|
-
if ( isSameValue( view[ i ], re ) && isSameValue( view[ i
|
|
131
|
+
if ( isSameValue( view[ i ], re ) && isSameValue( view[ i+1 ], im ) ) {
|
|
132
|
+
n += 1;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return n;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Counts the number of elements in a boolean array that are equal to a specified value.
|
|
140
|
+
*
|
|
141
|
+
* @private
|
|
142
|
+
* @param {Collection} x - input array
|
|
143
|
+
* @param {*} value - search value
|
|
144
|
+
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* var BooleanArray = require( '@stdlib/array-bool' );
|
|
148
|
+
*
|
|
149
|
+
* var x = new BooleanArray( [ true, false, true, false, true ] );
|
|
150
|
+
*
|
|
151
|
+
* var n = boolean( x, true );
|
|
152
|
+
* // returns 3
|
|
153
|
+
*/
|
|
154
|
+
function boolean( x, value ) {
|
|
155
|
+
var view;
|
|
156
|
+
var n;
|
|
157
|
+
var v;
|
|
158
|
+
var i;
|
|
159
|
+
|
|
160
|
+
if ( !isBoolean( value ) ) {
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
view = reinterpretBoolean( x, 0 );
|
|
164
|
+
|
|
165
|
+
v = ( value ) ? 1 : 0;
|
|
166
|
+
n = 0;
|
|
167
|
+
for ( i = 0; i < view.length; i++ ) {
|
|
168
|
+
if ( view[ i ] === v ) {
|
|
130
169
|
n += 1;
|
|
131
170
|
}
|
|
132
171
|
}
|
|
@@ -137,11 +176,18 @@ function complex( x, value ) {
|
|
|
137
176
|
// MAIN //
|
|
138
177
|
|
|
139
178
|
/**
|
|
140
|
-
* Counts the number of elements that are equal to a
|
|
179
|
+
* Counts the number of elements in an array that are equal to a specified value.
|
|
180
|
+
*
|
|
181
|
+
* ## Notes
|
|
182
|
+
*
|
|
183
|
+
* - The function uses the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5.
|
|
184
|
+
* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same.
|
|
185
|
+
*
|
|
186
|
+
* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
|
|
141
187
|
*
|
|
142
188
|
* @param {Collection} x - input array
|
|
143
|
-
* @param {*} value -
|
|
144
|
-
* @returns {NonNegativeInteger} number of elements that are equal to
|
|
189
|
+
* @param {*} value - search value
|
|
190
|
+
* @returns {NonNegativeInteger} number of elements that are equal to a specified value
|
|
145
191
|
*
|
|
146
192
|
* @example
|
|
147
193
|
* var x = [ 0, 1, 0, 1, 1 ];
|
|
@@ -154,6 +200,9 @@ function countSameValue( x, value ) {
|
|
|
154
200
|
if ( isComplexTypedArray( x, value ) ) {
|
|
155
201
|
return complex( x, value );
|
|
156
202
|
}
|
|
203
|
+
if ( isBooleanArray( x, value ) ) {
|
|
204
|
+
return boolean( x, value );
|
|
205
|
+
}
|
|
157
206
|
return accessors( x, value );
|
|
158
207
|
}
|
|
159
208
|
return indexed( x, value );
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/array-base-count-same-value",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Count the number of elements that are equal to a
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Count the number of elements in an array that are equal to a specified value.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "The Stdlib Authors",
|
|
@@ -30,14 +30,17 @@
|
|
|
30
30
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@stdlib/array-base-assert-is-accessor-array": "^0.2.
|
|
34
|
-
"@stdlib/array-base-assert-is-
|
|
35
|
-
"@stdlib/array-base-
|
|
36
|
-
"@stdlib/
|
|
37
|
-
"@stdlib/assert-is-
|
|
38
|
-
"@stdlib/complex-
|
|
39
|
-
"@stdlib/
|
|
40
|
-
"@stdlib/
|
|
33
|
+
"@stdlib/array-base-assert-is-accessor-array": "^0.2.2",
|
|
34
|
+
"@stdlib/array-base-assert-is-booleanarray": "^0.0.2",
|
|
35
|
+
"@stdlib/array-base-assert-is-complex-typed-array": "^0.1.2",
|
|
36
|
+
"@stdlib/array-base-resolve-getter": "^0.2.1",
|
|
37
|
+
"@stdlib/assert-is-boolean": "^0.2.2",
|
|
38
|
+
"@stdlib/assert-is-complex-like": "^0.2.2",
|
|
39
|
+
"@stdlib/assert-is-same-value": "^0.2.2",
|
|
40
|
+
"@stdlib/complex-float64-imag": "^0.1.1",
|
|
41
|
+
"@stdlib/complex-float64-real": "^0.1.1",
|
|
42
|
+
"@stdlib/strided-base-reinterpret-boolean": "^0.0.2",
|
|
43
|
+
"@stdlib/strided-base-reinterpret-complex": "^0.1.2"
|
|
41
44
|
},
|
|
42
45
|
"devDependencies": {},
|
|
43
46
|
"engines": {
|
|
@@ -67,7 +70,8 @@
|
|
|
67
70
|
"summation",
|
|
68
71
|
"countif",
|
|
69
72
|
"total",
|
|
70
|
-
"same"
|
|
73
|
+
"same",
|
|
74
|
+
"equal"
|
|
71
75
|
],
|
|
72
76
|
"funding": {
|
|
73
77
|
"type": "opencollective",
|