回复下方公众号【面试宝典】,即可获取为你整理的107页后端笔试题。
在昨天的文章中,我将与大家分享20个有用的TypeScript单行代码typescript空数组,可以快速帮助我们提高开发效率,希望对大家有用。
那么我们现在就开始吧。
01.等待特定的时间(以纳秒为单位)
const wait = (ms: number): Promise => 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
基于两个参数生成随机整数。
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. 切换布尔值
切换布尔值typescript空数组,将 false 变为 true,将 true 变为 false。
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 = (arr: T[]): T[] => arr.sort(() => Math.random() - 0.5);
shuffleArray([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 = (arr: T[]): T => arr[(Math.random() * arr.length) | 0];
randomItem( [1, 2, 3, 4, 5]); // -> 4
20. 将小写字符串转换为大写
const decapitalize = (str: string): string => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
decapitalize('Hello world'); // -> hello world
-结尾-
近期新课程:
Vue 基础知识 | 小程序开发 | JQuery 入门 | PC端页面布局 | 打字稿 | JS 基本运算符 | 数据类型之间的存储差异服务器端渲染和客户端渲染|
发表评论