Skip to content

Commit

Permalink
fix: resolve collection of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 5, 2019
1 parent 4dfecad commit d2b2b45
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/metascraper/src/get-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { map, fromPairs, isEmpty, isArray } = require('lodash')
const { map, fromPairs, isEmpty, isObject, isArray, mapValues, isPlainObject } = require('lodash')
const xss = require('xss')

const getValue = async ({ htmlDom, url, conditions, meta }) => {
Expand All @@ -15,11 +15,14 @@ const getValue = async ({ htmlDom, url, conditions, meta }) => {
return value
}

const escapeValue = (value, { escape }) => {
if (!escape) return value
return isArray(value) ? map(value, xss) : xss(value)
const mapValuesDeep = (object, fn) => {
if (isArray(object)) return map(object, innerObject => mapValuesDeep(innerObject, fn))
if (isObject(object)) return mapValues(object, value => mapValuesDeep(value, fn))
return fn(object)
}

const escapeValue = (value, { escape }) => (!escape ? value : mapValuesDeep(value, xss))

const getData = async ({ rules, htmlDom, url, escape }) => {
const data = await Promise.all(
map(rules, async ([propName, conditions]) => {
Expand Down

0 comments on commit d2b2b45

Please sign in to comment.