Skip to content

Commit 4fd2ecd

Browse files
authored
Merge pull request infinitered#570 from megazoll/fix-log-command
Add preview for object, null and undefined
2 parents 4aa0219 + 0dfd1a9 commit 4fd2ecd

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/reactotron-app/App/Commands/LogCommand.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
33
import { inject, observer } from 'mobx-react'
44
import Command from '../Shared/Command'
5-
import { max, min, slice, is, join, take, replace, merge, map, split, last, takeLast } from 'ramda'
5+
import {
6+
max, min, slice, is, join, take, replace, merge, map, split, last, takeLast,
7+
lt, keys, length, pickBy, when, always, pipe, isNil
8+
} from 'ramda'
69
import { isNilOrEmpty, dotPath } from 'ramdasauce'
710
import Colors from '../Theme/Colors'
811
import AppStyles from '../Theme/AppStyles'
912
import Content from '../Shared/Content'
1013
import ReactTooltip from 'react-tooltip'
1114
import fs from 'fs'
15+
import stringifyObject from 'stringify-object'
1216

1317
const PREVIEW_LENGTH = 500
1418
const SOURCE_LINES_UP = 3
1519
const SOURCE_LINES_DOWN = 3
1620
const SOURCE_FILE_PATH_COUNT = 3
21+
const OBJECT_KEYS_COUNT = 5
22+
const OBJECT_PROPERTY_PREVIEW_LENGTH = 80
1723

1824
const getName = level => {
1925
switch (level) {
@@ -328,7 +334,27 @@ class LogCommand extends Component {
328334

329335
getPreview (message) {
330336
if (typeof message === 'string') {
331-
return `${take(PREVIEW_LENGTH, message)}`
337+
return take(PREVIEW_LENGTH, message)
338+
} else if (is(Object, message)) {
339+
const moreKeys = lt(OBJECT_KEYS_COUNT, length(keys(message)))
340+
let i = 0
341+
const previewMessage = moreKeys
342+
? pickBy(() => i++ < OBJECT_KEYS_COUNT, message)
343+
: message;
344+
345+
const preview = stringifyObject(previewMessage, {
346+
transform: (obj, prop, originalResult) => {
347+
if (is(Object, obj[prop])) {
348+
return '{...}';
349+
} else {
350+
return take(OBJECT_PROPERTY_PREVIEW_LENGTH, originalResult);
351+
}
352+
}
353+
})
354+
355+
return when(always(moreKeys), replace(/\s\}$/i, ', ...}'))(preview)
356+
} else if (isNil(message)) {
357+
return String(message)
332358
}
333359
return null
334360
}

0 commit comments

Comments
 (0)