@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,87 @@
1
+
2
+ {{alias}}( obj, path, value[, options] )
3
+ Sets a nested property value.
4
+
5
+ Parameters
6
+ ----------
7
+ obj: ObjectLike
8
+ Input object.
9
+
10
+ path: string|Array
11
+ Key path.
12
+
13
+ value: any
14
+ Value to set.
15
+
16
+ options: Object (optional)
17
+ Options.
18
+
19
+ options.create: boolean (optional)
20
+ Boolean indicating whether to create a path if the key path does not
21
+ already exist. Default: false.
22
+
23
+ options.sep: string (optional)
24
+ Key path separator. Default: '.'.
25
+
26
+ Returns
27
+ -------
28
+ bool: boolean
29
+ Boolean indicating if the property was successfully set.
30
+
31
+ Examples
32
+ --------
33
+ > var obj1 = { 'a': { 'b': { 'c': 'd' } } };
34
+ > var bool = {{alias}}( obj1, 'a.b.c', 'beep' )
35
+ true
36
+
37
+ // Specify an alternative separator via the sep option:
38
+ > var obj2 = { 'a': { 'b': { 'c': 'd' } } };
39
+ > bool = {{alias}}( obj2, 'a/b/c', 'beep', { 'sep': '/' } );
40
+ > obj2
41
+ { 'a': { 'b': { 'c': 'beep' } } }
42
+
43
+ // To create a key path which does not exist, set the create option to true:
44
+ > var obj3 = { 'a': { 'b': { 'c': 'd' } } };
45
+ > bool = {{alias}}( obj3, 'a.e.c', 'boop', { 'create': true } );
46
+ > obj3
47
+ { 'a': { 'b': { 'c': 'd' }, 'e': { 'c': 'boop' } } }
48
+
49
+
50
+ {{alias}}.factory( path[, options] )
51
+ Creates a reusable deep set function.
52
+
53
+ Parameters
54
+ ----------
55
+ path: string|Array
56
+ Key path.
57
+
58
+ options: Object (optional)
59
+ Options.
60
+
61
+ options.create: boolean (optional)
62
+ Boolean indicating whether to create a path if the key path does not
63
+ already exist. Default: false.
64
+
65
+ options.sep: string (optional)
66
+ Key path separator. Default: '.'.
67
+
68
+ Returns
69
+ -------
70
+ out: Function
71
+ Deep set function.
72
+
73
+ Examples
74
+ --------
75
+ > var dset = {{alias}}.factory( 'a/b/c', {
76
+ ... 'create': true,
77
+ ... 'sep': '/'
78
+ ... });
79
+ > var obj = { 'a': { 'b': { 'c': 'd' } } };
80
+ > var bool = dset( obj, 'beep' )
81
+ true
82
+ > obj
83
+ { 'a': { 'b': { 'c': 'beep' } } }
84
+
85
+ See Also
86
+ --------
87
+
@@ -0,0 +1,180 @@
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 create a path if the key path does not already exist.
27
+ */
28
+ create?: boolean;
29
+
30
+ /**
31
+ * Key path separator.
32
+ */
33
+ sep?: string;
34
+ }
35
+
36
+ /**
37
+ * Sets a nested property.
38
+ *
39
+ * @param obj - input object
40
+ * @param value - value to set
41
+ * @returns boolean indicating if the property was successfully set
42
+ */
43
+ type Unary = ( obj: any, value: any ) => boolean;
44
+
45
+ /**
46
+ * Interface for setting nested property values.
47
+ */
48
+ interface DeepSet {
49
+ /**
50
+ * Sets a nested property value.
51
+ *
52
+ * @param obj - input object
53
+ * @param path - key path
54
+ * @param value - value to set
55
+ * @param options - function options
56
+ * @param options.create - boolean indicating whether to create a path if the key path does not already exist (default: false)
57
+ * @param options.sep - key path separator (default: '.')
58
+ * @returns boolean indicating if the property was successfully set
59
+ *
60
+ * @example
61
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
62
+ * var bool = deepSet( obj, 'a.b.c', 'woot' );
63
+ * // returns true
64
+ *
65
+ * @example
66
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
67
+ * var bool = deepSet( obj, 'a.beep.c', 'boop' );
68
+ * // returns false
69
+ *
70
+ * @example
71
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
72
+ * var bool = deepSet( null, 'a.beep.c', 'boop' );
73
+ * // returns false
74
+ *
75
+ * @example
76
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
77
+ * bool = deepSet( 'bap', 'a.beep.c', 'boop' );
78
+ * // returns false
79
+ *
80
+ * @example
81
+ * var arr = [
82
+ * { 'a': [ {'x': 5} ] },
83
+ * { 'a': [ {'x': 10} ] }
84
+ * ];
85
+ * var bool = deepSet( arr, '1.a.0.x', 25 );
86
+ * // returns true
87
+ *
88
+ * @example
89
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
90
+ * var bool = deepSet( obj, 'a/b/c', 'beep', {
91
+ * 'sep': '/'
92
+ * });
93
+ * // returns true
94
+ *
95
+ * @example
96
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
97
+ * var bool = deepSet( obj, 'a.e.c', 'boop', {
98
+ * 'create': true
99
+ * });
100
+ * // returns true
101
+ */
102
+ ( obj: any, path: string | Array<string>, value: any, options?: Options ): boolean;
103
+
104
+ /**
105
+ * Creates a reusable deep set function.
106
+ *
107
+ * @param path - key path
108
+ * @param options - function options
109
+ * @param options.create - boolean indicating whether to create a path if the key path does not already exist (default: false)
110
+ * @param options.sep - key path separator (default: '.')
111
+ * @returns deep set function
112
+ *
113
+ * @example
114
+ * var dset = deepSet.factory( 'a/b/c', {
115
+ * 'create': true,
116
+ * 'sep': '/'
117
+ * });
118
+ */
119
+ factory( path: string | Array<string>, options?: Options ): Unary;
120
+ }
121
+
122
+ /**
123
+ * Sets a nested property value.
124
+ *
125
+ * @param obj - input object
126
+ * @param path - key path
127
+ * @param value - value to set
128
+ * @param options - function options
129
+ * @param options.create - boolean indicating whether to create a path if the key path does not already exist (default: false)
130
+ * @param options.sep - key path separator (default: '.')
131
+ * @returns boolean indicating if the property was successfully set
132
+ *
133
+ * @example
134
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
135
+ * var bool = deepSet( obj, 'a.b.c', 'woot' );
136
+ * // returns true
137
+ *
138
+ * @example
139
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
140
+ * var bool = deepSet( obj, 'a.beep.c', 'boop' );
141
+ * // returns false
142
+ *
143
+ * @example
144
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
145
+ * var bool = deepSet( null, 'a.beep.c', 'boop' );
146
+ * // returns false
147
+ *
148
+ * @example
149
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
150
+ * bool = deepSet( 'bap', 'a.beep.c', 'boop' );
151
+ * // returns false
152
+ *
153
+ * @example
154
+ * var arr = [
155
+ * { 'a': [ {'x': 5} ] },
156
+ * { 'a': [ {'x': 10} ] }
157
+ * ];
158
+ * var bool = deepSet( arr, '1.a.0.x', 25 );
159
+ * // returns true
160
+ *
161
+ * @example
162
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
163
+ * var bool = deepSet( obj, 'a/b/c', 'beep', {
164
+ * 'sep': '/'
165
+ * });
166
+ * // returns true
167
+ *
168
+ * @example
169
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
170
+ * var bool = deepSet( obj, 'a.e.c', 'boop', {
171
+ * 'create': true
172
+ * });
173
+ * // returns true
174
+ */
175
+ declare var deepSet: DeepSet;
176
+
177
+
178
+ // EXPORTS //
179
+
180
+ export = deepSet;
@@ -0,0 +1,134 @@
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 deepSet = require( './index' );
20
+
21
+
22
+ // TESTS //
23
+
24
+ // The function returns a boolean...
25
+ {
26
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
27
+ deepSet( obj, 'a.b.c', 'e' ); // $ExpectType boolean
28
+ deepSet( obj, [ 'a', 'b', 'c' ], 'e' ); // $ExpectType boolean
29
+ deepSet( obj, 'a-b-c', 'e', { 'sep': '-' } ); // $ExpectType boolean
30
+ }
31
+
32
+ // The compiler throws an error if the function is provided a second argument which is not a string or array of strings...
33
+ {
34
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
35
+ deepSet( obj, true, 'e' ); // $ExpectError
36
+ deepSet( obj, false, 'e' ); // $ExpectError
37
+ deepSet( obj, 2.12, 'e' ); // $ExpectError
38
+ deepSet( obj, {}, 'e' ); // $ExpectError
39
+ deepSet( obj, [ 1, 2, 3 ], 'e' ); // $ExpectError
40
+ deepSet( obj, ( x: number ): number => x, 'e' ); // $ExpectError
41
+ }
42
+
43
+ // The compiler throws an error if the function is provided a fourth argument which is not an object...
44
+ {
45
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
46
+ deepSet( obj, 'a.b.c', 'e', null ); // $ExpectError
47
+ }
48
+
49
+ // The compiler throws an error if the function is provided a `create` option which is not a boolean...
50
+ {
51
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
52
+ deepSet( obj, 'a.b.c', 'e', { 'create': 'abc' } ); // $ExpectError
53
+ deepSet( obj, 'a.b.c', 'e', { 'create': 123 } ); // $ExpectError
54
+ deepSet( obj, 'a.b.c', 'e', { 'create': null } ); // $ExpectError
55
+ deepSet( obj, 'a.b.c', 'e', { 'create': [] } ); // $ExpectError
56
+ deepSet( obj, 'a.b.c', 'e', { 'create': {} } ); // $ExpectError
57
+ deepSet( obj, 'a.b.c', 'e', { 'create': ( x: number ): number => x } ); // $ExpectError
58
+ }
59
+
60
+ // The compiler throws an error if the function is provided a `sep` option which is not a string...
61
+ {
62
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
63
+ deepSet( obj, 'a.b.c', 'e', { 'sep': true } ); // $ExpectError
64
+ deepSet( obj, 'a.b.c', 'e', { 'sep': false } ); // $ExpectError
65
+ deepSet( obj, 'a.b.c', 'e', { 'sep': 123 } ); // $ExpectError
66
+ deepSet( obj, 'a.b.c', 'e', { 'sep': null } ); // $ExpectError
67
+ deepSet( obj, 'a.b.c', 'e', { 'sep': [] } ); // $ExpectError
68
+ deepSet( obj, 'a.b.c', 'e', { 'sep': {} } ); // $ExpectError
69
+ deepSet( obj, 'a.b.c', 'e', { 'sep': ( x: number ): number => x } ); // $ExpectError
70
+ }
71
+
72
+ // The compiler throws an error if the function is provided fewer than three arguments...
73
+ {
74
+ deepSet(); // $ExpectError
75
+ deepSet( {} ); // $ExpectError
76
+ deepSet( {}, 'a.b.c' ); // $ExpectError
77
+ }
78
+
79
+ // Attached to main export is a `factory` method which returns a function...
80
+ {
81
+ deepSet.factory( 'a.b.c' ); // $ExpectType Unary
82
+ deepSet.factory( 'a.b.c', {} ); // $ExpectType Unary
83
+ }
84
+
85
+ // The compiler throws an error if the `factory` method is provided an unsupported number of arguments...
86
+ {
87
+ deepSet.factory(); // $ExpectError
88
+ deepSet.factory( 'a.b.c', {}, 21 ); // $ExpectError
89
+ }
90
+
91
+ // The `factory` method returns a function which returns a boolean...
92
+ {
93
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
94
+ let fcn = deepSet.factory( 'a.b.c' );
95
+ fcn( obj, 'e' ); // $ExpectType boolean
96
+
97
+ fcn = deepSet.factory( 'a-b-c', { 'sep': '-' } );
98
+ fcn( obj, 'e' ); // $ExpectType boolean
99
+ }
100
+
101
+ // The compiler throws an error if the `factory` method is provided a second argument which is not an object...
102
+ {
103
+ deepSet.factory( 'a.b.c', null ); // $ExpectError
104
+ }
105
+
106
+ // The compiler throws an error if the `factory` method is provided a `create` option which is not a boolean...
107
+ {
108
+ deepSet.factory( 'a.b.c', { 'create': 123 } ); // $ExpectError
109
+ deepSet.factory( 'a.b.c', { 'create': 'abc' } ); // $ExpectError
110
+ deepSet.factory( 'a.b.c', { 'create': null } ); // $ExpectError
111
+ deepSet.factory( 'a.b.c', { 'create': [] } ); // $ExpectError
112
+ deepSet.factory( 'a.b.c', { 'create': {} } ); // $ExpectError
113
+ deepSet.factory( 'a.b.c', { 'create': ( x: number ): number => x } ); // $ExpectError
114
+ }
115
+
116
+ // The compiler throws an error if the `factory` method is provided a `sep` option which is not a string...
117
+ {
118
+ deepSet.factory( 'a.b.c', { 'sep': 123 } ); // $ExpectError
119
+ deepSet.factory( 'a.b.c', { 'sep': true } ); // $ExpectError
120
+ deepSet.factory( 'a.b.c', { 'sep': false } ); // $ExpectError
121
+ deepSet.factory( 'a.b.c', { 'sep': null } ); // $ExpectError
122
+ deepSet.factory( 'a.b.c', { 'sep': [] } ); // $ExpectError
123
+ deepSet.factory( 'a.b.c', { 'sep': {} } ); // $ExpectError
124
+ deepSet.factory( 'a.b.c', { 'sep': ( x: number ): number => x } ); // $ExpectError
125
+ }
126
+
127
+ // The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments...
128
+ {
129
+ const obj = { 'a': { 'b': { 'c': 'd' } } };
130
+ const fcn = deepSet.factory( 'a.b.c' );
131
+ fcn(); // $ExpectError
132
+ fcn( obj ); // $ExpectError
133
+ fcn( obj, 'e', 2 ); // $ExpectError
134
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2023 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Returns default options.
25
+ *
26
+ * @private
27
+ * @returns {Object} default options
28
+ */
29
+ function defaults() {
30
+ return {
31
+ 'create': false,
32
+ 'sep': '.'
33
+ };
34
+ }
35
+
36
+
37
+ // EXPORTS //
38
+
39
+ module.exports = defaults;
@@ -0,0 +1,80 @@
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 hasOwnProp = require( '@ohos-ports/stdlib-assert/has-own-property' );
25
+ var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Sets a nested property.
32
+ *
33
+ * @private
34
+ * @param {ObjectLike} obj - input object
35
+ * @param {Array} props - list of properties defining a key path
36
+ * @param {boolean} create - boolean indicating whether to create a path if the key path does not already exist
37
+ * @param {*} val - value to set
38
+ * @returns {boolean} boolean indicating if the property was successfully set
39
+ */
40
+ function deepSet( obj, props, create, val ) {
41
+ var bool;
42
+ var len;
43
+ var v;
44
+ var p;
45
+ var i;
46
+
47
+ len = props.length;
48
+ bool = false;
49
+ v = obj;
50
+ for ( i = 0; i < len; i++ ) {
51
+ p = props[ i ];
52
+ if ( isObjectLike( v ) ) {
53
+ if ( !hasOwnProp( v, p ) ) {
54
+ if ( create ) {
55
+ v[ p ] = {};
56
+ } else {
57
+ break;
58
+ }
59
+ }
60
+ if ( i === len-1 ) {
61
+ if ( isFunction( val ) ) {
62
+ v[ p ] = val( v[ p ] );
63
+ } else {
64
+ v[ p ] = val;
65
+ }
66
+ bool = true;
67
+ } else {
68
+ v = v[ p ];
69
+ }
70
+ } else {
71
+ break;
72
+ }
73
+ }
74
+ return bool;
75
+ }
76
+
77
+
78
+ // EXPORTS //
79
+
80
+ module.exports = deepSet;
@@ -0,0 +1,99 @@
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 isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
24
+ var isArray = require( '@ohos-ports/stdlib-assert/is-array' );
25
+ var isObjectLike = require( '@ohos-ports/stdlib-assert/is-object-like' );
26
+ var format = require( '@ohos-ports/stdlib-string/format' );
27
+ var validate = require( './validate.js' );
28
+ var defaults = require( './defaults.js' );
29
+ var dset = require( './dset.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Creates a reusable deep set function.
36
+ *
37
+ * @param {(string|Array)} path - key path
38
+ * @param {Options} [options] - function options
39
+ * @param {boolean} [options.create=false] - boolean indicating whether to create a path if the key path does not already exist
40
+ * @param {string} [options.sep='.'] - key path separator
41
+ * @throws {TypeError} first argument must be a string or key array
42
+ * @throws {TypeError} options argument must be an object
43
+ * @throws {TypeError} must provide valid options
44
+ * @returns {Function} deep set function
45
+ *
46
+ * @example
47
+ * var dset = factory( 'a/b/c', {
48
+ * 'create': true,
49
+ * 'sep': '/'
50
+ * });
51
+ */
52
+ function factory( path, options ) {
53
+ var isStr;
54
+ var props;
55
+ var opts;
56
+ var err;
57
+
58
+ isStr = isString( path );
59
+ if ( !isStr && !isArray( path ) ) {
60
+ throw new TypeError( format( 'invalid argument. Key path must be a string or a key array. Value: `%s`.', path ) );
61
+ }
62
+ opts = defaults();
63
+ if ( arguments.length > 1 ) {
64
+ err = validate( opts, options );
65
+ if ( err ) {
66
+ throw err;
67
+ }
68
+ }
69
+ if ( isStr ) {
70
+ props = path.split( opts.sep );
71
+ } else {
72
+ props = path;
73
+ }
74
+ return deepSet;
75
+
76
+ /**
77
+ * Sets a nested property.
78
+ *
79
+ * @private
80
+ * @param {ObjectLike} obj - input object
81
+ * @param {*} value - value to set
82
+ * @returns {boolean} boolean indicating if the property was successfully set
83
+ *
84
+ * @example
85
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
86
+ * var bool = dset( obj, 'beep' );
87
+ */
88
+ function deepSet( obj, value ) {
89
+ if ( isObjectLike( obj ) ) {
90
+ return dset( obj, props, opts.create, value );
91
+ }
92
+ return false;
93
+ }
94
+ }
95
+
96
+
97
+ // EXPORTS //
98
+
99
+ module.exports = factory;
@@ -0,0 +1,58 @@
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
+ * Set a nested property value.
23
+ *
24
+ * @module @stdlib/object/deep-set
25
+ *
26
+ * @example
27
+ * var deepSet = require( '@ohos-ports/stdlib-object/deep-set' );
28
+ *
29
+ * var obj = { 'a': { 'b': { 'c': 'd' } } };
30
+ * var bool = deepSet( obj, 'a.b.c', 'beep' );
31
+ * // returns true
32
+ *
33
+ * var dset = deepSet.factory( 'a/b/c', {
34
+ * 'create': true,
35
+ * 'sep': '/'
36
+ * });
37
+ *
38
+ * obj = { 'a': { 'b': { 'c': 'd' } } };
39
+ *
40
+ * bool = dset( obj, 'beep' );
41
+ * // returns true
42
+ */
43
+
44
+ // MODULES //
45
+
46
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
47
+ var main = require( './main.js' );
48
+ var factory = require( './factory.js' );
49
+
50
+
51
+ // MAIN //
52
+
53
+ setReadOnly( main, 'factory', factory );
54
+
55
+
56
+ // EXPORTS //
57
+
58
+ module.exports = main;