Skip to content

Commit

Permalink
chore: update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Sep 7, 2021
1 parent d449567 commit 2d51fea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
35 changes: 12 additions & 23 deletions packages/griffith-utils/src/__tests__/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,42 @@ import logger from '../logger'

describe('logger', () => {
beforeAll(() => {
// @ts-expect-error ts-migrate(2740) FIXME: Type '{ warn: Mock<any, any>; log: Mock<any, any>;... Remove this comment to see the full error message
global.console = {
Object.assign(console, {
warn: jest.fn(),
log: jest.fn(),
error: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
}
})
})

it('should be executing console log', () => {
logger.log('log', '@zhihu-griffith')
logger.log('log', 'foo')

expect(global.console.log).toHaveBeenCalledWith(
'[griffith] @zhihu-griffith'
)
expect(global.console.log).toHaveBeenCalledWith('[griffith]', 'foo')
})

it('should be executing console debug', () => {
logger.debug('@zhihu-griffith')
logger.debug('foo')

expect(global.console.debug).toHaveBeenCalledWith(
'[griffith] @zhihu-griffith'
)
expect(global.console.debug).toHaveBeenCalledWith('[griffith]', 'foo')
})

it('should be executing console info', () => {
logger.info('@zhihu-griffith')
logger.info('foo')

expect(global.console.info).toHaveBeenCalledWith(
'[griffith] @zhihu-griffith'
)
expect(global.console.info).toHaveBeenCalledWith('[griffith]', 'foo')
})

it('should be executing console warn', () => {
logger.warn('@zhihu-griffith')
logger.warn('foo')

expect(global.console.warn).toHaveBeenCalledWith(
'[griffith] @zhihu-griffith'
)
expect(global.console.warn).toHaveBeenCalledWith('[griffith]', 'foo')
})

it('should be executing console error', () => {
logger.error('@zhihu-griffith')
logger.error('foo')

expect(global.console.error).toHaveBeenCalledWith(
'[griffith] @zhihu-griffith'
)
expect(global.console.error).toHaveBeenCalledWith('[griffith]', 'foo')
})
})
4 changes: 4 additions & 0 deletions packages/griffith-utils/src/__tests__/ua.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment jsdom
*/

test('ua', () => {
Object.defineProperty(window.navigator, 'userAgent', {
value:
Expand Down
5 changes: 3 additions & 2 deletions packages/griffith-utils/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
enum LogLevel {
LOG = 'log',
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
}

const logger = {
log(level: LogLevel, ...params: any[]) {
log(level: `${LogLevel}`, ...params: any[]) {
/* eslint-disable no-console */
const method = console[level] ? level : 'log'
console[method].bind(console, '[griffith]')(...params)
console[method]('[griffith]', ...params)
/* eslint-enable no-console */
},

Expand Down
4 changes: 0 additions & 4 deletions packages/griffith-utils/src/math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* 获得最大公约数
* @param {number} a
* @param {number} b
*/
export function getGCD(a: number, b: number): number {
if (!Number.isInteger(a) || !Number.isInteger(b)) {
Expand All @@ -18,8 +16,6 @@ export function getGCD(a: number, b: number): number {

/**
* 约分
* @param {number} a
* @param {number} b
*/
export function reduce(a: number, b: number) {
if (a === 0 || b === 0) {
Expand Down

0 comments on commit 2d51fea

Please sign in to comment.