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: |

The Channel name and bio must be no more than 255 characters long But a Telegram statement also said: "Any requests related to political censorship or limiting human rights such as the rights to free speech or assembly are not and will not be considered." On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. A vandalised bank during the 2019 protest. File photo: May James/HKFP. 2How to set up a Telegram channel? (A step-by-step tutorial)
from us


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