All files / src/common/dto/article list.dto.js

92.3% Statements 12/13
66.66% Branches 6/9
66.66% Functions 2/3
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47      4x           4x           3x 2x   1x     3x   3x 3x         4x     4x   4x 2x                      
import Joi from "joi";
import { BaseOutputDto } from "../dto.js";
 
const bodyLimit = 512;
function truncate(content) {
    return content.substring(0, bodyLimit - 3) + "...";
}
 
class ListArticleInputDto {
    static SCHEMA = Joi.object({
        title: Joi.string().allow(""),
        tags: Joi.array().items(Joi.string().allow("")),
    }).options({ abortEarly: false });
 
    constructor(data) {
        if (typeof data.tags == "string") {
            data.tags = data.tags.split(",");
        } else {
            data.tags = Array.isArray(data.tags) ? data.tags : [];
        }
 
        data = Joi.attempt(data, this.constructor.SCHEMA);
 
        this.title = typeof data.title == "string" ? data.title : "";
        this.tags = data.tags;
    }
}
 
class ListArticleOutputDto extends BaseOutputDto {
    data = [];
 
    constructor(articles, success = true, info) {
        super(success, info);
 
        for (let k in articles) {
            this.data[k] = {
                _id: articles[k]._id,
                title: articles[k].title,
                body: articles[k].body.length > bodyLimit ? truncate(articles[k].body) : articles[k].body,
                tags: articles[k].tags,
            };
        }
    }
}
 
export { ListArticleInputDto, ListArticleOutputDto };