Skip to content
tinytip

Latest tips / 9

Move VS Code Side Bar to the right

November 17, 2021
#vscode

Visual Studio Code shows the side bar on the left side by default. However, you can move it to the right if you want. Just right-click on the side bar and choose "Move Side Bar Right".

Change the side bar position via context action

When collapsing / expanding the side bar your code does not jump anymore when the side bar is on the right side. This helps a lot if you toggle the side bar very often.

Map an RxJS stream to a static value with the mapTo operator

November 17, 2021
#rxjs

The RxJS operator map can be used to map the stream value to a new value. However, if you want to map to a static value that does not depend on the stream value, you can use the mapTo operator. This operator accepts the value directly and you don't need to create an additional function.

// Use `map` for dynamic values
stream$.pipe(map((user) => user.isAdmin));

// Use `mapTo` for static values
stream$.pipe(mapTo(true));

Note: the mapTo operator has been deprecated in RxJS 7 and will be removed in RxJS 8. It's better to use map with a function that returns the static value.

Angular HttpClient accepts query params as object

November 17, 2021
#angular #http

When making an HTTP request with query params in Angular using the HttpClient you don't need to compose the URL by yourself. Instead, you can pass the params as an object or an HttpParams instance.

  // Instead of passing query params as part of the URL...
function fetchSomeData() {
return this.httpClient.get('/my/endpoint?id=2');
}

// ... you can pass them as object
function fetchSomeData() {
const params: { id: 2 };
return this.httpClient.get('/my/endpoint', { params });
}

// ... or HttpParams
function fetchSomeData() {
const params = new HttpParams().set('id', 2);
return this.httpClient.get('/my/endpoint', { params });
}

How to use Array.map() with objects

October 9, 2021
#javascript

The Array.map() function allows to to map each value of an array to a new value. If you want to map an object - its keys, values or both - you can use Object.entries() and Object.fromEntries() to convert the object to an array and back to an object.

The Object.entries() function expects an object and returns an array with key-value pairs in the form of an array [key, value]. Then you can use Array.map() to map the keys and/or values.

The transformed array can be converted back to an object by using Object.fromEntries().

// 1. Create an object
const obj = { one: 1, two: 2, three: 3 };

// 2. Convert the object to an array of key-value pairs
const entries = Object.entries(obj);
// [ ["one", 1], ["two", 2], ["three", 3] ]

// 3. Map the array to new keys or values
const transformed = entries.map(([key, value]) => {
// Change the value to a string and add leading zeros
return [key, value.toString().padStart(3, "0")];
});

// 4. Convert the transformed array back to an object
const finalResult = Object.fromEntries(transformed);
// { one: "001", two: "002", three: "003" }

Style Angular Components based on their context

October 9, 2021
#angular #css

The Angular :host-context() selector allows you to style your component based on some conditions outside the component - for example based on a class set on the body element - without the need to set view encapsulation to none.

The selector will look for a matching element in any ancestor of your component up to the document root.

:host-context(.dark-theme) .my-component {
/* Styles are applied when there is a
"dark-theme" class on any ancestor */

}

This is not limited to CSS class selectors but works with any valid CSS selector.

:host-context([theme="dark"]) .my-component {
/* Styles are applied when there is an ancestor
that has an attribute theme="dark" */

}