@webdeveric/utils 1.2.0 → 1.3.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.
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineProperties = defineProperties;
4
+ /**
5
+ * Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const user = defineProperties({ name: 'Test' }, {
10
+ * age: { value: 42 },
11
+ * isAdmin: { get: () => false },
12
+ * title: {
13
+ * writable: true,
14
+ * },
15
+ * });
16
+ * // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
17
+ * ```
18
+ */
19
+ function defineProperties(input, attributeMap) {
20
+ return Object.defineProperties(input, attributeMap);
21
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineProperty = defineProperty;
4
+ /**
5
+ * Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
10
+ * // user: { name: string, readonly age: number }
11
+ * ```
12
+ *
13
+ * ```ts
14
+ * const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
15
+ * // user: { name: string, title: unknown }
16
+ * ```
17
+ */
18
+ function defineProperty(input, property, attributes) {
19
+ return Object.defineProperty(input, property, attributes);
20
+ }
@@ -14,7 +14,7 @@ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
14
14
  */
15
15
  function graphemeLength(input) {
16
16
  let count = 0;
17
- // eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-unused-vars
17
+ // eslint-disable-next-line @typescript-eslint/naming-convention
18
18
  for (const _ of segmenter.segment(input)) {
19
19
  count++;
20
20
  }
package/dist/cjs/index.js CHANGED
@@ -37,6 +37,8 @@ __exportStar(require("./counter.js"), exports);
37
37
  __exportStar(require("./dedent.js"), exports);
38
38
  __exportStar(require("./deepDecodeURI.js"), exports);
39
39
  __exportStar(require("./deepFreeze.js"), exports);
40
+ __exportStar(require("./defineProperties.js"), exports);
41
+ __exportStar(require("./defineProperty.js"), exports);
40
42
  __exportStar(require("./delay.js"), exports);
41
43
  __exportStar(require("./delayAnimationFrame.js"), exports);
42
44
  __exportStar(require("./delayAnimationFrames.js"), exports);
@@ -14,6 +14,6 @@ exports.assume = void 0;
14
14
  * assume<string>(42); // true (no runtime check is performed)
15
15
  * ```
16
16
  */
17
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
18
  const assume = (_input, ..._args) => true;
19
19
  exports.assume = assume;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const user = defineProperties({ name: 'Test' }, {
7
+ * age: { value: 42 },
8
+ * isAdmin: { get: () => false },
9
+ * title: {
10
+ * writable: true,
11
+ * },
12
+ * });
13
+ * // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
14
+ * ```
15
+ */
16
+ export function defineProperties(input, attributeMap) {
17
+ return Object.defineProperties(input, attributeMap);
18
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
7
+ * // user: { name: string, readonly age: number }
8
+ * ```
9
+ *
10
+ * ```ts
11
+ * const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
12
+ * // user: { name: string, title: unknown }
13
+ * ```
14
+ */
15
+ export function defineProperty(input, property, attributes) {
16
+ return Object.defineProperty(input, property, attributes);
17
+ }
@@ -11,7 +11,7 @@ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
11
11
  */
12
12
  export function graphemeLength(input) {
13
13
  let count = 0;
14
- // eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-unused-vars
14
+ // eslint-disable-next-line @typescript-eslint/naming-convention
15
15
  for (const _ of segmenter.segment(input)) {
16
16
  count++;
17
17
  }
package/dist/mjs/index.js CHANGED
@@ -21,6 +21,8 @@ export * from './counter.js';
21
21
  export * from './dedent.js';
22
22
  export * from './deepDecodeURI.js';
23
23
  export * from './deepFreeze.js';
24
+ export * from './defineProperties.js';
25
+ export * from './defineProperty.js';
24
26
  export * from './delay.js';
25
27
  export * from './delayAnimationFrame.js';
26
28
  export * from './delayAnimationFrames.js';
@@ -11,5 +11,5 @@
11
11
  * assume<string>(42); // true (no runtime check is performed)
12
12
  * ```
13
13
  */
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
15
  export const assume = (_input, ..._args) => true;
@@ -0,0 +1,21 @@
1
+ import type { ApplyPropertyAttribute, GetPropertyRecordFromAttribute, PropertyAttributeMap, RetainMutability } from './types/objects.js';
2
+ import type { Pretty, UnionToIntersection } from './types/utils.js';
3
+ export type WithPropertyMap<Type, AttributeMap extends PropertyAttributeMap> = Omit<Type, keyof AttributeMap> & UnionToIntersection<{
4
+ [Property in keyof AttributeMap]: ApplyPropertyAttribute<GetPropertyRecordFromAttribute<Type, Property, AttributeMap[Property]>, AttributeMap[Property], RetainMutability<Type, Property>>;
5
+ }[keyof AttributeMap]>;
6
+ /**
7
+ * Type-safe wrapper around `Object.defineProperties()` that reflects the new properties in the returned type.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const user = defineProperties({ name: 'Test' }, {
12
+ * age: { value: 42 },
13
+ * isAdmin: { get: () => false },
14
+ * title: {
15
+ * writable: true,
16
+ * },
17
+ * });
18
+ * // user: { name: string, readonly age: number, readonly isAdmin: boolean, title: unknown }
19
+ * ```
20
+ */
21
+ export declare function defineProperties<Type, AttributeMap extends PropertyAttributeMap>(input: Type, attributeMap: AttributeMap): Pretty<WithPropertyMap<Type, AttributeMap>>;
@@ -0,0 +1,18 @@
1
+ import type { ApplyPropertyAttribute, GetPropertyRecordFromAttribute, PropertyAttribute, RetainMutability } from './types/objects.js';
2
+ import type { Pretty } from './types/utils.js';
3
+ export type WithProperty<Type, Property extends PropertyKey, Attribute extends PropertyAttribute> = Omit<Type, Property> & ApplyPropertyAttribute<GetPropertyRecordFromAttribute<Type, Property, Attribute>, Attribute, RetainMutability<Type, Property>>;
4
+ /**
5
+ * Type-safe wrapper around `Object.defineProperty` that reflects the new property in the returned type.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const user = defineProperty({ name: 'Test' }, 'age', { value: 42 });
10
+ * // user: { name: string, readonly age: number }
11
+ * ```
12
+ *
13
+ * ```ts
14
+ * const user = defineProperty({ name: 'Test' }, 'title', { writable: true });
15
+ * // user: { name: string, title: unknown }
16
+ * ```
17
+ */
18
+ export declare function defineProperty<Type, Property extends PropertyKey, Attributes extends PropertyAttribute>(input: Type, property: Property, attributes: Attributes): Pretty<WithProperty<Type, Property, Attributes>>;
@@ -19,6 +19,8 @@ export * from './counter.js';
19
19
  export * from './dedent.js';
20
20
  export * from './deepDecodeURI.js';
21
21
  export * from './deepFreeze.js';
22
+ export * from './defineProperties.js';
23
+ export * from './defineProperty.js';
22
24
  export * from './delay.js';
23
25
  export * from './delayAnimationFrame.js';
24
26
  export * from './delayAnimationFrames.js';
@@ -3,7 +3,7 @@ import type { Primitive } from './common.js';
3
3
  import type { UnknownRecord } from './records.js';
4
4
  import type { AutoCompletableString, CamelCase } from './strings.js';
5
5
  import type { KeyValueTuple } from './tuples.js';
6
- import type { CanBeUndefined, IfNever, Pretty } from './utils.js';
6
+ import type { CanBeUndefined, IfNever, IsReadonlyKey, Pretty } from './utils.js';
7
7
  export type Assign<Target, Source> = IfNever<Target, Source, Omit<Target, keyof (Target | Source)> & Source>;
8
8
  export type Entries<T extends object> = {
9
9
  [P in keyof T]: KeyValueTuple<P, T[P]>;
@@ -112,3 +112,35 @@ export type MergeObjects<Left, Right> = {
112
112
  export type MergeArrayWithObject<Left extends unknown[], Right extends UnknownRecord> = Left & {
113
113
  [Key in keyof Right]: Key extends keyof Left ? Merge<Left[Key & keyof Left], Right[Key]> : Key extends `${infer Index extends number}` ? Index extends keyof Left ? Merge<Left[Index], Right[Key]> : Right[Key] : Right[Key];
114
114
  };
115
+ export type GetPropertyType<Type, Property extends PropertyKey, Default = unknown> = Property extends keyof Type ? Type[Property] : Default;
116
+ /**
117
+ * Whether redefining `Property` on `Type` should keep the property mutable when
118
+ * the new attribute doesn't say either way. This mirrors `Object.defineProperty()`,
119
+ * which keeps a property's existing `writable` flag when it isn't part of the new
120
+ * descriptor and the property already exists.
121
+ */
122
+ export type RetainMutability<Type, Property extends PropertyKey> = Property extends keyof Type ? IsReadonlyKey<Type, Property> extends true ? false : true : false;
123
+ export type AnyThisType = ThisType<any>;
124
+ export type PropertyAttribute = PropertyDescriptor & AnyThisType;
125
+ export type PropertyAttributeMap = PropertyDescriptorMap & AnyThisType;
126
+ export type ApplyPropertyAttribute<Type, Attribute extends PropertyAttribute, Retain extends boolean = false> = Attribute extends {
127
+ writable: true;
128
+ } | {
129
+ set(value: never): void;
130
+ } ? Type : Attribute extends {
131
+ writable: false;
132
+ } ? Readonly<Type> : Retain extends true ? Type : Readonly<Type>;
133
+ export type GetTypeFromAttributes<Attribute extends PropertyAttribute, DefaultType = unknown> = Attribute extends {
134
+ value: infer Type;
135
+ } | {
136
+ get(): infer Type;
137
+ } | {
138
+ set(value: infer Type): void;
139
+ } ? Type : DefaultType;
140
+ export type GetPropertyRecordFromAttribute<Type, Property extends PropertyKey, Attribute extends PropertyAttribute, DefaultType = GetPropertyType<Type, Property>> = Attribute extends {
141
+ value: infer PropertyType;
142
+ } | {
143
+ get(): infer PropertyType;
144
+ } | {
145
+ set(value: infer PropertyType): void;
146
+ } ? Record<Property, PropertyType> : Record<Property, DefaultType>;
@@ -4,6 +4,11 @@ export type IfDefined<Type, T, F> = undefined extends Type ? F : T;
4
4
  export type IfUnknown<Type, T, F> = unknown extends Type ? T : F;
5
5
  export type IfPromise<Type, T, F> = Type extends Promise<any> ? T : F;
6
6
  export type IfSame<Left, Right, T, F> = [Left] extends [Right] ? ([Right] extends [Left] ? T : F) : F;
7
+ /**
8
+ * Strict type equality check. Unlike `IfSame`, this distinguishes `readonly` modifiers,
9
+ * since ordinary mutual `extends` checks ignore them.
10
+ */
11
+ export type Equals<Left, Right> = (<Type>() => Type extends Left ? 1 : 2) extends <Type>() => (Type extends Right ? 1 : 2) ? true : false;
7
12
  export type NotPromise<Type> = Type extends Promise<any> ? never : Type;
8
13
  export type CanBeUndefined<Type, T, F> = Type | undefined extends Type ? T : F;
9
14
  export type ReturnTypeDefault<Func, Default = undefined> = Func extends (...args: any[]) => infer R ? R : Default;
@@ -14,6 +19,9 @@ export type Writable<Type> = {
14
19
  -readonly [Key in keyof Type]: Type[Key];
15
20
  };
16
21
  export type Unwritable<Type> = Type extends Writable<infer Inner> ? Readonly<Inner> : Readonly<Type>;
22
+ export type IsReadonlyKey<Type, Keys extends keyof Type> = Equals<Pick<Type, Keys>, {
23
+ -readonly [Property in Keys]: Type[Property];
24
+ }> extends true ? false : true;
17
25
  export type GetIndex<Type> = {
18
26
  [Key in keyof Type as symbol extends Key ? Key : string extends Key ? Key : number extends Key ? Key : never]: Type[Key];
19
27
  };
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.2.0",
4
+ "version": "1.3.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -66,14 +66,14 @@
66
66
  "url": "https://github.com/webdeveric/utils/issues"
67
67
  },
68
68
  "homepage": "https://github.com/webdeveric/utils/#readme",
69
- "packageManager": "pnpm@11.15.0+sha512.266f8957a30d2be6e9468e5e66bcdedd35a794175f71b067ba8504d686cce1d0c0f429b33c323c3c569ad4891e667574a49ff71d1b89a22cc66f13c65818c578",
69
+ "packageManager": "pnpm@11.19.0+sha512.7881f3ed590d472c4a955e2b88b2121791116066dcc88cbca3849ec9b60f1bbaa6d2ccb221fa91da4e1c65bef2bcbe379365aea7ac539c7bf86dedc3a1b22dce",
70
70
  "scripts": {
71
71
  "clean": "rimraf ./dist/ ./cache/ ./coverage/",
72
72
  "prebuild": "pnpm clean",
73
- "build": "tsgo --build tsconfig.cjs.json tsconfig.mjs.json --force",
73
+ "build": "tsc --build tsconfig.cjs.json tsconfig.mjs.json --force",
74
74
  "build:docs": "typedoc",
75
75
  "postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json && echo '{\"type\":\"module\"}' > ./dist/mjs/package.json && pnpm validate",
76
- "typecheck": "tsgo --build --verbose --noEmit",
76
+ "typecheck": "tsc --build --verbose --noEmit",
77
77
  "lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./bench/ ./test/ --ext .ts,.mjs,.cjs",
78
78
  "bench": "vitest bench",
79
79
  "test": "vitest --typecheck",
@@ -89,13 +89,13 @@
89
89
  "@commitlint/config-conventional": "^21.2.0",
90
90
  "@commitlint/types": "^21.2.0",
91
91
  "@types/node": "^24.13.3",
92
- "@typescript/native-preview": "7.0.0-dev.20260707.2",
92
+ "@typescript/native": "npm:typescript@^7.0.2",
93
93
  "@vitest/coverage-v8": "^4.1.10",
94
94
  "@webdeveric/eslint-config-ts": "^0.12.1",
95
95
  "@webdeveric/prettier-config": "^0.4.0",
96
96
  "@webdeveric/ts-data-structures": "^1.0.1",
97
97
  "commitlint": "^21.2.1",
98
- "commitlint-plugin-cspell": "^0.9.3",
98
+ "commitlint-plugin-cspell": "^0.9.4",
99
99
  "conventional-changelog-conventionalcommits": "^9.3.1",
100
100
  "cspell": "^10.0.1",
101
101
  "eslint": "^8.57.1",
@@ -103,13 +103,14 @@
103
103
  "eslint-import-resolver-typescript": "^4.4.5",
104
104
  "eslint-plugin-import": "^2.32.0",
105
105
  "husky": "^9.1.7",
106
- "jsdom": "^29.1.1",
107
- "lint-staged": "^17.0.8",
108
- "prettier": "^3.9.5",
106
+ "jsdom": "^30.0.1",
107
+ "lint-staged": "^17.3.0",
108
+ "prettier": "^3.9.6",
109
109
  "rimraf": "^6.1.3",
110
- "semantic-release": "^25.0.7",
110
+ "semantic-release": "^25.0.8",
111
111
  "typedoc": "^0.28.20",
112
- "validate-package-exports": "^1.3.1",
112
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
113
+ "validate-package-exports": "^1.3.3",
113
114
  "vitest": "^4.1.10"
114
115
  }
115
116
  }