@hkdigital/lib-core 0.3.8 → 0.3.10

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.
@@ -5,6 +5,10 @@ import { smallestFirst, largestFirst } from '../compare/index.js';
5
5
 
6
6
  import IterableTree from '../../classes/data/IterableTree.js';
7
7
 
8
+ /**
9
+ * @typedef {import('../../classes/data/typedef.js').IterableTreeOptions} IterableTreeOptions
10
+ */
11
+
8
12
  /* ------------------------------------------------------------------ Exports */
9
13
 
10
14
  /**
@@ -52,30 +56,34 @@ export function* map(iterable, transformFn) {
52
56
  * Get an Iterator object that can be used to iterate over all
53
57
  * [ path, value ] entries in the object
54
58
  *
55
- * @param {Object} obj - Object to iterate
59
+ * @param {object} obj - Object to iterate
56
60
  *
57
- * @param {Object} [options] - Iteration options
61
+ * @param {object} [options] - Iteration options
58
62
  *
59
63
  * @param {boolean} [options.walkArrays=false]
60
64
  * If set to true, the iterator will also iterate through Array objects
61
65
  *
62
- * @param {object} [options.expandPathKeys=false]
66
+ * @param {boolean} [options.expandPathKeys=false]
63
67
  * If true, keys in the root object like "some.path.to" will be interpreted
64
68
  * as paths in the object
65
69
  *
66
- * @param {object} [options.outputIntermediateNodes=false]
70
+ * @param {boolean} [options.outputIntermediateNodes=false]
67
71
  * If true, intermediate object nodes (not leaves) will be outputted too
68
72
  *
69
- * @param {object} [options.ignoreEmptyObjectLeaves=false]
73
+ * @param {boolean} [options.ignoreEmptyObjectLeaves=false]
70
74
  * If true, no output will be generated for empty objects at the leaves of
71
75
  * the object tree
72
76
  *
77
+ * @param {boolean} [options.depthFirst=true]
78
+ * If true, use depth-first traversal, otherwise breadth-first
79
+ *
73
80
  * @return {Iterator} iterator object
74
81
  */
