export default function()

in public/src/js/utils/serialize-article-meta.js [5:43]


export default function (article) {
    const cleanMeta = _.chain(article.meta)
        .pairs()
        // execute any knockout values:
        .map(p => [p[0], _.isFunction(p[1]) ? p[1]() : p[1]])
        // trim and sanitize strings:
        .map(p => [p[0], _.isString(p[1]) ? sanitizeHtml(fullTrim(p[1])).trim() : p[1]])
        // reject vals that are equivalent to their defaults (if set)
        .filter(p => _.has(article.metaDefaults, p[0]) ? article.metaDefaults[p[0]] !== p[1] : !!p[1])
        // reject vals that are equivalent to the fields (if any) that they're overwriting:
        .filter(p => _.isUndefined(article.fields[p[0]]) || p[1] !== fullTrim(article.fields[p[0]]()))
        // convert numbers to strings:
        .map(p => [p[0], maybeNumberToString(p[1])])
        // recurse into supporting links
        .map(p => [p[0], p[0] === 'supporting' ? _.map(p[1].items(), item => item.get()) : p[1]])
        // clean sparse arrays
        .map(function (p) {
            return [p[0], _.isArray(p[1]) ? _.filter(p[1], function (item) { return !!item; }) : p[1]];
        })
        // drop empty arrays:
        .filter(p => _.isArray(p[1]) ? p[1].length : true)
        // recurse convert numbers to strings:
        .map(p => [p[0], _.isArray(p[1]) ? _.map(p[1], function (nested) {
            return _.isObject(nested) ? _.mapObject(nested, maybeNumberToString) : nested; }) : p[1]]
        )
        // return as obj, or as undefined if empty (this omits it from any subsequent JSON.stringify result)
        .reduce(function(obj, p) {
            obj = obj || {};
            obj[p[0]] = p[1];
            return obj;
        }, {})
        .value();

    if (article.group && article.group.parentType === 'Collection') {
        cleanMeta.group = article.group.index + '';
    }

    return _.isEmpty(cleanMeta) ? undefined : cleanMeta;
}