From 327cb883eee0258c3cb3e2f4131ee613b06a1d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Zen?= Date: Tue, 7 Jun 2016 21:27:34 -0300 Subject: [PATCH] Refinment: truncate in middle --- src/components/Widgets/ImageControl.js | 9 +++++---- src/lib/textHelper.js | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 src/lib/textHelper.js diff --git a/src/components/Widgets/ImageControl.js b/src/components/Widgets/ImageControl.js index f6322fd0..8c05ce94 100644 --- a/src/components/Widgets/ImageControl.js +++ b/src/components/Widgets/ImageControl.js @@ -1,4 +1,5 @@ import React from 'react'; +import { truncateMiddle } from '../../lib/textHelper'; import ImageProxy from '../../valueObjects/ImageProxy'; const MAX_DISPLAY_LENGTH = 50; @@ -71,11 +72,11 @@ export default class ImageControl extends React.Component { renderImageName() { if (!this.state.currentImage) return null; - if (this.state.currentImage.uri.length < MAX_DISPLAY_LENGTH) { - return this.state.currentImage.uri; + const { uri } = this.state.currentImage; + if (uri.length <= MAX_DISPLAY_LENGTH) { + return uri; } - - return this.state.currentImage.uri.substring(0, MAX_DISPLAY_LENGTH) + '\u2026'; + return truncateMiddle(uri, MAX_DISPLAY_LENGTH); } render() { diff --git a/src/lib/textHelper.js b/src/lib/textHelper.js new file mode 100644 index 00000000..4fd0649a --- /dev/null +++ b/src/lib/textHelper.js @@ -0,0 +1,3 @@ +export function truncateMiddle(string, size) { + return string.substring(0, size / 2) + '\u2026' + string.substring(string.length - size / 2 + 1, string.length); +}