Skip to content

Commit

Permalink
Added Logger class, fixed lint and typecheck errors, release v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vpmedia committed Apr 13, 2024
1 parent 74272c7 commit 0a0a6fd
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 1.2.0

- Added Logger class
- Fixed lint and typecheck issues

## 1.1.0

- Updated dependencies
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @vpmedia/simplify

[![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.1.0)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
[![npm version](https://badge.fury.io/js/@vpmedia%2Fsimplify.svg?v=1.2.0)](https://badge.fury.io/js/@vpmedia%2Fsimplify)
[![Node.js CI](https://github.com/vpmedia/simplify/actions/workflows/ci.yml/badge.svg)](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)

@vpmedia/simplify TBD
Expand Down
9 changes: 3 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ export default [
'unicorn/filename-case': 'off',
'unicorn/no-null': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/no-useless-undefined': 'warn',
'unicorn/better-regex': 'warn',
'unicorn/prefer-string-replace-all': 'warn',
'no-unused-vars': 'warn',
'prefer-arrow-callback': 'warn',
'prefer-template': 'warn',
'prefer-arrow-callback': 'error',
'prefer-template': 'error',
'import/extensions': ['warn', 'always'],
'jsdoc/require-jsdoc': [
'warn',
'error',
{
require: {
FunctionDeclaration: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vpmedia/simplify",
"version": "1.1.0",
"version": "1.2.0",
"description": "@vpmedia/simplify",
"author": "Andras Csizmadia <[email protected]> (www.vpmedia.hu)",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// core
export { Logger } from './logging/Logger.js';
export { addLeadingZero } from './util/addLeadingZero.js';
export { capitalize } from './util/capitalize.js';
export { getObjValueByPath } from './util/getObjValueByPath.js';
Expand All @@ -7,4 +8,3 @@ export { getURLParam } from './util/getURLParam.js';
export { sanitizeURLParam } from './util/sanitizeURLParam.js';
export { setObjValueByPath } from './util/setObjValueByPath.js';
export { underscoreToCamelCase } from './util/underscoreToCamelCase.js';

113 changes: 113 additions & 0 deletions src/logging/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { getURLParam } from '../util/getURLParam.js';

export const LEVEL_DEBUG = 4;
export const LEVEL_INFO = 3;
export const LEVEL_WARN = 2;
export const LEVEL_ERROR = 1;
export const LEVEL_SILENT = 0;

export class Logger {
/**
* @type {(error) => {}}
*/
static exceptionHandler = null;

/**
* Creates a new Logger instance.
* @param {string} name - The logger name.
*/
constructor(name) {
this.name = name;
const appEnvironment = process.env.APP_ENVIRONMENT || 'local';
const isProduction = appEnvironment === 'production' || appEnvironment === 'release';
const defaultLevel = isProduction ? LEVEL_SILENT : LEVEL_DEBUG;
const parameterName = `log_${this.name.toLowerCase()}`;
this.level = Number.parseInt(getURLParam(parameterName, defaultLevel), 10);
const allLevel = Number.parseInt(getURLParam('log_all', LEVEL_SILENT), 10);
this.level = allLevel || this.level;
}

/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
debug(message, extraData = null) {
if (this.level < LEVEL_DEBUG) {
return;
}
const logMessage = `${Date.now()} [${this.name}] ${message}`;
if (extraData !== null) {
console.debug(logMessage, extraData);
return;
}
console.debug(logMessage);
}

/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
info(message, extraData = null) {
if (this.level < LEVEL_INFO) {
return;
}
const logMessage = `${Date.now()} [${this.name}] ${message}`;
if (extraData !== null) {
console.info(logMessage, extraData);
return;
}
console.info(logMessage);
}

/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
warn(message, extraData = null) {
if (this.level < LEVEL_WARN) {
return;
}
const logMessage = `${Date.now()} [${this.name}] ${message}`;
if (extraData !== null) {
console.warn(logMessage, extraData);
return;
}
console.warn(logMessage);
}

/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
error(message, extraData = null) {
if (this.level < LEVEL_ERROR) {
return;
}
const logMessage = `${Date.now()} [${this.name}] ${message}`;
if (extraData !== null) {
console.error(logMessage, extraData);
return;
}
console.error(logMessage);
}

/**
* TBD.
* @param {string} message - TBD.
* @param {Error} exception - TBD.
*/
exception(message, exception) {
if (Logger.exceptionHandler) {
Logger.exceptionHandler(exception);
}
if (this.level < LEVEL_ERROR) {
return;
}
const logMessage = `${Date.now()} [${this.name}] ${message}`;
console.error(logMessage, exception);
}
}
14 changes: 14 additions & 0 deletions src/logging/Logger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LEVEL_DEBUG, Logger } from './Logger.js';

test('Tests Logger', () => {
const logger = new Logger('test');
expect(Logger.exceptionHandler).toBe(null);
expect(logger.level).toBe(LEVEL_DEBUG);
let handledError = null;
const exceptionHandler = (error) => {
handledError = error;
};
Logger.exceptionHandler = exceptionHandler;
logger.exception('test', 'error');
expect(handledError).toBe('error');
});
3 changes: 2 additions & 1 deletion src/util/addLeadingZero.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/
export function addLeadingZero(value, size = 2) {
if (value === null || value === undefined) {
// @ts-ignore: Type 'string | number' is not assignable to type 'string'.
return value;
}
value = value.toString();
while (value.length < size) {
value = '0' + value;
value = `0${value}`;
}
return value;
}
2 changes: 1 addition & 1 deletion src/util/addLeadingZero.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test('Tests add leading zero', () => {
expect(addLeadingZero('21')).toBe('21');
expect(addLeadingZero(21)).toBe('21');
expect(addLeadingZero(null)).toBe(null);
expect(addLeadingZero(undefined)).toBe(undefined);
expect(addLeadingZero()).toBe(undefined);
});
2 changes: 1 addition & 1 deletion src/util/sanitizeURLParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export function sanitizeURLParam(input) {
if (!input) {
return input;
}
return input.replace(/[^a-zA-Z0-9-_]/gi, '');
return input.replaceAll(/[^\w-]/gi, '');
}
2 changes: 1 addition & 1 deletion src/util/underscoreToCamelCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @returns {string} The output string in camel case.
*/
export function underscoreToCamelCase(value) {
return value.replace(/(_\w)/g, function (m) {
return value.replaceAll(/(_\w)/g, (m) => {
return m[1].toUpperCase();
});
}
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"jsx": "react-jsx",
"module": "NodeNext",
"moduleResolution": "nodenext",
"types": [
"jest",
"node"
],
"types": ["jest", "node"],
"allowJs": true,
"checkJs": true,
"declaration": true,
Expand Down
17 changes: 9 additions & 8 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { capitalize } from './util/capitalize.js';
import { getObjValueByPath } from './util/getObjValueByPath.js';
import { getRandomInt } from './util/getRandomInt.js';
import { getURLParam } from './util/getURLParam.js';
import { sanitizeURLParam } from './util/sanitizeURLParam.js';
import { setObjValueByPath } from './util/setObjValueByPath.js';
import { underscoreToCamelCase } from './util/underscoreToCamelCase.js';
export { capitalize, getObjValueByPath, getRandomInt, getURLParam, sanitizeURLParam, setObjValueByPath, underscoreToCamelCase };
export { Logger } from "./logging/Logger.js";
export { addLeadingZero } from "./util/addLeadingZero.js";
export { capitalize } from "./util/capitalize.js";
export { getObjValueByPath } from "./util/getObjValueByPath.js";
export { getRandomInt } from "./util/getRandomInt.js";
export { getURLParam } from "./util/getURLParam.js";
export { sanitizeURLParam } from "./util/sanitizeURLParam.js";
export { setObjValueByPath } from "./util/setObjValueByPath.js";
export { underscoreToCamelCase } from "./util/underscoreToCamelCase.js";
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion types/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions types/logging/Logger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export const LEVEL_DEBUG: 4;
export const LEVEL_INFO: 3;
export const LEVEL_WARN: 2;
export const LEVEL_ERROR: 1;
export const LEVEL_SILENT: 0;
export class Logger {
/**
* @type {(error) => {}}
*/
static exceptionHandler: (error) => {};
/**
* Creates a new Logger instance.
* @param {string} name - The logger name.
*/
constructor(name: string);
name: string;
level: number;
/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
debug(message: string, extraData?: object): void;
/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
info(message: string, extraData?: object): void;
/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
warn(message: string, extraData?: object): void;
/**
* TBD.
* @param {string} message - TBD.
* @param {object} extraData - TBD.
*/
error(message: string, extraData?: object): void;
/**
* TBD.
* @param {string} message - TBD.
* @param {Error} exception - TBD.
*/
exception(message: string, exception: Error): void;
}
//# sourceMappingURL=Logger.d.ts.map
1 change: 1 addition & 0 deletions types/logging/Logger.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions types/util/addLeadingZero.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* TBD.
* @param {number|string} value - TBD.
* @param {number} size - TBD.
* @returns {string} TBD.
*/
export function addLeadingZero(value: number | string, size?: number): string;
//# sourceMappingURL=addLeadingZero.d.ts.map
1 change: 1 addition & 0 deletions types/util/addLeadingZero.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a0a6fd

Please sign in to comment.