@metamask-previews/type-utils 0.0.0-preview-e4c191e3a → 0.0.0-preview-a583980c5
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 +5 -0
- package/dist/array.d.ts +15 -0
- package/dist/array.d.ts.map +1 -0
- package/dist/array.js +11 -0
- package/dist/array.js.map +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -9
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +7 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +2 -0
- package/dist/json.js.map +1 -0
- package/dist/object.d.ts +66 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +66 -0
- package/dist/object.js.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,4 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add `Json`, `Mutable`, `NonEmptyArray`, `PartialOrAbsent`, and `RuntimeObject` types, moved from `@metamask/utils` ([#10908](https://github.com/MetaMask/core/pull/10908))
|
|
13
|
+
- Add `getKnownPropertyNames`, `hasProperty`, `isNullOrUndefined`, `isObject`, and `isPlainObject` functions, moved from `@metamask/utils` ([#10908](https://github.com/MetaMask/core/pull/10908))
|
|
14
|
+
|
|
10
15
|
[Unreleased]: https://github.com/MetaMask/core/
|
package/dist/array.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Like {@link Array}, but always non-empty.
|
|
3
|
+
*
|
|
4
|
+
* @template Element - The non-empty array member type.
|
|
5
|
+
*/
|
|
6
|
+
export type NonEmptyArray<Element> = [Element, ...Element[]];
|
|
7
|
+
/**
|
|
8
|
+
* A {@link NonEmptyArray} type guard.
|
|
9
|
+
*
|
|
10
|
+
* @template Element - The non-empty array member type.
|
|
11
|
+
* @param value - The value to check.
|
|
12
|
+
* @returns Whether the value is a non-empty array.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isNonEmptyArray<Element>(value: Element[]): value is NonEmptyArray<Element>;
|
|
15
|
+
//# sourceMappingURL=array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AAE7D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EACrC,KAAK,EAAE,OAAO,EAAE,GACf,KAAK,IAAI,aAAa,CAAC,OAAO,CAAC,CAEjC"}
|
package/dist/array.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link NonEmptyArray} type guard.
|
|
3
|
+
*
|
|
4
|
+
* @template Element - The non-empty array member type.
|
|
5
|
+
* @param value - The value to check.
|
|
6
|
+
* @returns Whether the value is a non-empty array.
|
|
7
|
+
*/
|
|
8
|
+
export function isNonEmptyArray(value) {
|
|
9
|
+
return Array.isArray(value) && value.length > 0;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC","sourcesContent":["/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export default function greeter(name: string): string;
|
|
1
|
+
export type { NonEmptyArray } from './array.js';
|
|
2
|
+
export { isNonEmptyArray } from './array.js';
|
|
3
|
+
export type { Json } from './json.js';
|
|
4
|
+
export type { Mutable, PartialOrAbsent, RuntimeObject } from './object.js';
|
|
5
|
+
export { getKnownPropertyNames, hasProperty, isNullOrUndefined, isObject, isPlainObject, } from './object.js';
|
|
6
|
+
export type { PublicInterface } from './types.js';
|
|
8
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,YAAY,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,YAAY,EAAE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @param name - The name to greet.
|
|
5
|
-
* @returns The greeting.
|
|
6
|
-
*/
|
|
7
|
-
export default function greeter(name) {
|
|
8
|
-
return `Hello, ${name}!`;
|
|
9
|
-
}
|
|
1
|
+
export { isNonEmptyArray } from './array.js';
|
|
2
|
+
export { getKnownPropertyNames, hasProperty, isNullOrUndefined, isObject, isPlainObject, } from './object.js';
|
|
10
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAG7C,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC","sourcesContent":["export type { NonEmptyArray } from './array.js';\nexport { isNonEmptyArray } from './array.js';\nexport type { Json } from './json.js';\nexport type { Mutable, PartialOrAbsent, RuntimeObject } from './object.js';\nexport {\n getKnownPropertyNames,\n hasProperty,\n isNullOrUndefined,\n isObject,\n isPlainObject,\n} from './object.js';\nexport type { PublicInterface } from './types.js';\n"]}
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,IAAI,GACZ,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,IAAI,EAAE,GACN;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC"}
|
package/dist/json.js
ADDED
package/dist/json.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n"]}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JavaScript object that is not `null`, a function, or an array.
|
|
3
|
+
*/
|
|
4
|
+
export type RuntimeObject = Record<PropertyKey, unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* Makes every specified property of the specified object type mutable.
|
|
7
|
+
*
|
|
8
|
+
* @template ObjectValue - The object whose readonly properties to make mutable.
|
|
9
|
+
* @template TargetKey - The property key(s) to make mutable.
|
|
10
|
+
*/
|
|
11
|
+
export type Mutable<ObjectValue extends Record<string, unknown>, TargetKey extends keyof ObjectValue> = {
|
|
12
|
+
-readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];
|
|
13
|
+
} & {
|
|
14
|
+
[Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Useful for representing some value that _might_ be present and / or complete.
|
|
18
|
+
*
|
|
19
|
+
* @template Value - The value that might be present or complete.
|
|
20
|
+
*/
|
|
21
|
+
export type PartialOrAbsent<Value> = Partial<Value> | null | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Type guard for "nullishness".
|
|
24
|
+
*
|
|
25
|
+
* @param value - Any value.
|
|
26
|
+
* @returns `true` if the value is null or undefined, `false` otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isNullOrUndefined(value: unknown): value is null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* A type guard for {@link RuntimeObject}.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to check.
|
|
33
|
+
* @returns Whether the specified value has a runtime type of `object` and is
|
|
34
|
+
* neither `null` nor an `Array`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function isObject(value: unknown): value is RuntimeObject;
|
|
37
|
+
/**
|
|
38
|
+
* Check if the value is plain object.
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value to be checked.
|
|
41
|
+
* @returns True if an object is the plain JavaScript object,
|
|
42
|
+
* false if the object is not plain (e.g. function).
|
|
43
|
+
*/
|
|
44
|
+
export declare function isPlainObject(value: unknown): value is RuntimeObject;
|
|
45
|
+
/**
|
|
46
|
+
* A type guard for ensuring an object has a property.
|
|
47
|
+
*
|
|
48
|
+
* @param objectToCheck - The object to check.
|
|
49
|
+
* @param name - The property name to check for.
|
|
50
|
+
* @returns Whether the specified object has an own property with the specified
|
|
51
|
+
* name, regardless of whether it is enumerable or not.
|
|
52
|
+
*/
|
|
53
|
+
export declare const hasProperty: <ObjectToCheck extends Object, Property extends PropertyKey>(objectToCheck: ObjectToCheck, name: Property) => objectToCheck is ObjectToCheck & Record<Property, Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* `Object.getOwnPropertyNames()` is intentionally generic: it returns the
|
|
56
|
+
* immediate property names of an object, but it cannot make guarantees about
|
|
57
|
+
* the contents of that object, so the type of the property names is merely
|
|
58
|
+
* `string[]`. While this is technically accurate, it is also unnecessary if we
|
|
59
|
+
* have an object with a type that we own (such as an enum).
|
|
60
|
+
*
|
|
61
|
+
* @param object - The plain object.
|
|
62
|
+
* @returns The own property names of the object which are assigned a type
|
|
63
|
+
* derived from the object itself.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getKnownPropertyNames<Key extends PropertyKey>(object: Partial<Record<Key, unknown>>): Key[];
|
|
66
|
+
//# sourceMappingURL=object.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;AAEzD;;;;;GAKG;AACH,MAAM,MAAM,OAAO,CACjB,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3C,SAAS,SAAS,MAAM,WAAW,IACjC;IACF,CAAC,UAAU,GAAG,IAAI,MAAM,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;CACxE,GAAG;KACD,GAAG,IAAI,MAAM,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC;CAC9D,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,eAAe,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;AAEvE;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,IAAI,GAAG,SAAS,CAE3E;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAE/D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAgBpE;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,GAEtB,aAAa,SAAS,MAAM,EAC5B,QAAQ,SAAS,WAAW,iBAEb,aAAa,QACtB,QAAQ,KACb,aAAa,IAAI,aAAa,GAC/B,MAAM,CACJ,QAAQ,EACR,QAAQ,SAAS,MAAM,aAAa,GAAG,aAAa,CAAC,QAAQ,CAAC,GAAG,OAAO,CACtB,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,SAAS,WAAW,EAC3D,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,GACpC,GAAG,EAAE,CAEP"}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard for "nullishness".
|
|
3
|
+
*
|
|
4
|
+
* @param value - Any value.
|
|
5
|
+
* @returns `true` if the value is null or undefined, `false` otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export function isNullOrUndefined(value) {
|
|
8
|
+
return value === null || value === undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A type guard for {@link RuntimeObject}.
|
|
12
|
+
*
|
|
13
|
+
* @param value - The value to check.
|
|
14
|
+
* @returns Whether the specified value has a runtime type of `object` and is
|
|
15
|
+
* neither `null` nor an `Array`.
|
|
16
|
+
*/
|
|
17
|
+
export function isObject(value) {
|
|
18
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Check if the value is plain object.
|
|
22
|
+
*
|
|
23
|
+
* @param value - Value to be checked.
|
|
24
|
+
* @returns True if an object is the plain JavaScript object,
|
|
25
|
+
* false if the object is not plain (e.g. function).
|
|
26
|
+
*/
|
|
27
|
+
export function isPlainObject(value) {
|
|
28
|
+
if (typeof value !== 'object' || value === null) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
let proto = value;
|
|
33
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
34
|
+
// oxlint-disable-next-line typescript/no-unsafe-assignment
|
|
35
|
+
proto = Object.getPrototypeOf(proto);
|
|
36
|
+
}
|
|
37
|
+
return Object.getPrototypeOf(value) === proto;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A type guard for ensuring an object has a property.
|
|
45
|
+
*
|
|
46
|
+
* @param objectToCheck - The object to check.
|
|
47
|
+
* @param name - The property name to check for.
|
|
48
|
+
* @returns Whether the specified object has an own property with the specified
|
|
49
|
+
* name, regardless of whether it is enumerable or not.
|
|
50
|
+
*/
|
|
51
|
+
export const hasProperty = (objectToCheck, name) => Object.hasOwnProperty.call(objectToCheck, name);
|
|
52
|
+
/**
|
|
53
|
+
* `Object.getOwnPropertyNames()` is intentionally generic: it returns the
|
|
54
|
+
* immediate property names of an object, but it cannot make guarantees about
|
|
55
|
+
* the contents of that object, so the type of the property names is merely
|
|
56
|
+
* `string[]`. While this is technically accurate, it is also unnecessary if we
|
|
57
|
+
* have an object with a type that we own (such as an enum).
|
|
58
|
+
*
|
|
59
|
+
* @param object - The plain object.
|
|
60
|
+
* @returns The own property names of the object which are assigned a type
|
|
61
|
+
* derived from the object itself.
|
|
62
|
+
*/
|
|
63
|
+
export function getKnownPropertyNames(object) {
|
|
64
|
+
return Object.getOwnPropertyNames(object);
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AA2BA;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,IAAI,KAAK,GAAG,KAAK,CAAC;QAClB,OAAO,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;YAC7C,2DAA2D;YAC3D,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAKzB,aAA4B,EAC5B,IAAc,EAKZ,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,qBAAqB,CACnC,MAAqC;IAErC,OAAO,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAAU,CAAC;AACrD,CAAC","sourcesContent":["/**\n * A JavaScript object that is not `null`, a function, or an array.\n */\nexport type RuntimeObject = Record<PropertyKey, unknown>;\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue,\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} & {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n};\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n/**\n * Check if the value is plain object.\n *\n * @param value - Value to be checked.\n * @returns True if an object is the plain JavaScript object,\n * false if the object is not plain (e.g. function).\n */\nexport function isPlainObject(value: unknown): value is RuntimeObject {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n try {\n let proto = value;\n while (Object.getPrototypeOf(proto) !== null) {\n // oxlint-disable-next-line typescript/no-unsafe-assignment\n proto = Object.getPrototypeOf(proto);\n }\n\n return Object.getPrototypeOf(value) === proto;\n } catch {\n return false;\n }\n}\n\n/**\n * A type guard for ensuring an object has a property.\n *\n * @param objectToCheck - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = <\n // eslint-disable-next-line @typescript-eslint/no-wrapper-object-types -- `Object` is deliberate: it accepts boxed primitives, so callers can pass a string or number. Narrowing to `object` would be a breaking change.\n ObjectToCheck extends Object,\n Property extends PropertyKey,\n>(\n objectToCheck: ObjectToCheck,\n name: Property,\n): objectToCheck is ObjectToCheck &\n Record<\n Property,\n Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown\n > => Object.hasOwnProperty.call(objectToCheck, name);\n\n/**\n * `Object.getOwnPropertyNames()` is intentionally generic: it returns the\n * immediate property names of an object, but it cannot make guarantees about\n * the contents of that object, so the type of the property names is merely\n * `string[]`. While this is technically accurate, it is also unnecessary if we\n * have an object with a type that we own (such as an enum).\n *\n * @param object - The plain object.\n * @returns The own property names of the object which are assigned a type\n * derived from the object itself.\n */\nexport function getKnownPropertyNames<Key extends PropertyKey>(\n object: Partial<Record<Key, unknown>>,\n): Key[] {\n return Object.getOwnPropertyNames(object) as Key[];\n}\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a type representing the public interface of the given type. The
|
|
3
|
+
* returned type will have all public properties, but will omit private
|
|
4
|
+
* properties.
|
|
5
|
+
*
|
|
6
|
+
* @template Interface - The interface to return a public representation of.
|
|
7
|
+
*/
|
|
8
|
+
export type PublicInterface<Interface> = Pick<Interface, keyof Interface>;
|
|
9
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,EAAE,MAAM,SAAS,CAAC,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Get a type representing the public interface of the given type. The\n * returned type will have all public properties, but will omit private\n * properties.\n *\n * @template Interface - The interface to return a public representation of.\n */\nexport type PublicInterface<Interface> = Pick<Interface, keyof Interface>;\n"]}
|