Skip to content

Commit ee3e456

Browse files
author
Kimon Kar
committed
Prep work for recursive cleanContent method
1 parent a26a0f3 commit ee3e456

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

XIVAPI.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const utils = require('./utils'),
2-
resources = require('./resources/'),
1+
const resources = require('./resources/'),
32

43
Search = require('./lib/search'),
54
Data = require('./lib/data'),
@@ -31,7 +30,6 @@ class XIVAPI {
3130
throw Error(`invalid language given, must be: ${this.resources.languages}`)
3231

3332
this.resources = resources
34-
this.utils = utils
3533

3634
this.search = Search.bind(this)
3735
this.data = new Data(this)

utils.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,38 @@ module.exports = {
6161
},
6262

6363
//transform URLs properly
64-
cleanContent(input) {
64+
cleanContent(input, deep) {
6565
let icon = module.exports.correctCase('icon', this.globalParams.snake_case),
6666
url = module.exports.correctCase('url', this.globalParams.snake_case)
6767

68-
const run = (entry) => {
68+
const properties = [icon, url]
69+
70+
const clean = (entry) => {
6971
entry[icon] = this.endpoint + entry[icon]
7072
entry[url] = this.endpoint + entry[url]
7173
return entry
7274
}
7375

74-
if(Array.isArray(input))
75-
for (let i = 0; i < input.length; i++)
76-
input[i] = run(input[i])
77-
else
78-
input = run(input)
76+
const deepClean = (obj) => {
77+
Object.keys(obj).some((k) => {
78+
if (properties.includes(k)) {
79+
obj[k] = this.endpoint + obj[k]
80+
}
81+
if (obj[k] && typeof(obj[k]) === 'object') {
82+
deepClean(obj[k])
83+
}
84+
})
85+
}
86+
87+
if(!deep) {
88+
if(Array.isArray(input))
89+
for (let i = 0; i < input.length; i++)
90+
input[i] = clean(input[i])
91+
else
92+
input = clean(input)
93+
} else {
94+
input = deepClean(input)
95+
}
7996

8097
return input
8198
},

0 commit comments

Comments
 (0)