resource-watch/vocabulary-tag

View on GitHub
app/src/serializers/resource.serializer.js

Summary

Maintainability
A
1 hr
Test Coverage
A
94%
/* eslint-disable no-param-reassign */

class ResourceSerializer {

    static serialize(data) {

        const result = {
            data: []
        };
        if (data) {
            if (!Array.isArray(data)) {
                data = [data];
            }
            data.forEach((el) => {
                el.vocabularies.forEach((vocabulary) => {
                    result.data.push({
                        id: vocabulary.id,
                        type: 'vocabulary',
                        attributes: {
                            tags: vocabulary.tags,
                            name: vocabulary.id,
                            application: vocabulary.application
                        }
                    });
                });
            });
        }
        return result;
    }

    static serializeByIds(data) {

        const result = {
            data: []
        };
        if (data) {
            if (!Array.isArray(data)) {
                data = [data];
            }
            data.forEach((el) => {
                el.vocabularies.forEach((vocabulary) => {
                    result.data.push({
                        type: 'vocabulary',
                        attributes: {
                            resource: {
                                id: el.id,
                                type: el.type
                            },
                            tags: vocabulary.tags,
                            name: vocabulary.id,
                            application: vocabulary.application
                        }
                    });
                });
            });
        }
        return result;
    }

}

module.exports = ResourceSerializer;