PROG_WAY_BLOG Telegram 343
progway — программирование, IT
Ещё один пример

Бывает такое, что нужно встроить в строку значение, которое может быть пустым. Обычно пишутся доп. проверки:
const order = {
city: "Москва" // представим, что возможно undefined или null
}

const { city } = order

// могут писать что-то типа такой проверки
city ? `Ваш город: ${city}` : null


Можно решить эту же задачу с помощью функции из поста выше, вот так это будет:
type SubstitutionPrimitive = string | number | boolean | undefined | null;

const isNullOrUndefined = (value: SubstitutionPrimitive): value is undefined | null => {
return value === undefined || value === null;
};

const safePaste = (strings: TemplateStringsArray, ...substitutions: SubstitutionPrimitive[]) => {
let result = strings[0];

for (let index = 0; index < substitutions.length; index++) {
const value = substitutions[index];

if (isNullOrUndefined(value)) return null;

result += value + strings[index + 1];
}

return result;
};


Просто вернем null вместо строки, если какое либо из значений в подстановках null или undefined. Вот так это будет вызываться:
const apple = {
name: 'Яблоко',
};
const orange = {};

safePaste`Товар: "${apple?.name}"`;
// Товар: "Яблоко"

safePaste`Товар: "${orange?.name}"`;
// null


Ну типа костыль. А вроде и нет. Просто ещё один пример посмотреть как это можно применить

@prog_way_blogчат — #javascript #code



tgoop.com/prog_way_blog/343
Create:
Last Update:

Ещё один пример

Бывает такое, что нужно встроить в строку значение, которое может быть пустым. Обычно пишутся доп. проверки:

const order = {
city: "Москва" // представим, что возможно undefined или null
}

const { city } = order

// могут писать что-то типа такой проверки
city ? `Ваш город: ${city}` : null


Можно решить эту же задачу с помощью функции из поста выше, вот так это будет:
type SubstitutionPrimitive = string | number | boolean | undefined | null;

const isNullOrUndefined = (value: SubstitutionPrimitive): value is undefined | null => {
return value === undefined || value === null;
};

const safePaste = (strings: TemplateStringsArray, ...substitutions: SubstitutionPrimitive[]) => {
let result = strings[0];

for (let index = 0; index < substitutions.length; index++) {
const value = substitutions[index];

if (isNullOrUndefined(value)) return null;

result += value + strings[index + 1];
}

return result;
};


Просто вернем null вместо строки, если какое либо из значений в подстановках null или undefined. Вот так это будет вызываться:
const apple = {
name: 'Яблоко',
};
const orange = {};

safePaste`Товар: "${apple?.name}"`;
// Товар: "Яблоко"

safePaste`Товар: "${orange?.name}"`;
// null


Ну типа костыль. А вроде и нет. Просто ещё один пример посмотреть как это можно применить

@prog_way_blogчат — #javascript #code

BY progway — программирование, IT


Share with your friend now:
tgoop.com/prog_way_blog/343

View MORE
Open in Telegram


Telegram News

Date: |

SUCK Channel Telegram Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. Add up to 50 administrators Avoid compound hashtags that consist of several words. If you have a hashtag like #marketingnewsinusa, split it into smaller hashtags: “#marketing, #news, #usa. Telegram Android app: Open the chats list, click the menu icon and select “New Channel.”
from us


Telegram progway — программирование, IT
FROM American