@webdeveric/utils 1.5.0 → 1.6.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/dist/cjs/get.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.get = get;
4
+ const getNode_js_1 = require("./internal/getNode.js");
4
5
  const pathParts_js_1 = require("./pathParts.js");
5
6
  /**
6
7
  * Get the value at `path` within `input`.
@@ -11,18 +12,5 @@ const pathParts_js_1 = require("./pathParts.js");
11
12
  * ```
12
13
  */
13
14
  function get(input, path) {
14
- if (path === '') {
15
- return input;
16
- }
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- let current = input;
19
- for (const part of (0, pathParts_js_1.pathParts)(path)) {
20
- if (typeof current === 'object' && current !== null && part in current) {
21
- current = current[part];
22
- }
23
- else {
24
- return;
25
- }
26
- }
27
- return current;
15
+ return path === '' ? input : (0, getNode_js_1.getNode)(input, (0, pathParts_js_1.pathParts)(path)).value;
28
16
  }
package/dist/cjs/has.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.has = has;
4
+ const getNode_js_1 = require("./internal/getNode.js");
4
5
  const pathParts_js_1 = require("./pathParts.js");
5
6
  const isAnyObject_js_1 = require("./predicate/isAnyObject.js");
6
7
  /**
@@ -12,21 +13,8 @@ const isAnyObject_js_1 = require("./predicate/isAnyObject.js");
12
13
  * ```
13
14
  */
