20个实用的 TypeScript 单行代码汇总

9d886bbe5b4ec49d6ef050518077376b.png

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

在今天的文章中,我将与你分享20有用的 TypeScript 单行代码,这些单行代码可以快速的帮助我们提升开发效率,希望对你有用。

那我们现在开始吧。

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)); // -> true
isWeekday(new Date(2021, 2, 20)); // -> false

03、反转字符串

const reverse = (s: string): string => s.split('').reverse().join('');


reverse('elon musk'); // -> 'ksum nole'

04、检查一个数字是否为偶数。

const isEven = (n: number): boolean => n % 2 === 0;


isEven(2); // -> true
isEven(3); // -> false

05、大写字符串

const capitalize = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1);


capitalize('lorem ipsum'); // -> Lorem ipsum

06、检查数组是否为空

const isArrayEmpty = (arr: unknown[]): boolean => Array.isArray(arr) && !arr.length;


isArrayEmpty([]); // -> true
isArrayEmpty([1, 2, 3]); // -> false

07、检查对象/数组是否为空

const isObjectEmpty = (obj: unknown): boolean => obj && Object.keys(obj).length === 0;


isObjectEmpty({}); // -> true
isObjectEmpty({ foo: 'bar' }); // -> false

08、随机生成整数

基于两个参数生成一个随机整数。

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、转换

将字符串转换为带“-”的连字字符串。

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


slugify('Hello World'); // -> hello-world

12、生成随数组组合

随机生成一组任何类型的数组。

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、将连字字符串转换为骆峰字符串

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


snakeToCamel('foo_bar'); // -> fooBar

14、随机整数

根据当前时间生成一个随机整数。

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


randomInteger(); // -> 1646617367345

15、随机数字符串

根据当前时间生成随机数字符串。

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


randomNumberString(); // -> 1646617484381wml196a8iso

16、将数字转换为字符/字母

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、获取数组的随机项

const randomItem = <T>(arr: T[]): T => arr[(Math.random() * arr.length) | 0];


randomItem(<number[]>[1, 2, 3, 4, 5]); // -> 4

20、将大写字符串转换为小写

const decapitalize = (str: string): string => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;


decapitalize('Hello world'); // -> hello world

写在最后

以上就是我今天与你分享的全部内容,如果你觉得有用的话,请点赞我,关注我,并将它分享分享给你身边做开发的朋友,也许能够帮助到他。

最后,感谢你的阅读,祝编程愉快!

学习更多技能

请点击下方公众号

1bc2d93395d9f277d36867aa7babe45c.gif

1f29220f228fed40a2960c97a9af9495.png

2065437f4fa1744dd494f97ffd67fec5.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值