@rimbu/base 0.8.2 → 0.9.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.
@@ -2,11 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RimbuError = exports.Entry = exports.Arr = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var Arr = tslib_1.__importStar(require("./arr"));
6
- exports.Arr = Arr;
7
- var Entry = tslib_1.__importStar(require("./entry"));
8
- exports.Entry = Entry;
9
- var RimbuError = tslib_1.__importStar(require("./rimbu-error"));
10
- exports.RimbuError = RimbuError;
5
+ exports.Arr = tslib_1.__importStar(require("./arr"));
6
+ exports.Entry = tslib_1.__importStar(require("./entry"));
7
+ exports.RimbuError = tslib_1.__importStar(require("./rimbu-error"));
8
+ tslib_1.__exportStar(require("./plain-object"), exports);
11
9
  tslib_1.__exportStar(require("./internal"), exports);
12
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,iDAA6B;AAIpB,kBAAG;AAHZ,qDAAiC;AAGnB,sBAAK;AAFnB,gEAA4C;AAEvB,gCAAU;AAE/B,qDAA2B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,qDAA6B;AAC7B,yDAAiC;AACjC,oEAA4C;AAC5C,yDAA+B;AAE/B,qDAA2B"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isIterable = exports.isPlainObj = void 0;
4
+ /**
5
+ * Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
6
+ * data object.
7
+ * @param obj - the object to check
8
+ * @returns true if the given object is a pure data object
9
+ * @note does not check whether a record's properties are not functions
10
+ */
11
+ function isPlainObj(obj) {
12
+ if (undefined === obj ||
13
+ null === obj ||
14
+ typeof obj !== 'object' ||
15
+ (obj.constructor instanceof Function &&
16
+ obj.constructor.name !== 'Object') ||
17
+ Symbol.iterator in obj ||
18
+ Symbol.asyncIterator in obj) {
19
+ return false;
20
+ }
21
+ var prototype = Object.getPrototypeOf(obj);
22
+ return null === prototype || prototype === Object.prototype;
23
+ }
24
+ exports.isPlainObj = isPlainObj;
25
+ /**
26
+ * Returns true if the given object is Iterable
27
+ * @param obj - the object to check
28
+ */
29
+ function isIterable(obj) {
30
+ return obj !== null && typeof obj === 'object' && Symbol.iterator in obj;
31
+ }
32
+ exports.isIterable = isIterable;
33
+ //# sourceMappingURL=plain-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plain-object.js","sourceRoot":"","sources":["../../src/plain-object.ts"],"names":[],"mappings":";;;AAqCA;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,GAAQ;IACjC,IACE,SAAS,KAAK,GAAG;QACjB,IAAI,KAAK,GAAG;QACZ,OAAO,GAAG,KAAK,QAAQ;QACvB,CAAC,GAAG,CAAC,WAAW,YAAY,QAAQ;YAClC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,QAAQ,CAAC;QACpC,MAAM,CAAC,QAAQ,IAAI,GAAG;QACtB,MAAM,CAAC,aAAa,IAAI,GAAG,EAC3B;QACA,OAAO,KAAK,CAAC;KACd;IAED,IAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IAE7C,OAAO,IAAI,KAAK,SAAS,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS,CAAC;AAC9D,CAAC;AAhBD,gCAgBC;AAED;;;GAGG;AACH,SAAgB,UAAU,CAAC,GAAQ;IACjC,OAAO,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC;AAC3E,CAAC;AAFD,gCAEC"}
@@ -1,6 +1,6 @@
1
- import * as Arr from './arr';
2
- import * as Entry from './entry';
3
- import * as RimbuError from './rimbu-error';
4
- export { Arr, Entry, RimbuError };
1
+ export * as Arr from './arr';
2
+ export * as Entry from './entry';
3
+ export * as RimbuError from './rimbu-error';
4
+ export * from './plain-object';
5
5
  export * from './internal';
6
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,UAAU,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC;AAElC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,UAAU,MAAM,eAAe,CAAC;AAC5C,cAAc,gBAAgB,CAAC;AAE/B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
3
+ * data object.
4
+ * @param obj - the object to check
5
+ * @returns true if the given object is a pure data object
6
+ * @note does not check whether a record's properties are not functions
7
+ */
8
+ export function isPlainObj(obj) {
9
+ if (undefined === obj ||
10
+ null === obj ||
11
+ typeof obj !== 'object' ||
12
+ (obj.constructor instanceof Function &&
13
+ obj.constructor.name !== 'Object') ||
14
+ Symbol.iterator in obj ||
15
+ Symbol.asyncIterator in obj) {
16
+ return false;
17
+ }
18
+ const prototype = Object.getPrototypeOf(obj);
19
+ return null === prototype || prototype === Object.prototype;
20
+ }
21
+ /**
22
+ * Returns true if the given object is Iterable
23
+ * @param obj - the object to check
24
+ */
25
+ export function isIterable(obj) {
26
+ return obj !== null && typeof obj === 'object' && Symbol.iterator in obj;
27
+ }
28
+ //# sourceMappingURL=plain-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plain-object.js","sourceRoot":"","sources":["../../src/plain-object.ts"],"names":[],"mappings":"AAqCA;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,IACE,SAAS,KAAK,GAAG;QACjB,IAAI,KAAK,GAAG;QACZ,OAAO,GAAG,KAAK,QAAQ;QACvB,CAAC,GAAG,CAAC,WAAW,YAAY,QAAQ;YAClC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,QAAQ,CAAC;QACpC,MAAM,CAAC,QAAQ,IAAI,GAAG;QACtB,MAAM,CAAC,aAAa,IAAI,GAAG,EAC3B;QACA,OAAO,KAAK,CAAC;KACd;IAED,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IAE7C,OAAO,IAAI,KAAK,SAAS,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,OAAO,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC;AAC3E,CAAC"}
@@ -1,5 +1,5 @@
1
- import * as Arr from './arr';
2
- import * as Entry from './entry';
3
- import * as RimbuError from './rimbu-error';
4
- export { Arr, Entry, RimbuError };
1
+ export * as Arr from './arr';
2
+ export * as Entry from './entry';
3
+ export * as RimbuError from './rimbu-error';
4
+ export * from './plain-object';
5
5
  export * from './internal';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Matches any type of function
