Skip to content

Commit

Permalink
Merge pull request #13 from codevor/develop
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
helderberto authored Jan 16, 2020
2 parents 6b208bf + 1e03727 commit 9769aab
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 16 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ Logger.warn('Warning message example.');
// => ⚠️ Warning message example
Logger.info('Information message example.');
// => ℹ️ Informational message example

// PROMISE STATUSES LOGS
Logger.pending('Promise message example.', { hello: true });
// => ℹ️ Promise message example | pending {"hello":true}

Logger.fulfilled('Promise message example.', { hello: true });
// => ✅ Promise message example | fulfilled {"hello":true}

Logger.rejected('Promise message example.', { hello: true });
// => 🚫 Promise message example | rejected {"hello":true}

Logger.settled('Promise message example.', { hello: true });
// => ✅ Promise message example | settled {"hello":true}
```

### With require
Expand All @@ -43,8 +56,23 @@ Logger.warn('Warning message example.');
// => ⚠️ Warning message example
Logger.info('Information message example.');
// => ℹ️ Informational message example

// PROMISE STATUSES LOGS
Logger.pending('Promise message example.', { hello: true });
// => ℹ️ Promise message example | pending {"hello":true}

Logger.fulfilled('Promise message example.', { hello: true });
// => ✅ Promise message example | fulfilled {"hello":true}

Logger.rejected('Promise message example.', { hello: true });
// => 🚫 Promise message example | rejected {"hello":true}

Logger.settled('Promise message example.', { hello: true });
// => ✅ Promise message example | settled {"hello":true}
```

_Note: All promises convert objects to strings_

### Change Emojis

Is it possible to change emojis for each message? **Yes**! Check it out:
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": "@codevor/logger.js",
"version": "0.5.2",
"version": "0.6.0",
"description": "💢 Make logging friendlier, simpler and use emojis in your messages. Try logger!",
"main": "dist/logger.js",
"unpkg": "dist/logger.min.js",
Expand Down
38 changes: 32 additions & 6 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@ import { intercept } from './utils';

const Logger = (() => {
return {
success: (...args) => intercept('success', args),
info: (...args) => intercept('info', args),
warn: (...args) => intercept('warn', args),
error: (...args) => intercept('error', args),
trace: (...args) => intercept('trace', args),
debug: (...args) => intercept('debug', args),
success: (...args) => intercept({ type: 'success', args }),
info: (...args) => intercept({ type: 'info', args }),
warn: (...args) => intercept({ type: 'warn', args }),
error: (...args) => intercept({ type: 'error', args }),
trace: (...args) => intercept({ type: 'trace', args }),
debug: (...args) => intercept({ type: 'debug', args }),

// PROMISE STATUSES LOGS
pending: (...args) => intercept({
type: 'info',
promiseStatus: 'pending',
args,
parse: true
}),
fulfilled: (...args) => intercept({
type: 'success',
promiseStatus: 'fulfilled',
args,
parse: true
}),
rejected: (...args) => intercept({
type: 'error',
promiseStatus: 'rejected',
args,
parse: true
}),
settled: (...args) => intercept({
type: 'success',
promiseStatus: 'settled',
args,
parse: true
}),
}
})();

Expand Down
45 changes: 36 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,49 @@ import Emoji from './emoji';

const isSuccess = type => type === 'success';

const createMessage = (type, message) => {
if (type) {
const emoji = Emoji.getEmoji(type);
return `${emoji} ${message}`;
const isStringEmpty = string => typeof string && string.length === 0;

const isObjectEmpty = (obj = {}) => typeof obj === 'object' && Object.keys(obj).length === 0;

const parseObject = (obj, parse) => !isObjectEmpty(obj) && parse === true ? JSON.stringify(obj) : obj;

const createMessage = (type, message, promiseStatus) => {
const emoji = Emoji.getEmoji(type);
let generatedMessage = `${emoji} ${message}`;

if (!isStringEmpty(promiseStatus)) {
generatedMessage = `${generatedMessage} | ${promiseStatus}`
}
return message;

return generatedMessage;
}

export const intercept = (type, args) => {
const createConsoleArgs = ({ message, params, parse, args }) => {
const consoleArgs = [message];

if (!isObjectEmpty(params)) {
const parsedObject = parseObject(params, parse);
consoleArgs.push(parsedObject);
}

consoleArgs.push(...args);

return consoleArgs;
}

const getLogType = type => isSuccess(type) ? 'log' : type;

export const intercept = ({ type, args, parse = false, promiseStatus = '' }) => {
const message = args.shift();
const params = args.shift();

const parsedMessage = createMessage(type, message, promiseStatus);

const parsedMessage = createMessage(type, message);
const consoleArgs = createConsoleArgs({ message: parsedMessage, params, parse, args });

const currentType = isSuccess(type) ? 'log' : type;
const currentType = getLogType(type);

console[currentType].call(console, parsedMessage, ...args);
console[currentType].call(console, ...consoleArgs);

return parsedMessage;
}
24 changes: 24 additions & 0 deletions tests/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ describe('Logger', () => {
'🐛 Debug message example'
);
});

test('should create Logger.pending', () => {
expect(Logger.pending('Pending message example', { helloWorld: true })).toEqual(
'ℹ️ Pending message example | pending'
);
});

test('should create Logger.fulfilled', () => {
expect(Logger.fulfilled('Fulfilled message example', { helloWorld: true })).toEqual(
'✅ Fulfilled message example | fulfilled'
);
});

test('should create Logger.rejected', () => {
expect(Logger.rejected('Rejected message example', { helloWorld: true })).toEqual(
'🚫 Rejected message example | rejected'
);
});

test('should create Logger.settled', () => {
expect(Logger.settled('Settled message example', { helloWorld: true })).toEqual(
'✅ Settled message example | settled'
);
});
});

0 comments on commit 9769aab

Please sign in to comment.