const start = 11 * 60 * 60;
const end = 18 * 60 * 60;
const step = 30 * 60;
const count = Math.floor((end - start) / step);
const result: string[] = [];
const date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
const format = (date: Date) => date.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' });
for (let index = 0; index < count; index++) {
const from = new Date(date.getTime() + (start + step * index) * 1000);
const to = new Date(date.getTime() + (start + step * (index + 1)) * 1000);
result.push(`${format(from)} - ${format(to)}`);
}
console.log(result);
/* [ '11:00 - 11:30', '11:30 - 12:00', '12:00 - 12:30', '12:30 - 13:00', '13:00 - 13:30',
'13:30 - 14:00', '14:00 - 14:30', '14:30 - 15:00', '15:00 - 15:30', '15:30 - 16:00',
'16:00 - 16:30', '16:30 - 17:00', '17:00 - 17:30', '17:30 - 18:00' ] */
Array.find
, так как он не поддерживается в IE.type URLParamsEntry = [string, string];
class URLParams {
entries: URLParamsEntry[] = [];
constructor(search: string = '') {
const trimmedSearch = search.trim();
if (trimmedSearch.length > 0) {
const pairs = trimmedSearch.replace(/^\?/, '').split('&');
for (const entry of pairs) {
const pair = entry.split('=');
if (pair[0] !== undefined && pair[1] !== undefined) {
const key = pair[0];
const value = decodeURIComponent(pair[1]);
this.entries.push([key, value]);
}
}
}
}
append(key: string, value: string) {
this.entries.push([key, value]);
return this;
}
delete(key: string) {
this.entries = this.entries.filter(entry => entry[0] !== key);
return this;
}
has(key: string) {
const inEntries = this.entries.some(entry => entry[0] === key);
return inEntries;
}
set(key: string, value: string) {
return this.delete(key).append(key, value);
}
get(key: string): string | null {
const entries = this.getAll(key);
return entries.length > 0 ? entries[0] : null;
}
getAll(key: string): string[] {
const entries = this.entries.reduce<string[]>((accumulator, entry) => {
if (entry[0] === key) {
accumulator.push(entry[1]);
}
return accumulator;
}, []);
return entries;
}
toString() {
return this.entries
.map(entry => `${entry[0]}=${encodeURIComponent(entry[1])}`)
.join('&');
}
}
for (const key of utm) {
params.delete(key);
}
const href = 'https://example.com/?user=John&some_key=1&id=3';
const url = new URL(href);
const availableKeys = ['user', 'id'];
for (const key of [...url.searchParams.keys()]) {
if (!availableKeys.includes(key)) {
url.searchParams.delete(key);
}
}
console.log(url.toString()); // 'https://example.com/?user=John&id=3'
.d.ts
-файлы работают для модулей.│ main.js
└───lib
greeting.ts
import { greeting } from './lib/greeting.js';
console.log(greeting('John'));
export const greeting = (name: string): string => `Hello, ${name}`;
.d.ts
можем использовать следующую команду: tsc lib\greeting.ts --target ES2020 -d
. Данная команда сгенерирует два файла:export const greeting = (name) => `Hello, ${name}`;
export declare const greeting: (name: string) => string;
greeting.ts
, то описания типов будут подтягиваться с .d.ts
файла (в основном используются в релизных сборках)..d.ts
должен выглядеть следующим образом:export declare class Message {
send(t: string, f: string): void;
}