Categories
JavaScript Answers

How to traverse all the nodes of a JSON object tree with JavaScript?

Sometimes, we want to traverse all the nodes of a JSON object tree with JavaScript.

In this article, we’ll look at how to traverse all the nodes of a JSON object tree with JavaScript.

How to traverse all the nodes of a JSON object tree with JavaScript?

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.

For instance, we write

const traverse = (jsonObj) => {
  if (jsonObj !== null && typeof jsonObj == "object") {
    Object.entries(jsonObj).forEach(([key, value]) => {
      traverse(value);
    });
  } else {
    console.log(jsonObj);
  }
};

to create the traverse function.

In it, we loop through the key-value pairs with the Object.entries method.

If jsonObj is an object, then we call traverse with its value in the forEach callback to traverse the level below the current level.

Otherwise, we log the jsonObj value.

Conclusion

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to mock the imports of an ES6 module with Jest and JavaScript?

Sometimes, we want to mock the imports of an ES6 module with Jest and JavaScript.

In this article, we’ll look at how to mock the imports of an ES6 module with Jest and JavaScript.

How to mock the imports of an ES6 module with Jest and JavaScript?

To mock the imports of an ES6 module with Jest and JavaScript, we can call jest.mock.

For instance, we write

import myfunc from "./mymod";
import shortid from "shortid";

jest.mock("shortid");

describe("mocks shortid", () => {
  it("works", () => {
    shortid.mockImplementation(() => 1);
    expect(myfunc()).toEqual(1);
  });
});

to import the real shortid module with

import shortid from "shortid";

Then we use

jest.mock("shortid");

to mock the shortid module.

And then we call shortid.mockImplementation with a callback to mock the return value of the shortid function.

Conclusion

To mock the imports of an ES6 module with Jest and JavaScript, we can call jest.mock.

Categories
JavaScript Answers

How to destructure onto an existing object with JavaScript?

Sometimes, we want to destructure onto an existing object with JavaScript.

In this article, we’ll look at how to destructure onto an existing object with JavaScript.

How to destructure onto an existing object with JavaScript?

To destructure onto an existing object with JavaScript, we can destructure the properties into variables and then put the properties into the new object.

For instance, we write

const { prop1, prop2, prop3 } = someObject;
const data = { prop1, prop2, prop3 };

to destructure the prop1, prop2 and prop3 properties from someObject.

Then we put the values of each variable as properties of the data object.

Conclusion

To destructure onto an existing object with JavaScript, we can destructure the properties into variables and then put the properties into the new object.

Categories
JavaScript Answers

How to insert Unicode character into JavaScript?

Sometimes, we want to insert Unicode character into JavaScript.

In this article, we’ll look at how to insert Unicode character into JavaScript.

How to insert Unicode character into JavaScript?

To insert Unicode character into JavaScript, we put them into strings.

For instance, we write

const omega = '\u03A9';

to assign omega to a string with the Unicode character for the letter omega.

Conclusion

To insert Unicode character into JavaScript, we put them into strings.

Categories
JavaScript Answers

How to downscale HTML5 canvas image with JavaScript?

Sometimes, we want to downscale HTML5 canvas image with JavaScript.

In this article, we’ll look at how to downscale HTML5 canvas image with JavaScript.

How to downscale HTML5 canvas image with JavaScript?

To downscale HTML5 canvas image with JavaScript, we use the blitz-hermite-resize package.

To install it, we run

npm install blitz-resize --save

Then we use it by writing

const blitz = Blitz.create();

//...

const output = await blitz({
  source,
  width: 400,
  height: 600,
});

to call blitz with an object with the image source and the width and height to scale the image to.

source can be a DOM img element, DOM canvas element, jQuery element, base64 data URL string, or a File object.

It returns a promise with the resized image.

output is an object with the image in DOM img element, DOM canvas element, jQuery element, base64 data URL string, and a File object,

Conclusion

To downscale HTML5 canvas image with JavaScript, we use the blitz-hermite-resize package.