Skip to content
tinytip

Latest tips / 8

Keep the separators when splitting a string in JavaScript

January 28, 2022
#javascript
Credits: Stefan Judis

The split() function in JavaScript splits a string into an array by a separator and removes the separator:

// Split by "-"
"hello-from-javascript".split("-");
// Result: ["hello", "from", "javascript"]

// Split by "-" or "_"
"hello-from_javascript".split(/-|_/g);
// Result: ["hello", "from", "javascript"]

Use a RegEx group (...) if you want to keep the separator in the array:

// Split by "-" or "_"
"hello-from_javascript".split(/(-|_)/g);
// Result: ["hello", "-", "from", "_", "javascript"]

Extract types in TypeScript

January 17, 2022
#typescript

The infer keyword in Typescript makes it possible to extract types. This can be used to extract function parameters, generics and more.

// Given the following types ...
export interface Stream<T> { next: (value: T) => void }
export type SpecificStream = Stream<string[]>;

// ... you can extract T into a type
export type ValueOfStream<S> = S extends Stream<infer T> ? T : never;

// ... and use like this
function emitValue(
stream: SpecificStream,
value: ValueOfStream<SpecificStream> // Extract T from Stream
) {}

TypeScript provides some built-in types like Parameters and ReturnType.

// Given this type ...
export type SomeFn = (first: string, second: boolean) => string[];

// ... you can extract parameters
export type SecondParam = Parameters<SomeFn>[1];

// ... and the return type
export type ReturnTypeOfSomeFn = ReturnType<SomeFn>;

Observe values in Browser DevTools with Live Expressions

December 19, 2021
#devtools

Live Expressions allow you to observe values in Browser DevTools. You can enter a JavaScript expression and it will automatically show the latest value. This can be useful to see how a value changes over time or to monitor the focused element (document.activeElement) to see if focus handling is working properly.

Open the DevTools, navigate to the "Console" Tab and click the eye icon to add a new Live Expression.

Add live expression in DevTools

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.