@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.
Files changed (207) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +262 -2
  4. package/SECURITY.md +5 -0
  5. package/any-in-by/README.md +217 -0
  6. package/any-in-by/docs/repl.txt +41 -0
  7. package/any-in-by/docs/types/index.d.ts +102 -0
  8. package/any-in-by/docs/types/test.ts +53 -0
  9. package/any-in-by/lib/index.js +50 -0
  10. package/any-in-by/lib/main.js +75 -0
  11. package/any-in-by/package.json +66 -0
  12. package/any-own-by/README.md +221 -0
  13. package/any-own-by/docs/repl.txt +43 -0
  14. package/any-own-by/docs/types/index.d.ts +101 -0
  15. package/any-own-by/docs/types/test.ts +63 -0
  16. package/any-own-by/lib/index.js +46 -0
  17. package/any-own-by/lib/main.js +77 -0
  18. package/any-own-by/package.json +70 -0
  19. package/assign/README.md +139 -0
  20. package/assign/docs/repl.txt +34 -0
  21. package/assign/docs/types/index.d.ts +113 -0
  22. package/assign/docs/types/test.ts +47 -0
  23. package/assign/lib/builtin.js +55 -0
  24. package/assign/lib/has_object_assign.js +33 -0
  25. package/assign/lib/index.js +52 -0
  26. package/assign/lib/polyfill.js +86 -0
  27. package/assign/package.json +64 -0
  28. package/assign-in/README.md +145 -0
  29. package/assign-in/docs/repl.txt +34 -0
  30. package/assign-in/docs/types/index.d.ts +175 -0
  31. package/assign-in/docs/types/test.ts +44 -0
  32. package/assign-in/lib/index.js +54 -0
  33. package/assign-in/lib/main.js +92 -0
  34. package/assign-in/package.json +60 -0
  35. package/bifurcate-in/README.md +298 -0
  36. package/bifurcate-in/docs/repl.txt +68 -0
  37. package/bifurcate-in/docs/types/index.d.ts +189 -0
  38. package/bifurcate-in/docs/types/test.ts +97 -0
  39. package/bifurcate-in/lib/index.js +106 -0
  40. package/bifurcate-in/lib/main.js +148 -0
  41. package/bifurcate-in/lib/return_keys.js +81 -0
  42. package/bifurcate-in/lib/return_pairs.js +87 -0
  43. package/bifurcate-in/lib/return_values.js +87 -0
  44. package/bifurcate-in/lib/validate.js +76 -0
  45. package/bifurcate-in/package.json +72 -0
  46. package/bifurcate-own/README.md +255 -0
  47. package/bifurcate-own/docs/repl.txt +63 -0
  48. package/bifurcate-own/docs/types/index.d.ts +183 -0
  49. package/bifurcate-own/docs/types/test.ts +97 -0
  50. package/bifurcate-own/lib/index.js +87 -0
  51. package/bifurcate-own/lib/main.js +127 -0
  52. package/bifurcate-own/lib/return_keys.js +81 -0
  53. package/bifurcate-own/lib/return_pairs.js +87 -0
  54. package/bifurcate-own/lib/return_values.js +87 -0
  55. package/bifurcate-own/lib/validate.js +76 -0
  56. package/bifurcate-own/package.json +71 -0
  57. package/capitalize-keys/README.md +139 -0
  58. package/capitalize-keys/docs/repl.txt +28 -0
  59. package/capitalize-keys/docs/types/index.d.ts +46 -0
  60. package/capitalize-keys/docs/types/test.ts +33 -0
  61. package/capitalize-keys/lib/index.js +45 -0
  62. package/capitalize-keys/lib/main.js +69 -0
  63. package/capitalize-keys/package.json +76 -0
  64. package/common-keys/README.md +125 -0
  65. package/common-keys/docs/repl.txt +29 -0
  66. package/common-keys/docs/types/index.d.ts +70 -0
  67. package/common-keys/docs/types/test.ts +33 -0
  68. package/common-keys/lib/index.js +51 -0
  69. package/common-keys/lib/main.js +106 -0
  70. package/common-keys/package.json +70 -0
  71. package/common-keys-in/README.md +125 -0
  72. package/common-keys-in/docs/repl.txt +29 -0
  73. package/common-keys-in/docs/types/index.d.ts +70 -0
  74. package/common-keys-in/docs/types/test.ts +33 -0
  75. package/common-keys-in/lib/index.js +51 -0
  76. package/common-keys-in/lib/main.js +106 -0
  77. package/common-keys-in/package.json +72 -0
  78. package/ctor/README.md +536 -0
  79. package/ctor/docs/repl.txt +592 -0
  80. package/ctor/docs/types/index.d.ts +56 -0
  81. package/ctor/docs/types/test.ts +33 -0
  82. package/ctor/lib/index.js +54 -0
  83. package/ctor/lib/main.js +58 -0
  84. package/ctor/package.json +58 -0
  85. package/deep-get/README.md +181 -0
  86. package/deep-get/docs/repl.txt +63 -0
  87. package/deep-get/docs/types/index.d.ts +125 -0
  88. package/deep-get/docs/types/test.ts +109 -0
  89. package/deep-get/lib/defaults.js +38 -0
  90. package/deep-get/lib/dget.js +57 -0
  91. package/deep-get/lib/factory.js +94 -0
  92. package/deep-get/lib/index.js +58 -0
  93. package/deep-get/lib/main.js +101 -0
  94. package/deep-get/lib/validate.js +66 -0
  95. package/deep-get/package.json +68 -0
  96. package/deep-set/README.md +273 -0
  97. package/deep-set/docs/repl.txt +87 -0
  98. package/deep-set/docs/types/index.d.ts +180 -0
  99. package/deep-set/docs/types/test.ts +134 -0
  100. package/deep-set/lib/defaults.js +39 -0
  101. package/deep-set/lib/dset.js +80 -0
  102. package/deep-set/lib/factory.js +99 -0
  103. package/deep-set/lib/index.js +58 -0
  104. package/deep-set/lib/main.js +120 -0
  105. package/deep-set/lib/validate.js +74 -0
  106. package/deep-set/package.json +69 -0
  107. package/docs/types/index.d.ts +964 -0
  108. package/every-in-by/README.md +156 -0
  109. package/every-in-by/docs/repl.txt +43 -0
  110. package/every-in-by/docs/types/index.d.ts +83 -0
  111. package/every-in-by/docs/types/test.ts +60 -0
  112. package/every-in-by/lib/index.js +51 -0
  113. package/every-in-by/lib/main.js +70 -0
  114. package/every-in-by/package.json +67 -0
  115. package/every-own-by/README.md +223 -0
  116. package/every-own-by/docs/repl.txt +43 -0
  117. package/every-own-by/docs/types/index.d.ts +102 -0
  118. package/every-own-by/docs/types/test.ts +62 -0
  119. package/every-own-by/lib/index.js +46 -0
  120. package/every-own-by/lib/main.js +77 -0
  121. package/every-own-by/package.json +68 -0
  122. package/for-in/README.md +218 -0
  123. package/for-in/docs/repl.txt +44 -0
  124. package/for-in/docs/types/index.d.ts +65 -0
  125. package/for-in/docs/types/test.ts +50 -0
  126. package/for-in/lib/index.js +54 -0
  127. package/for-in/lib/main.js +82 -0
  128. package/for-in/package.json +74 -0
  129. package/for-own/README.md +209 -0
  130. package/for-own/docs/repl.txt +49 -0
  131. package/for-own/docs/types/index.d.ts +63 -0
  132. package/for-own/docs/types/test.ts +50 -0
  133. package/for-own/lib/index.js +50 -0
  134. package/for-own/lib/main.js +85 -0
  135. package/for-own/package.json +71 -0
  136. package/inverse/README.md +163 -0
  137. package/inverse/docs/repl.txt +53 -0
  138. package/inverse/docs/types/index.d.ts +66 -0
  139. package/inverse/docs/types/test.ts +60 -0
  140. package/inverse/lib/index.js +59 -0
  141. package/inverse/lib/main.js +123 -0
  142. package/inverse/package.json +69 -0
  143. package/inverse-by/README.md +205 -0
  144. package/inverse-by/docs/repl.txt +67 -0
  145. package/inverse-by/docs/types/index.d.ts +160 -0
  146. package/inverse-by/docs/types/test.ts +81 -0
  147. package/inverse-by/lib/index.js +78 -0
  148. package/inverse-by/lib/main.js +148 -0
  149. package/inverse-by/package.json +75 -0
  150. package/lib/index.js +276 -0
  151. package/lowercase-keys/README.md +139 -0
  152. package/lowercase-keys/docs/repl.txt +28 -0
  153. package/lowercase-keys/docs/types/index.d.ts +46 -0
  154. package/lowercase-keys/docs/types/test.ts +33 -0
  155. package/lowercase-keys/lib/index.js +45 -0
  156. package/lowercase-keys/lib/main.js +63 -0
  157. package/lowercase-keys/package.json +74 -0
  158. package/move-property/README.md +144 -0
  159. package/move-property/docs/repl.txt +38 -0
  160. package/move-property/docs/types/index.d.ts +53 -0
  161. package/move-property/docs/types/test.ts +46 -0
  162. package/move-property/lib/index.js +46 -0
  163. package/move-property/lib/main.js +74 -0
  164. package/move-property/package.json +69 -0
  165. package/none-in-by/README.md +241 -0
  166. package/none-in-by/docs/repl.txt +42 -0
  167. package/none-in-by/docs/types/index.d.ts +101 -0
  168. package/none-in-by/docs/types/test.ts +61 -0
  169. package/none-in-by/lib/index.js +46 -0
  170. package/none-in-by/lib/main.js +71 -0
  171. package/none-in-by/package.json +68 -0
  172. package/none-own-by/README.md +177 -0
  173. package/none-own-by/docs/repl.txt +42 -0
  174. package/none-own-by/docs/types/index.d.ts +102 -0
  175. package/none-own-by/docs/types/test.ts +61 -0
  176. package/none-own-by/lib/index.js +50 -0
  177. package/none-own-by/lib/main.js +75 -0
  178. package/none-own-by/package.json +68 -0
  179. package/package.json +66 -4
  180. package/some-in-by/README.md +232 -0
  181. package/some-in-by/docs/repl.txt +45 -0
  182. package/some-in-by/docs/types/index.d.ts +104 -0
  183. package/some-in-by/docs/types/test.ts +66 -0
  184. package/some-in-by/lib/index.js +46 -0
  185. package/some-in-by/lib/main.js +87 -0
  186. package/some-in-by/package.json +75 -0
  187. package/some-own-by/README.md +251 -0
  188. package/some-own-by/docs/repl.txt +45 -0
  189. package/some-own-by/docs/types/index.d.ts +103 -0
  190. package/some-own-by/docs/types/test.ts +66 -0
  191. package/some-own-by/lib/index.js +46 -0
  192. package/some-own-by/lib/main.js +85 -0
  193. package/some-own-by/package.json +72 -0
  194. package/uncapitalize-keys/README.md +139 -0
  195. package/uncapitalize-keys/docs/repl.txt +28 -0
  196. package/uncapitalize-keys/docs/types/index.d.ts +46 -0
  197. package/uncapitalize-keys/docs/types/test.ts +33 -0
  198. package/uncapitalize-keys/lib/index.js +45 -0
  199. package/uncapitalize-keys/lib/main.js +69 -0
  200. package/uncapitalize-keys/package.json +76 -0
  201. package/uppercase-keys/README.md +139 -0
  202. package/uppercase-keys/docs/repl.txt +28 -0
  203. package/uppercase-keys/docs/types/index.d.ts +46 -0
  204. package/uppercase-keys/docs/types/test.ts +33 -0
  205. package/uppercase-keys/lib/index.js +45 -0
  206. package/uppercase-keys/lib/main.js +63 -0
  207. package/uppercase-keys/package.json +74 -0
