chain-simple 0.0.3 → 0.0.4

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/.eslintrc.js CHANGED
@@ -1,75 +1,38 @@
1
1
  module.exports = {
2
- 'env': {
3
- 'browser': true,
4
- 'commonjs': true,
5
- 'es6': true,
6
- 'node': true,
7
- 'mocha': true
2
+ plugins: ['sonarjs', 'promise', 'unicorn', 'prettier', 'no-only-tests'],
3
+ parser: '@typescript-eslint/parser',
4
+ rules: {
5
+ 'jsdoc/valid-types': 'off',
6
+ 'jsdoc/check-property-names': 'off',
7
+ 'no-await-in-loop': 'off',
8
+ 'unicorn/no-null': 'off',
9
+ 'jsdoc/no-undefined-types': 'off',
10
+ 'unicorn/explicit-length-check': 'off',
11
+ 'no-restricted-syntax': 'off',
12
+ 'func-names': 'off',
13
+ 'no-plusplus': 'off',
14
+ 'unicorn/prevent-abbreviations': 'off',
15
+ 'unicorn/no-reduce': 'off',
16
+ 'no-unused-expressions': 'off',
17
+ 'unicorn/prefer-node-protocol': 'off',
18
+ 'unicorn/import-style': 'off',
19
+ 'no-useless-constructor': 'off',
20
+ 'unicorn/prefer-module': 'off',
21
+ 'unicorn/prefer-spread': 'off',
22
+ 'unicorn/consistent-destructuring': 'off',
23
+ 'import/no-unresolved': 'off',
24
+ 'unicorn/no-this-assignment': 'off',
25
+ 'unicorn/no-array-for-each': 'off',
26
+ 'default-case': 'off',
27
+ 'sonarjs/no-duplicate-string': 'off',
28
+ 'unicorn/no-array-reduce': 'off',
29
+ 'unicorn/filename-case': 'off',
30
+ 'unicorn/no-abusive-eslint-disable': 'off',
31
+ 'no-only-tests/no-only-tests': 'error',
32
+ 'no-console': 'error',
8
33
  },
9
- 'globals': {
10
- 'describe:': 'readonly'
34
+ extends: ['plugin:sonarjs/recommended', 'plugin:unicorn/recommended', 'prettier', 'plugin:prettier/recommended'],
35
+ globals: {
36
+ browser: 'readonly',
11
37
  },
12
- 'parser': '@typescript-eslint/parser',
13
- 'plugins': [
14
- '@typescript-eslint',
15
- 'mocha',
16
- 'chai-expect',
17
- 'chai-friendly'
18
- ],
19
- 'parserOptions': {
20
- 'ecmaVersion': 2018
21
- },
22
- 'rules': {
23
- '@typescript-eslint/naming-convention': [
24
- 'error',
25
- {
26
- 'selector': 'typeParameter',
27
- 'format': ['PascalCase'],
28
- 'prefix': ['T']
29
- }
30
- ],
31
- 'no-undefined': 'off',
32
- '@typescript-eslint/ban-ts-comment': 'off',
33
- '@typescript-eslint/no-use-before-define': 'off',
34
- '@typescript-eslint/explicit-module-boundary-types': 'off',
35
- '@typescript-eslint/camelcase': 'off',
36
- '@typescript-eslint/ban-ts-ignore': 'off',
37
- '@typescript-eslint/no-explicit-any': 'off',
38
- '@typescript-eslint/interface-name-prefix': 'off',
39
- '@typescript-eslint/explicit-function-return-type': 'off',
40
- '@typescript-eslint/no-var-requires': 'off',
41
- 'no-console': 2,
42
- 'arrow-parens': 2,
43
- 'require-atomic-updates': 'off',
44
- 'eol-last': ['error', 'always'],
45
- 'max-len': ['error', {'code': 140, 'tabWidth': 2}],
46
- 'prefer-const': ['error', {'destructuring': 'any', 'ignoreReadBeforeAssign': false}],
47
- 'space-in-parens': [2, 'never'],
48
- 'no-trailing-spaces': ['error'],
49
- 'semi': [2, 'always'],
50
- 'no-multiple-empty-lines': [2, {'max': 2}],
51
- 'quotes': ['error', 'single', {'allowTemplateLiterals': true}],
52
- 'valid-jsdoc': ['error', {'requireParamDescription': false, 'requireReturnDescription': false}],
53
- 'no-shadow': 'off',
54
- 'space-before-function-paren': ['error', {'anonymous': 'never', 'named': 'never', 'asyncArrow': 'always'}],
55
- 'require-jsdoc': 'off', //['error', {'require': {'FunctionDeclaration': true, 'MethodDefinition': true}}],
56
- 'keyword-spacing': ['error'],
57
- 'chai-friendly/no-unused-expressions': 'off',
58
- 'no-undef-init': ['error'],
59
- 'comma-spacing': ['error'],
60
- 'brace-style': ['error'],
61
- '@typescript-eslint/no-unused-vars': 'off',
62
- 'no-irregular-whitespace': 'off',
63
- 'mocha/no-mocha-arrows': 'off',
64
- 'chai-expect/no-inner-compare': 'off',
65
- '@typescript-eslint/no-this-alias': 'off'
66
- },
67
- 'extends': [
68
- 'eslint:recommended',
69
- 'plugin:mocha/recommended',
70
- 'plugin:chai-expect/recommended',
71
- 'plugin:chai-friendly/recommended',
72
- 'plugin:@typescript-eslint/recommended',
73
- 'plugin:@typescript-eslint/eslint-recommended'
74
- ]
75
38
  };
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "singleQuote": true,
3
+ "trailingComma": "all",
4
+ "arrowParens": "avoid",
5
+ "printWidth": 120,
6
+ "tabWidth": 2,
7
+ "useTabs": false,
8
+ "semi": true,
9
+ "bracketSpacing": true,
10
+ "proseWrap": "never"
11
+ }
package/built/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- declare function wrapObj(item: any): any;
1
+ declare function wrapObj(item: any, config?: {
2
+ getEntity: string;
3
+ }): any;
2
4
  declare function wrapConstruct(constructorFunction: any): any;
3
5
  export { wrapConstruct, wrapObj };
package/built/index.js CHANGED
@@ -3,26 +3,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.wrapObj = exports.wrapConstruct = void 0;
4
4
  const sat_utils_1 = require("sat-utils");
5
5
  sat_utils_1.logger.setLogLevel(process.env.LOG_LEVEL);
6
- function wrapObj(item) {
6
+ function wrapObj(item, config) {
7
+ if (!sat_utils_1.canBeProxed(item)) {
8
+ throw new TypeError('first argument should be an entity that can be proxed');
9
+ }
10
+ if (!sat_utils_1.isUndefined(config) && !sat_utils_1.isObject(config)) {
11
+ throw new TypeError('second argument should be an object');
12
+ }
7
13
  let proxifiedResult = item;
8
14
  const proxed = new Proxy(item, {
9
15
  get(_t, p) {
16
+ if (config && config.getEntity === p) {
17
+ return item;
18
+ }
19
+ sat_utils_1.logger.info(p);
10
20
  if (p === Symbol.toStringTag) {
11
- return proxifiedResult;
21
+ return proxifiedResult[Symbol.toStringTag];
12
22
  }
13
23
  if (p === 'toString') {
14
24
  return function (...args) {
15
25
  return proxifiedResult.toString(...args);
16
26
  };
17
27
  }
18
- sat_utils_1.logger.info(p);
19
28
  if (p === 'toJSON') {
20
29
  sat_utils_1.logger.info('In to JSON');
21
30
  return function () {
22
31
  return proxifiedResult;
23
32
  };
24
33
  }
25
- if (!sat_utils_1.isFunction(item[p]) && !sat_utils_1.isAsyncFunction(item[p]) && !sat_utils_1.isPromise(proxifiedResult) && item[p] && !proxifiedResult[p]) {
34
+ if (!sat_utils_1.isFunction(item[p]) &&
35
+ !sat_utils_1.isAsyncFunction(item[p]) &&
36
+ !sat_utils_1.isPromise(proxifiedResult) &&
37
+ item[p] &&
38
+ !proxifiedResult[p]) {
26
39
  sat_utils_1.logger.info('In to not function, not async function, resulter is not a promise and target has prop');
27
40
  return item[p];
28
41
  }
@@ -63,8 +76,7 @@ function wrapObj(item) {
63
76
  }
64
77
  return async function (onRes, onRej) {
65
78
  const catcher = p === 'catch' ? onRes : onRej;
66
- proxifiedResult = await proxifiedResult
67
- .catch((error) => ({ error, ____proxed____error: true }));
79
+ proxifiedResult = await proxifiedResult.catch(error => ({ error, ____proxed____error: true }));
68
80
  if (proxifiedResult && proxifiedResult.____proxed____error) {
69
81
  return catcher(proxifiedResult.error);
70
82
  }
@@ -76,7 +88,7 @@ function wrapObj(item) {
76
88
  sat_utils_1.logger.info('In resulter has prop');
77
89
  return proxifiedResult[p];
78
90
  }
79
- if (!(p in item) && (p in proxifiedResult)) {
91
+ if (!(p in item) && p in proxifiedResult) {
80
92
  sat_utils_1.logger.info('In target does not have prop but resulter has prop');
81
93
  return proxifiedResult[p];
82
94
  }
@@ -90,7 +102,7 @@ function wrapObj(item) {
90
102
  },
91
103
  getOwnPropertyDescriptor(_t, p) {
92
104
  return Object.getOwnPropertyDescriptor(proxifiedResult, p);
93
- }
105
+ },
94
106
  });
95
107
  return proxed;
96
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chain-simple",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
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": {
@@ -47,6 +47,7 @@
47
47
  "eslint-plugin-chai-friendly": "^0.6.0",
48
48
  "eslint-plugin-mocha": "^8.0.0",
49
49
  "mocha": "^8.2.1",
50
+ "prettier": "^2.6.2",
50
51
  "ts-node": "^9.1.1",
51
52
  "typescript": "^4.1.3"
52
53
  },
@@ -54,6 +55,6 @@
54
55
  "node": ">=12.18.3"
55
56
  },
56
57
  "dependencies": {
57
- "sat-utils": "^0.0.4"
58
+ "sat-utils": "^0.0.42"
58
59
  }
59
60
  }
package/readme.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # chain-simple
2
2
 
3
- ## BETA
4
3
 
5
4
  ![npm downloads](https://img.shields.io/npm/dm/chain-simple.svg?style=flat-square)
5
+
6
+ The purpose of this library is - build simple and flexible chainable call of the object` methods
7
+
8
+ ```js
9
+ const {wrapObj} = require('chain-simple');
10
+
11
+ const yourObjectWithMethods = {
12
+ async method1() {
13
+ return Promise.resolve(1).then(value => {
14
+ console.log('method1', value);
15
+ return value;
16
+ });
17
+ },
18
+ async method2() {
19
+ return Promise.resolve(2).then(value => {
20
+ console.log('method2', value);
21
+ return value;
22
+ });
23
+ },
24
+ async method3() {
25
+ return Promise.resolve(3).then(value => {
26
+ console.log('method3', value);
27
+ return value;
28
+ });
29
+ },
30
+ };
31
+
32
+ const wrappedObj = wrapObj(yourObjectWithMethods);
33
+
34
+ testExample();
35
+ async function testExample() {
36
+ const result = await wrappedObj.method1().method2().method3().method1();
37
+
38
+ console.log(result);
39
+ }
40
+ ```