3
+ */
4
+ export declare type AnyFunc = (...args: any[]) => any;
5
+ /**
6
+ * A predicate type for any record that resolves to true if any of the record
7
+ * properties is a function, false otherwise.
8
+ * This is useful to have a coarse discrimination between pure data objects and class instances.
9
+ */
10
+ export declare type IsObjWithoutFunctions<T extends Record<any, any>> = AnyFunc extends T[keyof T] ? false : true;
11
+ /**
12
+ * A predicate type that resolves to true if the given type satisfies:
13
+ * - it is an object type (not a primitive)
14
+ * - it is not a function
15
+ * - it is not iterable
16
+ * - it does not have any properties that are functions
17
+ * Otherwise, it resolves to false
18
+ */
19
+ export declare type IsPlainObj<T> = T extends null | undefined | number | string | boolean | bigint | symbol | AnyFunc | Iterable<unknown> | AsyncIterable<unknown> ? false : IsObjWithoutFunctions<T>;
20
+ export declare type PlainObj<T> = IsPlainObj<T> extends true ? T : never;
21
+ /**
22
+ * Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
23
+ * data object.
24
+ * @param obj - the object to check
25
+ * @returns true if the given object is a pure data object
26
+ * @note does not check whether a record's properties are not functions
27
+ */
28
+ export declare function isPlainObj(obj: any): boolean;
29
+ /**
30
+ * Returns true if the given object is Iterable
31
+ * @param obj - the object to check
32
+ */
33
+ export declare function isIterable(obj: any): obj is Iterable<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimbu/base",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Utilities to implement Rimbu collections",
5
5
  "keywords": [
6
6
  "array",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "sideEffects": false,
53
53
  "dependencies": {
54
- "@rimbu/common": "^0.9.2",
54
+ "@rimbu/common": "^0.9.3",
55
55
  "tslib": "^2.4.0"
56
56
  },
57
57
  "publishConfig": {
@@ -61,5 +61,5 @@
61
61
  "index": "src/index.ts",
62
62
  "replacer": "../../config/denoify-rimbu-replacer.js"
63
63
  },
64
- "gitHead": "33dd7ca935f19f513586834a2ce1b1f886352ae7"
64
+ "gitHead": "4efaf8c469d606381517984436383fd6b1b61ec0"
65
65
  }
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
- import * as Arr from './arr';
2
- import * as Entry from './entry';
3
- import * as RimbuError from './rimbu-error';
4
-
5
- export { Arr, Entry, RimbuError };
1
+ export * as Arr from './arr';
2
+ export * as Entry from './entry';
3
+ export * as RimbuError from './rimbu-error';
4
+ export * from './plain-object';
6
5
 
7
6
  export * from './internal';
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Matches any type of function
3
+ */
4
+ export type AnyFunc = (...args: any[]) => any;
5
+
6
+ /**
7
+ * A predicate type for any record that resolves to true if any of the record
8
+ * properties is a function, false otherwise.
9
+ * This is useful to have a coarse discrimination between pure data objects and class instances.
10
+ */
11
+ export type IsObjWithoutFunctions<T extends Record<any, any>> =
12
+ AnyFunc extends T[keyof T] ? false : true;
13
+
14
+ /**
15
+ * A predicate type that resolves to true if the given type satisfies:
16
+ * - it is an object type (not a primitive)
17
+ * - it is not a function
18
+ * - it is not iterable
19
+ * - it does not have any properties that are functions
20
+ * Otherwise, it resolves to false
21
+ */
22
+ export type IsPlainObj<T> = T extends
23
+ | null
24
+ | undefined
25
+ | number
26
+ | string
27
+ | boolean
28
+ | bigint
29
+ | symbol
30
+ | AnyFunc
31
+ | Iterable<unknown>
32
+ | AsyncIterable<unknown>
33
+ ? false
34
+ : IsObjWithoutFunctions<T>;
35
+
36
+ export type PlainObj<T> = IsPlainObj<T> extends true ? T : never;
37
+
38
+ /**
39
+ * Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
40
+ * data object.
41
+ * @param obj - the object to check
42
+ * @returns true if the given object is a pure data object
43
+ * @note does not check whether a record's properties are not functions
44
+ */
45
+ export function isPlainObj(obj: any): boolean {
46
+ if (
47
+ undefined === obj ||
48
+ null === obj ||
49
+ typeof obj !== 'object' ||
50
+ (obj.constructor instanceof Function &&
51
+ obj.constructor.name !== 'Object') ||
52
+ Symbol.iterator in obj ||
53
+ Symbol.asyncIterator in obj
54
+ ) {
55
+ return false;
56
+ }
57
+
58
+ const prototype = Object.getPrototypeOf(obj);
59
+
60
+ return null === prototype || prototype === Object.prototype;
61
+ }
62
+
63
+ /**
64
+ * Returns true if the given object is Iterable
65
+ * @param obj - the object to check
66
+ */
67
+ export function isIterable(obj: any): obj is Iterable<unknown> {
68
+ return obj !== null && typeof obj === 'object' && Symbol.iterator in obj;
69
+ }