Skip to content

Commit d2b2b45

Browse files
committed
fix: resolve collection of objects
1 parent 4dfecad commit d2b2b45

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

packages/metascraper/src/get-data.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { map, fromPairs, isEmpty, isArray } = require('lodash')
3+
const { map, fromPairs, isEmpty, isObject, isArray, mapValues, isPlainObject } = require('lodash')
44
const xss = require('xss')
55

66
const getValue = async ({ htmlDom, url, conditions, meta }) => {
@@ -15,11 +15,14 @@ const getValue = async ({ htmlDom, url, conditions, meta }) => {
1515
return value
1616
}
1717

18-
const escapeValue = (value, { escape }) => {
19-
if (!escape) return value
20-
return isArray(value) ? map(value, xss) : xss(value)
18+
const mapValuesDeep = (object, fn) => {
19+
if (isArray(object)) return map(object, innerObject => mapValuesDeep(innerObject, fn))
20+
if (isObject(object)) return mapValues(object, value => mapValuesDeep(value, fn))
21+
return fn(object)
2122
}
2223

24+
const escapeValue = (value, { escape }) => (!escape ? value : mapValuesDeep(value, xss))
25+
2326
const getData = async ({ rules, htmlDom, url, escape }) => {
2427
const data = await Promise.all(
2528
map(rules, async ([propName, conditions]) => {

0 commit comments

Comments
 (0)