Skip to content

Commit

Permalink
refactoring: remove replaceUnwantedChar from abe-util
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 5, 2016
1 parent 434e734 commit 271bd58
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
22 changes: 22 additions & 0 deletions src/cli/cms/data/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,26 @@ export function getAll(str, json) {
attrs = Hooks.instance.trigger('afterAbeAttributes', attrs, str, json)

return attrs
}


export function sanitizeSourceAttribute(obj, jsonPage){
if(typeof obj.sourceString !== 'undefined' && obj.sourceString !== null && obj.sourceString.indexOf('{{') > -1) {
var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g)
if(matches !== null) {
Array.prototype.forEach.call(matches, (match) => {
var val = match.replace('{{', '')
val = val.replace('}}', '')

try {
val = eval('jsonPage.' + val)
}catch(e) {
val = ''
}
obj.sourceString = obj.sourceString.replace(match, val)
})
}
}

return obj
}
2 changes: 1 addition & 1 deletion src/cli/cms/data/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function nextDataList(tplPath, jsonPage, match) {
}

var obj = cmsData.attributes.getAll(match, jsonPage)
obj = Util.sanitizeSourceAttribute(obj, jsonPage)
obj = cmsData.attributes.sanitizeSourceAttribute(obj, jsonPage)

var type = cmsData.sql.getSourceType(obj.sourceString)

Expand Down
4 changes: 2 additions & 2 deletions src/cli/cms/templates/handlebars/cleanTab.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Util} from '../../../'
import {coreUtils} from '../../../'

export default function cleanTab(obj) {
obj = Util.replaceUnwantedChar(obj.replace(/ |&/g, '_'))
obj = coreUtils.text.replaceUnwantedChar(obj.replace(/ |&/g, '_'))

return obj
}
27 changes: 0 additions & 27 deletions src/cli/core/utils/abe-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,4 @@ export default class Utils {
this._key[obj.key] = true // save key for dontHaveKey()
this._form[obj.tab].item.push(obj)
}

static sanitizeSourceAttribute(obj, jsonPage){
if(typeof obj.sourceString !== 'undefined' && obj.sourceString !== null && obj.sourceString.indexOf('{{') > -1) {
var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g)
if(matches !== null) {
Array.prototype.forEach.call(matches, (match) => {
var val = match.replace('{{', '')
val = val.replace('}}', '')

try {
val = eval('jsonPage.' + val)
}catch(e) {
val = ''
}
obj.sourceString = obj.sourceString.replace(match, val)
})
}
}

return obj
}

static replaceUnwantedChar(str) {
var chars = {'’': '', '\'': '', '"': '', 'Š': 'S', 'š': 's', 'Ž': 'Z', 'ž': 'z', 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'A', 'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I', 'Î': 'I', 'Ï': 'I', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U', 'Ý': 'Y', 'Þ': 'B', 'ß': 'Ss', 'à': 'a', 'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'a', 'ç': 'c', 'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e', 'œ': 'oe', 'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i', 'ð': 'o', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ý': 'y', 'þ': 'b', 'ÿ': 'y'}
for(var prop in chars) str = str.replace(new RegExp(prop, 'g'), chars[prop])
return str
}
}
2 changes: 2 additions & 0 deletions src/cli/core/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as sort from './sort'
import * as text from './text'

export {
sort
,text
}
5 changes: 5 additions & 0 deletions src/cli/core/utils/text.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions test/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var cmsData = require('../src/cli').cmsData;
var Manager = require('../src/cli').Manager;
var fse = require('fs-extra');

describe('Request', function() {
describe('attributes', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
Expand All @@ -22,12 +22,21 @@ describe('Request', function() {

}.bind(this))
});

/**
* getAbeImport
* cmsData.values.removeDuplicate
*
*/
it('cmsData.values.removeDuplicate', function() {
var newJson = cmsData.values.removeDuplicate(this.fixture.html, this.fixture.json)
chai.expect(newJson.title).to.be.undefined;
var newJson = cmsData.values.removeDuplicate(this.fixture.html, this.fixture.json)
chai.expect(newJson.title).to.be.undefined;
});

/**
* cmsData.attributes.sanitizeSourceAttribute
*
*/
it('cmsData.attributes.sanitizeSourceAttribute', function() {
// not sure what it does
});
});
14 changes: 14 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var chai = require('chai');
var coreUtils = require('../src/cli').coreUtils

describe('Util', function() {

/**
* coreUtils.text.replaceUnwantedChar
*
*/
it('coreUtils.text.replaceUnwantedChar', function() {
var list = coreUtils.text.replaceUnwantedChar('Š')
chai.expect(list.indexOf('Š')).to.equal(-1);
});
});

0 comments on commit 271bd58

Please sign in to comment.