@ohos-ports/stdlib-object 0.0.0-stage → 0.4.1-beta.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/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +262 -2
- package/SECURITY.md +5 -0
- package/any-in-by/README.md +217 -0
- package/any-in-by/docs/repl.txt +41 -0
- package/any-in-by/docs/types/index.d.ts +102 -0
- package/any-in-by/docs/types/test.ts +53 -0
- package/any-in-by/lib/index.js +50 -0
- package/any-in-by/lib/main.js +75 -0
- package/any-in-by/package.json +66 -0
- package/any-own-by/README.md +221 -0
- package/any-own-by/docs/repl.txt +43 -0
- package/any-own-by/docs/types/index.d.ts +101 -0
- package/any-own-by/docs/types/test.ts +63 -0
- package/any-own-by/lib/index.js +46 -0
- package/any-own-by/lib/main.js +77 -0
- package/any-own-by/package.json +70 -0
- package/assign/README.md +139 -0
- package/assign/docs/repl.txt +34 -0
- package/assign/docs/types/index.d.ts +113 -0
- package/assign/docs/types/test.ts +47 -0
- package/assign/lib/builtin.js +55 -0
- package/assign/lib/has_object_assign.js +33 -0
- package/assign/lib/index.js +52 -0
- package/assign/lib/polyfill.js +86 -0
- package/assign/package.json +64 -0
- package/assign-in/README.md +145 -0
- package/assign-in/docs/repl.txt +34 -0
- package/assign-in/docs/types/index.d.ts +175 -0
- package/assign-in/docs/types/test.ts +44 -0
- package/assign-in/lib/index.js +54 -0
- package/assign-in/lib/main.js +92 -0
- package/assign-in/package.json +60 -0
- package/bifurcate-in/README.md +298 -0
- package/bifurcate-in/docs/repl.txt +68 -0
- package/bifurcate-in/docs/types/index.d.ts +189 -0
- package/bifurcate-in/docs/types/test.ts +97 -0
- package/bifurcate-in/lib/index.js +106 -0
- package/bifurcate-in/lib/main.js +148 -0
- package/bifurcate-in/lib/return_keys.js +81 -0
- package/bifurcate-in/lib/return_pairs.js +87 -0
- package/bifurcate-in/lib/return_values.js +87 -0
- package/bifurcate-in/lib/validate.js +76 -0
- package/bifurcate-in/package.json +72 -0
- package/bifurcate-own/README.md +255 -0
- package/bifurcate-own/docs/repl.txt +63 -0
- package/bifurcate-own/docs/types/index.d.ts +183 -0
- package/bifurcate-own/docs/types/test.ts +97 -0
- package/bifurcate-own/lib/index.js +87 -0
- package/bifurcate-own/lib/main.js +127 -0
- package/bifurcate-own/lib/return_keys.js +81 -0
- package/bifurcate-own/lib/return_pairs.js +87 -0
- package/bifurcate-own/lib/return_values.js +87 -0
- package/bifurcate-own/lib/validate.js +76 -0
- package/bifurcate-own/package.json +71 -0
- package/capitalize-keys/README.md +139 -0
- package/capitalize-keys/docs/repl.txt +28 -0
- package/capitalize-keys/docs/types/index.d.ts +46 -0
- package/capitalize-keys/docs/types/test.ts +33 -0
- package/capitalize-keys/lib/index.js +45 -0
- package/capitalize-keys/lib/main.js +69 -0
- package/capitalize-keys/package.json +76 -0
- package/common-keys/README.md +125 -0
- package/common-keys/docs/repl.txt +29 -0
- package/common-keys/docs/types/index.d.ts +70 -0
- package/common-keys/docs/types/test.ts +33 -0
- package/common-keys/lib/index.js +51 -0
- package/common-keys/lib/main.js +106 -0
- package/common-keys/package.json +70 -0
- package/common-keys-in/README.md +125 -0
- package/common-keys-in/docs/repl.txt +29 -0
- package/common-keys-in/docs/types/index.d.ts +70 -0
- package/common-keys-in/docs/types/test.ts +33 -0
- package/common-keys-in/lib/index.js +51 -0
- package/common-keys-in/lib/main.js +106 -0
- package/common-keys-in/package.json +72 -0
- package/ctor/README.md +536 -0
- package/ctor/docs/repl.txt +592 -0
- package/ctor/docs/types/index.d.ts +56 -0
- package/ctor/docs/types/test.ts +33 -0
- package/ctor/lib/index.js +54 -0
- package/ctor/lib/main.js +58 -0
- package/ctor/package.json +58 -0
- package/deep-get/README.md +181 -0
- package/deep-get/docs/repl.txt +63 -0
- package/deep-get/docs/types/index.d.ts +125 -0
- package/deep-get/docs/types/test.ts +109 -0
- package/deep-get/lib/defaults.js +38 -0
- package/deep-get/lib/dget.js +57 -0
- package/deep-get/lib/factory.js +94 -0
- package/deep-get/lib/index.js +58 -0
- package/deep-get/lib/main.js +101 -0
- package/deep-get/lib/validate.js +66 -0
- package/deep-get/package.json +68 -0
- package/deep-set/README.md +273 -0
- package/deep-set/docs/repl.txt +87 -0
- package/deep-set/docs/types/index.d.ts +180 -0
- package/deep-set/docs/types/test.ts +134 -0
- package/deep-set/lib/defaults.js +39 -0
- package/deep-set/lib/dset.js +80 -0
- package/deep-set/lib/factory.js +99 -0
- package/deep-set/lib/index.js +58 -0
- package/deep-set/lib/main.js +120 -0
- package/deep-set/lib/validate.js +74 -0
- package/deep-set/package.json +69 -0
- package/docs/types/index.d.ts +964 -0
- package/every-in-by/README.md +156 -0
- package/every-in-by/docs/repl.txt +43 -0
- package/every-in-by/docs/types/index.d.ts +83 -0
- package/every-in-by/docs/types/test.ts +60 -0
- package/every-in-by/lib/index.js +51 -0
- package/every-in-by/lib/main.js +70 -0
- package/every-in-by/package.json +67 -0
- package/every-own-by/README.md +223 -0
- package/every-own-by/docs/repl.txt +43 -0
- package/every-own-by/docs/types/index.d.ts +102 -0
- package/every-own-by/docs/types/test.ts +62 -0
- package/every-own-by/lib/index.js +46 -0
- package/every-own-by/lib/main.js +77 -0
- package/every-own-by/package.json +68 -0
- package/for-in/README.md +218 -0
- package/for-in/docs/repl.txt +44 -0
- package/for-in/docs/types/index.d.ts +65 -0
- package/for-in/docs/types/test.ts +50 -0
- package/for-in/lib/index.js +54 -0
- package/for-in/lib/main.js +82 -0
- package/for-in/package.json +74 -0
- package/for-own/README.md +209 -0
- package/for-own/docs/repl.txt +49 -0
- package/for-own/docs/types/index.d.ts +63 -0
- package/for-own/docs/types/test.ts +50 -0
- package/for-own/lib/index.js +50 -0
- package/for-own/lib/main.js +85 -0
- package/for-own/package.json +71 -0
- package/inverse/README.md +163 -0
- package/inverse/docs/repl.txt +53 -0
- package/inverse/docs/types/index.d.ts +66 -0
- package/inverse/docs/types/test.ts +60 -0
- package/inverse/lib/index.js +59 -0
- package/inverse/lib/main.js +123 -0
- package/inverse/package.json +69 -0
- package/inverse-by/README.md +205 -0
- package/inverse-by/docs/repl.txt +67 -0
- package/inverse-by/docs/types/index.d.ts +160 -0
- package/inverse-by/docs/types/test.ts +81 -0
- package/inverse-by/lib/index.js +78 -0
- package/inverse-by/lib/main.js +148 -0
- package/inverse-by/package.json +75 -0
- package/lib/index.js +276 -0
- package/lowercase-keys/README.md +139 -0
- package/lowercase-keys/docs/repl.txt +28 -0
- package/lowercase-keys/docs/types/index.d.ts +46 -0
- package/lowercase-keys/docs/types/test.ts +33 -0
- package/lowercase-keys/lib/index.js +45 -0
- package/lowercase-keys/lib/main.js +63 -0
- package/lowercase-keys/package.json +74 -0
- package/move-property/README.md +144 -0
- package/move-property/docs/repl.txt +38 -0
- package/move-property/docs/types/index.d.ts +53 -0
- package/move-property/docs/types/test.ts +46 -0
- package/move-property/lib/index.js +46 -0
- package/move-property/lib/main.js +74 -0
- package/move-property/package.json +69 -0
- package/none-in-by/README.md +241 -0
- package/none-in-by/docs/repl.txt +42 -0
- package/none-in-by/docs/types/index.d.ts +101 -0
- package/none-in-by/docs/types/test.ts +61 -0
- package/none-in-by/lib/index.js +46 -0
- package/none-in-by/lib/main.js +71 -0
- package/none-in-by/package.json +68 -0
- package/none-own-by/README.md +177 -0
- package/none-own-by/docs/repl.txt +42 -0
- package/none-own-by/docs/types/index.d.ts +102 -0
- package/none-own-by/docs/types/test.ts +61 -0
- package/none-own-by/lib/index.js +50 -0
- package/none-own-by/lib/main.js +75 -0
- package/none-own-by/package.json +68 -0
- package/package.json +66 -4
- package/some-in-by/README.md +232 -0
- package/some-in-by/docs/repl.txt +45 -0
- package/some-in-by/docs/types/index.d.ts +104 -0
- package/some-in-by/docs/types/test.ts +66 -0
- package/some-in-by/lib/index.js +46 -0
- package/some-in-by/lib/main.js +87 -0
- package/some-in-by/package.json +75 -0
- package/some-own-by/README.md +251 -0
- package/some-own-by/docs/repl.txt +45 -0
- package/some-own-by/docs/types/index.d.ts +103 -0
- package/some-own-by/docs/types/test.ts +66 -0
- package/some-own-by/lib/index.js +46 -0
- package/some-own-by/lib/main.js +85 -0
- package/some-own-by/package.json +72 -0
- package/uncapitalize-keys/README.md +139 -0
- package/uncapitalize-keys/docs/repl.txt +28 -0
- package/uncapitalize-keys/docs/types/index.d.ts +46 -0
- package/uncapitalize-keys/docs/types/test.ts +33 -0
- package/uncapitalize-keys/lib/index.js +45 -0
- package/uncapitalize-keys/lib/main.js +69 -0
- package/uncapitalize-keys/package.json +76 -0
- package/uppercase-keys/README.md +139 -0
- package/uppercase-keys/docs/repl.txt +28 -0
- package/uppercase-keys/docs/types/index.d.ts +46 -0
- package/uppercase-keys/docs/types/test.ts +33 -0
- package/uppercase-keys/lib/index.js +45 -0
- package/uppercase-keys/lib/main.js +63 -0
- package/uppercase-keys/package.json +74 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
# someInBy
|
|
22
|
+
|
|
23
|
+
> Test whether an object contains at least `n` properties which pass a test implemented by a predicate function.
|
|
24
|
+
|
|
25
|
+
<section class="intro">
|
|
26
|
+
|
|
27
|
+
</section>
|
|
28
|
+
|
|
29
|
+
<!-- /.intro -->
|
|
30
|
+
|
|
31
|
+
<section class="usage">
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
var someInBy = require( '@stdlib/object/some-in-by' );
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### someInBy( obj, n, predicate\[, thisArg ] )
|
|
40
|
+
|
|
41
|
+
Tests whether an `obj` contains at least `n` properties which pass a test implemented by a `predicate` function.
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
function isNegative( value ) {
|
|
45
|
+
return ( value < 0 );
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var obj = {
|
|
49
|
+
'a': 1,
|
|
50
|
+
'b': -2,
|
|
51
|
+
'c': 3,
|
|
52
|
+
'd': -1
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
var bool = someInBy( obj, 2, isNegative );
|
|
56
|
+
// returns true
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Once the function finds `n` successful properties, the function **immediately** returns `true`.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
function isPositive( value ) {
|
|
63
|
+
if ( value < 0 ) {
|
|
64
|
+
throw new Error( 'should never reach this line' );
|
|
65
|
+
}
|
|
66
|
+
return ( value > 0 );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var obj = {
|
|
70
|
+
'a': 1,
|
|
71
|
+
'b': 2,
|
|
72
|
+
'c': -3,
|
|
73
|
+
'd': 4
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
var bool = someInBy( obj, 2, isPositive );
|
|
77
|
+
// returns true
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The invoked `function` is provided three arguments:
|
|
81
|
+
|
|
82
|
+
- **value**: object property value.
|
|
83
|
+
- **key**: object property key.
|
|
84
|
+
- **obj**: input object.
|
|
85
|
+
|
|
86
|
+
To set the function execution context, provide a `thisArg`.
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
function sum( value ) {
|
|
90
|
+
this.sum += value;
|
|
91
|
+
this.count += 1;
|
|
92
|
+
return ( value < 0 );
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var obj = {
|
|
96
|
+
'a': 1,
|
|
97
|
+
'b': 2,
|
|
98
|
+
'c': 3,
|
|
99
|
+
'd': -5
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
var context = {
|
|
103
|
+
'sum': 0,
|
|
104
|
+
'count': 0
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
var bool = someInBy( obj, 1, sum, context );
|
|
108
|
+
// returns true
|
|
109
|
+
|
|
110
|
+
var mean = context.sum / context.count;
|
|
111
|
+
// returns 0.25
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
</section>
|
|
115
|
+
|
|
116
|
+
<!-- /.usage -->
|
|
117
|
+
|
|
118
|
+
<section class="notes">
|
|
119
|
+
|
|
120
|
+
## Notes
|
|
121
|
+
|
|
122
|
+
- If provided an empty `obj`, the function returns `false`.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
function alwaysTrue() {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
var bool = someInBy( {}, 1, alwaysTrue );
|
|
129
|
+
// returns false
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- The function does **not** skip `undefined` properties.
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
function log( value, key ) {
|
|
136
|
+
console.log( '%s: %s', key, value );
|
|
137
|
+
return ( value < 0 );
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
var obj = {
|
|
141
|
+
'a': 1,
|
|
142
|
+
'b': void 0,
|
|
143
|
+
'c': void 0,
|
|
144
|
+
'd': 4,
|
|
145
|
+
'e': -1
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
var bool = someInBy( obj, 1, log );
|
|
149
|
+
// logs
|
|
150
|
+
// a: 1
|
|
151
|
+
// b: void 0
|
|
152
|
+
// c: void 0
|
|
153
|
+
// d: 4
|
|
154
|
+
// e: -1
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
- The function provides limited support for dynamic objects (i.e., objects whose properties change during execution).
|
|
158
|
+
|
|
159
|
+
</section>
|
|
160
|
+
|
|
161
|
+
<!-- /.notes -->
|
|
162
|
+
|
|
163
|
+
<section class="examples">
|
|
164
|
+
|
|
165
|
+
## Examples
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
var randu = require( '@stdlib/random/base/randu' );
|
|
169
|
+
var someInBy = require( '@stdlib/object/some-in-by' );
|
|
170
|
+
|
|
171
|
+
function threshold( value ) {
|
|
172
|
+
return ( value > 0.95 );
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
var bool;
|
|
176
|
+
var obj = {};
|
|
177
|
+
var i;
|
|
178
|
+
|
|
179
|
+
for ( i = 0; i < 100; i++ ) {
|
|
180
|
+
obj[ 'key' + i ] = randu();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
bool = someInBy( obj, 5, threshold );
|
|
184
|
+
// returns <boolean>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
</section>
|
|
188
|
+
|
|
189
|
+
<!-- /.examples -->
|
|
190
|
+
|
|
191
|
+
<section class="references">
|
|
192
|
+
|
|
193
|
+
</section>
|
|
194
|
+
|
|
195
|
+
<!-- /.references -->
|
|
196
|
+
|
|
197
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
198
|
+
|
|
199
|
+
<section class="related">
|
|
200
|
+
|
|
201
|
+
* * *
|
|
202
|
+
|
|
203
|
+
## See Also
|
|
204
|
+
|
|
205
|
+
- <span class="package-name">[`@stdlib/object/any-in-by`][@stdlib/object/any-in-by]</span><span class="delimiter">: </span><span class="description">test whether at least one property in an object passes a test implemented by a predicate function.</span>
|
|
206
|
+
- <span class="package-name">[`@stdlib/object/every-in-by`][@stdlib/object/every-in-by]</span><span class="delimiter">: </span><span class="description">test whether all properties (own and inherited) of an object pass a test implemented by a predicate function.</span>
|
|
207
|
+
- <span class="package-name">[`@stdlib/utils/some-by`][@stdlib/utils/some-by]</span><span class="delimiter">: </span><span class="description">test whether a collection contains at least `n` elements which pass a test implemented by a predicate function.</span>
|
|
208
|
+
- <span class="package-name">[`@stdlib/object/some-own-by`][@stdlib/object/some-own-by]</span><span class="delimiter">: </span><span class="description">test whether some `own` properties of a provided object satisfy a predicate function for at least `n` properties.</span>
|
|
209
|
+
|
|
210
|
+
</section>
|
|
211
|
+
|
|
212
|
+
<!-- /.related -->
|
|
213
|
+
|
|
214
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
215
|
+
|
|
216
|
+
<section class="links">
|
|
217
|
+
|
|
218
|
+
<!-- <related-links> -->
|
|
219
|
+
|
|
220
|
+
[@stdlib/object/any-in-by]: https://github.com/stdlib-js/object/tree/main/any-in-by
|
|
221
|
+
|
|
222
|
+
[@stdlib/object/every-in-by]: https://github.com/stdlib-js/object/tree/main/every-in-by
|
|
223
|
+
|
|
224
|
+
[@stdlib/utils/some-by]: https://www.npmjs.com/package/@stdlib/utils-some-by
|
|
225
|
+
|
|
226
|
+
[@stdlib/object/some-own-by]: https://github.com/stdlib-js/object/tree/main/some-own-by
|
|
227
|
+
|
|
228
|
+
<!-- </related-links> -->
|
|
229
|
+
|
|
230
|
+
</section>
|
|
231
|
+
|
|
232
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( obj, n, predicate[, thisArg ] )
|
|
3
|
+
Tests whether an object contains at least `n` properties
|
|
4
|
+
(own and inherited) which pass a test
|
|
5
|
+
implemented by a predicate function.
|
|
6
|
+
|
|
7
|
+
The predicate function is provided three arguments:
|
|
8
|
+
|
|
9
|
+
- value: object value.
|
|
10
|
+
- key: object key.
|
|
11
|
+
- obj: the input object.
|
|
12
|
+
|
|
13
|
+
The function immediately returns upon finding `n` successful properties.
|
|
14
|
+
|
|
15
|
+
If provided an empty object, the function returns `false`.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
obj: Object
|
|
20
|
+
Input object over which to iterate.
|
|
21
|
+
|
|
22
|
+
n: number
|
|
23
|
+
Minimum number of successful properties.
|
|
24
|
+
|
|
25
|
+
predicate: Function
|
|
26
|
+
Test function.
|
|
27
|
+
|
|
28
|
+
thisArg: any (optional)
|
|
29
|
+
Execution context.
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
bool: boolean
|
|
34
|
+
The function returns `true` if an object contains at least `n`
|
|
35
|
+
successful properties; otherwise, the function returns `false`.
|
|
36
|
+
|
|
37
|
+
Examples
|
|
38
|
+
--------
|
|
39
|
+
> function negative( v ) { return ( v < 0 ); };
|
|
40
|
+
> var obj = { 'a': 1, 'b': 2, 'c': -3, 'd': 4, 'e': -1 };
|
|
41
|
+
> var bool = {{alias}}( obj, 2, negative )
|
|
42
|
+
true
|
|
43
|
+
|
|
44
|
+
See Also
|
|
45
|
+
--------
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Checks whether a property in an object passes a test.
|
|
25
|
+
*
|
|
26
|
+
* @returns boolean indicating whether a property in an object passes a test
|
|
27
|
+
*/
|
|
28
|
+
type Nullary<U> = ( this: U ) => boolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Checks whether a property in an object passes a test.
|
|
32
|
+
*
|
|
33
|
+
* @param value - object value
|
|
34
|
+
* @returns boolean indicating whether a property in an object passes a test
|
|
35
|
+
*/
|
|
36
|
+
type Unary<T, U> = ( this: U, value: T ) => boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Checks whether a property in an object passes a test.
|
|
40
|
+
*
|
|
41
|
+
* @param value - object value
|
|
42
|
+
* @param key - object key
|
|
43
|
+
* @returns boolean indicating whether a property in an object passes a test
|
|
44
|
+
*/
|
|
45
|
+
type Binary<T, U> = ( this: U, value: T, key: string ) => boolean;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks whether a property in an object passes a test.
|
|
49
|
+
*
|
|
50
|
+
* @param value - object value
|
|
51
|
+
* @param key - object key
|
|
52
|
+
* @param obj - input object
|
|
53
|
+
* @returns boolean indicating whether a property in an object passes a test
|
|
54
|
+
*/
|
|
55
|
+
type Ternary<T, U> = ( this: U, value: T, key: string, obj: Record<keyof any, any> ) => boolean;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Checks whether a property in an object passes a test.
|
|
59
|
+
*
|
|
60
|
+
* @param value - object value
|
|
61
|
+
* @param key - object key
|
|
62
|
+
* @param obj - input object
|
|
63
|
+
* @returns boolean indicating whether a property in an object passes a test
|
|
64
|
+
*/
|
|
65
|
+
type Predicate<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Tests whether an object contains at least `n` properties (own and inherited) which pass a test implemented by a predicate function.
|
|
69
|
+
*
|
|
70
|
+
* ## Notes
|
|
71
|
+
*
|
|
72
|
+
* - The predicate function is provided three arguments:
|
|
73
|
+
*
|
|
74
|
+
* - `value`: object value
|
|
75
|
+
* - `key`: object key
|
|
76
|
+
* - `obj`: the input object
|
|
77
|
+
*
|
|
78
|
+
* - The function immediately returns upon finding `n` successful properties.
|
|
79
|
+
*
|
|
80
|
+
* - If provided an empty object, the function returns `false`.
|
|
81
|
+
*
|
|
82
|
+
* @param obj - input object
|
|
83
|
+
* @param n - number of properties
|
|
84
|
+
* @param predicate - test function
|
|
85
|
+
* @param thisArg - execution context
|
|
86
|
+
* @throws second argument must be a positive integer
|
|
87
|
+
* @returns boolean indicating whether an object contains at least `n` properties which pass a test
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* function isNegative( v ) {
|
|
91
|
+
* return ( v < 0 );
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* var obj = { 'a': 1, 'b': 2, 'c': -3, 'd': 4, 'e': -1 };
|
|
95
|
+
*
|
|
96
|
+
* var bool = someInBy( obj, 2, isNegative );
|
|
97
|
+
* // returns true
|
|
98
|
+
*/
|
|
99
|
+
declare function someInBy<T = unknown, U = unknown>( obj: Record<keyof any, any>, n: number, predicate: Predicate<T, U>, thisArg?: ThisParameterType<Predicate<T, U>> ): boolean;
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
// EXPORTS //
|
|
103
|
+
|
|
104
|
+
export = someInBy;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
import someInBy = require( './index' );
|
|
20
|
+
|
|
21
|
+
const hasUpperCase = ( v: string, key: string ): boolean => {
|
|
22
|
+
return ( key.toUpperCase() === key );
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// TESTS //
|
|
26
|
+
|
|
27
|
+
// The function returns a boolean...
|
|
28
|
+
{
|
|
29
|
+
someInBy( { 'a': 0, 'B': 1, 'C': 1 }, 2, hasUpperCase ); // $ExpectType boolean
|
|
30
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, 3, hasUpperCase, {} ); // $ExpectType boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The compiler throws an error if the function is provided a first argument which is not an object...
|
|
34
|
+
{
|
|
35
|
+
someInBy( 2, 2, hasUpperCase ); // $ExpectError
|
|
36
|
+
someInBy( false, 2, hasUpperCase ); // $ExpectError
|
|
37
|
+
someInBy( true, 2, hasUpperCase ); // $ExpectError
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The compiler throws an error if the function is provided a second argument which is not a number...
|
|
41
|
+
{
|
|
42
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, ( x: number ): number => x, hasUpperCase ); // $ExpectError
|
|
43
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, false, hasUpperCase ); // $ExpectError
|
|
44
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, true, hasUpperCase ); // $ExpectError
|
|
45
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, 'abc', hasUpperCase ); // $ExpectError
|
|
46
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, {}, hasUpperCase ); // $ExpectError
|
|
47
|
+
someInBy( { 'a': -1, 'B': 1, 'c': 2 }, [], hasUpperCase ); // $ExpectError
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The compiler throws an error if the function is provided a third argument which is not a function...
|
|
51
|
+
{
|
|
52
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, 2 ); // $ExpectError
|
|
53
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, false ); // $ExpectError
|
|
54
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, true ); // $ExpectError
|
|
55
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, 'abc' ); // $ExpectError
|
|
56
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, {} ); // $ExpectError
|
|
57
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, [] ); // $ExpectError
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The compiler throws an error if the function is provided an invalid number of arguments...
|
|
61
|
+
{
|
|
62
|
+
someInBy(); // $ExpectError
|
|
63
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 } ); // $ExpectError
|
|
64
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1 ); // $ExpectError
|
|
65
|
+
someInBy( { 'a': 1, 'B': 2, 'c': 3 }, 1, hasUpperCase, {}, 3 ); // $ExpectError
|
|
66
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
/**
|
|
22
|
+
* Test whether an object contains at least `n` properties (own or inherited) which pass a test implemented by a predicate function.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/object/some-in-by
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var someInBy = require( '@ohos-ports/stdlib-object/some-in-by' );
|
|
28
|
+
*
|
|
29
|
+
* function isNegative( v ) {
|
|
30
|
+
* return ( v < 0 );
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* var obj = { a: 1, b: -2, c: 3, d: 4, e: -1 };
|
|
34
|
+
*
|
|
35
|
+
* var bool = someInBy( obj, 2, isNegative );
|
|
36
|
+
* // returns true
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
// MODULES //
|
|
40
|
+
|
|
41
|
+
var main = require( './main.js' );
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
// EXPORTS //
|
|
45
|
+
|
|
46
|
+
module.exports = main;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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 isObject = require( '@ohos-ports/stdlib-assert/is-object' );
|
|
24
|
+
var isPositiveInteger = require( '@ohos-ports/stdlib-assert/is-positive-integer' ).isPrimitive;
|
|
25
|
+
var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
|
|
26
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// MAIN //
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Tests whether an object contains at least `n` properties (own or inherited) which pass a test implemented by a predicate function.
|
|
33
|
+
*
|
|
34
|
+
* @param {Object} obj - input object
|
|
35
|
+
* @param {PositiveInteger} n - number of properties
|
|
36
|
+
* @param {Function} predicate - test function
|
|
37
|
+
* @param {*} [thisArg] - execution context
|
|
38
|
+
* @throws {TypeError} first argument must be an object
|
|
39
|
+
* @throws {TypeError} second argument must be a positive integer
|
|
40
|
+
* @throws {TypeError} third argument must be a function
|
|
41
|
+
* @returns {boolean} boolean indicating whether an object contains at least `n` properties which pass a test
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* function isNegative( v ) {
|
|
45
|
+
* return ( v < 0 );
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* var obj = { a: 1, b: -2, c: 3, d: 4, e: -1 };
|
|
49
|
+
*
|
|
50
|
+
* var bool = someInBy( obj, 2, isNegative );
|
|
51
|
+
* // returns true
|
|
52
|
+
*/
|
|
53
|
+
function someInBy( obj, n, predicate, thisArg ) {
|
|
54
|
+
var count;
|
|
55
|
+
var out;
|
|
56
|
+
var key;
|
|
57
|
+
if ( !isObject( obj ) ) {
|
|
58
|
+
throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) );
|
|
59
|
+
}
|
|
60
|
+
if ( !isPositiveInteger( n ) ) {
|
|
61
|
+
throw new TypeError( format( 'invalid argument. Second argument must be a positive integer. Value: `%s`.', n ) );
|
|
62
|
+
}
|
|
63
|
+
if ( !isFunction( predicate ) ) {
|
|
64
|
+
throw new TypeError( format( 'invalid argument. Third argument must be a function. Value: `%s`.', predicate ) );
|
|
65
|
+
}
|
|
66
|
+
count = 0;
|
|
67
|
+
for ( key in obj ) {
|
|
68
|
+
if (
|
|
69
|
+
Object.prototype.hasOwnProperty.call( obj, key ) ||
|
|
70
|
+
Object.prototype.hasOwnProperty.call( Object.getPrototypeOf( obj ), key )
|
|
71
|
+
) {
|
|
72
|
+
out = predicate.call( thisArg, obj[ key ], key, obj );
|
|
73
|
+
if ( out ) {
|
|
74
|
+
count += 1;
|
|
75
|
+
if ( count === n ) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// EXPORTS //
|
|
86
|
+
|
|
87
|
+
module.exports = someInBy;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/object/some-in-by",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Test whether an object contains at least n properties (own and inherited) which pass a test implemented by a predicate function.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {},
|
|
26
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"devDependencies": {},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=0.10.0",
|
|
38
|
+
"npm": ">2.7.0"
|
|
39
|
+
},
|
|
40
|
+
"os": [
|
|
41
|
+
"aix",
|
|
42
|
+
"darwin",
|
|
43
|
+
"freebsd",
|
|
44
|
+
"linux",
|
|
45
|
+
"macos",
|
|
46
|
+
"openbsd",
|
|
47
|
+
"sunos",
|
|
48
|
+
"win32",
|
|
49
|
+
"windows"
|
|
50
|
+
],
|
|
51
|
+
"keywords": [
|
|
52
|
+
"stdlib",
|
|
53
|
+
"stdutils",
|
|
54
|
+
"stdutil",
|
|
55
|
+
"utilities",
|
|
56
|
+
"utility",
|
|
57
|
+
"utils",
|
|
58
|
+
"util",
|
|
59
|
+
"test",
|
|
60
|
+
"predicate",
|
|
61
|
+
"any",
|
|
62
|
+
"every",
|
|
63
|
+
"all",
|
|
64
|
+
"object.some",
|
|
65
|
+
"object.every",
|
|
66
|
+
"some",
|
|
67
|
+
"property",
|
|
68
|
+
"own",
|
|
69
|
+
"inherited",
|
|
70
|
+
"iterate",
|
|
71
|
+
"collection",
|
|
72
|
+
"array-like",
|
|
73
|
+
"validate"
|
|
74
|
+
]
|
|
75
|
+
}
|