<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          20個實用的 TypeScript 單行代碼匯總

          共 3396字,需瀏覽 7分鐘

           ·

          2022-06-09 17:32

          來源 | https://blog.bitsrc.io/another-10-quick-typescript-one-liners-9f41713c158a


          在今天的文章中,我將與你分享20有用的 TypeScript 單行代碼,這些單行代碼可以快速的幫助我們提升開發(fā)效率,希望對你有用。

          那我們現(xiàn)在開始吧。

          01、等待特定的時間量(以毫秒為單位)

          const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
          await wait(1000); // waiting 1 second

          02、檢查日期是否為工作日

          const isWeekday = (d: Date): boolean => d.getDay() % 6 !== 0;
          isWeekday(new Date(2022, 2, 21)); // -> trueisWeekday(new Date(2021, 2, 20)); // -> false

          03反轉(zhuǎn)字符串


          const reverse = (s: string): string => s.split('').reverse().join('');
          reverse('elon musk'); // -> 'ksum nole'

          04、檢查一個數(shù)字是否為偶數(shù)。

          const isEven = (n: number): boolean => n % 2 === 0;
          isEven(2); // -> trueisEven(3); // -> false

          05、大寫字符串

          const capitalize = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);
          capitalize('lorem ipsum'); // -> Lorem ipsum

          06、檢查數(shù)組是否為空

          const isArrayEmpty = (arr: unknown[]): boolean => Array.isArray(arr) && !arr.length;
          isArrayEmpty([]); // -> trueisArrayEmpty([1, 2, 3]); // -> false

          07、檢查對象/數(shù)組是否為空

          const isObjectEmpty = (obj: unknown): boolean => obj && Object.keys(obj).length === 0;
          isObjectEmpty({}); // -> trueisObjectEmpty({ foo: 'bar' }); // -> false

          08、隨機生成整數(shù)

          基于兩個參數(shù)生成一個隨機整數(shù)。

          const randomInteger = (min: number, max: number): number => Math.floor(Math.random() * (max - min + 1)) + min;
          randomInteger(1, 10); // -> 7

          09、生成隨機布爾值

          const randomBoolean = (): boolean => Math.random() >= 0.5;
          randomBoolean(); // -> true

          10、切換布爾值

          切換布爾值,變假為真,變真為假。

          const toggleBoolean = (val: boolean): boolean => (val = !val);
          toggleBoolean(true); // -> false

          11、轉(zhuǎn)換

          將字符串轉(zhuǎn)換為帶“-”的連字字符串。

          const slugify = (str: string): string => str.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
          slugify('Hello World'); // -> hello-world

          12、生成隨數(shù)組組合

          隨機生成一組任何類型的數(shù)組。

          const shuffleArray = <T>(arr: T[]): T[] => arr.sort(() => Math.random() - 0.5);
          shuffleArray(<number[]>[1, 2, 3, 4, 5]); // -> [ 4, 5, 2, 1, 3 ]

          13、將連字字符串轉(zhuǎn)換為駱峰字符串

          const snakeToCamel = (s: string): string => s.toLowerCase().replace(/(_\w)/g, (w) => w.toUpperCase().substring(1));
          snakeToCamel('foo_bar'); // -> fooBar

          14、隨機整數(shù)

          根據(jù)當前時間生成一個隨機整數(shù)。

          const randomInteger = (): number => new Date().getTime();
          randomInteger(); // -> 1646617367345

          15、隨機數(shù)字符串

          根據(jù)當前時間生成隨機數(shù)字符串。

          const randomNumberString = (): string => new Date().getTime() + Math.random().toString(36).slice(2);
          randomNumberString(); // -> 1646617484381wml196a8iso

          16、將數(shù)字轉(zhuǎn)換為字符/字母

          const numberToLetter = (value: number): string => String.fromCharCode(94 + value);
          numberToLetter(4); // -> b

          17、生成隨機的十六進制顏色

          const randomHexColor = (): string => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0')}`;
          randomHexColor(); // -> #dc7c40

          18、刪除字符串的尾部斜杠

          const removeTrailingSlash = (value: string): string => value && value.charAt(value.length - 1) === '/' ? value.slice(0, -1) : value;
          removeTrailingSlash('foo-bar/'); // -> foo-bar

          19、獲取數(shù)組的隨機項

          const randomItem = <T>(arr: T[]): T => arr[(Math.random() * arr.length) | 0];
          randomItem(<number[]>[1, 2, 3, 4, 5]); // -> 4

          20、將大寫字符串轉(zhuǎn)換為小寫

          const decapitalize = (str: string): string => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
          decapitalize('Hello world'); // -> hello world

          寫在最后

          以上就是我今天與你分享的全部內(nèi)容,如果你覺得有用的話,請點贊我,關(guān)注我,并將它分享分享給你身邊做開發(fā)的朋友,也許能夠幫助到他。

          最后,感謝你的閱讀,祝編程愉快!


          學習更多技能

          請點擊下方公眾號

          瀏覽 50
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  2020天天日天天干 | 久久精品久久精品 | 插入青春美女视频网站 | 综合亚洲自拍 | 操屄一级片 |