75
82
  export function iterateObjectEntries(obj, options = {}) {
76
83
  let objectIterator;
77
84
 
78
- const depthFirst = undefined === options.depthFirst ? true : options.depthFirst;
85
+ const depthFirst =
86
+ undefined === options.depthFirst ? true : options.depthFirst;
79
87
 
80
88
  options = Object.assign({}, options);
81
89
 
@@ -99,30 +107,16 @@ export function iterateObjectEntries(obj, options = {}) {
99
107
  * Get an Iterator object that can be used to iterate over all paths in
100
108
  * the object
101
109
  *
102
- * @param {Object} obj - Object to iterate
103
- *
104
- * @param {Object} [options] - Iteration options
105
- *
106
- * @param {boolean} [options.walkArrays=false]
107
- * If set to true, the iterator will also iterate through Array objects
108
- *
109
- * @param {object} [options.expandPathKeys=false]
110
- * If true, keys in the root object like "some.path.to" will be interpreted
111
- * as paths in the object
112
- *
113
- * @param {object} [options.outputIntermediateNodes=false]
114
- * If true, intermediate object nodes (not leaves) will be outputted too
115
- *
116
- * @param {object} [options.ignoreEmptyObjectLeaves=false]
117
- * If true, no output will be generated for empty objects at the leaves of
118
- * the object tree
110
+ * @param {object} obj - Object to iterate
111
+ * @param {IterableTreeOptions & { depthFirst: boolean}} [options]
119
112
  *
120
113
  * @return {Iterator} iterator object
121
114
  */
122
- export function iterateObjectPaths(obj, options = {}) {
115
+ export function iterateObjectPaths(obj, options) {
123
116
  let objectIterator;
124
117
 
125
- const depthFirst = undefined === options.depthFirst ? true : options.depthFirst;
118
+ const depthFirst =
119
+ undefined === options.depthFirst ? true : options.depthFirst;
126
120
 
127
121
  options = Object.assign({}, options);
128
122
 
@@ -132,9 +126,6 @@ export function iterateObjectPaths(obj, options = {}) {
132
126
  objectIterator = new IterableTree(obj, options);
133
127
  } else {
134
128
  throw new Error('NOT IMPLEMENTED YET');
135
-
136
- // objectIterator
137
- // = new hk.iterate._BreadthFirstIterableTree( obj, options );
138
129
  }
139
130
 
140
131
  return objectIterator.paths();
@@ -146,23 +137,8 @@ export function iterateObjectPaths(obj, options = {}) {
146
137
  * Get an Iterator object that can be used to iterate over all values in
147
138
  * the object (at the leaves of the object tree)
148
139
  *
149
- * @param {Object} obj - Object to iterate
150
- *
151
- * @param {Object} [options] - Iteration options
152
- *
153
- * @param {boolean} [options.walkArrays=false]
154
- * If set to true, the iterator will also iterate through Array objects
155
- *
156
- * @param {object} [options.expandPathKeys=false]
157
- * If true, keys in the root object like "some.path.to" will be interpreted
158
- * as paths in the object
159
- *
160
- * @param {object} [options.outputIntermediateNodes=false]
161
- * If true, intermediate object nodes (not leaves) will be outputted too
162
- *
163
- * @param {object} [options.ignoreEmptyObjectLeaves=false]
164
- * If true, no output will be generated for empty objects at the leaves of
165
- * the object tree
140
+ * @param {object} obj - Object to iterate
141
+ * @param {IterableTreeOptions} [options] - Iteration options
166
142
  *
167
143
  * @return {Iterator} iterator object
168
144
  */
@@ -177,23 +153,17 @@ export function iterateObjectValues(obj, options) {
177
153
  /**
178
154
  * Get a list of objects returned by an iterator in sorted order
179
155
  *
180
- * --
181
- *
182
156
  * @note Sorting requires that all values are evaluated, so all items must be
183
157
  * iterated
184
158
  *
185
- * --
186
- *
187
- * @param {Iterable} it - Iterable items
188
- *
189
- * @param {function} getValueFn
159
+ * @param {object} _
160
+ * @param {Iterable} _.it - Iterable items
161
+ * @param {function} _.getValueFn
190
162
  * Function that gets the value from the iterated objects
191
- *
192
- * @param {boolean} [reversed=false]
163
+ * @param {boolean} [_.reversed=false]
193
164
  * Sort in reversed order
194
165
  *
195
- *
196
- * @returns {array} objects outputted in sorted order
166
+ * @returns {Promise<array>} objects outputted in sorted order
197
167
  */
198
168
  export async function sortObjects({ it, getValueFn, reversed = false }) {
199
169
  expect.iterable(it);
@@ -1,23 +1,23 @@
1
1
  /**
2
2
  * Returns true
3
- * - if the object has no (iterable) key value pairs
3
+ * - if the object has no enumerable key value pairs
4
4
  * - if the object is an empty array
5
5
  *
6
6
  * @param {object} obj
7
7
  *
8
8
  * @return {boolean}
9
9
  * true if the object has no key value pairs or the supplied object
10
- * is `falsey`
10
+ * is falsy
11
11
  */
12
12
  export function isEmpty(obj: object): boolean;
13
13
  /**
14
- * Get the number of (iterable) key value pairs in the specified object
14
+ * Get the number of enumerable key value pairs in the specified object
15
15
  *
16
16
  * @param {object} obj
17
17
  *
18
- * @return {number} number of iterable key value pairs
18
+ * @return {number} number of enumerable key value pairs
19
19
  */
20
- export function objectSize(obj: object): number;
20
+ export function size(obj: object): number;
21
21
  /**
22
22
  * Returns a shallow copy of the object without the properties that
23
23
  * have the value [null] or [undefined]
@@ -31,19 +31,30 @@ export function objectSize(obj: object): number;
31
31
  */
32
32
  export function exportNotNull(obj: object, onlyKeys?: string[]): object;
33
33
  /**
34
- * Returns a shallow copy of the object without the properties that
35
- * are `private`
36
- * - Private properties are properties that start with an underscore
37
- * `_`.
34
+ * Create a shallow copy of the object's public properties. Properties that
35
+ * start with an underscore are considered 'internal' properties and are not
36
+ * exported.
37
+ * - This method can e.g. be used to export a data object without it's
38
+ * 'internal' properties
38
39
  *
39
40
  * @param {object} obj
40
41
  *
41
42
  * @param {string[]} [keepKeys]
42
- * If specified, the sprecified private keys will be exported (e.g. `_id`)
43
+ * If specified, the specified private keys will be exported (e.g. `_id`)
43
44
  *
44
- * @returns {object} new object without the null properties
45
+ * @returns {object} new object without properties that start with an underscore
46
+ */
47
+ export function exportPublic(obj: object, keepKeys?: string[]): object;
48
+ /**
49
+ * Creates a copy of an object or array that contains only primitive values
50
+ * - Nested objects and arrays are completely removed
51
+ * - Only string, number, boolean, null, undefined values are kept
52
+ *
53
+ * @param {object|array} objectOrArray
54
+ *
55
+ * @returns {object|array} new object or array with only primitive values
45
56
  */
46
- export function exportNotPrivate(obj: object, keepKeys?: string[]): object;
57
+ export function exportNotNested(objectOrArray: object | any[]): object | any[];
47
58
  /**
48
59
  * Keep only the specified keys in the object
49
60
  * - deletes all other key-value pairs in the object
@@ -220,26 +231,22 @@ export function getPrototypeNames(obj: object): string[];
220
231
  * { someArray.0.name: 1 }
221
232
  *
222
233
  * @param {object} [options]
223
- * @param {object} [options.shallowLeaves=false]
224
- * If set to true, the values of the leaves of the tree will be converted
225
- * to shallow objects if the leaves are nested objects.
234
+ * @param {boolean} [options.shallowLeaves=false]
235
+ * If true, when extracted leaf values are objects, they are filtered to
236
+ * contain only primitive properties. Nested objects and arrays within
237
+ * the leaves are removed.
238
+ *
239
+ * Example: { profile: { name: "John", settings: {...} } }
240
+ * becomes: { profile: { name: "John" } }
226
241
  *
227
242
  * @return {object}
228
243
  * nested object with the values that were found or defaultValues
229
244
  */
230
245
  export function getTree(obj: object, tree: object, options?: {
231
- shallowLeaves?: object;
246
+ shallowLeaves?: boolean;
232
247
  }): object;
233
248
  /**
234
249
  * Deep clone an object or any kind of other variable
235
- * - Recursively clone all nested properties
236
- * - Properties in the prototype chain are cloned too, but all copied into a
237
- * single prototype object
238
- * - This method works on objects, but also on any other JS variable type
239
- * - If a value cannot be cloned, a reference is returned. o.a. for
240
- * - Error objects
241
- * - Browser objects
242
- * - Functions
243
250
  *
244
251
  * @param {any} objectToBeCloned - Variable to clone
245
252
  *
@@ -254,14 +261,6 @@ export function clone(objectToBeCloned: any, _seenObjects: any): any;
254
261
  * @param {any} value - Value to set
255
262
  */
256
263
  export function setReadOnlyProperty(obj: object, propertyName: string, value: any): void;
257
- /**
258
- * Returns a clone of a (nested) object that has a maximum depth
259
- *
260
- * @param {object|array} obj
261
- *
262
- * @returns {object|array} shallow object or array
263
- */
264
- export function shallowClone(objectOrArray: any): object | any[];
265
264
  /**
266
265
  * Update an object
267
266
  * - Sets the path-value pairs from the updateData in the object
@@ -271,7 +270,10 @@ export function shallowClone(objectOrArray: any): object | any[];
271
270
  *
272
271
  * @param {object} [obj] - Input object
273
272
  *
274
- * @param {object|iterable} updateData - Data to update
273
+ * @param {object|Iterable} updateData - Data to update
274
+ *
275
+ * @param {Object} [options]
276
+ * @param {boolean} [options.replaceArrays=false]
275
277
  *
276
278
  * Note that if using path-value pairs, the order of the pairs is relevant:
277
279
  * {
@@ -282,7 +284,9 @@ export function shallowClone(objectOrArray: any): object | any[];
282
284
  *
283
285
  * @returns {object} updated object
284
286
  */
285
- export function updateObject(obj?: object, updateData: object | iterable, options: any): object;
287
+ export function updateObject(obj?: object, updateData?: object | Iterable<any>, options?: {
288
+ replaceArrays?: boolean;
289
+ }): object;
286
290
  /**
287
291
  * Copy own properties from an object to another object if they do not
288
292
  * exist yet.
@@ -300,27 +304,4 @@ export function copyOwnProperties(from: object, to: object): void;
300
304
  * @returns {string[]} list of path values
301
305
  */
302
306
  export function ensureArrayPath(path: string | string[]): string[];
303
- /**
304
- * Create all parent objects on the object path if they do not yet exist yet
305
- * - This method will throw an exception if there is a non-object node in
306
- * the path
307
- *
308
- * @param {object} obj
309
- * Object to create the parent objects in
310
- *
311
- * @param {string[]} arrPath
312
- * The path that specified which parent oobjects to create
313
- *
314
- * @returns {object} the input object with the created object properties
315
- */
316
- export function _ensureParent(obj: object, arrPath: string[]): object;
317
- /**
318
- * Get parent object at the specified path
319
- *
320
- * @param {object} obj - Object to work in
321
- * @param {string[]} arrPath - Path to get the parent object for
322
- *
323
- * @returns {object|array|null} parent object or null if not found
324
- */
325
- export function _getParent(obj: object, arrPath: string[]): object | any[] | null;
326
307
  export const PATH_SEPARATOR: ".";