@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,205 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2018 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
+ # Object Inverse
22
+
23
+ > Invert an object, such that keys become values and values become keys, according to a transform function.
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var invertBy = require( '@stdlib/object/inverse-by' );
31
+ ```
32
+
33
+ #### invertBy( obj, \[options,] transform )
34
+
35
+ Inverts an object, such that keys become values and values become keys, according to a transform function.
36
+
37
+ ```javascript
38
+ function transform( key, value ) {
39
+ return value;
40
+ }
41
+ var obj = {
42
+ 'a': 'beep',
43
+ 'b': 'boop'
44
+ };
45
+ var out = invertBy( obj, transform );
46
+ // returns { 'beep': 'a', 'boop': 'b' }
47
+ ```
48
+
49
+ The function accepts the following options:
50
+
51
+ - **duplicates**: boolean indicating whether to store keys mapped to duplicate values in arrays. Default: `true`.
52
+
53
+ By default, keys mapped to duplicate values are stored in arrays.
54
+
55
+ ```javascript
56
+ function transform( key, value ) {
57
+ return value;
58
+ }
59
+ var obj = {
60
+ 'a': 'beep',
61
+ 'b': 'beep'
62
+ };
63
+ var out = invertBy( obj, transform );
64
+ // returns { 'beep': [ 'a', 'b' ] }
65
+ ```
66
+
67
+ To **not** allow duplicates, set the `duplicates` option to `false`. The output key-value pair will be the key most recently inserted into the input object.
68
+
69
+ ```javascript
70
+ function transform( key, value ) {
71
+ return value;
72
+ }
73
+ var obj = {};
74
+ obj.a = 'beep';
75
+ obj.b = 'boop';
76
+ obj.c = 'beep'; // inserted after `a`
77
+
78
+ var opts = {
79
+ 'duplicates': false
80
+ };
81
+ var out = invertBy( obj, opts, transform );
82
+ // returns { 'beep': 'c', 'boop': 'b' }
83
+ ```
84
+
85
+ The transform function is provided three arguments:
86
+
87
+ - **key**: object key.
88
+ - **value**: object value corresponding to `key`.
89
+ - **obj**: input object.
90
+
91
+ ```javascript
92
+ function transform( key, value, o ) {
93
+ if ( key === 'name' ) {
94
+ return value;
95
+ }
96
+ return o.name + ':' + value;
97
+ }
98
+ var obj = {
99
+ 'name': 'foo',
100
+ 'a': 'beep',
101
+ 'b': 'boop'
102
+ };
103
+ var out = invertBy( obj, transform );
104
+ // returns { 'foo': 'name', 'foo:beep': 'a', 'foo:boop': 'b' }
105
+ ```
106
+
107
+ </section>
108
+
109
+ <!-- /.usage -->
110
+
111
+ <section class="notes">
112
+
113
+ ## Notes
114
+
115
+ - Beware when providing objects having values which are themselves objects. This function relies on native object serialization (`#toString`) when converting transform function return values to keys.
116
+
117
+ ```javascript
118
+ function transform( key, value ) {
119
+ return value;
120
+ }
121
+ var obj = {
122
+ 'a': [ 1, 2, 3 ],
123
+ 'b': {
124
+ 'c': 'd'
125
+ }
126
+ };
127
+
128
+ var out = invertBy( obj, transform );
129
+ // returns { '1,2,3': 'a', '[object Object]': 'b' }
130
+ ```
131
+
132
+ - In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the [ECMAScript specification][ecma-262-for-in] in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
133
+
134
+ </section>
135
+
136
+ <!-- /.notes -->
137
+
138
+ <section class="examples">
139
+
140
+ ## Examples
141
+
142
+ <!-- eslint no-undef: "error" -->
143
+
144
+ ```javascript
145
+ var objectKeys = require( '@stdlib/utils/keys' );
146
+ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
147
+ var invertBy = require( '@stdlib/object/inverse-by' );
148
+
149
+ function transform( key, value ) {
150
+ return value;
151
+ }
152
+
153
+ // Create an array of random integers:
154
+ var arr = discreteUniform( 1000, 0, 100, {
155
+ 'dtype': 'generic'
156
+ });
157
+
158
+ // Invert the array to determine value frequency...
159
+ var out = invertBy( arr, transform );
160
+ var keys = objectKeys( out );
161
+
162
+ var i;
163
+ for ( i = 0; i < keys.length; i++ ) {
164
+ if ( out[ i ] ) {
165
+ out[ i ] = out[ i ].length;
166
+ } else {
167
+ out[ i ] = 0;
168
+ }
169
+ }
170
+ console.dir( out );
171
+ ```
172
+
173
+ </section>
174
+
175
+ <!-- /.examples -->
176
+
177
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
178
+
179
+ <section class="related">
180
+
181
+ * * *
182
+
183
+ ## See Also
184
+
185
+ - <span class="package-name">[`@stdlib/object/inverse`][@stdlib/object/inverse]</span><span class="delimiter">: </span><span class="description">invert an object, such that keys become values and values become keys.</span>
186
+
187
+ </section>
188
+
189
+ <!-- /.related -->
190
+
191
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
192
+
193
+ <section class="links">
194
+
195
+ [ecma-262-for-in]: https://262.ecma-international.org/5.1/#sec-12.6.4
196
+
197
+ <!-- <related-links> -->
198
+
199
+ [@stdlib/object/inverse]: https://github.com/stdlib-js/object/tree/main/inverse
200
+
201
+ <!-- </related-links> -->
202
+
203
+ </section>
204
+
205
+ <!-- /.links -->
@@ -0,0 +1,67 @@
1
+
2
+ {{alias}}( obj, [options,] transform )
3
+ Inverts an object, such that keys become values and values become keys,
4
+ according to a transform function.
5
+
6
+ The transform function is provided three arguments:
7
+
8
+ - key: object key.
9
+ - value: object value corresponding to `key`.
10
+ - obj: the input object.
11
+
12
+ The value returned by a transform function should be a value which can be
13
+ serialized as an object key. Hence, beware when providing objects having
14
+ values which are themselves objects. The function relies on native object
15
+ serialization (`#toString`) when converting transform function return values
16
+ to keys.
17
+
18
+ In older JavaScript engines, insertion order is not guaranteed, as object
19
+ key enumeration was not specified according to the ECMAScript specification
20
+ in earlier editions. In practice, however, most older engines use insertion
21
+ order to sort an object's keys, thus allowing for deterministic inversion.
22
+
23
+ Parameters
24
+ ----------
25
+ obj: ObjectLike
26
+ Input object.
27
+
28
+ options: Object (optional)
29
+ Options.
30
+
31
+ options.duplicates: boolean (optional)
32
+ Boolean indicating whether to store keys mapped to duplicate values in
33
+ arrays. Default: `true`.
34
+
35
+ transform: Function
36
+ Transform function.
37
+
38
+ Returns
39
+ -------
40
+ out: Object
41
+ Inverted object.
42
+
43
+ Examples
44
+ --------
45
+ // Basic usage:
46
+ > function transform( key, value ) { return key + value; };
47
+ > var obj = { 'a': 'beep', 'b': 'boop' };
48
+ > var out = {{alias}}( obj, transform )
49
+ { 'abeep': 'a', 'bboop': 'b' }
50
+
51
+ // Duplicate values:
52
+ > function transform( key, value ) { return value; };
53
+ > obj = { 'a': 'beep', 'b': 'beep' };
54
+ > out = {{alias}}( obj, transform )
55
+ { 'beep': [ 'a', 'b' ] }
56
+
57
+ // Override duplicate values:
58
+ > obj = {};
59
+ > obj.a = 'beep';
60
+ > obj.b = 'boop';
61
+ > obj.c = 'beep';
62
+ > out = {{alias}}( obj, { 'duplicates': false }, transform )
63
+ { 'beep': 'c', 'boop': 'b' }
64
+
65
+ See Also
66
+ --------
67
+
@@ -0,0 +1,160 @@
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
+ * Boolean indicating whether to store duplicate keys.
27
+ */
28
+ duplicates?: boolean;
29
+ }
30
+
31
+ /**
32
+ * Returns a value for an object element that can be serialized as an object key.
33
+ *
34
+ * @returns key in inverted object
35
+ */
36
+ type Nullary = () => string;
37
+
38
+ /**
39
+ * Returns a value for an object element that can be serialized as an object key.
40
+ *
41
+ * @param key - object key
42
+ * @returns key in inverted object
43
+ */
44
+ type Unary = ( key: string ) => string;
45
+
46
+ /**
47
+ * Returns a value for an object element that can be serialized as an object key.
48
+ *
49
+ * @param key - object key
50
+ * @param value - object value corresponding to `key`
51
+ * @returns key in inverted object
52
+ */
53
+ type Binary = ( key: string, value: any ) => string;
54
+
55
+ /**
56
+ * Returns a value for an object element that can be serialized as an object key.
57
+ *
58
+ * @param key - object key
59
+ * @param value - object value corresponding to `key`
60
+ * @param obj - the input object
61
+ * @returns key in inverted object
62
+ */
63
+ type Ternary = ( key: string, value: any, obj: any ) => string;
64
+
65
+ /**
66
+ * Returns a value for an object element that can be serialized as an object key.
67
+ *
68
+ * @param key - object key
69
+ * @param value - object value corresponding to `key`
70
+ * @param obj - the input object
71
+ * @returns key in inverted object
72
+ */
73
+ type Transform = Nullary | Unary | Binary | Ternary;
74
+
75
+ /**
76
+ * Inverts an object, such that keys become values and values become keys, according to a transform function.
77
+ *
78
+ * ## Notes
79
+ *
80
+ * - The transform function is provided three arguments:
81
+ *
82
+ * - `key`: object key.
83
+ * - `value`: object value corresponding to `key`.
84
+ * - `obj`: the input object.
85
+ *
86
+ * - The value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (`#toString`) when converting transform function return values to keys.
87
+ *
88
+ * - In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the ECMAScript specification in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
89
+ *
90
+ * @param obj - input object
91
+ * @param transform - transform function
92
+ * @returns inverted object
93
+ *
94
+ * @example
95
+ * function transform( key, value ) {
96
+ * return key + value;
97
+ * }
98
+ * var obj = {
99
+ * 'a': 'beep',
100
+ * 'b': 'boop'
101
+ * };
102
+ * var out = invertBy( obj, transform );
103
+ * // returns { 'abeep': 'a', 'bboop': 'b' }
104
+ *
105
+ * @example
106
+ * function transform( key, value ) {
107
+ * return value;
108
+ * }
109
+ * var obj = {
110
+ * 'a': 'beep',
111
+ * 'b': 'beep'
112
+ * };
113
+ * var out = invertBy( obj, transform );
114
+ * // returns { 'beep': [ 'a', 'b' ] }
115
+ */
116
+ declare function invertBy( obj: any, transform: Transform ): any;
117
+
118
+ /**
119
+ * Inverts an object, such that keys become values and values become keys, according to a transform function.
120
+ *
121
+ * ## Notes
122
+ *
123
+ * - The transform function is provided three arguments:
124
+ *
125
+ * - `key`: object key.
126
+ * - `value`: object value corresponding to `key`.
127
+ * - `obj`: the input object.
128
+ *
129
+ * - The value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (`#toString`) when converting transform function return values to keys.
130
+ *
131
+ * - In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the ECMAScript specification in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
132
+ *
133
+ * @param obj - input object
134
+ * @param opts - function options
135
+ * @param opts.duplicates - boolean indicating whether to store duplicate keys (default: true)
136
+ * @param transform - transform function
137
+ * @returns inverted object
138
+ *
139
+ * @example
140
+ * function transform( key, value ) {
141
+ * return value;
142
+ * }
143
+ *
144
+ * var obj = {};
145
+ * obj.a = 'beep';
146
+ * obj.b = 'boop';
147
+ * obj.c = 'beep'; // inserted after `a`
148
+ *
149
+ * var opts = {
150
+ * 'duplicates': false
151
+ * };
152
+ * var out = invertBy( obj, opts, transform );
153
+ * // returns { 'beep': 'c', 'boop': 'b' }
154
+ */
155
+ declare function invertBy( obj: any, opts: Options, transform: Transform ): any;
156
+
157
+
158
+ // EXPORTS //
159
+
160
+ export = invertBy;
@@ -0,0 +1,81 @@
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 invertBy = require( './index' );
20
+
21
+ const transform = ( key: string, value: string ): string => key + value;
22
+
23
+
24
+ // TESTS //
25
+
26
+ // The function returns an object...
27
+ {
28
+ const obj = {
29
+ 'a': 'beep',
30
+ 'b': 'boop'
31
+ };
32
+ const opts = {
33
+ 'duplicates': false
34
+ };
35
+ invertBy( obj, ( key: string, value: string ) => key + value ); // $ExpectType any
36
+ invertBy( obj, opts, ( key: string, value: string ) => key + value ); // $ExpectType any
37
+ }
38
+
39
+ // The compiler throws an error if the function is provided a last argument which is not a function...
40
+ {
41
+ const obj = {
42
+ 'a': 'beep',
43
+ 'b': 'boop'
44
+ };
45
+ invertBy( obj, false ); // $ExpectError
46
+ invertBy( obj, true ); // $ExpectError
47
+ invertBy( obj, 32 ); // $ExpectError
48
+ invertBy( obj, 'abc' ); // $ExpectError
49
+ invertBy( obj, [] ); // $ExpectError
50
+ invertBy( obj, {} ); // $ExpectError
51
+ }
52
+
53
+ // The compiler throws an error if the function is provided an options argument which is not an object...
54
+ {
55
+ const obj = {
56
+ 'a': 'beep',
57
+ 'b': 'boop'
58
+ };
59
+ invertBy( obj, null, transform ); // $ExpectError
60
+ }
61
+
62
+ // The compiler throws an error if the function is provided a `duplicates` option which is not a boolean...
63
+ {
64
+ const obj = {
65
+ 'a': 'beep',
66
+ 'b': 'boop'
67
+ };
68
+ invertBy( obj, { 'duplicates': '5' }, transform ); // $ExpectError
69
+ invertBy( obj, { 'duplicates': 123 }, transform ); // $ExpectError
70
+ invertBy( obj, { 'duplicates': null }, transform ); // $ExpectError
71
+ invertBy( obj, { 'duplicates': [] }, transform ); // $ExpectError
72
+ invertBy( obj, { 'duplicates': {} }, transform ); // $ExpectError
73
+ invertBy( obj, { 'duplicates': ( x: number ): number => x }, transform ); // $ExpectError
74
+ }
75
+
76
+ // The compiler throws an error if the function is provided an invalid number of arguments...
77
+ {
78
+ invertBy(); // $ExpectError
79
+ invertBy( {} ); // $ExpectError
80
+ invertBy( {}, {}, transform, 16 ); // $ExpectError
81
+ }
@@ -0,0 +1,78 @@
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
+ * Invert an object, such that keys become values and values become keys, according to a transform function.
23
+ *
24
+ * @module @stdlib/object/inverse-by
25
+ *
26
+ * @example
27
+ * var invertBy = require( '@ohos-ports/stdlib-object/inverse-by' );
28
+ *
29
+ * function transform( key, value ) {
30
+ * return key + value;
31
+ * }
32
+ * var obj = {
33
+ * 'a': 'beep',
34
+ * 'b': 'boop'
35
+ * };
36
+ * var out = invertBy( obj, transform );
37
+ * // returns { 'abeep': 'a', 'bboop': 'b' }
38
+ *
39
+ * @example
40
+ * var invertBy = require( '@ohos-ports/stdlib-object/inverse-by' );
41
+ *
42
+ * function transform( key, value ) {
43
+ * return value;
44
+ * }
45
+ * var obj = {
46
+ * 'a': 'beep',
47
+ * 'b': 'beep'
48
+ * };
49
+ * var out = invertBy( obj, transform );
50
+ * // returns { 'beep': [ 'a', 'b' ] }
51
+ *
52
+ * @example
53
+ * var invertBy = require( '@ohos-ports/stdlib-object/inverse-by' );
54
+ *
55
+ * function transform( key, value ) {
56
+ * return value;
57
+ * }
58
+ *
59
+ * var obj = {};
60
+ * obj.a = 'beep';
61
+ * obj.b = 'boop';
62
+ * obj.c = 'beep'; // inserted after `a`
63
+ *
64
+ * var opts = {
65
+ * 'duplicates': false
66
+ * };
67
+ * var out = invertBy( obj, opts, transform );
68
+ * // returns { 'beep': 'c', 'boop': 'b' }
69
+ */
70
+
71
+ // MODULES //
72
+
73
+ var main = require( './main.js' );
74
+
75
+
76
+ // EXPORTS //
77
+
78
+ module.exports = main;