jupyter-ijavascript-utils 1.16.3 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOCS.md +1 -0
- package/README.md +1 -0
- package/package.json +1 -1
- package/src/format.js +19 -0
- package/src/object.js +53 -0
package/DOCS.md
CHANGED
|
@@ -46,6 +46,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
|
|
|
46
46
|
|
|
47
47
|
## What's New
|
|
48
48
|
|
|
49
|
+
* 1.17 - provide object.propertyValueSample - as a way to list 'non-empty' property values
|
|
49
50
|
* 1.16 - provide file.matchFiles - as a way to find files or directories
|
|
50
51
|
* 1.15 - provide {@link module:object.formatProperties|object.formatProperties} - as a way to quickly convert to string, number, etc.
|
|
51
52
|
* 1.14 - provide {@link module:object.mapProperties|object.mapProperties()} and {@link module:format.compactNumber|format.compactNumber()}
|
package/README.md
CHANGED
|
@@ -46,6 +46,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
|
|
|
46
46
|
|
|
47
47
|
# What's New
|
|
48
48
|
|
|
49
|
+
* 1.17 - provide object.propertyValueSample - as a way to list 'non-empty' property values
|
|
49
50
|
* 1.16 - provide file.matchFiles - as a way to find files or directories
|
|
50
51
|
* 1.15 - provide object.formatProperties - as a way to quickly convert to string, number, etc.
|
|
51
52
|
* 1.14 - provide format.compactNumber and object.mapProperties
|
package/package.json
CHANGED
package/src/format.js
CHANGED
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
* * {@link module:format.safeConvertFloat|format.safeConvertFloat} - converts a value to a Number (123.4), or uses a default for any error or NaN
|
|
28
28
|
* * {@link module:format.safeConvertInteger|format.safeConvertInteger} - converts a value to a Number (123), or uses a default for any error or NaN
|
|
29
29
|
* * {@link module:format.safeConvertBoolean|format.safeConvertBoolean} - converts a value to a boolean
|
|
30
|
+
* * Identifying values
|
|
31
|
+
* * {@link module:format.isEmptyValue|format.isEmptyValue} - determine if a value is not 'empty'
|
|
30
32
|
*
|
|
31
33
|
* @module format
|
|
32
34
|
* @exports format
|
|
@@ -825,3 +827,20 @@ module.exports.prepareFormatterObject = function prepareFormatterObject(formatte
|
|
|
825
827
|
|
|
826
828
|
return result;
|
|
827
829
|
};
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* Tests for empty values:
|
|
833
|
+
*
|
|
834
|
+
* * (zeros are allowed)
|
|
835
|
+
* * not null
|
|
836
|
+
* * not undefined
|
|
837
|
+
* * not an empty string
|
|
838
|
+
* * not an empty array `[]`
|
|
839
|
+
*
|
|
840
|
+
* @param {any} val - a value to be tested
|
|
841
|
+
* @returns {Boolean} - TRUE if the value is not 'empty'
|
|
842
|
+
*/
|
|
843
|
+
module.exports.isEmptyValue = (val) =>
|
|
844
|
+
//-- allow for 0s
|
|
845
|
+
val === null || val === undefined || val === ''
|
|
846
|
+
|| (Array.isArray(val) && val.length === 0);
|
package/src/object.js
CHANGED
|
@@ -14,6 +14,7 @@ const FormatUtils = require('./format');
|
|
|
14
14
|
* * {@link module:object.findWithoutProperties|findWithoutProperties()} - find objects without ALL the properties specified
|
|
15
15
|
* * {@link module:object.findWithoutProperties|findWithProperties()} - find objects with any of the properties specified
|
|
16
16
|
* * {@link module:object.setPropertyDefaults|setPropertyDefaults()} - sets values for objects that don't currently have the property
|
|
17
|
+
* * {@link module:object.propertyValueSample|propertyValueSample(collection)} - finds non-empty values for all properties found in the collection
|
|
17
18
|
* * Manipulating objects
|
|
18
19
|
* * {@link module:object.objAssign|objAssign()} -
|
|
19
20
|
* * {@link module:object.objAssignEntities|objAssignEntities()} -
|
|
@@ -1048,3 +1049,55 @@ module.exports.mapProperties = function mapProperties(objCollection, formattingF
|
|
|
1048
1049
|
return clone;
|
|
1049
1050
|
});
|
|
1050
1051
|
};
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Finds all the properties for objects in a collection,
|
|
1055
|
+
* and provides the first 'non-empty' value found of each property.
|
|
1056
|
+
*
|
|
1057
|
+
* Non-Empty means:
|
|
1058
|
+
*
|
|
1059
|
+
* * not null
|
|
1060
|
+
* * not undefined
|
|
1061
|
+
* * not an empty string
|
|
1062
|
+
* * not an empty array
|
|
1063
|
+
*
|
|
1064
|
+
* This can be especially helpful for heterogeneous collections
|
|
1065
|
+
* and can be much faster than something like {@link https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.describe|danfojs.describe}
|
|
1066
|
+
*
|
|
1067
|
+
* @param {Object[]} objCollection - Array of objects that we want to understand
|
|
1068
|
+
* @returns {Map<String,any>} - Collection of all properties and the first 'non-empty' value found
|
|
1069
|
+
* @example
|
|
1070
|
+
* let collection = [
|
|
1071
|
+
* { first: 'jane', age: 23 },
|
|
1072
|
+
* { first: 'john', last: 'doe', age: 21 }
|
|
1073
|
+
* ];
|
|
1074
|
+
*
|
|
1075
|
+
* utils.object.propertyValueSample(collection);
|
|
1076
|
+
* // { first: 'jane', last: 'doe', age: 23 }
|
|
1077
|
+
*/
|
|
1078
|
+
module.exports.propertyValueSample = function propertyValueSample(objCollection) {
|
|
1079
|
+
if (!objCollection) {
|
|
1080
|
+
throw new Error('propertyValueSample(objectCollection): objectCollection is required');
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
const collection = Array.isArray(objCollection) ? objCollection : [objCollection];
|
|
1084
|
+
|
|
1085
|
+
const result = new Map();
|
|
1086
|
+
let entryValue;
|
|
1087
|
+
|
|
1088
|
+
collection.forEach((entry) => {
|
|
1089
|
+
if (entry && (typeof entry) === 'object') {
|
|
1090
|
+
for (const entryProperty of Object.keys(entry)) {
|
|
1091
|
+
entryValue = entry[entryProperty];
|
|
1092
|
+
if (
|
|
1093
|
+
!FormatUtils.isEmptyValue(entryValue)
|
|
1094
|
+
&& !result.has(entryProperty)
|
|
1095
|
+
) {
|
|
1096
|
+
result.set(entryProperty, entryValue);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
return result;
|
|
1103
|
+
};
|