@@ -0,0 +1,183 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 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
+ /**
22
+ * Interface defining function options.
23
+ */
24
+ interface Options {
25
+ /**
26
+ * Execution context.
27
+ */
28
+ thisArg?: any;
29
+
30
+ /**
31
+ * If `'values'`, values are returned; if `'keys'`, keys are returned; if `'*'`, both keys and values are returned.
32
+ */
33
+ returns?: 'values' | 'keys' | '*';
34
+ }
35
+
36
+ /**
37
+ * Returns a boolean indicating which group an object's own property values belongs to.
38
+ *
39
+ * @returns boolean indicating whether a property value should be placed in the first or second group
40
+ */
41
+ type Nullary = () => boolean;
42
+
43
+ /**
44
+ * Returns a boolean indicating which group an object's own property values belongs to.
45
+ *
46
+ * @param value - object value
47
+ * @returns boolean indicating whether a property value should be placed in the first or second group
48
+ */
49
+ type Unary = ( value: any ) => boolean;
50
+
51
+ /**
52
+ * Returns a boolean indicating which group an object's own property values belongs to.
53
+ *
54
+ * @param value - object value
55
+ * @param key - object key
56
+ * @returns boolean indicating whether a property value should be placed in the first or second group
57
+ */
58
+ type Binary = ( value: any, key: string | symbol ) => boolean;
59
+
60
+ /**
61
+ * Returns a boolean indicating which group an object's own property values belongs to.
62
+ *
63
+ * @param value - object value
64
+ * @param key - object key
65
+ * @returns boolean indicating whether a property value should be placed in the first or second group
66
+ */
67
+ type Predicate = Nullary | Unary | Binary;
68
+
69
+ /**
70
+ * Splits an object's own property values into two groups according to a predicate function.
71
+ *
72
+ * @param obj - input object
73
+ * @param predicate - predicate function indicating which group an element in the input object belongs to
74
+ * @returns group results
75
+ *
76
+ * @example
77
+ * function predicate( v ) {
78
+ * return v[ 0 ] === 'b';
79
+ * }
80
+ * var obj = {
81
+ * 'a': 'beep',
82
+ * 'b': 'boop',
83
+ * 'c': 'foo',
84
+ * 'd': 'bar'
85
+ * };
86
+ * var out = bifurcateOwn( obj, predicate );
87
+ * // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
88
+ *
89
+ * @example
90
+ * function predicate( v ) {
91
+ * return v[ 0 ] === 'b';
92
+ * }
93
+ * var obj = {
94
+ * 'a': 'beep',
95
+ * 'b': 'boop',
96
+ * 'c': 'foo',
97
+ * 'd': 'bar'
98
+ * };
99
+ * var opts = {
100
+ * 'returns': 'keys'
101
+ * };
102
+ * var out = bifurcateOwn( obj, opts, predicate );
103
+ * // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
104
+ *
105
+ * @example
106
+ * function predicate( v ) {
107
+ * return v[ 0 ] === 'b';
108
+ * }
109
+ * var obj = {
110
+ * 'a': 'beep',
111
+ * 'b': 'boop',
112
+ * 'c': 'foo',
113
+ * 'd': 'bar'
114
+ * };
115
+ * var opts = {
116
+ * 'returns': '*'
117
+ * };
118
+ * var out = bifurcateOwn( obj, opts, predicate );
119
+ * // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
120
+ */
121
+ declare function bifurcateOwn( obj: any, predicate: Predicate ): Array<Array<any>>;
122
+
123
+ /**
124
+ * Splits an object's own property values into two groups according to a predicate function.
125
+ *
126
+ * @param obj - input object
127
+ * @param options - function options
128
+ * @param options.thisArg - execution context
129
+ * @param options.returns - if `'values'`, values are returned; if `'keys'`, keys are returned; if `'*'`, both keys and values are returned
130
+ * @param predicate - predicate function indicating which group an element in the input object belongs to
131
+ * @returns group results
132
+ *
133
+ * @example
134
+ * function predicate( v ) {
135
+ * return v[ 0 ] === 'b';
136
+ * }
137
+ * var obj = {
138
+ * 'a': 'beep',
139
+ * 'b': 'boop',
140
+ * 'c': 'foo',
141
+ * 'd': 'bar'
142
+ * };
143
+ * var out = bifurcateOwn( obj, predicate );
144
+ * // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
145
+ *
146
+ * @example
147
+ * function predicate( v ) {
148
+ * return v[ 0 ] === 'b';
149
+ * }
150
+ * var obj = {
151
+ * 'a': 'beep',
152
+ * 'b': 'boop',
153
+ * 'c': 'foo',
154
+ * 'd': 'bar'
155
+ * };
156
+ * var opts = {
157
+ * 'returns': 'keys'
158
+ * };
159
+ * var out = bifurcateOwn( obj, opts, predicate );
160
+ * // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
161
+ *
162
+ * @example
163
+ * function predicate( v ) {
164
+ * return v[ 0 ] === 'b';
165
+ * }
166
+ * var obj = {
167
+ * 'a': 'beep',
168
+ * 'b': 'boop',
169
+ * 'c': 'foo',
170
+ * 'd': 'bar'
171
+ * };
172
+ * var opts = {
173
+ * 'returns': '*'
174
+ * };
175
+ * var out = bifurcateOwn( obj, opts, predicate );
176
+ * // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
177
+ */
178
+ declare function bifurcateOwn( obj: any, options: Options, predicate: Predicate ): Array<Array<any>>;
179
+
180
+
181
+ // EXPORTS //
182
+
183
+ export = bifurcateOwn;
@@ -0,0 +1,97 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 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 bifurcateOwn = require( './index' );
20
+
21
+ const predicate = ( v: Array<any> ): boolean => v[ 0 ] === 'b';
22
+
23
+ /**
24
+ * A class for instantiating objects with `a` and `b` dummy properties.
25
+ */
26
+ class Foo {
27
+ /**
28
+ * String property.
29
+ */
30
+ a: string;
31
+
32
+ /**
33
+ * String property.
34
+ */
35
+ b: string;
36
+
37
+ constructor() {
38
+ this.a = 'beep';
39
+ this.b = 'boop';
40
+ }
41
+ }
42
+
43
+
44
+ // TESTS //
45
+
46
+ // The function return an array of arrays...
47
+ {
48
+ const obj = new Foo();
49
+ bifurcateOwn( obj, predicate ); // $ExpectType any[][]
50
+ bifurcateOwn( {}, predicate ); // $ExpectType any[][]
51
+ const opts = {
52
+ 'returns': 'keys' as 'keys'
53
+ };
54
+ bifurcateOwn( obj, opts, predicate ); // $ExpectType any[][]
55
+ }
56
+
57
+ // The compiler throws an error if the function is provided a last argument which is not a function...
58
+ {
59
+ const obj = new Foo();
60
+ bifurcateOwn( obj, false ); // $ExpectError
61
+ bifurcateOwn( obj, true ); // $ExpectError
62
+ bifurcateOwn( obj, 32 ); // $ExpectError
63
+ bifurcateOwn( obj, 'abc' ); // $ExpectError
64
+ bifurcateOwn( obj, [] ); // $ExpectError
65
+ bifurcateOwn( obj, {} ); // $ExpectError
66
+
67
+ bifurcateOwn( obj, {}, false ); // $ExpectError
68
+ bifurcateOwn( obj, {}, true ); // $ExpectError
69
+ bifurcateOwn( obj, {}, 32 ); // $ExpectError
70
+ bifurcateOwn( obj, {}, 'abc' ); // $ExpectError
71
+ bifurcateOwn( obj, {}, [] ); // $ExpectError
72
+ bifurcateOwn( obj, {}, {} ); // $ExpectError
73
+ }
74
+
75
+ // The compiler throws an error if the function is provided an options argument which is not an object...
76
+ {
77
+ const obj = new Foo();
78
+ bifurcateOwn( obj, null, predicate ); // $ExpectError
79
+ }
80
+
81
+ // The compiler throws an error if the function is provided a `returns` option which is not one of 'keys', 'values', or '*'...
82
+ {
83
+ const obj = new Foo();
84
+ bifurcateOwn( obj, { 'returns': '5' }, predicate ); // $ExpectError
85
+ bifurcateOwn( obj, { 'returns': 123 }, predicate ); // $ExpectError
86
+ bifurcateOwn( obj, { 'returns': [] }, predicate ); // $ExpectError
87
+ bifurcateOwn( obj, { 'returns': {} }, predicate ); // $ExpectError
88
+ bifurcateOwn( obj, { 'returns': ( x: number ): number => x }, predicate ); // $ExpectError
89
+ }
90
+
91
+ // The compiler throws an error if the function is provided an invalid number of arguments...
92
+ {
93
+ const obj = new Foo();
94
+ bifurcateOwn(); // $ExpectError
95
+ bifurcateOwn( obj ); // $ExpectError
96
+ bifurcateOwn( obj, {}, predicate, 16 ); // $ExpectError
97
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ * Split an object's own property values into two groups according to a predicate function.
23
+ *
24
+ * @module @stdlib/object/bifurcate-own
25
+ *
26
+ * @example
27
+ * var bifurcateOwn = require( '@ohos-ports/stdlib-object/bifurcate-own' );
28
+ *
29
+ * function predicate( v ) {
30
+ * return v[ 0 ] === 'b';
31
+ * }
32
+ * var obj = {
33
+ * 'a': 'beep',
34
+ * 'b': 'boop',
35
+ * 'c': 'foo',
36
+ * 'd': 'bar'
37
+ * };
38
+ *
39
+ * var out = bifurcateOwn( obj, predicate );
40
+ * // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
41
+ *
42
+ * @example
43
+ * var bifurcateOwn = require( '@ohos-ports/stdlib-object/bifurcate-own' );
44
+ *
45
+ * function predicate( v ) {
46
+ * return v[ 0 ] === 'b';
47
+ * }
48
+ * var obj = {
49
+ * 'a': 'beep',
50
+ * 'b': 'boop',
51
+ * 'c': 'foo',
52
+ * 'd': 'bar'
53
+ * };
54
+ *
55
+ * var opts = {
56
+ * 'returns': 'keys'
57
+ * };
58
+ * var out = bifurcateOwn( obj, opts, predicate );
59
+ * // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
60
+ *
61
+ * @example
62
+ * var bifurcateOwn = require( '@ohos-ports/stdlib-object/bifurcate-own' );
63
+ *
64
+ * function predicate( v ) {
65
+ * return v[ 0 ] === 'b';
66
+ * }
67
+ * var obj = {
68
+ * 'a': 'beep',
69
+ * 'b': 'boop',
70
+ * 'c': 'foo',
71
+ * 'd': 'bar'
72
+ * };
73
+ * var opts = {
74
+ * 'returns': '*'
75
+ * };
76
+ * var out = bifurcateOwn( obj, opts, predicate );
77
+ * // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
78
+ */
79
+
80
+ // MODULES //
81
+
82
+ var main = require( './main.js' );
83
+
84
+
85
+ // EXPORTS //
86
+
87
+ module.exports = main;
@@ -0,0 +1,127 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 isObjectLike = require( '@ohos-ports/stdlib-assert/is-object-like' );
24
+ var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
25
+ var format = require( '@ohos-ports/stdlib-string/format' );
26
+ var validate = require( './validate.js' );
27
+ var returnValues = require( './return_values.js' );
28
+ var returnKeys = require( './return_keys.js' );
29
+ var returnPairs = require( './return_pairs.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Splits an object's own property values into two groups according to a predicate function.
36
+ *
37
+ * @param {(Object|Array|TypedArray)} obj - input object
38
+ * @param {Options} [options] - function options
39
+ * @param {*} [options.thisArg] - execution context
40
+ * @param {string} [options.returns="values"] - if `values`, values are returned; if `keys`, keys are returned; if `*`, both keys and values are returned
41
+ * @param {Function} predicate - predicate function indicating which group an element in the input object belongs to
42
+ * @throws {TypeError} first argument must be an object, array, or typed array
43
+ * @throws {TypeError} options argument must be an object
44
+ * @throws {TypeError} last argument must be a function
45
+ * @throws {TypeError} must provide valid options
46
+ * @returns {(Array<Array>|Array)} group results
47
+ *
48
+ * @example
49
+ * function predicate( v ) {
50
+ * return v[ 0 ] === 'b';
51
+ * }
52
+ * var obj = {
53
+ * 'a': 'beep',
54
+ * 'b': 'boop',
55
+ * 'c': 'foo',
56
+ * 'd': 'bar'
57
+ * };
58
+ * var out = bifurcateOwn( obj, predicate );
59
+ * // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ]
60
+ *
61
+ * @example
62
+ * function predicate( v ) {
63
+ * return v[ 0 ] === 'b';
64
+ * }
65
+ * var obj = {
66
+ * 'a': 'beep',
67
+ * 'b': 'boop',
68
+ * 'c': 'foo',
69
+ * 'd': 'bar'
70
+ * };
71
+ * var opts = {
72
+ * 'returns': 'keys'
73
+ * };
74
+ * var out = bifurcateOwn( obj, opts, predicate );
75
+ * // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
76
+ *
77
+ * @example
78
+ * function predicate( v ) {
79
+ * return v[ 0 ] === 'b';
80
+ * }
81
+ * var obj = {
82
+ * 'a': 'beep',
83
+ * 'b': 'boop',
84
+ * 'c': 'foo',
85
+ * 'd': 'bar'
86
+ * };
87
+ * var opts = {
88
+ * 'returns': '*'
89
+ * };
90
+ * var out = bifurcateOwn( obj, opts, predicate );
91
+ * // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
92
+ */
93
+ function bifurcateOwn( obj, options, predicate ) {
94
+ var opts;
95
+ var err;
96
+ var cb;
97
+ if ( !isObjectLike( obj ) ) {
98
+ throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) );
99
+ }
100
+ opts = {
101
+ 'returns': 'values'
102
+ };
103
+ if ( arguments.length === 2 ) {
104
+ cb = options;
105
+ } else {
106
+ err = validate( opts, options );
107
+ if ( err ) {
108
+ throw err;
109
+ }
110
+ cb = predicate;
111
+ }
112
+ if ( !isFunction( cb ) ) {
113
+ throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', cb ) );
114
+ }
115
+ if ( opts.returns === 'values' ) {
116
+ return returnValues( obj, opts, cb );
117
+ }
118
+ if ( opts.returns === 'keys' ) {
119
+ return returnKeys( obj, opts, cb );
120
+ }
121
+ return returnPairs( obj, opts, cb );
122
+ }
123
+
124
+
125
+ // EXPORTS //
126
+
127
+ module.exports = bifurcateOwn;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 hasOwnProp = require( '@ohos-ports/stdlib-assert/has-own-property' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Splits values into two groups according to a predicate function and outputs results as keys.
30
+ *
31
+ * @private
32
+ * @param {(Object|Array|TypedArray)} obj - input object
33
+ * @param {Options} opts - function options
34
+ * @param {*} [opts.thisArg] - execution context
35
+ * @param {Function} predicate - predicate function specifying which group an element in the input object belongs to
36
+ * @returns {(Array<Array>|Array)} results
37
+ *
38
+ * @example
39
+ * function predicate( v ) {
40
+ * return v[ 0 ] === 'b';
41
+ * }
42
+ * var obj = {
43
+ * 'a': 'beep',
44
+ * 'b': 'boop',
45
+ * 'c': 'foo',
46
+ * 'd': 'bar'
47
+ * };
48
+ * var out = bifurcateOwn( obj, {}, predicate );
49
+ * // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ]
50
+ */
51
+ function bifurcateOwn( obj, opts, predicate ) {
52
+ var thisArg;
53
+ var bool;
54
+ var out;
55
+ var key;
56
+ var flg;
57
+
58
+ thisArg = opts.thisArg;
59
+ out = [ [], [] ];
60
+ flg = true;
61
+ for ( key in obj ) {
62
+ if ( hasOwnProp( obj, key ) ) {
63
+ flg = false;
64
+ bool = predicate.call( thisArg, obj[ key ], key );
65
+ if ( bool ) {
66
+ out[ 0 ].push( key );
67
+ } else {
68
+ out[ 1 ].push( key );
69
+ }
70
+ }
71
+ }
72
+ if ( flg ) {
73
+ out.length = 0;
74
+ }
75
+ return out;
76
+ }
77
+
78
+
79
+ // EXPORTS //
80
+
81
+ module.exports = bifurcateOwn;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 hasOwnProp = require( '@ohos-ports/stdlib-assert/has-own-property' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Splits values into two groups according to a predicate function and outputs results as key-value values.
30
+ *
31
+ * ## Notes
32
+ *
33
+ * - We need to cache the object value to prevent the edge case where, during the invocation of the predicate function, the value at key `k` is swapped for some other value. For some, that might be a feature; here, we take the stance that one should be less clever.
34
+ *
35
+ * @private
36
+ * @param {(Object|Array|TypedArray)} obj - input object
37
+ * @param {Options} opts - function options
38
+ * @param {*} [opts.thisArg] - execution context
39
+ * @param {Function} predicate - predicate function indicating which group an element in the input object belongs to
40
+ * @returns {(Array<Array>|Array)} results
41
+ *
42
+ * @example
43
+ * function predicate( v ) {
44
+ * return v[ 0 ] === 'b';
45
+ * }
46
+ * var obj = {
47
+ * 'a': 'beep',
48
+ * 'b': 'boop',
49
+ * 'c': 'foo',
50
+ * 'd': 'bar'
51
+ * };
52
+ * var out = bifurcateOwn( obj, {}, predicate );
53
+ * // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ]
54
+ */
55
+ function bifurcateOwn( obj, opts, predicate ) {
56
+ var thisArg;
57
+ var bool;
58
+ var out;
59
+ var key;
60
+ var flg;
61
+ var v;
62
+
63
+ thisArg = opts.thisArg;
64
+ out = [ [], [] ];
65
+ flg = true;
66
+ for ( key in obj ) {
67
+ if ( hasOwnProp( obj, key ) ) {
68
+ flg = false;
69
+ v = obj[ key ];
70
+ bool = predicate.call( thisArg, v, key );
71
+ if ( bool ) {
72
+ out[ 0 ].push( [ key, v ] );
73
+ } else {
74
+ out[ 1 ].push( [ key, v ] );
75
+ }
76
+ }
77
+ }
78
+ if ( flg ) {
79
+ out.length = 0;
80
+ }
81
+ return out;
82
+ }
83
+
84
+
85
+ // EXPORTS //
86
+
87
+ module.exports = bifurcateOwn;