@vpmedia/simplify 1.1.0 → 1.2.1

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/CHANGES.md CHANGED
@@ -3,6 +3,15 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## 1.2.1
7
+
8
+ - Fixed logger issue with vite compiled apps
9
+
10
+ ## 1.2.0
11
+
12
+ - Added Logger class
13
+ - Fixed lint and typecheck issues
14
+
6
15
  ## 1.1.0
7
16
 
8
17
  - Updated dependencies
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @vpmedia/simplify
2
2
 
3
- [![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.1.0)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
3
+ [![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.2.1)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
4
4
  [![Node.js CI](https://github.com/vpmedia/simplify/actions/workflows/ci.yml/badge.svg)](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
5
5
 
6
6
  @vpmedia/simplify TBD
@@ -0,0 +1,88 @@
1
+ import js from '@eslint/js';
2
+ import importPlugin from 'eslint-plugin-import';
3
+ import jsdocPlugin from 'eslint-plugin-jsdoc';
4
+ import unicornPlugin from 'eslint-plugin-unicorn';
5
+ import globals from 'globals';
6
+
7
+ /** @type { import('eslint').Linter.FlatConfig[] } */
8
+ export default [
9
+ {
10
+ ignores: [
11
+ '.github/**/*.*',
12
+ '.idea/**/*.*',
13
+ '.vscode/**/*.*',
14
+ 'build/**/*.*',
15
+ 'coverage/**/*.*',
16
+ 'dist/**/*.*',
17
+ 'public/**/*.*',
18
+ 'types/**/*.*',
19
+ 'node_modules/**/*.*',
20
+ ],
21
+ },
22
+ {
23
+ languageOptions: {
24
+ globals: {
25
+ ...globals.jest,
26
+ ...globals.browser,
27
+ ...globals.node,
28
+ ...globals.es2021,
29
+ },
30
+ parserOptions: {
31
+ ecmaVersion: 'latest',
32
+ sourceType: 'module',
33
+ ecmaFeatures: {
34
+ jsx: true,
35
+ },
36
+ },
37
+ },
38
+ files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
39
+ plugins: {
40
+ import: importPlugin,
41
+ jsdoc: jsdocPlugin,
42
+ unicorn: unicornPlugin,
43
+ },
44
+ settings: {
45
+ 'import/parsers': {
46
+ espree: ['.js', '.cjs', '.mjs', '.jsx'],
47
+ },
48
+ 'import/resolver': {
49
+ node: true,
50
+ },
51
+ },
52
+ rules: {
53
+ ...js.configs.recommended.rules,
54
+ ...jsdocPlugin.configs['flat/recommended'].rules,
55
+ ...importPlugin.configs.recommended.rules,
56
+ ...unicornPlugin.configs['flat/recommended'].rules,
57
+ 'unicorn/filename-case': 'off',
58
+ 'unicorn/no-null': 'off',
59
+ 'unicorn/prevent-abbreviations': 'off',
60
+ 'no-unused-vars': 'warn',
61
+ 'prefer-arrow-callback': 'error',
62
+ 'prefer-template': 'error',
63
+ 'import/extensions': ['warn', 'always'],
64
+ 'jsdoc/require-jsdoc': [
65
+ 'error',
66
+ {
67
+ require: {
68
+ FunctionDeclaration: true,
69
+ MethodDefinition: true,
70
+ ClassDeclaration: false,
71
+ ArrowFunctionExpression: false,
72
+ FunctionExpression: false,
73
+ },
74
+ },
75
+ ],
76
+ 'jsdoc/check-indentation': 1,
77
+ 'jsdoc/check-line-alignment': 1,
78
+ 'jsdoc/check-syntax': 1,
79
+ 'jsdoc/require-asterisk-prefix': 1,
80
+ 'jsdoc/require-description': 1,
81
+ 'jsdoc/require-description-complete-sentence': 1,
82
+ 'jsdoc/require-hyphen-before-param-description': 1,
83
+ 'jsdoc/require-throws': 1,
84
+ 'jsdoc/sort-tags': 1,
85
+ 'jsdoc/no-undefined-types': 0,
86
+ },
87
+ },
88
+ ];
package/lefthook.yml CHANGED
@@ -10,13 +10,8 @@ pre-commit:
10
10
  run: |
11
11
  set -e
12
12
  yq 'true' {staged_files} > /dev/null
13
- lint_json:
14
- glob: "*.{json}"
15
- run: |
16
- set -e
17
- jq empty {staged_files}
18
13
  lint_js:
19
- glob: "*.{js,ts,jsx,tsx}"
14
+ glob: "*.{js,jsx}"
20
15
  run: |
21
16
  set -e
22
17
  ./node_modules/.bin/eslint {staged_files}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vpmedia/simplify",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "@vpmedia/simplify",
5
5
  "author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
6
6
  "license": "MIT",
@@ -19,24 +19,27 @@
19
19
  "types": "./types/index.d.ts",
20
20
  "type": "module",
21
21
  "devDependencies": {
22
- "@babel/preset-env": "^7.23.9",
22
+ "@babel/preset-env": "^7.24.4",
23
+ "@eslint/js": "^8.57.0",
23
24
  "@jest/globals": "^29.7.0",
24
25
  "@types/jest": "^29.5.12",
25
- "eslint": "^8.56.0",
26
+ "eslint": "^8.57.0",
26
27
  "eslint-plugin-import": "^2.29.1",
27
- "eslint-plugin-jest": "^27.6.3",
28
- "eslint-plugin-jsdoc": "^48.0.4",
28
+ "eslint-plugin-jest": "^27.9.0",
29
+ "eslint-plugin-jsdoc": "^48.2.3",
30
+ "eslint-plugin-unicorn": "^52.0.0",
31
+ "globals": "^15.0.0",
29
32
  "jest": "^29.7.0",
30
33
  "jest-environment-jsdom": "^29.7.0",
31
- "lefthook": "^1.6.1",
32
- "prettier": "^3.2.4",
33
- "typescript": "^5.3.3"
34
+ "lefthook": "^1.6.10",
35
+ "prettier": "^3.2.5",
36
+ "typescript": "^5.4.5"
34
37
  },
35
38
  "scripts": {
36
39
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
37
40
  "lint": "eslint \"**/*.{js,jsx}\"",
38
41
  "typecheck": "tsc",
39
- "format": "prettier --write \"**/*.{css,js,jsx}\"",
42
+ "format": "prettier --write \"./**/*.{js,jsx,mjs,cjs,ts,tsx,json,md,css}\"",
40
43
  "lefthook:install": "lefthook install",
41
44
  "lefthook:uninstall": "lefthook uninstall"
42
45
  },
package/src/index.js CHANGED
@@ -1,20 +1,10 @@
1
1
  // core
2
- import { addLeadingZero } from './util/addLeadingZero.js';
3
- import { capitalize } from './util/capitalize.js';
4
- import { getObjValueByPath } from './util/getObjValueByPath.js';
5
- import { getRandomInt } from './util/getRandomInt.js';
6
- import { getURLParam } from './util/getURLParam.js';
7
- import { sanitizeURLParam } from './util/sanitizeURLParam.js';
8
- import { setObjValueByPath } from './util/setObjValueByPath.js';
9
- import { underscoreToCamelCase } from './util/underscoreToCamelCase.js';
10
- // exports
11
- export {
12
- addLeadingZero,
13
- capitalize,
14
- getObjValueByPath,
15
- getRandomInt,
16
- getURLParam,
17
- sanitizeURLParam,
18
- setObjValueByPath,
19
- underscoreToCamelCase,
20
- };
2
+ export { Logger } from './logging/Logger.js';
3
+ export { addLeadingZero } from './util/addLeadingZero.js';
4
+ export { capitalize } from './util/capitalize.js';
5
+ export { getObjValueByPath } from './util/getObjValueByPath.js';
6
+ export { getRandomInt } from './util/getRandomInt.js';
7
+ export { getURLParam } from './util/getURLParam.js';
8
+ export { sanitizeURLParam } from './util/sanitizeURLParam.js';
9
+ export { setObjValueByPath } from './util/setObjValueByPath.js';
10
+ export { underscoreToCamelCase } from './util/underscoreToCamelCase.js';
@@ -0,0 +1,113 @@
1
+ import { getURLParam } from '../util/getURLParam.js';
2
+
3
+ export const LEVEL_DEBUG = 4;
4
+ export const LEVEL_INFO = 3;
5
+ export const LEVEL_WARN = 2;
6
+ export const LEVEL_ERROR = 1;
7
+ export const LEVEL_SILENT = 0;
8
+
9
+ export class Logger {
10
+ /**
11
+ * @type {(error) => {}}
12
+ */
13
+ static exceptionHandler = null;
14
+
15
+ /**
16
+ * Creates a new Logger instance.
17
+ * @param {string} name - The logger name.
18
+ */
19
+ constructor(name) {
20
+ this.name = name;
21
+ const appEnvironment = process?.env.APP_ENVIRONMENT || 'local';
22
+ const isProduction = appEnvironment === 'production' || appEnvironment === 'release';
23
+ const defaultLevel = isProduction ? LEVEL_SILENT : LEVEL_DEBUG;
24
+ const parameterName = `log_${this.name.toLowerCase()}`;
25
+ this.level = Number.parseInt(getURLParam(parameterName, defaultLevel), 10);
26
+ const allLevel = Number.parseInt(getURLParam('log_all', LEVEL_SILENT), 10);
27
+ this.level = allLevel || this.level;
28
+ }
29
+
30
+ /**
31
+ * TBD.
32
+ * @param {string} message - TBD.
33
+ * @param {object} extraData - TBD.
34
+ */
35
+ debug(message, extraData = null) {
36
+ if (this.level < LEVEL_DEBUG) {
37
+ return;
38
+ }
39
+ const logMessage = `${Date.now()} [${this.name}] ${message}`;
40
+ if (extraData !== null) {
41
+ console.debug(logMessage, extraData);
42
+ return;
43
+ }
44
+ console.debug(logMessage);
45
+ }
46
+
47
+ /**
48
+ * TBD.
49
+ * @param {string} message - TBD.
50
+ * @param {object} extraData - TBD.
51
+ */
52
+ info(message, extraData = null) {
53
+ if (this.level < LEVEL_INFO) {
54
+ return;
55
+ }
56
+ const logMessage = `${Date.now()} [${this.name}] ${message}`;
57
+ if (extraData !== null) {
58
+ console.info(logMessage, extraData);
59
+ return;
60
+ }
61
+ console.info(logMessage);
62
+ }
63
+
64
+ /**
65
+ * TBD.
66
+ * @param {string} message - TBD.
67
+ * @param {object} extraData - TBD.
68
+ */
69
+ warn(message, extraData = null) {
70
+ if (this.level < LEVEL_WARN) {
71
+ return;
72
+ }
73
+ const logMessage = `${Date.now()} [${this.name}] ${message}`;
74
+ if (extraData !== null) {
75
+ console.warn(logMessage, extraData);
76
+ return;
77
+ }
78
+ console.warn(logMessage);
79
+ }
80
+
81
+ /**
82
+ * TBD.
83
+ * @param {string} message - TBD.
84
+ * @param {object} extraData - TBD.
85
+ */
86
+ error(message, extraData = null) {
87
+ if (this.level < LEVEL_ERROR) {
88
+ return;
89
+ }
90
+ const logMessage = `${Date.now()} [${this.name}] ${message}`;
91
+ if (extraData !== null) {
92
+ console.error(logMessage, extraData);
93
+ return;
94
+ }
95
+ console.error(logMessage);
96
+ }
97
+
98
+ /**
99
+ * TBD.
100
+ * @param {string} message - TBD.
101
+ * @param {Error} exception - TBD.
102
+ */
103
+ exception(message, exception) {
104
+ if (Logger.exceptionHandler) {
105
+ Logger.exceptionHandler(exception);
106
+ }
107
+ if (this.level < LEVEL_ERROR) {
108
+ return;
109
+ }
110
+ const logMessage = `${Date.now()} [${this.name}] ${message}`;
111
+ console.error(logMessage, exception);
112
+ }
113
+ }
@@ -0,0 +1,14 @@
1
+ import { LEVEL_DEBUG, Logger } from './Logger.js';
2
+
3
+ test('Tests Logger', () => {
4
+ const logger = new Logger('test');
5
+ expect(Logger.exceptionHandler).toBe(null);
6
+ expect(logger.level).toBe(LEVEL_DEBUG);
7
+ let handledError = null;
8
+ const exceptionHandler = (error) => {
9
+ handledError = error;
10
+ };
11
+ Logger.exceptionHandler = exceptionHandler;
12
+ logger.exception('test', 'error');
13
+ expect(handledError).toBe('error');
14
+ });
@@ -6,11 +6,12 @@
6
6
  */
7
7
  export function addLeadingZero(value, size = 2) {
8
8
  if (value === null || value === undefined) {
9
+ // @ts-ignore: Type 'string | number' is not assignable to type 'string'.
9
10
  return value;
10
11
  }
11
12
  value = value.toString();
12
13
  while (value.length < size) {
13
- value = '0' + value;
14
+ value = `0${value}`;
14
15
  }
15
16
  return value;
16
17
  }
@@ -7,5 +7,5 @@ test('Tests add leading zero', () => {
7
7
  expect(addLeadingZero('21')).toBe('21');
8
8
  expect(addLeadingZero(21)).toBe('21');
9
9
  expect(addLeadingZero(null)).toBe(null);
10
- expect(addLeadingZero(undefined)).toBe(undefined);
10
+ expect(addLeadingZero()).toBe(undefined);
11
11
  });
@@ -7,5 +7,5 @@ export function sanitizeURLParam(input) {
7
7
  if (!input) {
8
8
  return input;
9
9
  }
10
- return input.replace(/[^a-zA-Z0-9-_]/gi, '');
10
+ return input.replaceAll(/[^\w-]/gi, '');
11
11
  }
@@ -4,7 +4,7 @@
4
4
  * @returns {string} The output string in camel case.
5
5
  */
6
6
  export function underscoreToCamelCase(value) {
7
- return value.replace(/(_\w)/g, function (m) {
7
+ return value.replaceAll(/(_\w)/g, (m) => {
8
8
  return m[1].toUpperCase();
9
9
  });
10
10
  }
package/types/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { capitalize } from './util/capitalize.js';
2
- import { getObjValueByPath } from './util/getObjValueByPath.js';
3
- import { getRandomInt } from './util/getRandomInt.js';
4
- import { getURLParam } from './util/getURLParam.js';
5
- import { sanitizeURLParam } from './util/sanitizeURLParam.js';
6
- import { setObjValueByPath } from './util/setObjValueByPath.js';
7
- import { underscoreToCamelCase } from './util/underscoreToCamelCase.js';
8
- export { capitalize, getObjValueByPath, getRandomInt, getURLParam, sanitizeURLParam, setObjValueByPath, underscoreToCamelCase };
1
+ export { Logger } from "./logging/Logger.js";
2
+ export { addLeadingZero } from "./util/addLeadingZero.js";
3
+ export { capitalize } from "./util/capitalize.js";
4
+ export { getObjValueByPath } from "./util/getObjValueByPath.js";
5
+ export { getRandomInt } from "./util/getRandomInt.js";
6
+ export { getURLParam } from "./util/getURLParam.js";
7
+ export { sanitizeURLParam } from "./util/sanitizeURLParam.js";
8
+ export { setObjValueByPath } from "./util/setObjValueByPath.js";
9
+ export { underscoreToCamelCase } from "./util/underscoreToCamelCase.js";
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"2BAC2B,sBAAsB;kCACf,6BAA6B;6BAClC,wBAAwB;4BACzB,uBAAuB;iCAClB,4BAA4B;kCAC3B,6BAA6B;sCACzB,iCAAiC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
@@ -0,0 +1,49 @@
1
+ export const LEVEL_DEBUG: 4;
2
+ export const LEVEL_INFO: 3;
3
+ export const LEVEL_WARN: 2;
4
+ export const LEVEL_ERROR: 1;
5
+ export const LEVEL_SILENT: 0;
6
+ export class Logger {
7
+ /**
8
+ * @type {(error) => {}}
9
+ */
10
+ static exceptionHandler: (error) => {};
11
+ /**
12
+ * Creates a new Logger instance.
13
+ * @param {string} name - The logger name.
14
+ */
15
+ constructor(name: string);
16
+ name: string;
17
+ level: number;
18
+ /**
19
+ * TBD.
20
+ * @param {string} message - TBD.
21
+ * @param {object} extraData - TBD.
22
+ */
23
+ debug(message: string, extraData?: object): void;
24
+ /**
25
+ * TBD.
26
+ * @param {string} message - TBD.
27
+ * @param {object} extraData - TBD.
28
+ */
29
+ info(message: string, extraData?: object): void;
30
+ /**
31
+ * TBD.
32
+ * @param {string} message - TBD.
33
+ * @param {object} extraData - TBD.
34
+ */
35
+ warn(message: string, extraData?: object): void;
36
+ /**
37
+ * TBD.
38
+ * @param {string} message - TBD.
39
+ * @param {object} extraData - TBD.
40
+ */
41
+ error(message: string, extraData?: object): void;
42
+ /**
43
+ * TBD.
44
+ * @param {string} message - TBD.
45
+ * @param {Error} exception - TBD.
46
+ */
47
+ exception(message: string, exception: Error): void;
48
+ }
49
+ //# sourceMappingURL=Logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../../src/logging/Logger.js"],"names":[],"mappings":"AAEA,4BAA6B;AAC7B,2BAA4B;AAC5B,2BAA4B;AAC5B,4BAA6B;AAC7B,6BAA8B;AAE9B;IACE;;OAEG;IACH,yBAFU,CAAC,KAAK,KAAK,EAAE,CAEQ;IAE/B;;;OAGG;IACH,kBAFW,MAAM,EAWhB;IARC,aAAgB;IAKhB,cAA0E;IAK5E;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,cAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,eAHW,MAAM,cACN,MAAM,QAYhB;IAED;;;;OAIG;IACH,mBAHW,MAAM,aACN,KAAK,QAWf;CACF"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * TBD.
3
+ * @param {number|string} value - TBD.
4
+ * @param {number} size - TBD.
5
+ * @returns {string} TBD.
6
+ */
7
+ export function addLeadingZero(value: number | string, size?: number): string;
8
+ //# sourceMappingURL=addLeadingZero.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addLeadingZero.d.ts","sourceRoot":"","sources":["../../src/util/addLeadingZero.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,sCAJW,MAAM,GAAC,MAAM,SACb,MAAM,GACJ,MAAM,CAYlB"}