14
15
  function has(input, path) {
15
- if (!(0, isAnyObject_js_1.isAnyObject)(input)) {
16
+ if (!(0, isAnyObject_js_1.isAnyObject)(input) || path === '') {
16
17
  return false;
17
18
  }
18
- if (path === '') {
19
- return false;
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- let current = input;
23
- for (const part of (0, pathParts_js_1.pathParts)(path)) {
24
- if (typeof current === 'object' && current !== null && part in current) {
25
- current = current[part];
26
- }
27
- else {
28
- return false;
29
- }
30
- }
31
- return true;
19
+ return (0, getNode_js_1.getNode)(input, (0, pathParts_js_1.pathParts)(path)).found;
32
20
  }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasSameOrigin = void 0;
4
+ const hasSameOrigin = (left, right) => new URL(left).origin === new URL(right).origin;
5
+ exports.hasSameOrigin = hasSameOrigin;
package/dist/cjs/index.js CHANGED
@@ -62,6 +62,7 @@ __exportStar(require("./getType.js"), exports);
62
62
  __exportStar(require("./graphemeLength.js"), exports);
63
63
  __exportStar(require("./has.js"), exports);
64
64
  __exportStar(require("./hasAdditionalProperties.js"), exports);
65
+ __exportStar(require("./hasSameOrigin.js"), exports);
65
66
  __exportStar(require("./indent.js"), exports);
66
67
  __exportStar(require("./inRange.js"), exports);
67
68
  __exportStar(require("./isEmpty.js"), exports);
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNode = getNode;
4
+ const isAnyObjectWith_js_1 = require("../predicate/isAnyObjectWith.js");
5
+ /**
6
+ * Retrieves the value of a nested property from an object based on a sequence of keys.
7
+ *
8
+ * @internal
9
+ */
10
+ function getNode(input, pathKeys) {
11
+ let current = input;
12
+ const visitedNodes = [];
13
+ for (const key of pathKeys) {
14
+ visitedNodes.push(key);
15
+ if ((0, isAnyObjectWith_js_1.isAnyObjectWith)(current, key)) {
16
+ current = current[key];
17
+ }
18
+ else {
19
+ return {
20
+ value: undefined,
21
+ found: false,
22
+ visitedNodes,
23
+ };
24
+ }
25
+ }
26
+ return {
27
+ value: current,
28
+ found: true,
29
+ visitedNodes,
30
+ };
31
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -28,6 +28,7 @@ __exportStar(require("./nullable.js"), exports);
28
28
  __exportStar(require("./nullish.js"), exports);
29
29
  __exportStar(require("./optional.js"), exports);
30
30
  __exportStar(require("./range.js"), exports);
31
+ __exportStar(require("./record.js"), exports);
31
32
  __exportStar(require("./shape.js"), exports);
32
33
  __exportStar(require("./simple.js"), exports);
33
34
  __exportStar(require("./stringLength.js"), exports);
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.record = void 0;
4
+ const isObject_js_1 = require("../isObject.js");
5
+ const record = (keyPredicate, valuePredicate) => {
6
+ return (input) => (0, isObject_js_1.isObject)(input)
7
+ ? Reflect.ownKeys(input).every((key) => keyPredicate(key) && valuePredicate(Reflect.get(input, key)))
8
+ : false;
9
+ };
10
+ exports.record = record;
@@ -3,15 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isAnyObjectWith = void 0;
4
4
  const isAnyObject_js_1 = require("./isAnyObject.js");
5
5
  /**
6
- * Determine if `input` is an object and has the provided properties.
6
+ * Determine if `input` is an object and has the provided property or properties.
7
7
  *
8
8
  * @example
9
9
  * ```ts
10
+ * isAnyObjectWith({ a: 1 }, 'a'); // true
11
+ * isAnyObjectWith({ a: 1 }, 'b'); // false
10
12
  * isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
11
13
  * isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
14
+ * isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
15
+ * isAnyObjectWith(['a'], 0); // true (arrays are objects)
16
+ * isAnyObjectWith(null, 'a'); // false
12
17
  * ```
13
18
  */
14
19
  const isAnyObjectWith = (input, properties) => {
15
- return (0, isAnyObject_js_1.isAnyObject)(input) && properties.every((property) => property in input);
20
+ return ((0, isAnyObject_js_1.isAnyObject)(input) &&
21
+ (Array.isArray(properties) ? properties.every((property) => property in input) : properties in input));
16
22
  };
17
23
  exports.isAnyObjectWith = isAnyObjectWith;
package/dist/cjs/set.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.set = set;
4
+ const assertIsObject_js_1 = require("./assertion/assertIsObject.js");
5
+ const getNode_js_1 = require("./internal/getNode.js");
4
6
  const pathParts_js_1 = require("./pathParts.js");
5
7
  const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
6
8
  /**
@@ -26,18 +28,10 @@ function set(input, path, value) {
26
28
  if (typeof lastPart === 'undefined') {
27
29
  throw new Error('Path must have at least one part');
28
30
  }
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
- let current = input;
31
- const useParts = [];
32
- // get a reference to the last node that will be updated
33
- for (const part of parts) {
34
- useParts.push(part);
35
- if (typeof current === 'object' && current !== null && part in current) {
36
- current = current[part];
37
- }
38
- else {
39
- throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
40
- }
31
+ const result = (0, getNode_js_1.getNode)(input, parts);
32
+ if (!result.found) {
33
+ throw new Error(`Path "${result.visitedNodes.join('.')}" does not exist in the input object`);
41
34
  }
42
- return (current[lastPart] = value);
35
+ (0, assertIsObject_js_1.assertIsObject)(result.value);
36
+ return (result.value[lastPart] = value);
43
37
  }
package/dist/mjs/get.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { getNode } from './internal/getNode.js';
1
2
  import { pathParts } from './pathParts.js';
2
3
  /**
3
4
  * Get the value at `path` within `input`.
@@ -8,18 +9,5 @@ import { pathParts } from './pathParts.js';
8
9
  * ```
9
10
  */
10
11
  export function get(input, path) {
11
- if (path === '') {
12
- return input;
13
- }
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- let current = input;
16
- for (const part of pathParts(path)) {
17
- if (typeof current === 'object' && current !== null && part in current) {
18
- current = current[part];
19
- }
20
- else {
21
- return;
22
- }
23
- }
24
- return current;
12
+ return path === '' ? input : getNode(input, pathParts(path)).value;
25
13
  }
package/dist/mjs/has.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { getNode } from './internal/getNode.js';
1
2
  import { pathParts } from './pathParts.js';
2
3
  import { isAnyObject } from './predicate/isAnyObject.js';
3
4
  /**
@@ -9,21 +10,8 @@ import { isAnyObject } from './predicate/isAnyObject.js';
9
10
  * ```
10
11
  */
11
12
  export function has(input, path) {
12
- if (!isAnyObject(input)) {
13
+ if (!isAnyObject(input) || path === '') {
13
14
  return false;
14
15
  }
15
- if (path === '') {
16
- return false;
17
- }
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- let current = input;
20
- for (const part of pathParts(path)) {
21
- if (typeof current === 'object' && current !== null && part in current) {
22
- current = current[part];
23
- }
24
- else {
25
- return false;
26
- }
27
- }
28
- return true;
16
+ return getNode(input, pathParts(path)).found;
29
17
  }
@@ -0,0 +1 @@
1
+ export const hasSameOrigin = (left, right) => new URL(left).origin === new URL(right).origin;
package/dist/mjs/index.js CHANGED
@@ -46,6 +46,7 @@ export * from './getType.js';
46
46
  export * from './graphemeLength.js';
47
47
  export * from './has.js';
48
48
  export * from './hasAdditionalProperties.js';
49
+ export * from './hasSameOrigin.js';
49
50
  export * from './indent.js';
50
51
  export * from './inRange.js';
51
52
  export * from './isEmpty.js';
@@ -0,0 +1,28 @@
1
+ import { isAnyObjectWith } from '../predicate/isAnyObjectWith.js';
2
+ /**
3
+ * Retrieves the value of a nested property from an object based on a sequence of keys.
4
+ *
5
+ * @internal
6
+ */
7
+ export function getNode(input, pathKeys) {
8
+ let current = input;
9
+ const visitedNodes = [];
10
+ for (const key of pathKeys) {
11
+ visitedNodes.push(key);
12
+ if (isAnyObjectWith(current, key)) {
13
+ current = current[key];
14
+ }
15
+ else {
16
+ return {
17
+ value: undefined,
18
+ found: false,
19
+ visitedNodes,
20
+ };
21
+ }
22
+ }
23
+ return {
24
+ value: current,
25
+ found: true,
26
+ visitedNodes,
27
+ };
28
+ }
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -12,6 +12,7 @@ export * from './nullable.js';
12
12
  export * from './nullish.js';
13
13
  export * from './optional.js';
14
14
  export * from './range.js';
15
+ export * from './record.js';
15
16
  export * from './shape.js';
16
17
  export * from './simple.js';
17
18
  export * from './stringLength.js';
@@ -0,0 +1,6 @@
1
+ import { isObject } from '../isObject.js';
2
+ export const record = (keyPredicate, valuePredicate) => {
3
+ return (input) => isObject(input)
4
+ ? Reflect.ownKeys(input).every((key) => keyPredicate(key) && valuePredicate(Reflect.get(input, key)))
5
+ : false;
6
+ };
@@ -1,13 +1,19 @@
1
1
  import { isAnyObject } from './isAnyObject.js';
2
2
  /**
3
- * Determine if `input` is an object and has the provided properties.
3
+ * Determine if `input` is an object and has the provided property or properties.
4
4
  *
5
5
  * @example
6
6
  * ```ts
7
+ * isAnyObjectWith({ a: 1 }, 'a'); // true
8
+ * isAnyObjectWith({ a: 1 }, 'b'); // false
7
9
  * isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
8
10
  * isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
11
+ * isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
12
+ * isAnyObjectWith(['a'], 0); // true (arrays are objects)
13
+ * isAnyObjectWith(null, 'a'); // false
9
14
  * ```
10
15
  */
11
16
  export const isAnyObjectWith = (input, properties) => {
12
- return isAnyObject(input) && properties.every((property) => property in input);
17
+ return (isAnyObject(input) &&
18
+ (Array.isArray(properties) ? properties.every((property) => property in input) : properties in input));
13
19
  };
package/dist/mjs/set.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { assertIsObject } from './assertion/assertIsObject.js';
2
+ import { getNode } from './internal/getNode.js';
1
3
  import { pathParts } from './pathParts.js';
2
4
  const prototypePollutionPattern = /\b(__proto__|constructor|prototype)\b/;
3
5
  /**
@@ -23,18 +25,10 @@ export function set(input, path, value) {
23
25
  if (typeof lastPart === 'undefined') {
24
26
  throw new Error('Path must have at least one part');
25
27
  }
26
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
- let current = input;
28
- const useParts = [];
29
- // get a reference to the last node that will be updated
30
- for (const part of parts) {
31
- useParts.push(part);
32
- if (typeof current === 'object' && current !== null && part in current) {
33
- current = current[part];
34
- }
35
- else {
36
- throw new Error(`Path "${useParts.join('.')}" does not exist in the input object`);
37
- }
28
+ const result = getNode(input, parts);
29
+ if (!result.found) {
30
+ throw new Error(`Path "${result.visitedNodes.join('.')}" does not exist in the input object`);
38
31
  }
39
- return (current[lastPart] = value);
32
+ assertIsObject(result.value);
33
+ return (result.value[lastPart] = value);
40
34
  }
@@ -0,0 +1 @@
1
+ export declare const hasSameOrigin: (left: string | URL, right: string | URL) => boolean;
@@ -44,6 +44,7 @@ export * from './getType.js';
44
44
  export * from './graphemeLength.js';
45
45
  export * from './has.js';
46
46
  export * from './hasAdditionalProperties.js';
47
+ export * from './hasSameOrigin.js';
47
48
  export * from './indent.js';
48
49
  export * from './inRange.js';
49
50
  export * from './isEmpty.js';
@@ -0,0 +1,15 @@
1
+ export type GetNodeResult = {
2
+ value: unknown;
3
+ found: true;
4
+ visitedNodes: PropertyKey[];
5
+ } | {
6
+ value: undefined;
7
+ found: false;
8
+ visitedNodes: PropertyKey[];
9
+ };
10
+ /**
11
+ * Retrieves the value of a nested property from an object based on a sequence of keys.
12
+ *
13
+ * @internal
14
+ */
15
+ export declare function getNode<Input extends object>(input: Input, pathKeys: Iterable<PropertyKey>): GetNodeResult;
@@ -12,6 +12,7 @@ export * from './nullable.js';
12
12
  export * from './nullish.js';
13
13
  export * from './optional.js';
14
14
  export * from './range.js';
15
+ export * from './record.js';
15
16
  export * from './shape.js';
16
17
  export * from './simple.js';
17
18
  export * from './stringLength.js';
@@ -0,0 +1,2 @@
1
+ import type { TypePredicateFn } from '../../types/functions.js';
2
+ export declare const record: <Key extends string | symbol, Value>(keyPredicate: TypePredicateFn<Key>, valuePredicate: TypePredicateFn<Value>) => TypePredicateFn<Record<Key, Value>>;
@@ -1,10 +1,15 @@
1
1
  /**
2
- * Determine if `input` is an object and has the provided properties.
2
+ * Determine if `input` is an object and has the provided property or properties.
3
3
  *
4
4
  * @example
5
5
  * ```ts
6
+ * isAnyObjectWith({ a: 1 }, 'a'); // true
7
+ * isAnyObjectWith({ a: 1 }, 'b'); // false
6
8
  * isAnyObjectWith({ a: 1, b: 2 }, ['a', 'b']); // true
7
9
  * isAnyObjectWith({ a: 1 }, ['a', 'b']); // false
10
+ * isAnyObjectWith({}, 'toString'); // true (inherited properties are included)
11
+ * isAnyObjectWith(['a'], 0); // true (arrays are objects)
12
+ * isAnyObjectWith(null, 'a'); // false
8
13
  * ```
9
14
  */
10
- export declare const isAnyObjectWith: <T, P extends PropertyKey>(input: T, properties: P[]) => input is T & Record<P, unknown>;
15
+ export declare const isAnyObjectWith: <T, P extends PropertyKey>(input: T, properties: P | P[]) => input is T & Record<P, unknown>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webdeveric/utils",
3
3
  "description": "Just another collection of utilities.",
4
- "version": "1.5.0",
4
+ "version": "1.6.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -22,6 +22,20 @@
22
22
  "engines": {
23
23
  "node": ">=18.0.0"
24
24
  },
25
+ "devEngines": {
26
+ "packageManager": {
27
+ "name": "pnpm",
28
+ "version": ">=12.0.0 <13.0.0",
29
+ "onFail": "warn"
30
+ },
31
+ "runtime": [
32
+ {
33
+ "name": "node",
34
+ "version": "^26.10.0",
35
+ "onFail": "download"
36
+ }
37
+ ]
38
+ },
25
39
  "exports": {
26
40
  ".": {
27
41
  "types": "./dist/types/index.d.ts",
@@ -66,7 +80,6 @@
66
80
  "url": "https://github.com/webdeveric/utils/issues"
67
81
  },
68
82
  "homepage": "https://github.com/webdeveric/utils/#readme",
69
- "packageManager": "pnpm@11.25.0+sha512.5cde925b4f075f725eb71fbae18a42ffe784524789f19b61c731cb8721ec28aaee160e01a8d5af4fedb2a42cdbf300efe23db356b0d4a17b4d63e11f8ab7c956",
70
83
  "scripts": {
71
84
  "clean": "rimraf ./dist/ ./cache/ ./coverage/",
72
85
  "prebuild": "pnpm clean",
@@ -78,7 +91,7 @@
78
91
  "bench": "vitest bench",
79
92
  "test": "vitest --typecheck",
80
93
  "coverage": "vitest run --coverage --typecheck",
81
- "validate": "validate-package-exports --check",
94
+ "validate": "validate-package-exports --check --strict-module-type",
82
95
  "spellcheck": "cspell './{.github,src,bench,test}/**/*.{ts,json}' './*.{md,mjs,mts}' './package.json' --no-progress",
83
96
  "prepublishOnly": "pnpm typecheck && pnpm spellcheck && pnpm lint && pnpm coverage && pnpm build",
84
97
  "format": "prettier --write ./*.{js,json,md} ./src/ ./bench/ ./test/",
@@ -86,31 +99,31 @@
86
99
  },
87
100
  "prettier": "@webdeveric/prettier-config",
88
101
  "devDependencies": {
89
- "@commitlint/config-conventional": "^21.2.2",
90
- "@commitlint/types": "^21.2.0",
91
- "@types/node": "^24.13.3",
102
+ "@commitlint/config-conventional": "^21.2.3",
103
+ "@commitlint/types": "^21.2.3",
104
+ "@types/node": "^24.19.0",
92
105
  "@typescript/native": "npm:typescript@^7.0.2",
93
106
  "@vitest/coverage-v8": "^4.1.11",
94
107
  "@webdeveric/eslint-config-ts": "^0.12.1",
95
108
  "@webdeveric/prettier-config": "^0.4.0",
96
109
  "@webdeveric/ts-data-structures": "^1.0.1",
97
- "commitlint": "^21.2.2",
98
- "commitlint-plugin-cspell": "^0.9.4",
110
+ "commitlint": "^21.2.3",
111
+ "commitlint-plugin-cspell": "^0.9.5",
99
112
  "conventional-changelog-conventionalcommits": "^9.3.1",
100
- "cspell": "^10.1.1",
113
+ "cspell": "^10.3.5",
101
114
  "eslint": "^8.57.1",
102
115
  "eslint-config-prettier": "^10.1.8",
103
116
  "eslint-import-resolver-typescript": "^4.4.5",
104
117
  "eslint-plugin-import": "^2.32.0",
105
118
  "husky": "^9.1.7",
106
- "jsdom": "^30.0.1",
107
- "lint-staged": "^17.4.1",
108
- "prettier": "^3.9.6",
119
+ "jsdom": "^30.1.1",
120
+ "lint-staged": "^17.6.0",
121
+ "prettier": "^3.9.9",
109
122
  "rimraf": "^6.1.3",
110
123
  "semantic-release": "^25.0.9",
111
124
  "typedoc": "^0.28.20",
112
125
  "typescript": "npm:@typescript/typescript6@^6.0.2",
113
- "validate-package-exports": "^1.4.0",
126
+ "validate-package-exports": "^1.5.0",
114
127
  "vitest": "^4.1.11"
115
128
  }
116
129
  }