Categories
Angular Answers

How to append HTML to container element in Angular and TypeScript?

Sometimes, we want to append HTML to container element in Angular and TypeScript.

In this article, we’ll look at how to append HTML to container element in Angular and TypeScript.

How to append HTML to container element in Angular and TypeScript?

To append HTML to container element in Angular and TypeScript, we set the [innerHtml] attribute.

For instance, we write

this.htmlToAdd = '<div class="two">two</div>';

to set the htmlToAdd instance variable to a HTML string.

Then we render the content by writing

<div [innerHtml]="htmlToAdd"></div>

to set [innerHtml] to htmlToAdd to render the htmlToAdd string as raw HTML.

Conclusion

To append HTML to container element in Angular and TypeScript, we set the [innerHtml] attribute.

Categories
JavaScript Answers

How to stub a promise with sinon and JavaScript?

Sometimes, we want to stub a promise with sinon and JavaScript.

In this article, we’ll look at how to stub a promise with sinon and JavaScript.

How to stub a promise with sinon and JavaScript?

To stub a promise with sinon and JavaScript, we can return a promise with a stub.

For instance, we write

import sinon from "sinon";

const sandbox = sinon.sandbox.create();

const promiseResolved = () =>
  sandbox.stub().returns(Promise.resolve("resolved"));
const promiseRejected = () =>
  sandbox.stub().returns(Promise.reject("rejected"));

to call sandbox.stub to return a stub.

And we make it return a promise by calling returns with a promise.

Promise.resolve returns a promise that resolves.

And Promise.reject returns a promise that rejects.

Conclusion

To stub a promise with sinon and JavaScript, we can return a promise with a stub.

Categories
JavaScript Answers

How to put an array inside a JavaScript object?

Sometimes, we want to put an array inside a JavaScript object.

In this article, we’ll look at how to put an array inside a JavaScript object.

How to put an array inside a JavaScript object?

To put an array inside a JavaScript object, we set its as a property’s value.

For instance, we write

const defaults = {
  backgroundcolor: "#000",
  color: "#fff",
  weekdays: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
};

to set the default.weekdays property to the ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] array.

Conclusion

To put an array inside a JavaScript object, we set its as a property’s value.

Categories
JavaScript Answers

How to sanitize user input before adding it to the DOM in JavaScript?

Sometimes, we want to sanitize user input before adding it to the DOM in JavaScript.

In this article, we’ll look at how to sanitize user input before adding it to the DOM in JavaScript.

How to sanitize user input before adding it to the DOM in JavaScript?

To sanitize user input before adding it to the DOM in JavaScript, we use the string replace method.

For instance, we write

const sanitize = (string) => {
  const map = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&#x27;",
    "/": "&#x2F;",
  };
  const reg = /[&<>"'/]/gi;
  return string.replace(reg, (match) => map[match]);
};

to call string.replace with reg and a callback to replace the characters to be escaped with the corresponding HTML entities.

reg matches all values since it has the g flag.

And the i flag makes replace match in a case-insensitive manner.

Conclusion

To sanitize user input before adding it to the DOM in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to change property name with JavaScript?

Sometimes, we want to change property name with JavaScript.

In this article, we’ll look at how to change property name with JavaScript.

How to change property name with JavaScript?

To change property name with JavaScript, we use the delete operator.

For instance, we write

a.prop3 = a.prop1;
delete a.prop1;

to assign a.prop1 to a.prop3.

Then we remove a.prop1 from a with delete.

Conclusion

To change property name with JavaScript, we use the delete operator.