Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Update stringUtils to use lodash library
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed May 13, 2016
1 parent 13cc160 commit 4b124b8
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 64 deletions.
4 changes: 2 additions & 2 deletions frontend/src/main/web/docs/styles.md
Expand Up @@ -2,7 +2,7 @@

## Design Concepts

There are some core concepts that Zanata and each component are designed around
There are some core concepts that Zanata and each component are designed around
to keep a consistent look and feel.

### Color Palette
Expand Down Expand Up @@ -103,7 +103,7 @@ overrides, as it takes precedence over any theme classes.

Each component should declare a single `classes` variable, directly after it's
imports. This classes variable can then be merged with it's own theme prop (to
allow for external theming) and then it can be merged again for any state-based
allow for external theming) and then it can be merged again for any state-based
classes.

The `classes` variable can contain classes for more than 1 element, if more than
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/main/web/src/components/Flex/index.jsx
Expand Up @@ -80,14 +80,14 @@ Flex.propTypes = {
* This defines the default behaviour for how flex items are laid out along
* the cross axis on the current line. Think of it as the justify-content
* version for the cross-axis (perpendicular to the main-axis).
* See http://acss.io/reference for more information on Flex box
* See http://acss.io/reference for atomic class letter code meanings.
*/
align: PropTypes.oneOf(['fs', 'fe', 'c', 'b', 'st']),
/**
* This aligns a flex container's lines within when there is extra space in
* the cross-axis, similar to how justify-content aligns individual items
* within the main-axis.
* See http://acss.io/reference for more information on Flex box
* See http://acss.io/reference for atomic class letter code meanings.
* **Note:** this property has no effect when there is only one line of flex
* items.
*/
Expand All @@ -98,7 +98,7 @@ Flex.propTypes = {
* placed in the flex container. Flexbox is (aside from optional wrapping) a
* single-direction layout concept. Think of flex items as primarily laying
* out either in horizontal rows or vertical columns.
* See http://acss.io/reference for more information on Flex box
* See http://acss.io/reference for atomic class letter code meanings.
*/
dir: PropTypes.oneOf(['r', 'rr', 'c', 'cr']),
/**
Expand All @@ -107,14 +107,14 @@ Flex.propTypes = {
* inflexible, or are flexible but have reached their maximum size. It also
* exerts some control over the alignment of items when they overflow
* the line.
* See http://acss.io/reference for more information on Flex box
* See http://acss.io/reference for atomic class letter code meanings.
*/
justify: PropTypes.oneOf(['fs', 'fe', 'c', 'sb', 'sa']),
/**
* By default, flex items will all try to fit onto one line. You can change
* that and allow the items to wrap as needed with this property. Direction
* also plays a role here, determining the direction new lines are stacked in.
* See http://acss.io/reference for more information on Flex box
* See http://acss.io/reference for atomic class letter code meanings.
*/
wrap: PropTypes.oneOf(['nw', 'w', 'wr']),
theme: PropTypes.object
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/main/web/src/containers/Glossary/EntryModal.js
@@ -1,4 +1,5 @@
import React, { Component, PropTypes } from 'react'
import { isEmpty } from 'lodash'
import {
ButtonLink,
ButtonRound,
Expand All @@ -8,7 +9,6 @@ import {
Modal,
Row
} from '../../components'
import { isEmptyOrNull } from '../../utils/StringUtils'

/**
* Popup windows to display a glossary entry
Expand All @@ -33,7 +33,7 @@ class EntryModal extends Component {
const comment = entry.transTerm ? entry.transTerm.comment : ''

const enableComment = transSelected &&
entry.transTerm && !isEmptyOrNull(transContent)
entry.transTerm && !isEmpty(transContent)

return (
<Modal show={show}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/main/web/src/containers/Glossary/ImportModal.js
@@ -1,5 +1,6 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { isEmpty } from 'lodash'

import {
ButtonLink,
Expand Down Expand Up @@ -51,7 +52,7 @@ class ImportModal extends Component {

if (this.isSupportedFile(fileExtension) && !isUploading) {
if (fileExtension === 'po') {
if (!StringUtils.isEmptyOrNull(transLocale)) {
if (!isEmpty(transLocale)) {
disableUpload = false
}
langSelection = (<Select
Expand Down
@@ -1,6 +1,6 @@
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { cloneDeep } from 'lodash'
import { cloneDeep, isEmpty } from 'lodash'

import {
ButtonLink,
Expand Down Expand Up @@ -78,8 +78,7 @@ class NewEntryModal extends Component {
handleNewEntryDisplay,
handleNewEntryCreate
} = this.props
const isAllowSave =
!StringUtils.isEmptyOrNull(this.state.entry.srcTerm.content)
const isAllowSave = !isEmpty(this.state.entry.srcTerm.content)

return (
<Modal
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/main/web/src/containers/Glossary/ViewHeader.js
Expand Up @@ -212,7 +212,7 @@ ViewHeader.propTypes = {
})
).isRequired,
filterText: PropTypes.string,
selectedTransLocale: PropTypes.object,
selectedTransLocale: PropTypes.string,
permission: PropTypes.shape({
canAddNewEntry: PropTypes.bool,
canUpdateEntry: PropTypes.bool,
Expand All @@ -234,7 +234,6 @@ const mapStateToProps = (state) => {
sort
} = state.glossary
const query = state.routing.location.query
console.info(sort)
return {
termCount,
statsLoading,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/main/web/src/reducers/common.js
Expand Up @@ -11,7 +11,7 @@ export default handleActions({
notification: null
}
}
},
},
// default state
{
locales: [],
Expand Down
18 changes: 6 additions & 12 deletions frontend/src/main/web/src/reducers/glossary.js
Expand Up @@ -40,7 +40,6 @@ import {
DEFAULT_LOCALE
} from '../actions/common'
import GlossaryHelper from '../utils/GlossaryHelper'
import { isEmptyOrNull } from '../utils/StringUtils'

const glossary = handleActions({
[CLEAR_MESSAGE]: (state, action) => {
Expand Down Expand Up @@ -133,41 +132,37 @@ const glossary = handleActions({
}
}),
[GLOSSARY_UPDATE_IMPORT_FILE]: (state, action) => {
const importFile = state.importFile
return {
...state,
importFile: {
...importFile,
...state.importFile,
file: action.payload
}
}
},
[GLOSSARY_UPDATE_IMPORT_FILE_LOCALE]: (state, action) => {
const importFile = state.importFile
return {
...state,
importFile: {
...importFile,
...state.importFile,
transLocale: action.payload
}
}
},
[GLOSSARY_TOGGLE_IMPORT_DISPLAY]: (state, action) => {
const importFile = state.importFile
return {
...state,
importFile: {
...importFile,
...state.importFile,
show: action.payload
}
}
},
[GLOSSARY_TOGGLE_NEW_ENTRY_DISPLAY]: (state, action) => {
const newEntry = state.newEntry
return {
...state,
newEntry: {
...newEntry,
...state.newEntry,
show: action.payload
}
}
Expand All @@ -192,7 +187,7 @@ const glossary = handleActions({
GlossaryHelper.generateEmptyTerm(state.locale)
newSelectedTerm.transTerm.content = action.payload.value
}
if (isEmptyOrNull(newSelectedTerm.transTerm.content)) {
if (isEmpty(newSelectedTerm.transTerm.content)) {
newSelectedTerm.transTerm.comment = null
}
break
Expand Down Expand Up @@ -279,11 +274,10 @@ const glossary = handleActions({
}

const entryId = action.payload
const deleting = state.deleting
return {
...state,
deleting: {
...deleting,
...state.deleting,
[entryId]: entryId
}
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/main/web/src/utils/DateHelper.js
@@ -1,6 +1,7 @@
import moment from 'moment'
//this import MUST come after import for moment
import 'moment-range'
import { isEmptyOrNull } from './StringUtils'
import { isEmpty } from 'lodash'

var DateHelper = {
dateFormat: 'YYYY-MM-DD',
Expand Down Expand Up @@ -67,7 +68,7 @@ var DateHelper = {
},

getDate: function (milliseconds) {
if (!isEmptyOrNull(milliseconds)) {
if (!isEmpty(milliseconds)) {
return new Date(milliseconds)
}
return
Expand Down
25 changes: 13 additions & 12 deletions frontend/src/main/web/src/utils/GlossaryHelper.js
@@ -1,20 +1,21 @@
import { isUndefined, filter, forOwn } from 'lodash'
import { trim, isEmptyOrNull } from './StringUtils'
import { trim } from './StringUtils'
import { isEmpty } from 'lodash'
import DateHelpers from './DateHelper'
import defined from 'defined'

var GlossaryHelper = {
/**
* Generate org.zanata.rest.dto.GlossaryTerm object
* returns null if data is undefined or locale is empty
* returns undefined if data is undefined or locale is empty
*
* @param data
*/
generateTermDTO: function (data, trimContent) {
if (isUndefined(data) || isEmptyOrNull(data.locale)) {
if (isUndefined(data) || isEmpty(data.locale)) {
return
}
const comment = isEmptyOrNull(data.content) ? null : trim(data.comment)
const comment = isEmpty(data.content) ? undefined : trim(data.comment)
return {
content: trimContent ? trim(data.content) : data.content,
locale: data.locale,
Expand Down Expand Up @@ -58,21 +59,21 @@ var GlossaryHelper = {
}
},

generateSrcTerm: function (localeId) {
generateEmptySrcTerm: function (localeId) {
let term = this.generateEmptyTerm(localeId)
term['reference'] = ''
term.reference = ''
return term
},

getTermByLocale: function (terms, localeId) {
let term = filter(terms, ['locale', localeId])
return term.length ? term[0] : null
return term.length ? term[0] : undefined
},

generateEmptyEntry: function (srcLocaleId) {
return {
description: null,
pos: null,
description: undefined,
pos: undefined,
srcTerm: this.generateEmptyTerm(srcLocaleId)
}
},
Expand All @@ -81,7 +82,7 @@ var GlossaryHelper = {
let srcTerm =
this.getTermByLocale(entry.glossaryTerms, entry.srcLang)
srcTerm.reference = entry.sourceReference
if (!isEmptyOrNull(srcTerm.lastModifiedDate)) {
if (!isEmpty(srcTerm.lastModifiedDate)) {
srcTerm.lastModifiedDate =
DateHelpers.shortDate(DateHelpers.getDate(srcTerm.lastModifiedDate))
}
Expand Down Expand Up @@ -112,7 +113,7 @@ var GlossaryHelper = {
},

toEmptyString: (val) => {
return isEmptyOrNull(val) ? '' : val
return isEmpty(val) ? '' : val
},

getEntryStatus: function (entry, originalEntry) {
Expand All @@ -139,7 +140,7 @@ var GlossaryHelper = {
(source !== ori_source)
let isTransModified = (trans !== ori_trans) || (comment !== ori_comment)

let isSrcValid = !isEmptyOrNull(trim(source))
let isSrcValid = !isEmpty(trim(source))

return {
isSrcModified: isSrcModified,
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/main/web/src/utils/StringUtils.js
@@ -1,11 +1,10 @@
import isEmpty from 'lodash/isEmpty'
import { isEmpty } from 'lodash'

export function isEmptyOrNull (str) { return isEmpty(str) }
export function trimLeadingSpace (str) {
return isEmptyOrNull(str) ? str : str.replace(/^\s+/g, '')
return isEmpty(str) ? str : str.replace(/^\s+/g, '')
}
export function trim (str) {
return isEmptyOrNull(str) ? str : str.trim()
return isEmpty(str) ? str : str.trim()
}

export function isJsonString (str) {
Expand All @@ -18,7 +17,6 @@ export function isJsonString (str) {
}

export default {
isEmptyOrNull,
trimLeadingSpace,
trim,
isJsonString
Expand Down
Expand Up @@ -100,7 +100,7 @@ describe('GlossaryHelperTest', function() {

it('test generate empty src term', function() {
var localeId = 'de';
var term = GlossaryHelper.generateSrcTerm(localeId);
var term = GlossaryHelper.generateEmptySrcTerm(localeId);
expect(term.locale).toEqual(localeId);
});

Expand Down Expand Up @@ -144,7 +144,7 @@ describe('GlossaryHelperTest', function() {
function generateEntry(srcLocale, transLocale) {
return {
id: '', pos: '', description: '',
srcTerm: GlossaryHelper.generateSrcTerm(srcLocale),
srcTerm: GlossaryHelper.generateEmptySrcTerm(srcLocale),
transTerm: GlossaryHelper.generateEmptyTerm(transLocale),
status: GlossaryHelper.getDefaultEntryStatus()
};
Expand Down
18 changes: 3 additions & 15 deletions frontend/src/main/web/src/utils/__tests__/StringUtilsTest.js
Expand Up @@ -2,21 +2,6 @@
var StringUtils = require('../StringUtils')

describe('StringUtilsTest', function () {
it('test empty, null and undefined value', function () {
var value = ''
expect(StringUtils.isEmptyOrNull(value)).toEqual(true)

value = null
expect(StringUtils.isEmptyOrNull(value)).toEqual(true)

value = undefined
expect(StringUtils.isEmptyOrNull(value)).toEqual(true)
})

it('test not empty value', function () {
var value = '123'
expect(StringUtils.isEmptyOrNull(value)).toEqual(false)
})

it('test trim leading space', function () {
var value = ' 123'
Expand All @@ -25,6 +10,9 @@ describe('StringUtilsTest', function () {
value = '123'
expect(StringUtils.trimLeadingSpace(value)).toEqual(value)

value = '123 '
expect(StringUtils.trimLeadingSpace(value)).toEqual(value)

value = null
expect(StringUtils.trimLeadingSpace(value)).toEqual(value)
})
Expand Down

0 comments on commit 4b124b8

Please sign in to comment.