Skip to content

Commit

Permalink
refactor remove deep_value
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 21, 2016
1 parent d5b5ab2 commit 517bb26
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 192 deletions.
35 changes: 22 additions & 13 deletions src/cli/handlebars/abe/sourceAttr.js
Expand Up @@ -8,29 +8,38 @@ export default function sourceAttr(val, params) {
if(typeof hiddenVal === 'object' && Object.prototype.toString.call(hiddenVal) === '[object Object]') {
hiddenVal = JSON.stringify(hiddenVal).replace(/'/g, "'")

var displayVal = Sql.deep_value_array(val, params.display)
if(typeof params.display !== 'undefined' && params.display !== null
&& typeof displayVal !== 'undefined' && displayVal !== null) {
val = displayVal
}else {
try {
var displayVal = eval('val.' + params.display)
if(typeof params.display !== 'undefined' && params.display !== null
&& typeof displayVal !== 'undefined' && displayVal !== null) {
val = displayVal
}else {
val = val[Object.keys(val)[0]]
}
}catch(e) {
val = val[Object.keys(val)[0]]
}
}

if(typeof params.value === 'object' && Object.prototype.toString.call(params.value) === '[object Array]') {
Array.prototype.forEach.call(params.value, (v) => {
var item = v
var displayV = Sql.deep_value_array(item, params.display)
if(typeof params.display !== 'undefined' && params.display !== null
&& typeof displayV !== 'undefined' && displayV !== null) {
item = displayV
}else {
if(typeof v === 'string') {
item = v
try {
var displayV = eval('item.' + params.display)
if(typeof params.display !== 'undefined' && params.display !== null
&& typeof displayV !== 'undefined' && displayV !== null) {
item = displayV
}else {
item = v[Object.keys(v)[0]]
if(typeof v === 'string') {
item = v
}else {
item = v[Object.keys(v)[0]]
}
}
}catch(e) {
item = v[Object.keys(v)[0]]
}

if(typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
&& typeof item === 'object' && Object.prototype.toString.call(item) === '[object Array]') {

Expand Down
33 changes: 0 additions & 33 deletions src/cli/helpers/abe-deep-val.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/cli/helpers/abe-set-deep-value.js

This file was deleted.

85 changes: 24 additions & 61 deletions src/cli/helpers/abe-sql.js
Expand Up @@ -87,11 +87,16 @@ export default class Sql {
while (fromMatch = matchVariable.exec(toReplace)) {
if(typeof fromMatch !== 'undefined' && fromMatch !== null
&& typeof fromMatch[1] !== 'undefined' && fromMatch[1] !== null) {
var value = Sql.deep_value_array(jsonPage, fromMatch[1])
if(typeof value !== 'undefined' && value !== null) {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value)
}else {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '')

try {
var value = eval('jsonPage.' + fromMatch[1])
if(typeof value !== 'undefined' && value !== null) {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value)
}else {
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '')
}
}catch(e) {

}
}
}
Expand Down Expand Up @@ -391,57 +396,6 @@ export default class Sql {
return 'other'
}

static deep_value(obj, pathDeep) {

if(pathDeep.indexOf('.') === -1) {
return (typeof obj[pathDeep] !== 'undefined' && obj[pathDeep] !== null) ? obj[pathDeep] : null
}

var pathSplit = pathDeep.split('.')
var res = JSON.parse(JSON.stringify(obj))
for (var i = 0; i < pathSplit.length; i++) {
if(typeof res[pathSplit[i]] !== 'undefined' && res[pathSplit[i]] !== null) {
res = res[pathSplit[i]]
}else {
return null
}
}

return res
}

static deep_value_array(obj, pathDeep) {

if(pathDeep.indexOf('.') === -1) {
return (typeof obj[pathDeep] !== 'undefined' && obj[pathDeep] !== null) ? obj[pathDeep] : null
}

var pathSplit = pathDeep.split('.')
var res = JSON.parse(JSON.stringify(obj))

while(pathSplit.length > 0) {

if(typeof res[pathSplit[0]] !== 'undefined' && res[pathSplit[0]] !== null) {
if(typeof res[pathSplit[0]] === 'object' && Object.prototype.toString.call(res[pathSplit[0]]) === '[object Array]') {
var resArray = []

Array.prototype.forEach.call(res[pathSplit[0]], (item) => {
resArray.push(Sql.deep_value_array(item, pathSplit.join('.').replace(`${pathSplit[0]}.`, '')))
})
res = resArray
pathSplit.shift()
}else {
res = res[pathSplit[0]]
}
}else {
return null
}
pathSplit.shift()
}

return res
}

static executeWhereClause(files, wheres, maxLimit, columns, jsonPage){
var res = []
var limit = 0
Expand Down Expand Up @@ -634,17 +588,26 @@ export default class Sql {
if(where.left === 'template' || where.left === 'abe_meta.template') {
value = FileParser.getTemplate(json[meta].template)
}else {
value = Sql.deep_value_array(json, where.left)
try {
value = eval('json.' + where.left)
}catch(e) {
// console.log('e', e)
}
}
compare = where.right

var matchVariable = /^{{(.*)}}$/.exec(compare)
if(typeof matchVariable !== 'undefined' && matchVariable !== null && matchVariable.length > 0) {
var shouldCompare = Sql.deep_value_array(jsonPage, matchVariable[1])
if(typeof shouldCompare !== 'undefined' && shouldCompare !== null) {
compare = shouldCompare
}else {
try {
var shouldCompare = eval('jsonPage.' + matchVariable[1])
if(typeof shouldCompare !== 'undefined' && shouldCompare !== null) {
compare = shouldCompare
}else {
shouldAdd = false
}
}catch(e) {
shouldAdd = false
// console.log('e', e)
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/cli/helpers/abe-time-mesure.js
Expand Up @@ -30,8 +30,6 @@ export default class TimeMesure {
*/
duration(str) {
var d = new Date(new Date().getTime() - this._dateStart.getTime()).getTime()
console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
console.log('d', d)
console.log((this._name ? 'end ' + this._name : "") + "(" + clc.green(this._msToTime(d)) + ") " + (str ? str : ""))
}
}
6 changes: 4 additions & 2 deletions src/cli/helpers/abe-utils.js
Expand Up @@ -292,8 +292,10 @@ export default class Utils {
Array.prototype.forEach.call(matches, (match) => {
var val = match.replace('{{', '')
val = val.replace('}}', '')
val = Sql.deep_value_array(jsonPage, val)
if(typeof val === 'undefined' || val === null) {

try {
val = eval('jsonPage.' + val)
}catch(e) {
val = ''
}
obj.sourceString = obj.sourceString.replace(match, val)
Expand Down
64 changes: 3 additions & 61 deletions src/cli/helpers/file-parser.js
Expand Up @@ -16,8 +16,6 @@ import {
,Hooks
,Plugins
,Manager
,set_deep_value
,deep_value
,TimeMesure
} from '../'

Expand Down Expand Up @@ -410,78 +408,22 @@ export default class FileParser {
if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
cleanFile.abe_meta = json.abe_meta
}
/**
* OLD

Array.prototype.forEach.call(withKeys, (key) => {
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log(key, key.split('.').reduce((o,i)=>o[i], json))
var keys = key.split('.')
var firstKey = keys[0]
if(typeof json !== 'undefined' && json !== null
&& typeof json[firstKey] !== 'undefined' && json[firstKey] !== null) {
set_deep_value(cleanFile, key, deep_value(json, key))
}
})
*/
Array.prototype.forEach.call(withKeys, (key) => {
var current = json
key.split('.').forEach(function(p){ current = current[p] })

console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
console.log('current', current)
var keyFirst = key.split('.')[0]
cleanFile[keyFirst] = json[keyFirst]
})
filesArr.push(cleanFile)
// t.duration()
})
t2.duration()

console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
console.log('filesArr[0]', filesArr[0])
var merged = fileUtils.getFilesMerged(filesArr)

Hooks.instance.trigger('afterGetAllFiles', merged)
return merged
}

// static getAllFiles(withKeys) {
// var site = folderUtils.folderInfos(config.root)
// var allDraft = []
// var allPublished = []

// let draft = config.draft.url
// let publish = config.publish.url

// var drafted = FileParser.getFilesByType(path.join(site.path, draft), 'd')
// var published = FileParser.getFilesByType(path.join(site.path, publish))

// drafted = Hooks.instance.trigger('beforeGetAllFilesDraft', drafted)
// published = Hooks.instance.trigger('beforeGetAllFilesPublished', published)

// drafted = FileParser.getMetas(drafted, 'draft')
// published = FileParser.getMetas(published, 'draft')
// var truePublished = []

// published.forEach(function (pub) {

// var json = FileParser.getJson(
// FileParser.changePathEnv(pub.path, config.data.url)
// .replace(new RegExp("\\." + config.files.templates.extension), '.json')
// )

// if(typeof json[config.meta.name] !== 'undefined'
// && json[config.meta.name] !== null
// && typeof json[config.meta.name][config.draft.url] !== 'undefined'
// && json[config.meta.name][config.draft.url] !== null) {
// pub.filePath = json[config.meta.name][config.draft.url].latest.abeUrl
// truePublished.push(pub)
// }
// })
// var merged = fileUtils.mergeFiles(drafted, truePublished)

// site.files = Hooks.instance.trigger('afterGetAllFiles', merged)
// return [site]
// }

// TODO : change the signature of this method to removeFile(file)
static removeFile(file, json) {
if(fileUtils.isFile(file)) {
Expand Down
4 changes: 0 additions & 4 deletions src/cli/index.js
Expand Up @@ -45,8 +45,6 @@ import log from './helpers/abe-logs'
import removeDuplicateAttr from './helpers/abe-remove-duplicate-attr'
import abeCreate from './helpers/abe-create'
import abeDuplicate from './helpers/abe-duplicate'
import deep_value from './helpers/abe-deep-val'
import set_deep_value from './helpers/abe-set-deep-value'
import Sql from './helpers/abe-sql'
import abeProcess from './helpers/abe-process'
import {save, checkRequired, saveJson} from './controllers/Save'
Expand All @@ -65,8 +63,6 @@ export {
,clc
,Util
,abeCreate
,deep_value
,set_deep_value
,abeDuplicate
,slugify
,cleanSlug
Expand Down

0 comments on commit 517bb26

Please sign in to comment.