Great way to map up objects in TypeScript to keep objects initialized

Available in TypeScript 2.1 there is now the possibility to set partial properties values of objects, making it very easy to initialize new objects.

I had the problem before that whenever I wanted to create a new object and also initilize a new object, I had to set my values in the constructor, all my values.

export class Example{

    text: string;
    constructor(init?: Partial<Example>) {

        // This will set values for each properties that can be found
        Object.assign(this, init);

    }
}

Now with the above code I can run a map function on an array and use the object itself as a constructor parameter. Like below.

// array filled with data is coming from firestore in this example
const ExamplesObs = <Observable<Example>>this.db.doc(`example/mycoolexampleDoc`).valueChanges().pipe(
            map((fbl: Example) => {
                let ex: Example = new Example(ex);
                return ex
})

// Subscribe to the Observable etc...

The Observable will return an initialized object of the Example class. Now I am using firestore to get the data (in json format) and what will come back is simply just the json structure so this would be a great way to put the data into the real object with the help of a constructor with Partial.

Problem not preserving \n (linebreaks) with article-parser and sanitizeHtml

I was using article-parser 4.2.1 for my node.js program to get article data from a news website. Works perfectly until I realised I couldn’t get the line breaks from the article . With line breaks I mean the “\n” characters.

I solved this by going through the plugin and change some properties concerning the htmlmin npm package that is a dependency of article-parser, and replacing \n to <br> with cheerio.

By going to node_modules\article-parser\src\utils\standalizeArticle.js and changing where htmlmin is used at row 32, the properties must be changed so that preserveLineBreaks are set to true.

I made a repo which you can use npm install mosesweb/article-parser of said changes.

  const minifiedHtml = htmlmin($.html(), {
    removeComments: true,
    removeEmptyElements: true,
    removeEmptyAttributes: true,
    collapseWhitespace: false,
    conservativeCollapse: true,
    removeTagWhitespace: false,
    preserveLineBreaks: true
  });

In the nodejs application   This means that next time you fetch data with extract you will get your content with the lovely \n characters.

let myUrl = "mycoolurlforanarticle.com"
extract(myUrl).then((article) => {
    
    // Loading in the article DOM to a cheerio object
    let con = $.load(article.content)

    // Print in to console: the html content with html line breaks <br> instead.
    console.log(con.html({ decodeEntities: false }).replaceAll('\n', '<br />'))
    
    // Note you need this function to use replaceAll as used above
    String.prototype.replaceAll = function (search, replacement) {
        var target = this;
        return target.replace(new RegExp(search, 'g'), replacement);
    }
});