Skip to content

Commit

Permalink
move titleCase to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchappell committed Jan 26, 2020
1 parent 14f492c commit 3e742a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/demo/js/actionButtons.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { titleCase } from './utils'
import { titleCase } from '../../js/utils'

export const setCurrentFieldIdValues = value => {
const currentFieldIds = document.querySelectorAll('.current-field-id')
Expand Down
51 changes: 44 additions & 7 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export const mobileClass = () => {
// eslint-disable-next-line
if (
/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
a
a,
)
) {
mobileClass = 'formbuilder-mobile'
Expand Down Expand Up @@ -575,7 +575,7 @@ export const insertStyle = srcs => {
srcs = Array.isArray(srcs) ? srcs : [srcs]
const promises = srcs.map(
({ src, id }) =>
new Promise((resolve) => {
new Promise(resolve => {
if (window.fbLoaded.css.includes(src)) {
return resolve(src)
}
Expand All @@ -586,7 +586,7 @@ export const insertStyle = srcs => {
})

document.head.insertBefore(formeoStyle, document.head.firstChild)
})
}),
)

return Promise.all(promises)
Expand All @@ -597,6 +597,42 @@ export const removeStyle = id => {
return elem.parentElement.removeChild(elem)
}

/**
*
* @param {String} str
* @return {String} titleized string
*/
export function titleCase(str) {
const lowers = [
'a',
'an',
'and',
'as',
'at',
'but',
'by',
'for',
'for',
'from',
'in',
'into',
'near',
'nor',
'of',
'on',
'onto',
'or',
'the',
'to',
'with',
].map(lower => `\\s${lower}\\s`)
const regex = new RegExp(`(?!${lowers.join('|')})\\w\\S*`, 'g')
return `${str}`.replace(
regex,
txt => txt.charAt(0).toUpperCase() + txt.substr(1).replace(/[A-Z]/g, word => ` ${word}`),
)
}

const utils = {
addEventListeners,
attrString,
Expand Down Expand Up @@ -629,6 +665,7 @@ const utils = {
trimObj,
unique,
validAttr,
titleCase,
}

/**
Expand All @@ -649,11 +686,11 @@ utils.splitObject = (obj, keys) => {
}

const kept = Object.keys(obj)
.filter(key => keys.includes(key))
.reduce(reconstructObj(obj), {})
.filter(key => keys.includes(key))
.reduce(reconstructObj(obj), {})
const rest = Object.keys(obj)
.filter(key => !keys.includes(key))
.reduce(reconstructObj(obj), {})
.filter(key => !keys.includes(key))
.reduce(reconstructObj(obj), {})
return [kept, rest]
}

Expand Down

0 comments on commit 3e742a0

Please sign in to comment.