@valkyriestudios/utils 8.2.0 → 8.4.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/CHANGELOG.md +19 -0
- package/README.md +2 -1
- package/array/mapKey.js +1 -1
- package/array/sort.js +4 -2
- package/deep/get.js +2 -2
- package/object/merge.js +1 -1
- package/package.json +4 -3
- package/src/array/mapKey.mjs +3 -1
- package/src/array/sort.mjs +4 -2
- package/src/deep/get.mjs +2 -2
- package/src/object/merge.mjs +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic
|
|
6
6
|
Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [8.4.0] - 2023-12-06
|
|
9
|
+
### Improved
|
|
10
|
+
- deep/get: Removed internal usage of `X.hasOwnProperty(key)` in favor of `Object.prototype.hasOwnProperty.call(X, key)`, allowing passing objects without prototype (eg: Object.create(null))
|
|
11
|
+
- object/merge: Removed internal usage of `X.hasOwnProperty(key)` in favor of `Object.prototype.hasOwnProperty.call(X, key)`, allowing passing objects without prototype (eg: Object.create(null))
|
|
12
|
+
- array/mapKey: Removed internal usage of `X.hasOwnProperty(key)` in favor of `Object.prototype.hasOwnProperty.call(X, key)`, allowing passing objects without prototype (eg: Object.create(null))
|
|
13
|
+
- array/sort: Removed internal usage of `X.hasOwnProperty(key)` in favor of `Object.prototype.hasOwnProperty.call(X, key)`, allowing passing objects without prototype (eg: Object.create(null))
|
|
14
|
+
- array/sort: Now wraps a custom filter function to ensure non-empty object checks are always applied, this also means custom filter functions no longer need to pass this themselves
|
|
15
|
+
|
|
16
|
+
## [8.3.0] - 2023-12-06
|
|
17
|
+
### Added
|
|
18
|
+
- Dev Dep: babel-plugin-module-extension@0.1.3
|
|
19
|
+
|
|
20
|
+
### Improved
|
|
21
|
+
- Dev Dep: Upgrade eslint to 8.55.0
|
|
22
|
+
- Rerun benchmark for v8 on intel i9
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
- deep/get: Fix edge case issue where cursor is not an object or array when parts still exist afterwards
|
|
26
|
+
|
|
8
27
|
## [8.2.0] - 2023-11-29
|
|
9
28
|
### Improved
|
|
10
29
|
- Dev Dep: Upgrade @babel/core to 7.23.5
|
package/README.md
CHANGED
|
@@ -218,6 +218,7 @@ const out = sort([
|
|
|
218
218
|
```
|
|
219
219
|
|
|
220
220
|
allows passing custom filter function to clean input
|
|
221
|
+
Take note: Sort will still verify that the object is not an empty object, even when passing a custom filter function.
|
|
221
222
|
|
|
222
223
|
```js
|
|
223
224
|
const out = sort([
|
|
@@ -233,7 +234,7 @@ const out = sort([
|
|
|
233
234
|
{test: 'Bob'},
|
|
234
235
|
undefined,
|
|
235
236
|
{test: 'Alice'},
|
|
236
|
-
], el => el.test.toLowerCase(), 'desc', {filter_fn: el =>
|
|
237
|
+
], el => el.test.toLowerCase(), 'desc', {filter_fn: el => isNotEmptyString(el.test)});
|
|
237
238
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
|
|
238
239
|
```
|
|
239
240
|
|
package/array/mapKey.js
CHANGED
|
@@ -22,7 +22,7 @@ function mapKey(arr, key) {
|
|
|
22
22
|
try {
|
|
23
23
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
24
24
|
var el = _step.value;
|
|
25
|
-
if (Object.prototype.toString.call(el) !== _is.PROTO_OBJ || !
|
|
25
|
+
if (Object.prototype.toString.call(el) !== _is.PROTO_OBJ || !Object.prototype.hasOwnProperty.call(el, key)) continue;
|
|
26
26
|
if (OPTS.merge === !0 && map.hasOwnProperty(el[key])) {
|
|
27
27
|
map[el[key]] = Object.assign(map[el[key]], el);
|
|
28
28
|
} else {
|
package/array/sort.js
CHANGED
|
@@ -54,7 +54,9 @@ function sort(arr, by) {
|
|
|
54
54
|
if (dir !== 'asc' && dir !== 'desc') throw new Error('Direction should be either asc or desc');
|
|
55
55
|
var has_opts = Object.prototype.toString.call(options) === _is2.PROTO_OBJ;
|
|
56
56
|
var OPTS = {
|
|
57
|
-
filter_fn: has_opts && (0, _is3["default"])(options.filter_fn) ?
|
|
57
|
+
filter_fn: has_opts && (0, _is3["default"])(options.filter_fn) ? function (el) {
|
|
58
|
+
return (0, _isNotEmpty["default"])(el) && options.filter_fn(el);
|
|
59
|
+
} : _isNotEmpty["default"],
|
|
58
60
|
nokey_hide: has_opts && (0, _is["default"])(options.nokey_hide) ? options.nokey_hide : !1,
|
|
59
61
|
nokey_atend: has_opts && (0, _is["default"])(options.nokey_atend) ? options.nokey_atend : !0
|
|
60
62
|
};
|
|
@@ -67,7 +69,7 @@ function sort(arr, by) {
|
|
|
67
69
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
68
70
|
var el = _step.value;
|
|
69
71
|
if (!OPTS.filter_fn(el)) continue;
|
|
70
|
-
if (!
|
|
72
|
+
if (!Object.prototype.hasOwnProperty.call(el, by) || el[by] === undefined) {
|
|
71
73
|
nokey_arr.push(el);
|
|
72
74
|
} else {
|
|
73
75
|
prepared_arr.push({
|
package/deep/get.js
CHANGED
|
@@ -20,9 +20,9 @@ function deepGet(obj, path) {
|
|
|
20
20
|
var ix = parseInt(parts.shift());
|
|
21
21
|
if (!Number.isInteger(ix) || ix < 0 || ix > cursor.length - 1) return undefined;
|
|
22
22
|
cursor = cursor[ix];
|
|
23
|
-
} else {
|
|
23
|
+
} else if (Object.prototype.toString.call(cursor) === _is.PROTO_OBJ) {
|
|
24
24
|
var key = parts.shift();
|
|
25
|
-
if (!
|
|
25
|
+
if (!Object.prototype.hasOwnProperty.call(cursor, key)) return undefined;
|
|
26
26
|
cursor = cursor[key];
|
|
27
27
|
}
|
|
28
28
|
if (!Array.isArray(cursor) && Object.prototype.toString.call(cursor) !== _is.PROTO_OBJ && parts.length > 0) return undefined;
|
package/object/merge.js
CHANGED
|
@@ -12,7 +12,7 @@ var merge = function merge(target) {
|
|
|
12
12
|
if (Object.prototype.toString.call(target[key]) === _is.PROTO_OBJ && !Array.isArray(target[key])) {
|
|
13
13
|
acc[key] = merge(target[key], source[key] || {});
|
|
14
14
|
} else {
|
|
15
|
-
acc[key] =
|
|
15
|
+
acc[key] = Object.prototype.hasOwnProperty.call(source, key) ? source[key] : target[key];
|
|
16
16
|
}
|
|
17
17
|
return acc;
|
|
18
18
|
}, {});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@valkyriestudios/utils",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.0",
|
|
4
4
|
"description": "A collection of single-function utilities for common tasks",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"lint": "npm run lint:src && npm run lint:test",
|
|
24
24
|
"lint:src": "./node_modules/.bin/eslint --ext .js,.mjs ./src",
|
|
25
25
|
"lint:test": "./node_modules/.bin/eslint --ext .js,.mjs ./test",
|
|
26
|
-
"build": "npm run lint && npm run test:coverage &&
|
|
26
|
+
"build": "npm run lint && npm run test:coverage && npx babel src --out-dir ./",
|
|
27
27
|
"codecov": "codecov"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
@@ -39,9 +39,10 @@
|
|
|
39
39
|
"@babel/core": "^7.23.5",
|
|
40
40
|
"@babel/preset-env": "^7.23.5",
|
|
41
41
|
"@babel/register": "^7.22.15",
|
|
42
|
+
"babel-plugin-module-extension": "^0.1.3",
|
|
42
43
|
"babel-plugin-transform-minify-booleans": "^6.9.4",
|
|
43
44
|
"babel-plugin-transform-remove-console": "^6.9.4",
|
|
44
45
|
"c8": "^8.0.1",
|
|
45
|
-
"eslint": "^8.
|
|
46
|
+
"eslint": "^8.55.0"
|
|
46
47
|
}
|
|
47
48
|
}
|
package/src/array/mapKey.mjs
CHANGED
|
@@ -17,7 +17,9 @@ export default function mapKey (arr, key, opts = {}) {
|
|
|
17
17
|
for (const el of arr) {
|
|
18
18
|
if (
|
|
19
19
|
Object.prototype.toString.call(el) !== PROTO_OBJ ||
|
|
20
|
-
!
|
|
20
|
+
!Object.prototype.hasOwnProperty.call(el, key)
|
|
21
|
+
) continue;
|
|
22
|
+
|
|
21
23
|
if (OPTS.merge === true && map.hasOwnProperty(el[key])) {
|
|
22
24
|
map[el[key]] = Object.assign(map[el[key]], el);
|
|
23
25
|
} else {
|
package/src/array/sort.mjs
CHANGED
|
@@ -54,7 +54,9 @@ export default function sort (arr, by, dir = 'asc', options = {}) {
|
|
|
54
54
|
const has_opts = Object.prototype.toString.call(options) === PROTO_OBJ;
|
|
55
55
|
|
|
56
56
|
const OPTS = {
|
|
57
|
-
filter_fn : has_opts && isFunction(options.filter_fn)
|
|
57
|
+
filter_fn : has_opts && isFunction(options.filter_fn)
|
|
58
|
+
? el => isNotEmptyObject(el) && options.filter_fn(el)
|
|
59
|
+
: isNotEmptyObject,
|
|
58
60
|
nokey_hide : has_opts && isBoolean(options.nokey_hide) ? options.nokey_hide : false,
|
|
59
61
|
nokey_atend : has_opts && isBoolean(options.nokey_atend) ? options.nokey_atend : true,
|
|
60
62
|
};
|
|
@@ -66,7 +68,7 @@ export default function sort (arr, by, dir = 'asc', options = {}) {
|
|
|
66
68
|
for (const el of arr) {
|
|
67
69
|
if (!OPTS.filter_fn(el)) continue;
|
|
68
70
|
|
|
69
|
-
if (!
|
|
71
|
+
if (!Object.prototype.hasOwnProperty.call(el, by) || el[by] === undefined) {
|
|
70
72
|
nokey_arr.push(el);
|
|
71
73
|
} else {
|
|
72
74
|
prepared_arr.push({t: el[by], el});
|
package/src/deep/get.mjs
CHANGED
|
@@ -32,9 +32,9 @@ export default function deepGet (obj, path, get_parent = false) {
|
|
|
32
32
|
const ix = parseInt(parts.shift());
|
|
33
33
|
if (!Number.isInteger(ix) || ix < 0 || ix > (cursor.length - 1)) return undefined;
|
|
34
34
|
cursor = cursor[ix];
|
|
35
|
-
} else {
|
|
35
|
+
} else if (Object.prototype.toString.call(cursor) === PROTO_OBJ) {
|
|
36
36
|
const key = parts.shift();
|
|
37
|
-
if (!
|
|
37
|
+
if (!Object.prototype.hasOwnProperty.call(cursor, key)) return undefined;
|
|
38
38
|
cursor = cursor[key];
|
|
39
39
|
}
|
|
40
40
|
|
package/src/object/merge.mjs
CHANGED