chain-simple 1.2.2 → 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.
package/built/index.d.ts CHANGED
@@ -10,6 +10,9 @@ type TConfig = {
10
10
  } | ((item: any) => {
11
11
  [k: string]: any;
12
12
  });
13
+ getEntityPropList?: string[] | {
14
+ [k: string]: any;
15
+ };
13
16
  };
14
17
  /**
15
18
  * @example
package/built/index.js CHANGED
@@ -36,6 +36,15 @@ logger_1.logger.setLogLevel(process.env.CHAIN_SIMPLE_LOG_LEVEL);
36
36
  * @returns {object} object with chainable properties
37
37
  */
38
38
  function makePropertiesChainable(item, config) {
39
+ const propsList = [];
40
+ if ((0, sat_utils_1.isObject)(config) && config.getEntityPropList) {
41
+ if (!(0, sat_utils_1.isObject)(config.getEntityPropList) && !(0, sat_utils_1.isArray)(config.getEntityPropList)) {
42
+ throw new TypeError('config "getEntityPropList" should be an array or an object');
43
+ }
44
+ propsList.push(...((0, sat_utils_1.isObject)(config.getEntityPropList)
45
+ ? Object.keys(config.getEntityPropList)
46
+ : config.getEntityPropList));
47
+ }
39
48
  if (!(0, sat_utils_1.canBeProxed)(item)) {
40
49
  throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
41
50
  }
@@ -45,7 +54,15 @@ function makePropertiesChainable(item, config) {
45
54
  let proxifiedResult = item;
46
55
  const proxed = new Proxy(item, {
47
56
  get(_t, p, r) {
48
- if (config && config.getEntity === p) {
57
+ var _a;
58
+ if (propsList.length && propsList.includes(p)) {
59
+ const propValue = (_a = Reflect.getOwnPropertyDescriptor(item, p)) === null || _a === void 0 ? void 0 : _a.value;
60
+ if ((0, sat_utils_1.isFunction)(propValue) || (0, sat_utils_1.isAsyncFunction)(propValue)) {
61
+ return item[p].bind(item);
62
+ }
63
+ return item[p];
64
+ }
65
+ if ((0, sat_utils_1.isObject)(config) && config.getEntity === p) {
49
66
  return item;
50
67
  }
51
68
  if (p === Symbol.toStringTag) {
package/lib/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { isObject, isPromise, isFunction, isAsyncFunction, canBeProxed, isUndefined } from 'sat-utils';
1
+ import { isArray, isObject, isPromise, isFunction, isAsyncFunction, canBeProxed, isUndefined } from 'sat-utils';
2
2
 
3
3
  import { logger } from './logger';
4
4
 
@@ -15,6 +15,7 @@ export type TChainable<T extends Record<string, TFn>> = {
15
15
  type TConfig = {
16
16
  getEntity?: string;
17
17
  extendProxed?: (propName) => { [k: string]: any } | ((item: any) => { [k: string]: any });
18
+ getEntityPropList?: string[] | { [k: string]: any };
18
19
  };
19
20
 
20
21
  /**
@@ -49,6 +50,19 @@ type TConfig = {
49
50
  * @returns {object} object with chainable properties
50
51
  */
51
52
  function makePropertiesChainable(item, config?: TConfig) {
53
+ const propsList = [];
54
+ if (isObject(config) && config.getEntityPropList) {
55
+ if (!isObject(config.getEntityPropList) && !isArray(config.getEntityPropList)) {
56
+ throw new TypeError('config "getEntityPropList" should be an array or an object');
57
+ }
58
+
59
+ propsList.push(
60
+ ...(isObject(config.getEntityPropList)
61
+ ? Object.keys(config.getEntityPropList)
62
+ : (config.getEntityPropList as string[])),
63
+ );
64
+ }
65
+
52
66
  if (!canBeProxed(item)) {
53
67
  throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
54
68
  }
@@ -60,7 +74,15 @@ function makePropertiesChainable(item, config?: TConfig) {
60
74
  let proxifiedResult = item;
61
75
  const proxed = new Proxy(item, {
62
76
  get(_t, p, r) {
63
- if (config && config.getEntity === p) {
77
+ if (propsList.length && propsList.includes(p)) {
78
+ const propValue = Reflect.getOwnPropertyDescriptor(item, p)?.value;
79
+ if (isFunction(propValue) || isAsyncFunction(propValue)) {
80
+ return item[p].bind(item);
81
+ }
82
+ return item[p];
83
+ }
84
+
85
+ if (isObject(config) && config.getEntity === p) {
64
86
  return item;
65
87
  }
66
88
 
@@ -79,6 +101,7 @@ function makePropertiesChainable(item, config?: TConfig) {
79
101
  return proxifiedResult;
80
102
  };
81
103
  }
104
+
82
105
  if (
83
106
  p !== 'then' &&
84
107
  p !== 'catch' &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chain-simple",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "Main purpose of this package is - provide simple way to build chain between any item methods",
5
5
  "main": "built/index.js",
6
6
  "directories": {